Skip to content

Commit

Permalink
feat: remove player
Browse files Browse the repository at this point in the history
  • Loading branch information
aashutoshrathi committed Jul 3, 2024
1 parent 02e08c7 commit d90a094
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const stfSchemaMap = {
endMatch: schemas.endMatch,
logByes: schemas.logByes,
addPlayer: schemas.addPlayer,
removePlayer: schemas.removePlayer,
removeGoal: schemas.logGoal,
};

Expand Down
7 changes: 7 additions & 0 deletions src/stackr/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ const addPlayerSchema = new ActionSchema("addPlayer", {
...baseTimeStamp,
});

const removePlayerSchema = new ActionSchema("removePlayer", {
teamId: SolidityType.UINT,
playerId: SolidityType.UINT,
...baseTimeStamp,
});

export const schemas = {
startMatch: matchAction,
endMatch: matchAction,
logGoal: matchPlayerAction,
startTournament: startTournamentSchema,
logByes: teamActionSchema,
addPlayer: addPlayerSchema,
removePlayer: removePlayerSchema,
};
34 changes: 33 additions & 1 deletion src/stackr/transitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,23 @@ const logByes: STF<League, TeamRequest> = {

const addPlayer: STF<League, { teamId: number; playerName: string }> = {
handler: ({ state, inputs }) => {
const { logs } = state;
const { teamId, playerName } = inputs;
const maxPlayerIdFromLogs = logs.reduce((acc, l) => {
if (l.playerId > acc) {
return l.playerId;
}
return acc;
}, 0);

const lastMaxId = Math.max(
state.players.at(-1)?.id || 0,
state.players.length,
maxPlayerIdFromLogs
);

state.players.push({
id: state.players.length + 1,
id: lastMaxId + 1,
name: playerName,
teamId,
});
Expand All @@ -453,6 +467,23 @@ const addPlayer: STF<League, { teamId: number; playerName: string }> = {
},
};

const removePlayer: STF<League, { teamId: number; playerId: number }> = {
handler: ({ state, inputs }) => {
const { teamId, playerId } = inputs;
const player = state.players.find((p) => p.id === playerId);
if (!player) {
throw new Error("PLAYER_NOT_FOUND");
}

if (player.teamId !== teamId) {
throw new Error("INVALID_TEAM");
}

state.players = state.players.filter((p) => p.id !== playerId);
return state;
},
};

export const transitions: Transitions<League> = {
startMatch,
penaltyShootout,
Expand All @@ -466,4 +497,5 @@ export const transitions: Transitions<League> = {
logPenaltyHit,
logPenaltyMiss,
addPlayer,
removePlayer,
};

0 comments on commit d90a094

Please sign in to comment.