<commit_before>import { database } from './firebase';
import { updatePlayer } from './players';

// DB Refs
export const challengesDb = database().ref('challenges');

export const challengesDbByCustom = custom => database().ref(`challenges${custom}`);

// DB CRUD
export const createChallenge = async ({ displayName, playerDisplayNames }) => {
  if (!playerDisplayNames || playerDisplayNames.length === 0) {
    throw new Error('Must have at least one player to create a challenge!');
  }

  const newChallengeRef = challengesDb.push();
  newChallengeRef.set({
    displayName,
    players: playerDisplayNames.map(name => ({ [name]: true }))
  }, err => {
    if (err) {
      throw new Error(err);
    }
  });
  return newChallengeRef;
};

export const findChallengeByChallengeIdOnce = challengeId =>
  challengesDbByCustom(`/${challengeId}`).once('value');

// Flows
export const createChallengeForPlayer = async (challengeDisplayName, playerDisplayName) => {
  const challenge = await createChallenge({ displayName: challengeDisplayName, playerDisplayNames: [playerDisplayName] });
  return updatePlayer(`${playerDisplayName}/challenges`, { [challenge.key]: true });
};

export const getChallengesByChallengeKeys = async challengeKeys => {
  const requests = challengeKeys.map(challengeKey => findChallengeByChallengeIdOnce(challengeKey));
  const challengesRefs = await Promise.all(requests);
  return challengesRefs.map(challengeRef => Object.assign(challengeRef.val(), { key: challengeRef.key }));
};<commit_msg>Added challenges in descending order<commit_after>import { database } from './firebase';
import { updatePlayer } from './players';

// DB Refs
export const challengesDb = database().ref('challenges');

export const challengesDbByCustom = custom => database().ref(`challenges${custom}`);

// DB CRUD
export const createChallenge = async ({ displayName, playerDisplayNames }) => {
  if (!playerDisplayNames || playerDisplayNames.length === 0) {
    throw new Error('Must have at least one player to create a challenge!');
  }

  const newChallengeRef = challengesDb.push();
  newChallengeRef.set({
    displayName,
    players: playerDisplayNames.map(name => ({ [name]: true }))
  }, err => {
    if (err) {
      throw new Error(err);
    }
  });
  return newChallengeRef;
};

export const findChallengeByChallengeIdOnce = challengeId =>
  challengesDbByCustom(`/${challengeId}`).once('value');

// Flows
export const createChallengeForPlayer = async (challengeDisplayName, playerDisplayName) => {
  const challenge = await createChallenge({ displayName: challengeDisplayName, playerDisplayNames: [playerDisplayName] });
  return updatePlayer(`${playerDisplayName}/challenges`, { [challenge.key]: true });
};

export const getChallengesByChallengeKeys = async challengeKeys => {
  const requests = challengeKeys.map(challengeKey => findChallengeByChallengeIdOnce(challengeKey));
  const challengesRefs = await Promise.all(requests);
  return challengesRefs.map(challengeRef => Object.assign(challengeRef.val(), { key: challengeRef.key })).reverse();
};
{'fraction_non_alphanumeric': 0.13261648745519714, 'fraction_numerical': 0.0006516780710329097, 'mean_word_length': 5.422594142259414, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '3552878', 'n_tokens_mistral': 897, 'n_tokens_neox': 836, 'n_words': 209}