2020-05-30 23:01:13 +00:00
|
|
|
const Game = require("./Game").Game;
|
2020-01-31 05:03:27 +00:00
|
|
|
|
2020-06-20 23:25:09 +00:00
|
|
|
const GameService = ({ moveQueries, gameQueries }) => {
|
2020-05-30 23:01:13 +00:00
|
|
|
const storeGame = (game) => {
|
|
|
|
gamesInProgress[game.id] = Game(game);
|
|
|
|
return gamesInProgress[game.id];
|
|
|
|
};
|
|
|
|
const gamesInProgress = {};
|
2020-01-31 05:03:27 +00:00
|
|
|
|
2020-07-07 23:52:45 +00:00
|
|
|
const storeMove = (gameId) => async ({ player, pos: { x, y } }) => {
|
|
|
|
let move = { player, pos: { x, y } };
|
|
|
|
try {
|
|
|
|
if (moveQueries) {
|
|
|
|
const { id } = await moveQueries.addMove({
|
|
|
|
gameId,
|
|
|
|
player,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
gameRecord: true,
|
|
|
|
priorMove: null,
|
|
|
|
});
|
|
|
|
move.id = id;
|
|
|
|
move.success = true;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
move.success = false;
|
|
|
|
} finally {
|
|
|
|
return move;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
return {
|
|
|
|
initGame({ id, gameRecord = [], ...gameData }) {
|
|
|
|
if (gamesInProgress[id]) return this.getDataForUI(id);
|
|
|
|
if (gameRecord.length) {
|
|
|
|
gamesInProgress[id] = Game({ gameData, gameRecord });
|
|
|
|
} else {
|
|
|
|
gamesInProgress[id] = Game({ gameData }).initGame();
|
|
|
|
}
|
|
|
|
return this.getDataForUI(id);
|
|
|
|
},
|
2020-01-31 05:03:27 +00:00
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
async makeMove({ id, move }) {
|
2020-05-30 23:43:41 +00:00
|
|
|
// check cache
|
|
|
|
if (!gamesInProgress[id]) {
|
|
|
|
try {
|
|
|
|
let gameRecord;
|
|
|
|
if (moveQueries) {
|
|
|
|
gameRecord = await moveQueries.findGameRecord(id);
|
|
|
|
}
|
|
|
|
storeGame({ id, gameRecord }).initGame();
|
|
|
|
} catch {
|
|
|
|
return { message: "error restoring game" };
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 23:52:45 +00:00
|
|
|
gamesInProgress[id] = await gamesInProgress[id].checkMove(move);
|
2020-05-30 23:01:13 +00:00
|
|
|
gamesInProgress[id] = gamesInProgress[id].makeMove(move);
|
|
|
|
if (gamesInProgress[id].success === false)
|
|
|
|
return { message: "illegal move" };
|
|
|
|
try {
|
|
|
|
if (moveQueries) {
|
|
|
|
const priorMove = gamesInProgress[id].gameRecord.length;
|
|
|
|
const moveInsert = {
|
|
|
|
gameId: id,
|
|
|
|
player: move.player,
|
|
|
|
x: move.pos.x,
|
|
|
|
y: move.pos.y,
|
|
|
|
gameRecord: true,
|
|
|
|
priorMove,
|
|
|
|
};
|
|
|
|
let moveDbResult;
|
|
|
|
moveDbResult = await moveQueries.addMove(moveInsert);
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
gamesInProgress[id].returnToMove(-1);
|
|
|
|
} finally {
|
|
|
|
return this.getDataForUI(id);
|
|
|
|
}
|
|
|
|
},
|
2020-01-31 05:03:27 +00:00
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
getDataForUI: (id) => {
|
|
|
|
return {
|
|
|
|
board: gamesInProgress[id].legalMoves,
|
2020-06-08 00:06:26 +00:00
|
|
|
territory: gamesInProgress[id].territory,
|
2020-05-30 23:01:13 +00:00
|
|
|
...gamesInProgress[id].getMeta(),
|
|
|
|
};
|
|
|
|
},
|
2020-01-31 05:03:27 +00:00
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
dropGame: (id) => {
|
|
|
|
return { message: `${delete gamesInProgress[id]}` };
|
|
|
|
},
|
2020-05-05 06:22:50 +00:00
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
getAllGames: () => {
|
|
|
|
return gamesInProgress;
|
|
|
|
},
|
2020-06-07 20:47:09 +00:00
|
|
|
|
|
|
|
resign: ({ id, player }) => {
|
2020-07-01 18:42:16 +00:00
|
|
|
// add resign gamesQueries
|
2020-06-07 20:47:09 +00:00
|
|
|
return gamesInProgress[id].submitResign(player).getMeta();
|
|
|
|
},
|
2020-06-08 00:06:26 +00:00
|
|
|
|
|
|
|
async pass({ id, player }) {
|
|
|
|
gamesInProgress[id] = gamesInProgress[id].submitPass(player);
|
|
|
|
if (gamesInProgress[id].success === false)
|
|
|
|
return { message: "illegal move" };
|
|
|
|
try {
|
|
|
|
if (moveQueries) {
|
|
|
|
const priorMove = gamesInProgress[id].gameRecord.length;
|
|
|
|
const movePass = {
|
|
|
|
gameId: id,
|
|
|
|
player,
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
gameRecord: true,
|
|
|
|
priorMove,
|
|
|
|
};
|
|
|
|
let moveDbResult;
|
|
|
|
moveDbResult = await moveQueries.addMove(movePass);
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
gamesInProgress[id].returnToMove(-1);
|
|
|
|
} finally {
|
|
|
|
return this.getDataForUI(id);
|
|
|
|
}
|
|
|
|
},
|
2020-06-17 18:59:21 +00:00
|
|
|
|
|
|
|
toggleTerritory({ id, point }) {
|
|
|
|
gamesInProgress[id] = gamesInProgress[id].toggleTerritory(point);
|
|
|
|
return this.getDataForUI(id);
|
|
|
|
},
|
2020-06-20 23:25:09 +00:00
|
|
|
|
|
|
|
async endGame({ id }) {
|
|
|
|
gamesInProgress[id] = gamesInProgress[id].endGame();
|
2020-07-01 18:42:16 +00:00
|
|
|
const { winner, score, playerState } = gamesInProgress[id];
|
|
|
|
const { bCaptures, wCaptures } = playerState;
|
|
|
|
const winType = winner > 0 ? "B+" : "W+";
|
2020-06-20 23:25:09 +00:00
|
|
|
try {
|
|
|
|
if (gameQueries) {
|
2020-07-01 18:42:16 +00:00
|
|
|
const result = await gameQueries.endGame({
|
|
|
|
id,
|
|
|
|
winType,
|
|
|
|
score,
|
|
|
|
bCaptures,
|
|
|
|
wCaptures,
|
|
|
|
});
|
|
|
|
console.log(result);
|
2020-06-20 23:25:09 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
return this.getDataForUI(id);
|
|
|
|
},
|
2020-05-30 23:01:13 +00:00
|
|
|
};
|
|
|
|
};
|
2020-01-31 05:03:27 +00:00
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
module.exports = GameService;
|