proper game start logic

This commit is contained in:
Ben
2021-05-05 02:38:17 +01:00
parent 2e089810bf
commit 66edbbf51e
4 changed files with 66 additions and 19 deletions

View File

@@ -116,7 +116,7 @@ function onGameBegin(socket, args)
return;
}
console.log(args);
}

View File

@@ -73,7 +73,7 @@ let ActiveGames = [];
function BeginGame(lobby)
{
// game uses the owners language
// game uses the owners language - assumes it's valid
const gameowner = Registrar.GetUserByUID(lobby.owneruid);
let tilebag = Dist.GenerateStartStateDistribution(gameowner.locale);
@@ -101,11 +101,22 @@ function BeginGame(lobby)
}
}
const gamestate = {
playeruid: -1,
turn: 0,
outcome: {
valid: false
},
oldboardtiles: [],
boardtiles: []
};
ActiveGames[lobby.uid] = {
uid: lobby.uid,
locale: gameowner.locale,
players: players,
turn: 0,
gamestates: [gamestate],
tilebag: tilebag,
tileset: Dist.GetTileSet(gameowner.locale)
};
@@ -114,9 +125,38 @@ function BeginGame(lobby)
}
/*
TURN OBJECT
TURN OBJECT - Un-filled in GAMESTATE object
{
// UID of the player that played the turn
playeruid: uid,
turn: int,
// Generated after turn is processed
outcome: {
valid: bool,
points: pointsgained,
words: [{
word: word,
points: points,
tiles: [{
pos: {x: x, y: y},
modifier: modifier,
letter: letter,
score: int
}]
}],
}
oldboardtiles: [{
pos: {x: x, y: y},
modifier: modifier,
letter: letter,
score: int
}]
boardtiles: [{
pos: {x: x, y: y},
modifier: modifier,
letter: letter,
score: int
}]
}
NOTES
- Turns are handled a little weird, client sends turn on turn end and
@@ -135,12 +175,6 @@ function ExchangeTiles(tileset, tilesToExchange)
}
// does not alter tileset
function SelectTilesFromBag(tileset, num)
{
}
module.exports = {
// Game validation exports

View File

@@ -160,6 +160,14 @@ function GetUserbyConnection(connectionid)
return false;
}
function GetConnectionByUser(useruid)
{
for (const user in OnlineUsers)
if (OnlineUsers[user].uid === useruid && OnlineUsers[user].state === 'CONNECTED')
return OnlineUsers[user].connectionid;
return false;
}
// TODO: User intent
function UserConnect(useruid, connectionid, intent)
{
@@ -209,6 +217,7 @@ module.exports = {
// Get user connection exports
GetUserbyConnection: GetUserbyConnection,
GetConnectionByUser: GetConnectionByUser,
// Change user connection state exports
UserConnect: UserConnect,

View File

@@ -416,14 +416,18 @@ function LobbyUpdateCallback(user, lobby, state)
// send the client their user as well as the rest of the game
function EmitGameBegin(game)
{
// TODO: consider not sending all users the entire game state
// due to cheating
io.to(game.uid).emit('game-begin', {
game: game
});
// Instead of using io.to(room), i'm sending an individual packet to everyone
// in the game so that i can customise the game object that they recieve
for (const user of game.players)
{
const gameuser = game.players.filter(i => i.uid === user.uid)[0];
const gameuserconnection = Game.Registrar.GetConnectionByUser(gameuser.uid);
// for (const user of game.players)
// {
// const gameuser = game.players.filter(i => i.uid === user.uid)[0];
// }
// TODO: consider not sending all users the entire game state
// due to cheating
io.to(gameuserconnection).emit('game-begin', {
game: game,
gameuser: gameuser
})
}
}