<commit_before>
const cmd = require('../core/protocol');
const ServerPlayer = require('./ServerPlayer');
const {shuffle} = require('../core/util');

class GameDriver {

	constructor(room) {
		this.room = room;
		this.players = [];
		this.capacity = 8;
	}

	async start() {
		this.room.broadcast(cmd.StartGame);
		this.arrangeSeats();
		await this.arrangeGenerals();
		this.room.broadcast(cmd.ToBattle);
	}

	arrangeSeats() {
		let users = this.room.users;
		this.players = Array.from(users);
		for (let i = 0; i < this.players.length; i++) {
			let player = new ServerPlayer(this.players[i]);
			player.seat = i + 1;
			this.players[i] = player;
		}

		let args = [];
		for (let player of this.players) {
			args.push({
				uid: player.user.id,
				seat: player.seat,
				name: 'Seat ' + player.seat,
			});
		}
		this.room.broadcast(cmd.ArrangeSeats, args);
	}

	async arrangeGenerals() {
		let generals = [];

		const Collections = require('../collection');
		for (let Collection of Collections) {
			generals.push(...Collection.generals);
		}

		let candidateNum = 7;
		let players = this.players;
		shuffle(generals);
		generals = generals.splice(0, candidateNum * players.length);

		let sessions = players.map(player => ({
			player: player,
			candidates: [],
			selected: [],
		}));
		for (let i = 0, j = 0; i < generals.length; i++) {
			let session = sessions[j];
			session.candidates.push(generals[i]);
			j++;
			if (j >= sessions.length) {
				j = 0;
			}
		}

		let replies = [];
		for (let s of sessions) {
			replies.push(
				s.player.askForGeneral(s.candidates, {
					timeout: 5, //TO-DO: make this longer
					num: 2,
					sameKingdom: true,
					forced: true,
				})
				.then(generals => {
					s.selected = generals;
				})
			);
		}

		await Promise.all(replies);
		for (let s of sessions) {
			let player = s.player;
			let headGeneral = s.selected[0];
			let deputyGeneral = s.selected[1];
			player.setHeadGeneral(headGeneral);
			player.broadcastProperty('headGeneral', headGeneral.toJSON());
			player.setDeputyGeneral(deputyGeneral);
			player.broadcastProperty('deputyGeneral', deputyGeneral.toJSON());
		}
	}

}

module.exports = GameDriver;<commit_msg>Update hp and max hp<commit_after>
const cmd = require('../core/protocol');
const ServerPlayer = require('./ServerPlayer');
const {shuffle} = require('../core/util');

class GameDriver {

	constructor(room) {
		this.room = room;
		this.players = [];
		this.capacity = 8;
	}

	async start() {
		this.room.broadcast(cmd.StartGame);
		this.arrangeSeats();
		await this.arrangeGenerals();
		this.room.broadcast(cmd.ToBattle);
	}

	arrangeSeats() {
		let users = this.room.users;
		this.players = Array.from(users);
		for (let i = 0; i < this.players.length; i++) {
			let player = new ServerPlayer(this.players[i]);
			player.seat = i + 1;
			this.players[i] = player;
		}

		let args = [];
		for (let player of this.players) {
			args.push({
				uid: player.user.id,
				seat: player.seat,
				name: 'Seat ' + player.seat,
			});
		}
		this.room.broadcast(cmd.ArrangeSeats, args);
	}

	async arrangeGenerals() {
		let generals = [];

		const Collections = require('../collection');
		for (let Collection of Collections) {
			generals.push(...Collection.generals);
		}

		let candidateNum = 7;
		let players = this.players;
		shuffle(generals);
		generals = generals.splice(0, candidateNum * players.length);

		let sessions = players.map(player => ({
			player: player,
			candidates: [],
			selected: [],
		}));
		for (let i = 0, j = 0; i < generals.length; i++) {
			let session = sessions[j];
			session.candidates.push(generals[i]);
			j++;
			if (j >= sessions.length) {
				j = 0;
			}
		}

		let replies = [];
		for (let s of sessions) {
			replies.push(
				s.player.askForGeneral(s.candidates, {
					timeout: 5, //TO-DO: make this longer
					num: 2,
					sameKingdom: true,
					forced: true,
				})
				.then(generals => {
					s.selected = generals;
				})
			);
		}

		await Promise.all(replies);
		for (let s of sessions) {
			let player = s.player;
			let headGeneral = s.selected[0];
			let deputyGeneral = s.selected[1];
			player.setHeadGeneral(headGeneral);
			player.broadcastProperty('headGeneral', headGeneral.toJSON());
			player.setDeputyGeneral(deputyGeneral);
			player.broadcastProperty('deputyGeneral', deputyGeneral.toJSON());

			let hp = headGeneral.headMaxHp() + deputyGeneral.deputyMaxHp();
			if (hp & 1) {
				player.halfHp = true;
			}
			hp = Math.floor(hp / 2);
			player.setHp(hp);
			player.setMaxHp(hp);
			player.broadcastProperty('maxHp', hp);
			player.broadcastProperty('hp', hp);
		}
	}

}

module.exports = GameDriver;
{'fraction_non_alphanumeric': 0.1589589158958916, 'fraction_numerical': 0.005592600559260056, 'mean_word_length': 3.155495978552279, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '5782548', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1618, 'n_words': 345}