<commit_before>angular.module('app.services', [])

.factory('Game', ['$q', '$http', 'store', 'socket', '$timeout', 'ionicToast', '$rootScope', '$state', function(
  $q,
  $http,
  store,
  socket,
  $timeout,
  ionicToast,
  $rootScope,
  $state){

  var obj = {

    ai: false,

    submitting: false,

    started: false,

    revealing: false,

    response: '',

    topic: '',

    isReader: false,

    isCreator: false,

    submitting_response: false,

    submitting_topic: false,

    submitting_guess: false,

    game: {

      id: undefined,

      max_score: undefined,

      winner: undefined,

      completed: false,

      skip_if_guessed: undefined,

      voice: undefined,

      guesser: undefined,

      //Array of objects, with id, name, guessed, and score
      players: [],

      //Object containing all pertinent info for the round
      current_round: {

        reader_name: undefined,

        reader_id: undefined,

        //All answers in, ready to start guessing
        ready: false,

        //Array of objects, with id, text, guessed, and user_id
        responses: [

        ],

        //Current round's topic
        topic: ''
      },

      //Array of rounds from the server
      rounds: []
    },

    // The guess a user is going to make
    guess: {
      user: undefined,
      response: undefined
    },

    saved_topics: {
      is_empty: true
    },

    checkGame: function () {
      var remote_id = store.get('remote_id');
      if (!remote_id) {
        return $q.resolve();
      }
      return $http({
        url: Config.api + '/users/' + remote_id,
        method: 'get'
      })
      .then(function (response) {
        var current_game_id = response.data.user.current_game_id;
        store.set('current_game_id', current_game_id);
        obj.game.id = current_game_id;
        return !!current_game_id;
      })
      .catch(function (error) {
        console.log('check game error', error);
      })
    },

    getGame: function () {
      if (obj.saved_topics.is_empty) {
        socket.emit('retrieve saved', store.get('remote_id'));
      }
      obj.game.id = store.get('current_game_id');
      if (!obj.game.id) {
        obj.resetGame();
        return $q.resolve();
      }
      return $http({
        url: Config.api + '/games/' + obj.game.id,
        method: 'get'
      })
      .then(function (response) {
        var results = response.data.results;
        obj.game.players = results.players;
        obj.game.guesser = results.game.guesser;
        obj.game.winner = results.game.winner;
        obj.game.ai = results.game.ai;
        var my_id = store.get('remote_id');
        for (var i = 0; i < obj.game.players.length; i++) {
          if (obj.game.players[i].id === my_id) {
            obj.isCreator = obj.game.players[i].creator ? true : false;
            break;
          }
        }
        obj.game.rounds = results.rounds;
        var lastRound = obj.game.rounds[0];
        if (lastRound && lastRound.reader_id === store.get('remote_id')) {
          obj.isReader = true;
        } else {
          obj.isReader = false;
        }
        obj.game.current_round = lastRound;
        if (obj.game.current_round && ((obj.game.current_round.responses.length === obj.game.players.length) || (obj.game.ai && obj.game.current_round.responses.length === obj.game.players.length - 1))) {
          var randomizedResponses = [];
          var numResponses = obj.game.current_round.responses.length;
          for (var i = 0; i < numResponses; i++) {
            randomizedResponses.push(obj.game.current_round.responses.splice(Math.floor(Math.random() * obj.game.current_round.responses.length), 1)[0]);
          }
          obj.game.current_round.responses = JSON.parse(JSON.stringify(randomizedResponses));
          obj.game.current_round.ready = true;
        }
        obj.submitting_response = false;
        obj.submitting_topic = false;
        obj.submitting_guess = false;
        obj.game.completed = response.data.results.game.completed;
        obj.started = response.data.results.game.started;
        obj.game.id = response.data.results.game.id;
        obj.game.max_score = response.data.results.game.max_score;
        obj.game.skip_if_guessed = response.data.results.game.skip_if_guessed;
        obj.game.voice = response.data.results.game.voice;
        socket.emit('room', obj.game.id);
      })
      .catch(function (error) {
        return error;
      })
    },

    resetGame: function () {
      obj.isReader = false;
      obj.started = false;
      obj.isCreator = false;
      obj.submitting_response = false;
      obj.submitting_topic = false;
      obj.submitting_guess = false;
      obj.game.players = [];
      obj.game.rounds = [];
      obj.game.topic = '';
      obj.game.id = undefined;
      obj.game.guesser = undefined;
      obj.game.response = '';
      obj.game.winner = undefined;
      obj.game.completed = false;
      obj.submitting = false;
      obj.game.voice = undefined;
      obj.game.currentRound  = {

        reader_name: undefined,

        reader_id: undefined,

        //All answers in, ready to start guessing
        ready: false,

        skip_if_guessed: undefined,

        //Array of objects, with id, text, guessed, and user_id<commit_msg>(chore) update reset game to reset ai setting<commit_after>angular.module('app.services', [])

.factory('Game', ['$q', '$http', 'store', 'socket', '$timeout', 'ionicToast', '$rootScope', '$state', function(
  $q,
  $http,
  store,
  socket,
  $timeout,
  ionicToast,
  $rootScope,
  $state){

  var obj = {

    ai: false,

    submitting: false,

    started: false,

    revealing: false,

    response: '',

    topic: '',

    isReader: false,

    isCreator: false,

    submitting_response: false,

    submitting_topic: false,

    submitting_guess: false,

    game: {

      id: undefined,

      max_score: undefined,

      winner: undefined,

      completed: false,

      skip_if_guessed: undefined,

      voice: undefined,

      guesser: undefined,

      //Array of objects, with id, name, guessed, and score
      players: [],

      //Object containing all pertinent info for the round
      current_round: {

        reader_name: undefined,

        reader_id: undefined,

        //All answers in, ready to start guessing
        ready: false,

        //Array of objects, with id, text, guessed, and user_id
        responses: [

        ],

        //Current round's topic
        topic: ''
      },

      //Array of rounds from the server
      rounds: []
    },

    // The guess a user is going to make
    guess: {
      user: undefined,
      response: undefined
    },

    saved_topics: {
      is_empty: true
    },

    checkGame: function () {
      var remote_id = store.get('remote_id');
      if (!remote_id) {
        return $q.resolve();
      }
      return $http({
        url: Config.api + '/users/' + remote_id,
        method: 'get'
      })
      .then(function (response) {
        var current_game_id = response.data.user.current_game_id;
        store.set('current_game_id', current_game_id);
        obj.game.id = current_game_id;
        return !!current_game_id;
      })
      .catch(function (error) {
        console.log('check game error', error);
      })
    },

    getGame: function () {
      if (obj.saved_topics.is_empty) {
        socket.emit('retrieve saved', store.get('remote_id'));
      }
      obj.game.id = store.get('current_game_id');
      if (!obj.game.id) {
        obj.resetGame();
        return $q.resolve();
      }
      return $http({
        url: Config.api + '/games/' + obj.game.id,
        method: 'get'
      })
      .then(function (response) {
        var results = response.data.results;
        obj.game.players = results.players;
        obj.game.guesser = results.game.guesser;
        obj.game.winner = results.game.winner;
        obj.game.ai = results.game.ai;
        var my_id = store.get('remote_id');
        for (var i = 0; i < obj.game.players.length; i++) {
          if (obj.game.players[i].id === my_id) {
            obj.isCreator = obj.game.players[i].creator ? true : false;
            break;
          }
        }
        obj.game.rounds = results.rounds;
        var lastRound = obj.game.rounds[0];
        if (lastRound && lastRound.reader_id === store.get('remote_id')) {
          obj.isReader = true;
        } else {
          obj.isReader = false;
        }
        obj.game.current_round = lastRound;
        if (obj.game.current_round && ((obj.game.current_round.responses.length === obj.game.players.length) || (obj.game.ai && obj.game.current_round.responses.length === obj.game.players.length - 1))) {
          var randomizedResponses = [];
          var numResponses = obj.game.current_round.responses.length;
          for (var i = 0; i < numResponses; i++) {
            randomizedResponses.push(obj.game.current_round.responses.splice(Math.floor(Math.random() * obj.game.current_round.responses.length), 1)[0]);
          }
          obj.game.current_round.responses = JSON.parse(JSON.stringify(randomizedResponses));
          obj.game.current_round.ready = true;
        }
        obj.submitting_response = false;
        obj.submitting_topic = false;
        obj.submitting_guess = false;
        obj.game.completed = response.data.results.game.completed;
        obj.started = response.data.results.game.started;
        obj.game.id = response.data.results.game.id;
        obj.game.max_score = response.data.results.game.max_score;
        obj.game.skip_if_guessed = response.data.results.game.skip_if_guessed;
        obj.game.voice = response.data.results.game.voice;
        socket.emit('room', obj.game.id);
      })
      .catch(function (error) {
        return error;
      })
    },

    resetGame: function () {
      obj.ai = false;
      obj.isReader = false;
      obj.started = false;
      obj.isCreator = false;
      obj.submitting_response = false;
      obj.submitting_topic = false;
      obj.submitting_guess = false;
      obj.game.players = [];
      obj.game.rounds = [];
      obj.game.topic = '';
      obj.game.id = undefined;
      obj.game.guesser = undefined;
      obj.game.response = '';
      obj.game.winner = undefined;
      obj.game.completed = false;
      obj.submitting = false;
      obj.game.voice = undefined;
      obj.game.currentRound  = {

        reader_name: undefined,

        reader_id: undefined,

        //All answers in, ready to start guessing
        ready: false,

        skip_if_guessed: undefined,

        //Array of objects, with id, text, guessed, and user_id
{'fraction_non_alphanumeric': 0.13091216216216217, 'fraction_numerical': 0.0011261261261261261, 'mean_word_length': 2.0943670150987224, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '2197016', 'n_tokens_mistral': 3402, 'n_tokens_neox': 3283, 'n_words': 676}