Skip to content

Commit

Permalink
rework lobby_chat
Browse files Browse the repository at this point in the history
  • Loading branch information
salvoilmiosi committed Sep 28, 2024
1 parent a37e812 commit ae3eca4
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 48 deletions.
8 changes: 6 additions & 2 deletions src/Components/LobbyChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ export default function LobbyChat({ connection, lobbyState: { myUserId, users, c
const MessageTag = useCallback((props: ChatMessageState) => {
if (props.type === 'user') {
const pClass = props.user_id === myUserId ? 'text-right' : '';
return (<p className={pClass}><span className='username'>{clipUsername(props.username)}</span> : {props.message}</p>);
return <p className={pClass}><span className='username'>{clipUsername(props.username)}</span> : {props.message}</p>;
} else {
return props.message.split('\n').map((line, index) => <p key={index} className='server-message'>{line}</p>);
if (props.translated) {
return <p className='server-message'>{getLabel('chat', props.message, ...props.args)}</p>;
} else {
return props.message.split('\n').map((line, index) => <p key={index} className='server-message'>{line}</p>);
}
}
}, [myUserId]);

Expand Down
21 changes: 19 additions & 2 deletions src/Locale/English/Labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ export const LABELS_ENGLISH: LabelRegistry = {
},

lobby: {
USER_JOINED_LOBBY: (username) => `${username} joined the lobby`,
USER_LEFT_LOBBY: (username) => `${username} left the lobby`,
CONNECTION_ERROR: "Errore di connessione",
ERROR_INVALID_LOBBY: "Invalid Lobby ID",
ERROR_PLAYER_IN_LOBBY: "Player already in a lobby",
Expand All @@ -111,6 +109,25 @@ export const LABELS_ENGLISH: LabelRegistry = {
INVALID_COMMAND_NAME: "Invalid command name",
},

chat: {
HELP_DESCRIPTION: cmd => `${cmd} : print this message`,
USERS_DESCRIPTION: cmd => `${cmd} : print list of users in this lobby`,
KICK_DESCRIPTION: cmd => `${cmd} username_or_id : kick an user in this lobby`,
MUTE_DESCRIPTION: cmd => `${cmd} username_or_id : mute an user in this lobby`,
UNMUTE_DESCRIPTION: cmd => `${cmd} username_or_id : unmute an user in this lobby`,
GET_OPTIONS_DESCRIPTION: cmd => `${cmd} : print game options`,
SET_OPTION_DESCRIPTION: cmd => `${cmd} name value : set a game option`,
RESET_OPTIONS_DESCRIPTION: cmd => `${cmd} : reset game options`,
GIVE_CARD_DESCRIPTION: cmd => `${cmd} card_name : give yourself a card`,
GET_RNG_SEED_DESCRIPTION: cmd => `${cmd} : print rng seed (only during game over screen)`,
QUIT_DESCRIPTION: cmd => `${cmd} : disconnect from server`,

GAME_SEED: seed => `The game seed is ${seed}`,

USER_JOINED_LOBBY: username => `${username} joined the lobby`,
USER_LEFT_LOBBY: username => `${username} left the lobby`,
},

server: {
ERROR_CANNOT_CONNECT_TO_SERVER: "Cannot connect to the server",
ERROR_DISCONNECTED_FROM_SERVER: "Disconnected from the server",
Expand Down
21 changes: 19 additions & 2 deletions src/Locale/Italian/Labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ export const LABELS_ITALIAN: LabelRegistry = {
},

lobby: {
USER_JOINED_LOBBY: (username) => `${username} entra in lobby`,
USER_LEFT_LOBBY: (username) => `${username} esce dalla lobby`,
CONNECTION_ERROR: "Errore di connessione",
ERROR_INVALID_LOBBY: "ID lobby non valido",
ERROR_PLAYER_IN_LOBBY: "Giocatore già in una lobby",
Expand All @@ -111,6 +109,25 @@ export const LABELS_ITALIAN: LabelRegistry = {
INVALID_COMMAND_NAME: "Comando non valido",
},

chat: {
HELP_DESCRIPTION: cmd => `${cmd} : visualizza questo messaggio`,
USERS_DESCRIPTION: cmd => `${cmd} : visualizza la lista di tutti gli utenti in questa lobby`,
KICK_DESCRIPTION: cmd => `${cmd} nome_utente_o_id : caccia un utente da questa lobby`,
MUTE_DESCRIPTION: cmd => `${cmd} nome_utente_o_id : muta un utente in questa lobby`,
UNMUTE_DESCRIPTION: cmd => `${cmd} nome_utente_o_id : smuta un utente in questa lobby`,
GET_OPTIONS_DESCRIPTION: cmd => `${cmd} : visualizza le opzioni di gioco`,
SET_OPTION_DESCRIPTION: cmd => `${cmd} name value : modifica un'opzione di gioco`,
RESET_OPTIONS_DESCRIPTION: cmd => `${cmd} : resetta le opzioni di gioco`,
GIVE_CARD_DESCRIPTION: cmd => `${cmd} card_name : prendi una carta`,
GET_RNG_SEED_DESCRIPTION: cmd => `${cmd} : visualizza il seed di gioco (solo durante schermata di partita finita)`,
QUIT_DESCRIPTION: cmd => `${cmd} : disconnettiti dal server`,

GAME_SEED: seed => `Il seed di gioco è ${seed}`,

USER_JOINED_LOBBY: (username) => `${username} entra in lobby`,
USER_LEFT_LOBBY: (username) => `${username} esce dalla lobby`,
},

server: {
ERROR_CANNOT_CONNECT_TO_SERVER: "Impossibile connettersi al server",
ERROR_DISCONNECTED_FROM_SERVER: "Disconnessione dal server",
Expand Down
4 changes: 3 additions & 1 deletion src/Model/SceneState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export type ChatMessageState =
{
type: 'lobby',
message: string,
isRead: boolean
args: string[],
isRead: boolean,
translated: boolean
};

export interface LobbyState {
Expand Down
5 changes: 2 additions & 3 deletions src/Model/ServerMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ export interface LobbyRemoved {
lobby_id: LobbyId;
}

export type LobbyChatFlag = 'is_read';
export type LobbyChatFlag = 'is_read' | 'server_message' | 'translated';

export interface LobbyAddUser {
user_id: UserId;
username: string;
team: LobbyTeam;
flags: LobbyChatFlag[];
lifetime: Milliseconds;
}

Expand All @@ -61,6 +60,7 @@ export interface ChatMessage {
user_id: number;
username: string;
message: string;
args: string[];
flags: LobbyChatFlag[];
}

Expand All @@ -77,6 +77,5 @@ export type ServerMessage =
{lobby_remove_user: number} |
{lobby_kick: Empty} |
{lobby_chat: ChatMessage} |
{lobby_message: string} |
{game_update: GameUpdate} |
{game_started: Empty};
57 changes: 19 additions & 38 deletions src/Model/UseBangConnection.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useEffect, useMemo, useReducer } from "react";
import useEvent from "react-use-event-hook";
import getLabel from "../Locale/GetLabel";
import { GameUpdate } from "../Scenes/Game/Model/GameUpdate";
import { getUser } from "../Scenes/Lobby/Lobby";
import { UserValue } from "../Scenes/Lobby/LobbyUser";
import { LobbyValue } from "../Scenes/WaitingArea/LobbyElement";
import { deserializeImage, PROPIC_SIZE, serializeImage } from "../Utils/ImageSerial";
import { createUnionDispatch } from "../Utils/UnionUtils";
Expand All @@ -12,8 +11,7 @@ import { useSettings } from "./AppSettings";
import { ClientMessage } from "./ClientMessage";
import Env from "./Env";
import { defaultCurrentScene, LobbyState, sceneReducer, UpdateFunction } from "./SceneState";
import { LobbyAddUser, LobbyUpdate, LobbyUserPropic, ServerMessage } from "./ServerMessage";
import { UserValue } from "../Scenes/Lobby/LobbyUser";
import { ChatMessage, LobbyAddUser, LobbyUpdate, LobbyUserPropic, ServerMessage } from "./ServerMessage";

function handleUpdateLobbies({ lobby_id, name, num_players, num_spectators, max_players, state }: LobbyUpdate): UpdateFunction<LobbyValue[]> {
return lobbies => {
Expand All @@ -29,26 +27,18 @@ function handleUpdateLobbies({ lobby_id, name, num_players, num_spectators, max_
};
}

function handleLobbyAddUser({ user_id, username, team, flags, lifetime }: LobbyAddUser): UpdateFunction<LobbyState> {
function handleLobbyAddUser({ user_id, username, team, lifetime }: LobbyAddUser): UpdateFunction<LobbyState> {
return lobbyState => {
let chatMessages = lobbyState.chatMessages;
let users = lobbyState.users.slice();

const index = users.findIndex(user => user.id === user_id);
const newUser: UserValue = { id: user_id, name: username, team, lifetime }
if (index >= 0) {
users[index] = { ...users[index], ...newUser };
} else {
if (user_id >= 0) {
chatMessages = chatMessages.concat({
type: 'lobby',
message: getLabel('lobby', 'USER_JOINED_LOBBY', username),
isRead: flags.includes('is_read') || user_id === lobbyState.myUserId
});
}
users.push(newUser);
}
return { ...lobbyState, users, chatMessages };
return { ...lobbyState, users };
};
}

Expand All @@ -62,23 +52,21 @@ function handleLobbyUserPropic({ user_id, propic }: LobbyUserPropic): UpdateFunc
}

function handleLobbyRemoveUser(user_id: number): UpdateFunction<LobbyState> {
return lobbyState => ({
...lobbyState,
users: lobbyState.users.filter(user => user.id !== user_id)
});
}

function handleLobbyChat({ user_id, username, message, args, flags }: ChatMessage): UpdateFunction<LobbyState> {
return lobbyState => {
let users = lobbyState.users;
let chatMessages = lobbyState.chatMessages;

if (user_id >= 0) {
const user = getUser(users, user_id);
if (user) {
chatMessages = chatMessages.concat({
type: 'lobby',
message: getLabel('lobby', 'USER_LEFT_LOBBY', user.name),
isRead: false
});
}
let chatMessages = lobbyState.chatMessages.slice();
if (flags.includes('server_message')) {
chatMessages.push({ type: 'lobby', message, args, isRead: flags.includes('is_read'), translated: flags.includes('translated') });
} else {
chatMessages.push({ type: 'user', user_id, username, message, isRead: flags.includes('is_read') });
}

users = users.filter(user => user.id !== user_id);
return { ...lobbyState, users, chatMessages };
return { ...lobbyState, chatMessages };
};
}

Expand Down Expand Up @@ -173,15 +161,8 @@ export default function useBangConnection() {
lobby_kick() {
sceneDispatch({ gotoWaitingArea: {} });
},
lobby_chat({ user_id, username, message, flags }) {
sceneDispatch({ updateLobbyState: lobbyState => ({
...lobbyState, chatMessages: lobbyState.chatMessages.concat({ type:'user', user_id, username, message, isRead: flags.includes('is_read') })
}) });
},
lobby_message(message) {
sceneDispatch({ updateLobbyState: lobbyState => ({
...lobbyState, chatMessages: lobbyState.chatMessages.concat({ type:'lobby', message, isRead: false })
})});
lobby_chat(message) {
sceneDispatch({ updateLobbyState: handleLobbyChat(message) })
},
game_update(update) {
gameChannel.update(update);
Expand Down

0 comments on commit ae3eca4

Please sign in to comment.