add support for ending game from game record to game service
This commit is contained in:
parent
cbf9d461ba
commit
c4192f2e33
3 changed files with 30 additions and 4 deletions
|
@ -181,13 +181,18 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => {
|
|||
this.kos = [];
|
||||
},
|
||||
};
|
||||
|
||||
if (gameRecord.length) {
|
||||
// play through all the moves
|
||||
return gameRecord.reduce(
|
||||
const game = gameRecord.reduce(
|
||||
(game, move) => game.makeMove(move),
|
||||
Game({ gameData }).initGame()
|
||||
);
|
||||
// ? why is this being wrapped?
|
||||
if (gameData.gameData.winner) {
|
||||
const { winner, score } = gameData.gameData;
|
||||
return game.manualEnd({ winner, score });
|
||||
}
|
||||
return game;
|
||||
}
|
||||
return {
|
||||
winner: gameData.winner || null,
|
||||
|
@ -389,6 +394,14 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => {
|
|||
this.playerState.bScore - (this.playerState.wScore + this.komi);
|
||||
return { ...this, score, winner: score > 0 ? 1 : -1 };
|
||||
},
|
||||
|
||||
// for playing historic games
|
||||
manualEnd: function ({ winner, score }) {
|
||||
this.turn = 0;
|
||||
this.winner = winner;
|
||||
this.score = score;
|
||||
return this;
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
@ -11,7 +11,6 @@ const GameService = ({ moveQueries, gameQueries }) => {
|
|||
initGame({ id, gameRecord = [], ...gameData }) {
|
||||
if (gamesInProgress[id]) return this.getDataForUI(id);
|
||||
if (gameRecord.length) {
|
||||
console.log("here");
|
||||
gamesInProgress[id] = Game({ gameData, gameRecord });
|
||||
} else {
|
||||
gamesInProgress[id] = Game({ gameData }).initGame();
|
||||
|
|
|
@ -24,8 +24,22 @@ io.on("connection", async (socket) => {
|
|||
socket.on("connect_game", (data) => {
|
||||
const game = `game-${data.game.id}`;
|
||||
socket.join(game, async () => {
|
||||
const gameData = await gameQueries.findGameById(data.game.id);
|
||||
const convertWinType = (winType) => {
|
||||
if (winType.includes("B")) return 1;
|
||||
if (winType.includes("W")) return -1;
|
||||
if (winType.includes("0")) return "D";
|
||||
return "?";
|
||||
};
|
||||
gameData.winner = gameData.win_type
|
||||
? convertWinType(gameData.win_type)
|
||||
: 0;
|
||||
const gameRecord = await moveQueries.findGameRecord(data.game.id);
|
||||
await gameServices.initGame({ id: data.game.id, gameRecord });
|
||||
await gameServices.initGame({
|
||||
id: data.game.id,
|
||||
gameRecord,
|
||||
gameData,
|
||||
});
|
||||
const { board, ...meta } = await gameServices.getDataForUI(
|
||||
data.game.id
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue