Skip to content

Commit

Permalink
Merge pull request #359 from ItsSammyM/lobbystate-on-frontend
Browse files Browse the repository at this point in the history
Lobbystate on frontend
  • Loading branch information
ItsSammyM authored Dec 18, 2023
2 parents 3424c8d + 4c0d4f8 commit 448507c
Show file tree
Hide file tree
Showing 34 changed files with 625 additions and 429 deletions.
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"entriy",
"inno",
"killable",
"Maselli",
"necronomicon",
"repr",
"Retributionist",
Expand Down
107 changes: 50 additions & 57 deletions client/src/components/ChatMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ export default function ChatElement(props: {message: ChatMessage}): ReactElement
}

if (
find(GAME_MANAGER.gameState.myName ?? "").test(message.text) ||
GAME_MANAGER.state.stateType === "game" &&
(find(GAME_MANAGER.state.players[GAME_MANAGER.state.myIndex!].name ?? "").test(message.text) ||
(
GAME_MANAGER.gameState.myIndex !== null &&
find("" + (GAME_MANAGER.gameState.myIndex + 1)).test(message.text)
)
GAME_MANAGER.state.stateType === "game" &&
GAME_MANAGER.state.myIndex !== null &&
find("" + (GAME_MANAGER.state.myIndex + 1)).test(message.text)
))

) {
style += " mention";
Expand All @@ -54,12 +56,26 @@ export default function ChatElement(props: {message: ChatMessage}): ReactElement
return <StyledText className={"chat-message " + style}>{text}</StyledText>;
}

function playerListToString(playerList: PlayerIndex[]): string {

return playerList.map((playerIndex) => {
if(GAME_MANAGER.state.stateType === "game")
return GAME_MANAGER.state.players[playerIndex].toString();
else
return "ERROR: outside game"
}).join(", ");
}

