<commit_before>
'use strict';

// import Immutable from 'immutable';

// import Interpreter from 'js-interpreter';

import {
  MODAL_CLOSE,
  NEXT_CHALLENGE,
  TESTS_STARTED,
  CODE_CHANGED,
  OUTPUT_CHANGED,
  START_CHALLENGE,
  TESTS_FINISHED,
  TIMER_STARTED,
  TIMER_UPDATED,
  STOP_TIMER,
  FINISH_SESSION,
  TIMER_MAX_VALUE_CHANGED
} from '../actions/ArcadeAction';

import UserData from '../model/UserData';
import Challenges from '../../../json/challenges.json';
import TestResults from '../model/TestResults';
import Challenge from '../model/Challenge';

// const initialState = Immutable.Map();
//

const timerDefaultValue = 5 * 1000; // 15 minutes

export default function arcadeReducer(state, action) {
  if (typeof state === 'undefined') {
    return {
      modal: true,
      title: '',
      description: [],
      code: `
        The code to work with will show up here.
        When you are ready, enter a time at the top and press start to begin!
      `,
      // Challenges.challenges[0].challengeSeed.join('\n'),
      userOutput: 'The output of your code will show up here.',
      interpreterError: false,
      isRunningTests: false,
      userData: new UserData({ username: '' }),
      testResults: new TestResults([]),
      challengeNumber: 0,
      currChallenge: new Challenge(Challenges.challenges[0]),
      nextChallenge: '',

      // Timer handling
      isTimerFinished: false,
      timerMaxValue: timerDefaultValue,
      timeLeft: timerDefaultValue,
      timerStart: 0,

      // Session control variables
      isSessionFinished: false
    };
  }

  const nextState = Object.assign({}, state);

  switch (action.type) {
    case MODAL_CLOSE: {
      nextState.modal = false;
      break;
    }
    case TESTS_STARTED: {
      nextState.isRunningTests = true;
      break;
    }
    case TESTS_FINISHED: {
      nextState.isRunningTests = false;
      nextState.testResults = new TestResults(action.testResults);
      break;
    }
    case CODE_CHANGED: {
      nextState.code = action.code;
      break;
    }
    case NEXT_CHALLENGE: {
      nextState.currChallenge = state.nextChallenge;
      nextState.title = state.nextChallenge.getTitle();
      nextState.description = state.nextChallenge.getDescription();
      nextState.code = state.nextChallenge.getSeed().join('\n');
      nextState.testResults = new TestResults([]);
      nextState.challengeNumber++;
      nextState.userOutput = '';
      nextState.nextChallenge = new Challenge(Challenges.challenges[state.challengeNumber + 1]);
      break;
    }
    case OUTPUT_CHANGED: {
      nextState.userOutput = action.userOutput;
      break;
    }
    case TIMER_MAX_VALUE_CHANGED: {
      nextState.timerMaxValue = action.timerMaxValue;
      break;
    }
    case TIMER_STARTED: {
      nextState.isTimerFinished = false;
      nextState.timeLeft = timerDefaultValue;
      nextState.timerStart = new Date().getTime();
      break;
    }
    case START_CHALLENGE: {
      nextState.title = state.currChallenge.getTitle();
      nextState.description = state.currChallenge.getDescription();
      nextState.code = state.currChallenge.getSeed().join('\n');
      nextState.challengeNumber++;
      nextState.nextChallenge = new Challenge(Challenges.challenges[state.challengeNumber + 1]);
      nextState.isSessionFinished = false;
      break;
    }
    case TIMER_UPDATED: {
      const timeNow = new Date().getTime();
      nextState.timeLeft = state.timerMaxValue - (timeNow - state.timerStart);
      break;
    }
    case STOP_TIMER: {
      nextState.isTimerFinished = true;
      nextState.timeLeft = 0;
      break;
    }
    case FINISH_SESSION: {
      nextState.isSessionFinished = true;
      break;
    }
    default: console.log('ERROR. ArcadeReducer default reached.');
  }

  return nextState;
}
<commit_msg>Added getScoreForChallenge(). Removed interpreterError. Added sessionScore.<commit_after>
'use strict';

// import Immutable from 'immutable';

// import Interpreter from 'js-interpreter';

