Automatically choose a random word after 10 seconds
This commit is contained in:
parent
7e28a2d9d9
commit
fd7637230b
14
js/game.js
14
js/game.js
|
@ -112,7 +112,19 @@ function onReceiveMessage(messageText)
|
|||
{
|
||||
//TODO: Mark word finders, mark this player
|
||||
let playerItem = document.createElement("li");
|
||||
playerItem.innerText = (player == message.data.drawer ? player + " ✏️" : player);
|
||||
playerItem.innerText = player;
|
||||
if(player == nickname)
|
||||
{
|
||||
playerItem.innerText += " (You)";
|
||||
}
|
||||
if(player == message.data.drawer)
|
||||
{
|
||||
playerItem.innerText += " ✏️";
|
||||
}
|
||||
if(message.data.wordFinders.includes(player))
|
||||
{
|
||||
playerItem.style.color = "green";
|
||||
}
|
||||
playerList.appendChild(playerItem);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ module doodul.game;
|
|||
|
||||
import std.algorithm : find, canFind, remove, map, filter;
|
||||
import std.array : array;
|
||||
import std.random : choice;
|
||||
|
||||
import vibe.vibe;
|
||||
|
||||
|
@ -26,36 +27,37 @@ void game(HTTPServerRequest request, HTTPServerResponse response)
|
|||
}
|
||||
|
||||
|
||||
void sendGameState(Player p, Game game)
|
||||
{
|
||||
struct GameState
|
||||
{
|
||||
string drawer;
|
||||
string[] players;
|
||||
int numberOfRounds;
|
||||
int currentRound;
|
||||
bool ended;
|
||||
string word;
|
||||
string[] wordFinders;
|
||||
int secondsLeft;
|
||||
}
|
||||
auto gameState = GameState(game.currentRound.drawer.nickname,
|
||||
game.players.map!(p => p.nickname).array,
|
||||
game.lobby.numberOfRounds,
|
||||
game.currentRound.number,
|
||||
game.ended,
|
||||
p.role != PlayerRole.guesser ? game.currentRound.currentWord : game.currentRound.redactedWord,
|
||||
game.players.filter!(p => p.role == PlayerRole.wordFinder).map!(p => p.nickname).array,
|
||||
game.currentRound.secondsLeft);
|
||||
|
||||
p.sendMessage("setGameState", gameState.serializeToJson);
|
||||
}
|
||||
|
||||
void wsGame(scope WebSocket socket)
|
||||
{
|
||||
Player player;
|
||||
Game game;
|
||||
Lobby lobby;
|
||||
|
||||
auto sendGameState = delegate(Player p) {
|
||||
struct GameState
|
||||
{
|
||||
string drawer;
|
||||
string[] players;
|
||||
int numberOfRounds;
|
||||
int currentRound;
|
||||
bool ended;
|
||||
string word;
|
||||
string[] wordFinders;
|
||||
int secondsLeft;
|
||||
}
|
||||
auto gameState = GameState(game.currentRound.drawer.nickname,
|
||||
game.players.map!(p => p.nickname).array,
|
||||
lobby.numberOfRounds,
|
||||
game.currentRound.number,
|
||||
game.ended,
|
||||
p.role != PlayerRole.guesser ? game.currentRound.currentWord : game.currentRound.redactedWord,
|
||||
game.players.filter!(p => p.role == PlayerRole.wordFinder).map!(p => p.nickname).array,
|
||||
game.currentRound.secondsLeft);
|
||||
|
||||
p.sendMessage("setGameState", gameState.serializeToJson);
|
||||
};
|
||||
|
||||
while(socket.connected)
|
||||
{
|
||||
auto messageText = socket.receiveText();
|
||||
|
@ -110,7 +112,7 @@ void wsGame(scope WebSocket socket)
|
|||
|
||||
foreach(p; game.players)
|
||||
{
|
||||
sendGameState(p);
|
||||
sendGameState(p, game);
|
||||
}
|
||||
break;
|
||||
case "setStartPoint":
|
||||
|
@ -144,7 +146,7 @@ void wsGame(scope WebSocket socket)
|
|||
|
||||
foreach(p; game.players)
|
||||
{
|
||||
sendGameState(p);
|
||||
sendGameState(p, game);
|
||||
}
|
||||
|
||||
if(game.players.filter!(p => p.role == PlayerRole.guesser).empty)
|
||||
|
@ -205,7 +207,7 @@ void wsGame(scope WebSocket socket)
|
|||
{
|
||||
p.role = PlayerRole.guesser;
|
||||
}
|
||||
sendGameState(p);
|
||||
sendGameState(p, game);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
@ -220,7 +222,7 @@ void wsGame(scope WebSocket socket)
|
|||
|
||||
foreach(p; game.players)
|
||||
{
|
||||
sendGameState(p);
|
||||
sendGameState(p, game);
|
||||
}
|
||||
//TODO: Check if player was drawer
|
||||
//TODO: If no players left, pause game?
|
||||
|
@ -235,9 +237,12 @@ class Game
|
|||
bool started;
|
||||
bool ended;
|
||||
|
||||
this(int numberOfRounds)
|
||||
Lobby lobby;
|
||||
|
||||
this(Lobby lobby, int numberOfRounds)
|
||||
{
|
||||
roundsLeft = numberOfRounds;
|
||||
this.lobby = lobby;
|
||||
this.roundsLeft = numberOfRounds;
|
||||
}
|
||||
|
||||
void start()
|
||||
|
@ -336,6 +341,25 @@ class Round
|
|||
}
|
||||
|
||||
drawer.sendMessage("setWordOptions", wordOptions.serializeToJson);
|
||||
|
||||
setTimer(10.seconds, delegate() {
|
||||
if(!currentWord.empty)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
game.currentRound.currentWord = choice(wordOptions);
|
||||
game.currentRound.timer = setTimer(1.seconds, &game.currentRound.tick, true);
|
||||
|
||||
foreach(p; game.players)
|
||||
{
|
||||
if(p.role != PlayerRole.drawer)
|
||||
{
|
||||
p.role = PlayerRole.guesser;
|
||||
}
|
||||
sendGameState(p, game);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void tick()
|
||||
|
|
|
@ -107,7 +107,7 @@ void wsLobby(scope WebSocket socket)
|
|||
continue;
|
||||
}
|
||||
|
||||
lobby.game = new Game(lobby.numberOfRounds);
|
||||
lobby.game = new Game(lobby, lobby.numberOfRounds);
|
||||
|
||||
foreach(p; lobby.players)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue