Skip to content

Commit

Permalink
Tweaks for team settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot committed Jan 9, 2025
1 parent cf5a13d commit f4a73bd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
30 changes: 15 additions & 15 deletions src/frontend/components/menus/lobby.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,12 @@ export function ActiveLobby({
[proposedTeams, storedLocalTeams],
);

// @ts-ignore

Check failure on line 102 in src/frontend/components/menus/lobby.tsx

View workflow job for this annotation

GitHub Actions / ci

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
const nextTeamGroup: TeamGroup = TeamGroup[useMemo(() => Object.values(TeamGroup).find(v => !proposedTeams.some(t => t.group === v)) as string ?? TeamGroup.Red, [proposedTeams])];

const addTeam = useCallback(
(_evt: MouseEvent, team: StoredTeam) => {
gameInstance.addProposedTeam(team, MAX_WORMS).catch((ex) => {
gameInstance.addProposedTeam(team, MAX_WORMS, nextTeamGroup).catch((ex) => {
logger.warning("Failed to add team", team, ex);
setError("Failed to add team");
});
Expand All @@ -119,13 +122,14 @@ export function ActiveLobby({
[gameInstance],
);


const viableToStart = useMemo(() =>
gameInstance.isHost && members.length >= 2 && proposedTeams.length >= 2 &&
proposedTeams.reduce<Partial<Record<TeamGroup, number>>>((v, o) => ({
gameInstance.isHost &&
members.length >= 2 &&
proposedTeams.length >= 2 &&
Object.keys(proposedTeams.reduce<Partial<Record<TeamGroup, number>>>((v, o) => ({
...v,
[o.group]: (v[o.group] ?? 0) + 1
}), { })
}), { })).length >= 2
, [gameInstance, members, proposedTeams]);

const lobbyLink = `${window.location.origin}${window.location.pathname}?gameRoomId=${encodeURIComponent(gameInstance.roomId)}`;
Expand Down Expand Up @@ -187,20 +191,16 @@ export function ActiveLobby({
}
const onRemoveTeam = () => removeTeam(t);
const incrementWormCount = () => {
const newWormCount =
const wormCount =
t.wormCount >= MAX_WORMS ? 1 : t.wormCount + 1;
gameInstance.addProposedTeam(t, newWormCount, t.group);
gameInstance.updateProposedTeam(t, { wormCount });
};
const changeTeamColor = () => {
let newGroup = t.group + 1;
if (TeamGroup[newGroup] === undefined) {
newGroup = TeamGroup.Red;
let teamGroup = t.group + 1;
if (TeamGroup[teamGroup] === undefined) {
teamGroup = TeamGroup.Red;
}
gameInstance.addProposedTeam(
t,
t.wormCount,
newGroup as TeamGroup,
);
gameInstance.updateProposedTeam(t, { teamGroup });
};
return (
<li key={t.name}>
Expand Down
26 changes: 20 additions & 6 deletions src/net/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ export class NetGameInstance {
}

public async addProposedTeam(
proposedTeam: StoredTeam | ProposedTeam,
proposedTeam: StoredTeam,
wormCount: number,
teamGroup: TeamGroup = TeamGroup.Red,
teamGroup: TeamGroup,
) {
await this.client.client.sendStateEvent(
this.roomId,
Expand All @@ -179,15 +179,29 @@ export class NetGameInstance {
...proposedTeam,
group: teamGroup,
wormCount,
playerUserId:
"playerUserId" in proposedTeam
? proposedTeam.playerUserId
: this.client.userId,
playerUserId: this.client.userId,
// TODO: What should the proper stateKey be?
},
`${this.client.userId.slice(1)}/${proposedTeam.name}`,
);
}


public async updateProposedTeam(
proposedTeam: ProposedTeam,
updates: { wormCount?: number, teamGroup?: TeamGroup},
) {
await this.client.client.sendStateEvent(
this.roomId,
GameProposedTeamEventType,
{
...proposedTeam,
...(updates.teamGroup !== undefined && { group: updates.teamGroup }),
...(updates.wormCount !== undefined && { wormCount: updates.wormCount })
},
`${proposedTeam.playerUserId.slice(1)}/${proposedTeam.name}`,
);
}

public async removeProposedTeam(proposedTeam: ProposedTeam) {
await this.client.client.sendStateEvent(
Expand Down

0 comments on commit f4a73bd

Please sign in to comment.