Skip to content

Commit

Permalink
remove LobbyInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
salvoilmiosi committed Oct 15, 2024
1 parent 19fdf2e commit 5dfbb2a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export default function App() {
/>;
case 'lobby':
return <LobbyScene
lobbyInfo={scene.lobbyInfo}
gameOptions={scene.gameOptions}
setGameOptions={setGameOptions}
lobbyState={scene.lobbyState}
connection={connection}
setGameOptions={setGameOptions}
/>;
case 'game':
return <GameScene
Expand Down
6 changes: 3 additions & 3 deletions src/Components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ function Header({ scene, settings, connection }: HeaderProps) {
<div className="flex items-center">
<span className="self-center text-2xl font-semibold whitespace-nowrap text-white">{getLabel('ui', 'APP_TITLE')}</span>
</div>
{'lobbyInfo' in scene && <div className="text-blue-500 font-medium whitespace-nowrap overflow-x-hidden text-ellipsis">
{ scene.lobbyInfo.name }
{ (scene.type === 'lobby' || scene.type === 'game') && <div className="text-blue-500 font-medium whitespace-nowrap overflow-x-hidden text-ellipsis">
{ scene.lobbyName }
</div>}
<div className="flex items-center">
<button type="button" className="flex text-sm bg-gray-800 rounded-full focus:ring-4 focus:ring-gray-600">
Expand All @@ -89,7 +89,7 @@ function Header({ scene, settings, connection }: HeaderProps) {
<UserMenuItem onClick={handleToggleSounds}>{getLabel('ui', settings.muteSounds ? 'BUTTON_ENABLE_SOUNDS' : 'BUTTON_DISABLE_SOUNDS')}</UserMenuItem>
{ scene.type === 'game' && isLobbyOwner(scene.lobbyState) && <UserMenuItem onClick={closeMenuAnd(handleReturnLobby)}>{getLabel('ui', 'BUTTON_RETURN_LOBBY')}</UserMenuItem>}
{ scene.type === 'lobby' && <UserMenuItem onClick={handleToggleSpectate}>{getLabel('ui', isSpectator ? 'BUTTON_SPECTATE_OFF' : 'BUTTON_SPECTATE_ON')}</UserMenuItem> }
{ 'lobbyInfo' in scene
{ scene.type === 'game' || scene.type === 'lobby'
? <UserMenuItem onClick={closeMenuAnd(handleLeaveLobby)}>{getLabel('ui', 'BUTTON_LEAVE_LOBBY')}</UserMenuItem>
: <UserMenuItem onClick={closeMenuAnd(handleDisconnect)}>{getLabel('ui', 'BUTTON_DISCONNECT')}</UserMenuItem> }
</UserMenu> }
Expand Down
4 changes: 2 additions & 2 deletions src/Model/ClientMessage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GameAction } from "../Scenes/Game/Model/GameAction";
import { GameOptions } from "../Scenes/Game/Model/GameUpdate";
import { ImagePixels } from "../Utils/ImageSerial";
import { Empty, LobbyId, LobbyInfo, UserId } from "./ServerMessage";
import { Empty, LobbyId, UserId } from "./ServerMessage";

export interface ClientConnect {
username: string;
Expand All @@ -26,7 +26,7 @@ export type ClientMessage =
{user_set_name: string} |
{user_set_propic: ImagePixels | null} |
{lobby_make: LobbyMakeArgs} |
{lobby_edit: LobbyInfo} |
{lobby_edit: GameOptions} |
{lobby_join: LobbyJoinArgs} |
{lobby_leave: Empty } |
{lobby_chat: { message: string }} |
Expand Down
59 changes: 27 additions & 32 deletions src/Model/SceneState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { UserValue } from "../Scenes/Lobby/LobbyUser";
import { LobbyValue } from "../Scenes/WaitingArea/LobbyElement";
import { deserializeImage } from "../Utils/ImageSerial";
import { createUnionReducer } from "../Utils/UnionUtils";
import { ChatMessage, Empty, LobbyEntered, LobbyId, LobbyInfo, LobbyUpdate, LobbyUserPropic, LobbyUserUpdate, UserId } from "./ServerMessage";
import { ChatMessage, Empty, LobbyEntered, LobbyId, LobbyUpdate, LobbyUserPropic, LobbyUserUpdate, UserId } from "./ServerMessage";

export interface LobbyState {
lobbyId: LobbyId;
Expand Down Expand Up @@ -34,28 +34,28 @@ export type ErrorState =
{ type: 'lobby', message: string } |
{ type: 'server', code: number | null, message: string };

export type SceneState =
{ type: 'home', error?: ErrorState } |
{ type: 'loading', error?: ErrorState, message: string } |
{ type: 'waiting_area', error?: ErrorState, lobbies: LobbyValue[] } |
{ type: 'lobby' | 'game', error?: ErrorState, lobbyInfo: LobbyInfo, lobbyState: LobbyState };
export type SceneState = { error?: ErrorState } & (
{ type: 'home' } |
{ type: 'loading', message: string } |
{ type: 'waiting_area', lobbies: LobbyValue[] } |
{ type: 'lobby' | 'game', lobbyName: string, gameOptions: GameOptions, lobbyState: LobbyState }
);

export type UpdateFunction<T> = (value: T) => T;

export type SceneUpdate =
{ reset: Empty } |
{ setError: ErrorState | null } |
{ gotoHome: Empty } |
{ gotoLoading: string } |
{ gotoWaitingArea: Empty } |
{ gotoLobby: LobbyEntered } |
{ gotoGame: Empty } |
{ setError: ErrorState | null } |
{ updateLobbies: LobbyUpdate } |
{ removeLobby: LobbyId } |
{ setLobbyInfo: LobbyInfo } |
{ setGameOptions: GameOptions } |
{ updateLobbyUser: LobbyUserUpdate } |
{ updateUserPropic: LobbyUserPropic } |
{ addLobbyChatMessage: ChatMessage } |
{ handleLobbyEntered: LobbyEntered };
{ addLobbyChatMessage: ChatMessage };

export function defaultCurrentScene(sessionId?: number): SceneState {
if (sessionId) {
Expand Down Expand Up @@ -85,24 +85,34 @@ function newUserValue({ user_id, username, flags, lifetime }: LobbyUserUpdate):
}

export const sceneReducer = createUnionReducer<SceneState, SceneUpdate>({
reset() {
gotoHome() {
return { type: 'home' };
},
setError(error) {
return { ...this, error: error ?? undefined };
},
gotoLoading(message) {
return { type: 'loading', error: this.error, message };
},
gotoWaitingArea() {
return { type: 'waiting_area', lobbies: [] };
},
gotoLobby({ user_id, lobby_id, name, options }) {
return {
type: 'lobby',
lobbyName: name,
gameOptions: options,
lobbyState: 'lobbyState' in this
? { ...this.lobbyState, users: this.lobbyState.users.filter(user => user.id >= 0) }
: newLobbyState(lobby_id, user_id)
};
},
gotoGame() {
if (this.type !== 'lobby') {
throw new Error('Invalid scene type for gotoGame: ' + this.type);
}
return { ...this, type: 'game' };
},
setError(error) {
return { ...this, error: error ?? undefined };
},
updateLobbies(update) {
if (this.type !== 'waiting_area') {
throw new Error('Invalid scene type for updateLobbies: ' + this.type);
Expand All @@ -115,17 +125,11 @@ export const sceneReducer = createUnionReducer<SceneState, SceneUpdate>({
}
return { ...this, lobbies: this.lobbies.filter(lobby => lobby.id !== lobby_id) };
},
setLobbyInfo(lobbyInfo) {
if (this.type !== 'lobby') {
throw new Error('Invalid scene type for setLobbyInfo: ' + this.type);
}
return { ...this, lobbyInfo };
},
setGameOptions(options) {
setGameOptions(gameOptions) {
if (this.type !== 'lobby') {
throw new Error('Invalid scene type for setGameOptions: ' + this.type);
}
return { ...this, lobbyInfo: { ...this.lobbyInfo, options } };
return { ...this, gameOptions };
},
updateLobbyUser(update) {
if (this.type !== 'lobby' && this.type !== 'game') {
Expand All @@ -149,14 +153,5 @@ export const sceneReducer = createUnionReducer<SceneState, SceneUpdate>({
throw new Error('Invalid scene type for addLobbyChatMessage: ' + this.type);
}
return { ...this, lobbyState: { ...this.lobbyState, chatMessages: this.lobbyState.chatMessages.concat(message) }};
},
handleLobbyEntered({ user_id, lobby_id, name, options }) {
return {
...this, type: 'lobby',
lobbyInfo: { name, options },
lobbyState: 'lobbyState' in this
? { ...this.lobbyState, users: this.lobbyState.users.filter(user => user.id >= 0) }
: newLobbyState(lobby_id, user_id)
};
}
});
9 changes: 1 addition & 8 deletions src/Model/ServerMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,13 @@ export interface LobbyUpdate {
state: LobbyStateEnum;
}

export interface LobbyInfo {
name: string;
options: GameOptions;
}

export interface LobbyEntered {
user_id: UserId;
lobby_id: LobbyId;
name: string;
options: GameOptions;
}

export type LobbyEdited = LobbyInfo;

export interface LobbyRemoved {
lobby_id: LobbyId;
}
Expand Down Expand Up @@ -75,7 +68,7 @@ export type ServerMessage =
{lobby_error: string} |
{lobby_update: LobbyUpdate} |
{lobby_entered: LobbyEntered} |
{lobby_edited: LobbyEdited} |
{lobby_edited: GameOptions} |
{lobby_removed: LobbyRemoved} |
{lobby_user_update: LobbyUserUpdate} |
{lobby_user_propic: LobbyUserPropic} |
Expand Down
10 changes: 5 additions & 5 deletions src/Model/UseBangConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function useBangConnection() {
});

const disconnected = useEvent((code: number | null, reason: string | null) => {
sceneDispatch({ reset: {} });
sceneDispatch({ gotoHome: {} });
if (reason) {
sceneDispatch({ setError: { type: 'server', code, message: reason }});
} else if (scene.type === 'loading') {
Expand Down Expand Up @@ -84,10 +84,10 @@ export default function useBangConnection() {
},
lobby_entered(message) {
gameChannel.clear();
sceneDispatch({ handleLobbyEntered: message });
sceneDispatch({ gotoLobby: message });
},
lobby_edited(message) {
sceneDispatch({ setLobbyInfo: message });
lobby_edited(options) {
sceneDispatch({ setGameOptions: options });
},
lobby_removed({ lobby_id }) {
sceneDispatch({ removeLobby: lobby_id });
Expand Down Expand Up @@ -127,7 +127,7 @@ export default function useBangConnection() {
if (scene.type !== 'lobby') {
throw new Error('Invalid scene type for setGameOptions: ' + scene.type);
}
connection.sendMessage({ lobby_edit: { name: scene.lobbyInfo.name, options: gameOptions } });
connection.sendMessage({ lobby_edit: gameOptions });
sceneDispatch({ setGameOptions: gameOptions });
settings.setGameOptions(gameOptions);
});
Expand Down
8 changes: 4 additions & 4 deletions src/Scenes/Lobby/Lobby.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import useEvent from 'react-use-event-hook';
import Button from '../../Components/Button';
import getLabel from '../../Locale/GetLabel';
import { LobbyState, isLobbyOwner, newLobbyState } from '../../Model/SceneState';
import { LobbyInfo, UserId } from '../../Model/ServerMessage';
import { UserId } from '../../Model/ServerMessage';
import { BangConnection } from '../../Model/UseBangConnection';
import { GameOptions } from '../Game/Model/GameUpdate';
import GameOptionsEditor from './GameOptionsEditor';
Expand All @@ -20,15 +20,15 @@ export function getUser(users: UserValue[], id: UserId): UserValue {
export type SetGameOptions = (value: GameOptions) => void;

export interface LobbyProps {
lobbyInfo: LobbyInfo;
gameOptions: GameOptions;
setGameOptions: SetGameOptions;
connection: BangConnection;
lobbyState: LobbyState;
}

export const LobbyContext = createContext<LobbyState>(newLobbyState(0, 0));

export default function LobbyScene({ lobbyInfo, setGameOptions, connection, lobbyState }: LobbyProps) {
export default function LobbyScene({ gameOptions, setGameOptions, connection, lobbyState }: LobbyProps) {
const handleStartGame = useEvent(() => connection.sendMessage({ game_start: {} }));

return (
Expand All @@ -38,7 +38,7 @@ export default function LobbyScene({ lobbyInfo, setGameOptions, connection, lobb
<Button color='green' onClick={handleStartGame}>{getLabel('ui', 'BUTTON_START_GAME')}</Button>
</div> }
<div className='flex flex-col md:flex-row items-center md:items-start mb-24'>
<GameOptionsEditor gameOptions={lobbyInfo.options} setGameOptions={setGameOptions} readOnly={!isLobbyOwner(lobbyState)} />
<GameOptionsEditor gameOptions={gameOptions} setGameOptions={setGameOptions} readOnly={!isLobbyOwner(lobbyState)} />
<div className='flex flex-col -order-1 md:order-none'>
{lobbyState.users.flatMap(user => {
if (user.flags.includes('disconnected')) {
Expand Down

0 comments on commit 5dfbb2a

Please sign in to comment.