add move persistence to gameService
This commit is contained in:
parent
2e613daa1b
commit
a324ece0af
5 changed files with 64 additions and 19 deletions
|
@ -10,8 +10,9 @@ const show = async (req, res, next) => {
|
||||||
|
|
||||||
// TODO Promise.all()
|
// TODO Promise.all()
|
||||||
const game = await gameQueries.findGameById(gameId);
|
const game = await gameQueries.findGameById(gameId);
|
||||||
const record = await moveQueries.findGameRecord(gameId);
|
// const record = await moveQueries.findGameRecord(gameId);
|
||||||
res.status(200).json({game, record})
|
// console.log(record)
|
||||||
|
res.status(200).json({game})
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
res.status(500).json(err);
|
res.status(500).json(err);
|
||||||
|
|
|
@ -10,8 +10,9 @@ const getAll = async (req, res, next) => {
|
||||||
res.status(200).json({rooms: [...publicRooms]})
|
res.status(200).json({rooms: [...publicRooms]})
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (err) {
|
catch (e) {
|
||||||
res.status(500).json(err);
|
console.log(e)
|
||||||
|
res.status(500).json(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,9 +29,9 @@ const show = async (req, res, next) => {
|
||||||
const body = {currentRoom, messages, roomGames};
|
const body = {currentRoom, messages, roomGames};
|
||||||
res.status(200).json(body);
|
res.status(200).json(body);
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (e) {
|
||||||
console.log(err)
|
console.log(e)
|
||||||
res.status(500).json(err);
|
res.status(500).json(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,10 +2,35 @@ const knex = require('../db');
|
||||||
|
|
||||||
const findGameRecord = async (gameId) => {
|
const findGameRecord = async (gameId) => {
|
||||||
return await knex('move')
|
return await knex('move')
|
||||||
.where({'game': gameId, 'game_record': true})
|
.where({ 'game': gameId, 'game_record': true })
|
||||||
.select('*');
|
.select('player', 'point_x', 'point_y', 'number')
|
||||||
|
.orderBy('number')
|
||||||
|
.then(record => record.map(({player, point_x, point_y}) => ({player, pos: { x: point_x, y: point_y } }) ))
|
||||||
|
// .then(res => res)
|
||||||
|
}
|
||||||
|
|
||||||
|
// id: 1, player: 'black', point_x: 3, point_y: 3, number: 1, game_record: true, game: 1, prior_move: null
|
||||||
|
|
||||||
|
const addMove = async ({ gameId, player, x, y, gameRecord, priorMove }) => {
|
||||||
|
const number = priorMove + 1;
|
||||||
|
let result;
|
||||||
|
try {
|
||||||
|
result = await knex('move')
|
||||||
|
.returning('*')
|
||||||
|
.insert({ game: gameId, player, point_x: x, point_y: y, number, game_record: gameRecord, prior_move: priorMove })
|
||||||
|
.then(res => res);
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
result = e
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
console.log(result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
findGameRecord
|
findGameRecord,
|
||||||
|
addMove
|
||||||
}
|
}
|
|
@ -1,25 +1,40 @@
|
||||||
const Game = require('./Game').Game;
|
const Game = require('./Game').Game;
|
||||||
|
const moveQueries = require('../data/queries/move');
|
||||||
|
|
||||||
const gamesInProgress = { }
|
const gamesInProgress = { }
|
||||||
|
|
||||||
const storeGame = (game) => {
|
const storeGame = (game) => {
|
||||||
gamesInProgress[game.id] = Game(game);
|
gamesInProgress[game.id] = Game(game);
|
||||||
|
return gamesInProgress[game.id];
|
||||||
}
|
}
|
||||||
|
|
||||||
const initGame = ({id, gameRecord = [], ...gameData}) => {
|
const initGame = ({id, gameRecord = [], ...gameData}) => {
|
||||||
if (gamesInProgress[id]) return getDataForUI(id);
|
if (gamesInProgress[id]) return getDataForUI(id);
|
||||||
gamesInProgress[id] = Game({ gameData, gameRecord })
|
if (gameRecord.length) {
|
||||||
gamesInProgress[id].initGame();
|
console.log('here')
|
||||||
|
gamesInProgress[id] = Game({ gameData, gameRecord })
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
gamesInProgress[id] = Game({gameData}).initGame();
|
||||||
|
}
|
||||||
return getDataForUI(id)
|
return getDataForUI(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
const makeMove = ({id, move}) => {
|
const makeMove = async ({id, move}) => {
|
||||||
// console.log(id, move)
|
if (!gamesInProgress[id]) storeGame({id}).initGame();
|
||||||
// console.log(gamesInProgress[id])
|
|
||||||
if (!gamesInProgress[id]) storeGame({id});
|
|
||||||
gamesInProgress[id] = gamesInProgress[id].makeMove(move)
|
gamesInProgress[id] = gamesInProgress[id].makeMove(move)
|
||||||
if (gamesInProgress[id].success === false) return { message: 'illegal move' };
|
if (gamesInProgress[id].success === false) return { message: 'illegal move' };
|
||||||
return getDataForUI(id)
|
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;
|
||||||
|
try {
|
||||||
|
moveDbResult = await moveQueries.addMove(moveInsert);
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
gamesInProgress[id].returnToMove(-1);
|
||||||
|
} finally {
|
||||||
|
return getDataForUI(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const getDataForUI = (id) => {
|
const getDataForUI = (id) => {
|
||||||
|
|
|
@ -2,7 +2,8 @@
|
||||||
const socketIO = require('socket.io');
|
const socketIO = require('socket.io');
|
||||||
const io = socketIO({ cookie: false });
|
const io = socketIO({ cookie: false });
|
||||||
|
|
||||||
const gameQueries = require('./data/queries/game');
|
// const gameQueries = require('./data/queries/game');
|
||||||
|
const moveQueries = require('./data/queries/move');
|
||||||
const gameServices = require('./services/gameServices');
|
const gameServices = require('./services/gameServices');
|
||||||
|
|
||||||
io.on('connection', async socket=> {
|
io.on('connection', async socket=> {
|
||||||
|
@ -20,7 +21,9 @@ io.on('connection', async socket=> {
|
||||||
const game = `game-${data.game.id}`;
|
const game = `game-${data.game.id}`;
|
||||||
socket.join(game, async () => {
|
socket.join(game, async () => {
|
||||||
// ! temp
|
// ! temp
|
||||||
await gameServices.initGame({id: data.game.id})
|
const gameRecord = await moveQueries.findGameRecord(data.game.id);
|
||||||
|
console.log(gameRecord)
|
||||||
|
await gameServices.initGame({id: data.game.id, gameRecord})
|
||||||
// ! end-temp
|
// ! end-temp
|
||||||
const { board, ...meta } = await gameServices.getDataForUI(data.game.id);
|
const { board, ...meta } = await gameServices.getDataForUI(data.game.id);
|
||||||
io.of(room).to(game).emit('game_connected', { board, meta });
|
io.of(room).to(game).emit('game_connected', { board, meta });
|
||||||
|
|
Loading…
Reference in a new issue