<commit_before>'use strict';

const _ = require('lodash');
const uuid = require('uuid');
const generateName = require('adjective-adjective-animal');
const config = require('nconf');

const logger = require('src/util/logger.js')('game');
const EventEmitter = require('src/util/eventEmitter.js');
const Cache = require('src/util/cache.js');

const PlayerDB = require('./player.data.js');
const GameSetup = require('./game.setup.js');

const GameDB = {

	prepareKey: (id) => {
		return `game-${id}`;
	},

	getKeyPattern: () => {
		return 'game-*';
	},

	prepareNewData: () => {
		return generateName({
			adjectives: 1,
			format: 'lower',
		}).then((name) => {
			return {
				id: uuid.v4(),
				name: name,
				state: 'waiting for players',
				players: [],
			};
		});
	},

	create: () => {
		return GameDB.build()
			.then((game) => {
				return GameDB.save(game);
			});
	},

	build: () => {
		return GameDB.prepareNewData();
	},

	save: (game) => {
		if (!game.id) {
			return Promise.reject(new Error('Game to save has no ID.'));
		}
		const key = GameDB.prepareKey(game.id);
		return Cache.put(key, game)
			.then((game) => {
				EventEmitter.emit('game|save', game);
				const expiry = config.get('data:game:expirySeconds');
				logger.trace('expiring game after', expiry);
				return Cache.expire(key, expiry)
					.then(() => {
						return game;
					});
			});
	},<commit_msg>Add num* fields to game data.<commit_after>'use strict';

const _ = require('lodash');
const uuid = require('uuid');
const generateName = require('adjective-adjective-animal');
const config = require('nconf');

const logger = require('src/util/logger.js')('game');
const EventEmitter = require('src/util/eventEmitter.js');
const Cache = require('src/util/cache.js');

const PlayerDB = require('./player.data.js');
const GameSetup = require('./game.setup.js');

const GameDB = {

	prepareKey: (id) => {
		return `game-${id}`;
	},

	getKeyPattern: () => {
		return 'game-*';
	},

	prepareNewData: () => {
		return generateName({
			adjectives: 1,
			format: 'lower',
		}).then((name) => {
			return {
				id: uuid.v4(),
				name: name,
				state: 'waiting for players',
				players: [],
				numSuccesses: 0,
				numFails: 0,
			};
		});
	},

	create: () => {
		return GameDB.build()
			.then((game) => {
				return GameDB.save(game);
			});
	},

	build: () => {
		return GameDB.prepareNewData();
	},

	save: (game) => {
		if (!game.id) {
			return Promise.reject(new Error('Game to save has no ID.'));
		}
		const key = GameDB.prepareKey(game.id);
		return Cache.put(key, game)
			.then((game) => {
				EventEmitter.emit('game|save', game);
				const expiry = config.get('data:game:expirySeconds');
				logger.trace('expiring game after', expiry);
				return Cache.expire(key, expiry)
					.then(() => {
						return game;
					});
			});
	},
{'fraction_non_alphanumeric': 0.19886765746638357, 'fraction_numerical': 0.0021231422505307855, 'mean_word_length': 3.3965785381026437, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '4188488', 'n_tokens_mistral': 1145, 'n_tokens_neox': 980, 'n_words': 205}