add move persistence to gameService

This commit is contained in:
Sorrel Bri 2020-05-11 18:18:42 -07:00
parent 2e613daa1b
commit a324ece0af
5 changed files with 64 additions and 19 deletions

View file

@ -10,8 +10,9 @@ const show = async (req, res, next) => {
// TODO Promise.all()
const game = await gameQueries.findGameById(gameId);
const record = await moveQueries.findGameRecord(gameId);
res.status(200).json({game, record})
// const record = await moveQueries.findGameRecord(gameId);
// console.log(record)
res.status(200).json({game})
}
catch (err) {
res.status(500).json(err);

View file

@ -10,8 +10,9 @@ const getAll = async (req, res, next) => {
res.status(200).json({rooms: [...publicRooms]})
}
catch (err) {
res.status(500).json(err);
catch (e) {
console.log(e)
res.status(500).json(e);
}
}
@ -28,9 +29,9 @@ const show = async (req, res, next) => {
const body = {currentRoom, messages, roomGames};
res.status(200).json(body);
}
catch (err) {
console.log(err)
res.status(500).json(err);
catch (e) {
console.log(e)
res.status(500).json(e);
}
}

View file

@ -2,10 +2,35 @@ const knex = require('../db');
const findGameRecord = async (gameId) => {
return await knex('move')
.where({'game': gameId, 'game_record': true})
.select('*');
.where({ 'game': gameId, 'game_record': true })
.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 = {
findGameRecord
findGameRecord,
addMove
}

View file

@ -1,25 +1,40 @@
const Game = require('./Game').Game;
const moveQueries = require('../data/queries/move');
const gamesInProgress = { }
const storeGame = (game) => {
gamesInProgress[game.id] = Game(game);
return gamesInProgress[game.id];
}
const initGame = ({id, gameRecord = [], ...gameData}) => {
if (gamesInProgress[id]) return getDataForUI(id);
gamesInProgress[id] = Game({ gameData, gameRecord })
gamesInProgress[id].initGame();
if (gameRecord.length) {
console.log('here')
gamesInProgress[id] = Game({ gameData, gameRecord })
}
else {
gamesInProgress[id] = Game({gameData}).initGame();
}
return getDataForUI(id)
}
const makeMove = ({id, move}) => {
// console.log(id, move)
// console.log(gamesInProgress[id])
if (!gamesInProgress[id]) storeGame({id});
const makeMove = async ({id, move}) => {
if (!gamesInProgress[id]) storeGame({id}).initGame();
gamesInProgress[id] = gamesInProgress[id].makeMove(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) => {

View file

@ -2,7 +2,8 @@
const socketIO = require('socket.io');
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');
io.on('connection', async socket=> {
@ -20,7 +21,9 @@ io.on('connection', async socket=> {
const game = `game-${data.game.id}`;
socket.join(game, async () => {
// ! 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
const { board, ...meta } = await gameServices.getDataForUI(data.game.id);
io.of(room).to(game).emit('game_connected', { board, meta });