export function translateChatMessage(message: ChatMessage): string {
if(GAME_MANAGER.state.stateType !== "game"){
return "ERROR: outside game"
}

switch (message.type) {
case "normal":
if(message.messageSender.type === "player"){
return translate("chatMessage.normal",
GAME_MANAGER.gameState.players[message.messageSender.player].toString(),
GAME_MANAGER.state.players[message.messageSender.player].toString(),
message.text
);
} else {
Expand All @@ -70,14 +86,14 @@ export function translateChatMessage(message: ChatMessage): string {
}
case "whisper":
return translate("chatMessage.whisper",
GAME_MANAGER.gameState.players[message.fromPlayerIndex].toString(),
GAME_MANAGER.gameState.players[message.toPlayerIndex].toString(),
GAME_MANAGER.state.players[message.fromPlayerIndex].toString(),
GAME_MANAGER.state.players[message.toPlayerIndex].toString(),
message.text
);
case "broadcastWhisper":
return translate("chatMessage.broadcastWhisper",
GAME_MANAGER.gameState.players[message.whisperer].toString(),
GAME_MANAGER.gameState.players[message.whisperee].toString(),
GAME_MANAGER.state.players[message.whisperer].toString(),
GAME_MANAGER.state.players[message.whisperee].toString(),
);
case "roleAssignment":
return translate("chatMessage.roleAssignment",
Expand Down Expand Up @@ -109,7 +125,7 @@ export function translateChatMessage(message: ChatMessage): string {
}

return translate(message.grave.will.length === 0 ? "chatMessage.playerDied.noWill": "chatMessage.playerDied",
GAME_MANAGER.gameState.players[message.grave.playerIndex].toString(),
GAME_MANAGER.state.players[message.grave.playerIndex].toString(),
graveRoleString,
deathCause
);
Expand All @@ -128,66 +144,63 @@ export function translateChatMessage(message: ChatMessage): string {
case "voted":
if (message.votee !== null) {
return translate("chatMessage.voted",
GAME_MANAGER.gameState.players[message.voter].toString(),
GAME_MANAGER.gameState.players[message.votee].toString(),
GAME_MANAGER.state.players[message.voter].toString(),
GAME_MANAGER.state.players[message.votee].toString(),
);
} else {
return translate("chatMessage.voted.cleared",
GAME_MANAGER.gameState.players[message.voter].toString(),
GAME_MANAGER.state.players[message.voter].toString(),
);
}
case "playerOnTrial":
return translate("chatMessage.playerOnTrial",
GAME_MANAGER.gameState.players[message.playerIndex].toString(),
GAME_MANAGER.state.players[message.playerIndex].toString(),
);
case "judgementVote":
return translate("chatMessage.judgementVote",
GAME_MANAGER.gameState.players[message.voterPlayerIndex].toString(),
GAME_MANAGER.state.players[message.voterPlayerIndex].toString(),
);
case "judgementVerdict":
return translate("chatMessage.judgementVerdict",
GAME_MANAGER.gameState.players[message.voterPlayerIndex].toString(),
GAME_MANAGER.state.players[message.voterPlayerIndex].toString(),
translate("verdict."+message.verdict.toLowerCase())
);
case "trialVerdict":
return translate("chatMessage.trialVerdict",
GAME_MANAGER.gameState.players[message.playerOnTrial].toString(),
GAME_MANAGER.state.players[message.playerOnTrial].toString(),
message.innocent>=message.guilty?translate("verdict.innocent"):translate("verdict.guilty"),
message.innocent,
message.guilty
);
case "targeted":
if (message.targets.length > 0) {
return translate("chatMessage.targeted",
GAME_MANAGER.gameState.players[message.targeter].toString(),
message.targets.map((target) => GAME_MANAGER.gameState.players[target].toString()).join(", ")
);
GAME_MANAGER.state.players[message.targeter].toString(),
playerListToString(message.targets));
} else {
return translate("chatMessage.targeted.cleared",
GAME_MANAGER.gameState.players[message.targeter].toString(),
GAME_MANAGER.state.players[message.targeter].toString(),
);
}
case "mayorRevealed":
return translate("chatMessage.mayorRevealed",
GAME_MANAGER.gameState.players[message.playerIndex].toString(),
GAME_MANAGER.state.players[message.playerIndex].toString(),
);
case "jailedTarget":
return translate("chatMessage.jailedTarget",
GAME_MANAGER.gameState.players[message.playerIndex].toString(),
GAME_MANAGER.state.players[message.playerIndex].toString(),
);
case "jailedSomeone":
return translate("chatMessage.jailedSomeone",
GAME_MANAGER.gameState.players[message.playerIndex].toString()
GAME_MANAGER.state.players[message.playerIndex].toString()
);
case "deputyKilled":
return translate("chatMessage.deputyKilled",
GAME_MANAGER.gameState.players[message.shotIndex].toString()
GAME_MANAGER.state.players[message.shotIndex].toString()
);
case "jailorDecideExecute":
if (message.targets.length > 0) {
return translate("chatMessage.jailorDecideExecute",
message.targets.map((target) => GAME_MANAGER.gameState.players[target].toString()).join(", ")
);
return translate("chatMessage.jailorDecideExecute", playerListToString(message.targets));
} else {
return translate("chatMessage.jailorDecideExecute.nobody");
}
Expand All @@ -200,44 +213,28 @@ export function translateChatMessage(message: ChatMessage): string {
if (message.players.length === 0) {
return translate("chatMessage.lookoutResult.nobody");
} else {
return translate("chatMessage.lookoutResult",
message.players.map(playerIndex =>
GAME_MANAGER.gameState.players[playerIndex].toString()
).join(", ")
);
return translate("chatMessage.lookoutResult", playerListToString(message.players));
}
case "spyMafiaVisit":
if (message.players.length === 0) {
return translate("chatMessage.spyMafiaVisit.nobody");
} else {
return translate("chatMessage.spyMafiaVisit",
message.players.map(playerIndex =>
GAME_MANAGER.gameState.players[playerIndex].toString()
).join(", ")
);
return translate("chatMessage.spyMafiaVisit", playerListToString(message.players));
}
case "spyBug":
return translate("chatMessage.spyBug."+message.bug);
case "trackerResult":
if (message.players.length === 0) {
return translate("chatMessage.trackerResult.nobody");
} else {
return translate("chatMessage.trackerResult",
message.players.map(playerIndex =>
GAME_MANAGER.gameState.players[playerIndex].toString()
).join(", ")
);
return translate("chatMessage.trackerResult", playerListToString(message.players));
}
case "seerResult":
return translate("chatMessage.seerResult." + (message.enemies ? "enemies" : "friends"));
case "psychicEvil":
return translate("chatMessage.psychicEvil",message.players.map(playerIndex =>
GAME_MANAGER.gameState.players[playerIndex].toString()
).join(", "));
return translate("chatMessage.psychicEvil", playerListToString(message.players));
case "psychicGood":
return translate("chatMessage.psychicGood",message.players.map(playerIndex =>
GAME_MANAGER.gameState.players[playerIndex].toString()
).join(", "));
return translate("chatMessage.psychicGood", playerListToString(message.players));
case "playerRoleAndWill":
return translate("chatMessage.playersRoleAndWill",
translate("role."+message.role+".name"),
Expand All @@ -251,23 +248,19 @@ export function translateChatMessage(message: ChatMessage): string {
translate("chatMessage.consigliereResult.role", translate("role."+message.role+".name")),
visitedNobody
? translate("chatMessage.consigliereResult.visited.nobody")
: translate("chatMessage.consigliereResult.visited",
message.visited.map((playerIndex) => GAME_MANAGER.gameState.players[playerIndex].toString()).join(", "),
),
: translate("chatMessage.consigliereResult.visited", playerListToString(message.visited)),
visitedByNobody
? translate("chatMessage.consigliereResult.visitedBy.nobody")
: translate("chatMessage.consigliereResult.visitedBy",
message.visitedBy.map((playerIndex) => GAME_MANAGER.gameState.players[playerIndex].toString()).join(", "),
)
: translate("chatMessage.consigliereResult.visitedBy", playerListToString(message.visitedBy))
);
case "silenced":
return translate("chatMessage.silenced");
case "mediumSeance":
return translate("chatMessage.mediumSeance", GAME_MANAGER.gameState.players[message.player].toString());
return translate("chatMessage.mediumSeance", GAME_MANAGER.state.players[message.player].toString());
case "witchedYou":
return translate("chatMessage.witchedYou" + (message.immune ? ".immune" : ""));
case "playerWithNecronomicon":
return translate("chatMessage.playerWithNecronomicon", GAME_MANAGER.gameState.players[message.playerIndex].toString());
return translate("chatMessage.playerWithNecronomicon", GAME_MANAGER.state.players[message.playerIndex].toString());
case "deputyShotSomeoneSurvived":
case "deathCollectedSouls":
case "arsonistCleanedSelf":
Expand Down
15 changes: 8 additions & 7 deletions client/src/components/StyledText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,14 @@ function getKeywordData(): KeywordDataMap {

const DATA = require("../resources/keywords.json");

for(const player of GAME_MANAGER.gameState.players) {
keywordData[player.toString()] = [
{ style: "keyword-player-number", replacement: (player.index + 1).toString() },
{ replacement: " " },
{ style: "keyword-player", replacement: player.name }
];
}
if(GAME_MANAGER.state.stateType === "game")
for(const player of GAME_MANAGER.state.players) {
keywordData[player.toString()] = [
{ style: "keyword-player-number", replacement: (player.index + 1).toString() },
{ replacement: " " },
{ style: "keyword-player", replacement: player.name }
];
}

for(const role of Object.keys(ROLES)){
const data = DATA["faction." + getFactionFromRole(role as Role)];
Expand Down
5 changes: 3 additions & 2 deletions client/src/game/gameManager.d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ export type GameManager = {
playerId: number | null,


// state: State,
gameState: GameState,
state: State,
getMyName(): string | undefined,
getMyHost(): boolean | undefined,

server: Server,
listeners: StateListener[],
Expand Down
48 changes: 33 additions & 15 deletions client/src/game/gameManager.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createGameState } from "./gameState";
import Anchor from "./../menu/Anchor";
import StartMenu from "./../menu/main/StartMenu";
import GAME_MANAGER from "./../index";
Expand All @@ -18,11 +17,25 @@ export function createGameManager(): GameManager {
roomCode: null,
playerId: null,

// state: {
// stateType: "outsideLobby"
// },
state: {
stateType: "disconnected"
},

getMyName() {
if (gameManager.state.stateType === "lobby")
return gameManager.state.players.get(gameManager.state.myId!)?.name;
if (gameManager.state.stateType === "game")
return gameManager.state.players[gameManager.state.myIndex!]?.name;
return undefined;
},
getMyHost() {
if (gameManager.state.stateType === "lobby")
return gameManager.state.players.get(gameManager.state.myId!)?.host;
if (gameManager.state.stateType === "game")
return gameManager.state.players[gameManager.state.myIndex!]?.host;
return undefined;
},

gameState : createGameState(),

server : createServer(),

Expand Down Expand Up @@ -54,7 +67,7 @@ export function createGameManager(): GameManager {
},

leaveGame() {
if (this.gameState.inGame) {
if (this.state.stateType === "game") {
this.server.sendPacket({type: "leave"});
}
// Set URL to main menu and refresh
Expand Down Expand Up @@ -216,16 +229,21 @@ export function createGameManager(): GameManager {
},

tick(timePassedMs) {
if (!gameManager.gameState.ongoing) return;

const newTimeLeft = gameManager.gameState.timeLeftMs - timePassedMs;
if (Math.floor(newTimeLeft / 1000) < Math.floor(gameManager.gameState.timeLeftMs / 1000)) {
gameManager.invokeStateListeners("tick");
}
gameManager.gameState.timeLeftMs = newTimeLeft;
if (gameManager.gameState.timeLeftMs < 0) {
gameManager.gameState.timeLeftMs = 0;
if (gameManager.state.stateType === "game"){
if (!gameManager.state.still_ticking) return;

const newTimeLeft = gameManager.state.timeLeftMs - timePassedMs;
if (Math.floor(newTimeLeft / 1000) < Math.floor(gameManager.state.timeLeftMs / 1000)) {
gameManager.invokeStateListeners("tick");
}
gameManager.state.timeLeftMs = newTimeLeft;
if (gameManager.state.timeLeftMs < 0) {
gameManager.state.timeLeftMs = 0;
}
}



},
}
return gameManager;
Expand Down
Loading

1 comment on commit 448507c

@vercel
Copy link

@vercel vercel bot commented on 448507c Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mafia-game – ./

mafia-game-itssammym.vercel.app
mafia-game.vercel.app
mafia-game-git-00x-main-itssammym.vercel.app

Please sign in to comment.