<commit_before>/*@ngInject*/
function GameService (StorageService, ngAudio) {
  var currentUser;
  var previousPosition;
  var sounds = {
    'buzzer': ngAudio.load('sounds/buzzer.mp3'),
    'first': ngAudio.load('sounds/first.mp3'),
    '1': ngAudio.load('sounds/1.mp3'),
    '2': ngAudio.load('sounds/2.mp3'),
    '3': ngAudio.load('sounds/3.mp3'),
    '4': ngAudio.load('sounds/4.mp3'),
    '5': ngAudio.load('sounds/5.mp3')
  };

  var service = {
    registerUser: registerUser,
    setCurrentUser: setCurrentUser,
    getCurrentUser: getCurrentUser,
    getUsers: getUsers,
    setUserTime: setUserTime,
    getBestTime: getBestTime,
    getUserPosition: getUserPosition,
    resetCurrentUser: resetCurrentUser,
    removeUser: removeUser,
    playSound: playSound,
    stopSound: stopSound,
    toggleSound: toggleSound,
    isSoundMuted: isSoundMuted,
    isFirst: isFirst
  };

  function registerUser (user) {
    var newUser = StorageService.save(user);
    if (angular.isUndefined(newUser)) {
      return;
    }
    service.setCurrentUser(newUser);
    return newUser;
  }

  function setCurrentUser (user) {
    currentUser = user;
    previousPosition = service.getUserPosition(user);
  }

  function getCurrentUser () {
    return currentUser;
  }

  function getUsers () {
    return StorageService.list();
  }

  function setUserTime (user, time) {
    user.time = time;
    StorageService.update(user);
    service.setCurrentUser(user);
    sortUsers();
  }

  function getBestTime () {
    var users = service.getUsers();
    var index = (previousPosition === 1 && users.length > 1) ? 1 : 0;
    return users[index].time;
  }

  function getUserPosition (user) {
    var index = StorageService.indexOf(user);
    return index === -1 ? undefined : index + 1;
  }

  function resetCurrentUser () {
    currentUser = undefined;
  }

  function removeUser (user) {
    return StorageService.remove(user);
  }

  /**
   * Play a sound.
   *
   * @param  {String} sound The sound string id.
   */
  function playSound (sound) {<commit_msg>Sort users before setting current user to fix issue with previous position when user played again.<commit_after>/*@ngInject*/
function GameService (StorageService, ngAudio) {
  var currentUser;
  var previousPosition;
  var sounds = {
    'buzzer': ngAudio.load('sounds/buzzer.mp3'),
    'first': ngAudio.load('sounds/first.mp3'),
    '1': ngAudio.load('sounds/1.mp3'),
    '2': ngAudio.load('sounds/2.mp3'),
    '3': ngAudio.load('sounds/3.mp3'),
    '4': ngAudio.load('sounds/4.mp3'),
    '5': ngAudio.load('sounds/5.mp3')
  };

  var service = {
    registerUser: registerUser,
    setCurrentUser: setCurrentUser,
    getCurrentUser: getCurrentUser,
    getUsers: getUsers,
    setUserTime: setUserTime,
    getBestTime: getBestTime,
    getUserPosition: getUserPosition,
    resetCurrentUser: resetCurrentUser,
    removeUser: removeUser,
    playSound: playSound,
    stopSound: stopSound,
    toggleSound: toggleSound,
    isSoundMuted: isSoundMuted,
    isFirst: isFirst
  };

  function registerUser (user) {
    var newUser = StorageService.save(user);
    if (angular.isUndefined(newUser)) {
      return;
    }
    service.setCurrentUser(newUser);
    return newUser;
  }

  function setCurrentUser (user) {
    currentUser = user;
    previousPosition = service.getUserPosition(user);
  }

  function getCurrentUser () {
    return currentUser;
  }

  function getUsers () {
    return StorageService.list();
  }

  function setUserTime (user, time) {
    user.time = time;
    StorageService.update(user);
    sortUsers();
    service.setCurrentUser(user);
  }

  function getBestTime () {
    var users = service.getUsers();
    if (users.length <= 1) {
      return undefined;
    }
    var index = (previousPosition === 1) ? 1 : 0;
    return users[index].time;
  }

  function getUserPosition (user) {
    var index = StorageService.indexOf(user);
    return index === -1 ? undefined : index + 1;
  }

  function resetCurrentUser () {
    currentUser = undefined;
  }

  function removeUser (user) {
    return StorageService.remove(user);
  }

  /**
   * Play a sound.
   *
   * @param  {String} sound The sound string id.
   */
  function playSound (sound) {
{'fraction_non_alphanumeric': 0.125531914893617, 'fraction_numerical': 0.010874704491725768, 'mean_word_length': 3.252261306532663, 'pattern_counts': {'":': 0, '<': 4, '<?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': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3494785', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1352, 'n_words': 298}