import {
  MODAL_CLOSE,
  NEXT_CHALLENGE,
  TESTS_STARTED,
  CODE_CHANGED,
  OUTPUT_CHANGED,
  START_CHALLENGE,
  TESTS_FINISHED,
  TIMER_STARTED,
  TIMER_UPDATED,
  STOP_TIMER,
  FINISH_SESSION,
  TIMER_MAX_VALUE_CHANGED
} from '../actions/ArcadeAction';

import UserData from '../model/UserData';
import Challenges from '../../../json/challenges.json';
import TestResults from '../model/TestResults';
import Challenge from '../model/Challenge';

// const initialState = Immutable.Map();
//

const timerDefaultValue = 5 * 1000; // 15 minutes

/* TODO: Returns score for completed challenge. */
const getScoreForChallenge = challenge => {
  return 1;
};

export default function arcadeReducer(state, action) {
  if (typeof state === 'undefined') {
    return {
      modal: true,
      title: '',
      description: [],
      code: `
        The code to work with will show up here.
        When you are ready, enter a time at the top and press start to begin!
      `,
      // Challenges.challenges[0].challengeSeed.join('\n'),
      userOutput: 'The output of your code will show up here.',
      isRunningTests: false,
      userData: new UserData({ username: '' }),
      testResults: new TestResults([]),
      challengeNumber: 0,
      currChallenge: new Challenge(Challenges.challenges[0]),
      currChallengeStartedAt: 0,
      nextChallenge: '',

      // Timer handling
      isTimerFinished: false,
      timerMaxValue: timerDefaultValue,
      timeLeft: timerDefaultValue,
      timerStart: 0,

      // Session control variables
      isSessionFinished: false,
      sessionScore: 0
    };
  }

  const nextState = Object.assign({}, state);

  switch (action.type) {
    case MODAL_CLOSE: {
      nextState.modal = false;
      break;
    }
    case TESTS_STARTED: {
      nextState.isRunningTests = true;
      break;
    }
    case TESTS_FINISHED: {
      nextState.isRunningTests = false;
      nextState.testResults = new TestResults(action.testResults);
      break;
    }
    case CODE_CHANGED: {
      nextState.code = action.code;
      break;
    }
    case NEXT_CHALLENGE: {
      nextState.sessionScore = state.sessionScore + getScoreForChallenge(state.currChallenge);
      nextState.currChallenge = state.nextChallenge;
      nextState.currChallengeStartedAt = action.startTime;
      nextState.title = state.nextChallenge.getTitle();
      nextState.description = state.nextChallenge.getDescription();
      nextState.code = state.nextChallenge.getSeed().join('\n');
      nextState.testResults = new TestResults([]);
      nextState.challengeNumber++;
      nextState.userOutput = '';
      nextState.nextChallenge = new Challenge(Challenges.challenges[state.challengeNumber + 1]);
      break;
    }
    case OUTPUT_CHANGED: {
      nextState.userOutput = action.userOutput;
      break;
    }
    case TIMER_MAX_VALUE_CHANGED: {
      nextState.timerMaxValue = action.timerMaxValue;
      break;
    }
    case TIMER_STARTED: {
      nextState.isTimerFinished = false;
      nextState.timeLeft = timerDefaultValue;
      nextState.timerStart = action.startTime;
      break;
    }
    case START_CHALLENGE: {
      nextState.title = state.currChallenge.getTitle();
      nextState.description = state.currChallenge.getDescription();
      nextState.code = state.currChallenge.getSeed().join('\n');
      nextState.challengeNumber++;
      nextState.nextChallenge = new Challenge(Challenges.challenges[state.challengeNumber + 1]);
      nextState.isSessionFinished = false;
      nextState.currChallengeStartedAt = action.startTime;
      break;
    }
    case TIMER_UPDATED: {
      const timeNow = action.timeNow;
      nextState.timeLeft = state.timerMaxValue - (timeNow - state.timerStart);
      break;
    }
    case STOP_TIMER: {
      nextState.isTimerFinished = true;
      nextState.timeLeft = 0;
      break;
    }
    case FINISH_SESSION: {
      nextState.isSessionFinished = true;
      break;
    }
    default: console.log('ERROR. ArcadeReducer default reached.');
  }

  return nextState;
}

{'fraction_non_alphanumeric': 0.10798063795457366, 'fraction_numerical': 0.0038475859501054983, 'mean_word_length': 2.9078564500484965, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '249533', 'n_tokens_mistral': 2594, 'n_tokens_neox': 2452, 'n_words': 519}