redoing how i deal with turns

This commit is contained in:
Ben
2021-05-10 15:33:50 +01:00
parent fa564614c2
commit 04a1a0ba36
8 changed files with 82 additions and 34 deletions

View File

@@ -181,9 +181,10 @@ function BeginGame(lobby)
{
let t, r;
do {
// TODO: this goes out of range
r = Math.floor(Math.random() * tilebag.length + 1);
t = tilebag[r];
} while (t === null)
} while (t === undefined)
tilebag.splice(r, 1);
players[player].activetiles.push(t);
}
@@ -260,28 +261,37 @@ NOTES
function PlayTurn(gameuid, playeruid, turn)
{
const game = ActiveGames[gameuid];
ActiveGames[gameuid].gamestates.push(turn);
const turninfo = gameNextTurn(gameuid);
ActiveGames[gameuid].gamestates.push(turn);
// give user new tiles
console.log(game);
return [turn, turninfo];
}
function SkipTurn(gameuid, playeruid)
{
gameNextTurn(gameuid);
const turninfo = gameNextTurn(gameuid);
// get last game state
const turn = {
playeruid:
};
return [turn, turninfo];
}
function gameNextTurn(gameuid)
{
const PlayerCount = ActiveGames[gameuid].players.length;
ActiveGames[gameuid].turn++;
ActiveGames[gameuid].turn %= PlayerCount;
ActiveGames[gameuid].turntotal++;
const playerCount = ActiveGames[gameuid].players.length;
let newTurn = ActiveGames[gameuid].turn += 1;
newTurn = ActiveGames[gameuid].turn % PlayerCount;
const newTotalTurn = ActiveGames[gameuid].turntotal += 1;
return {
// i forgot why this is an object, there's more attributes i forgot about
turnplayer: ActiveGames[gameuid].players[ActiveGames[gameuid].turn],
newTurn: newTurn,
newTotalTurn: newTotalTurn
};
}

View File

@@ -401,17 +401,27 @@ function GamePlayTurn(socket, args)
return;
}
Logger.game(`USER ${user.uid} (${user.name}) IS ATTEMPTING TO PLAY A TURN IN GAME ${game.uid}`);
Logger.game(`USER ${user.uid} (${user.username}) IS ATTEMPTING TO PLAY A TURN IN GAME ${game.uid}`);
if (args.skip === true)
{
const {outcome, turninfo} = Game.Logic.SkipTurn(game.uid, user.uid);
const [outcome, turninfo] = Game.Logic.SkipTurn(game.uid, user.uid);
io.to(game.uid).emit('game-turn-processed', {
outcome: outcome
});
const nextuser = Game.Registrar.GetConnectionByUser(turninfo.turnplayer.uid);
io.to(game.uid).emit('game-turn-start', {
turninfo: turninfo
});
io.to(nextuser).emit('game-your-turn');
} else
{
// TODO: validate args
const [outcome, turninfo] = Game.Logic.PlayTurn(game.uid, user.uid, args)
// give user new tiles
// process errorsq
io.to(game.uid).emit('game-turn-processed', {