-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Try to support scoreboard diff"
This reverts commit 8bea34e.
- Loading branch information
Showing
11 changed files
with
84 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,57 @@ | ||
import _ from "lodash"; | ||
import { Award, OptimismLevel, ScoreboardDiff, ScoreboardRow, TeamId } from "@shared/api"; | ||
import { createSlice, PayloadAction } from "@reduxjs/toolkit"; | ||
import { LegacyScoreboardRow, OptimismLevel } from "@shared/api"; | ||
|
||
const ActionTypes = { | ||
SCOREBOARD_SET: "SCOREBOARD_SET", | ||
}; | ||
|
||
export type ScoreboardData = { | ||
ids: Record<TeamId, ScoreboardRow & {rank: number}>, // maybe stable? | ||
idAwards: Record<TeamId, undefined | Exclude<Award, "teams">[]>, | ||
order: TeamId[], | ||
orderById: Record<TeamId, number>, | ||
ranks: number[], | ||
awards: Award[] | ||
rows: LegacyScoreboardRow[], | ||
ids: Record<LegacyScoreboardRow["teamId"], LegacyScoreboardRow> | ||
}; | ||
|
||
export type ScoreboardState = { | ||
[key in OptimismLevel]: ScoreboardData | ||
} | ||
|
||
const initialState: ScoreboardState = Object.fromEntries( | ||
Object.keys(OptimismLevel).map((key) => [key, { | ||
ids: {}, | ||
idAwards: {}, | ||
order: [], | ||
ranks: [], | ||
awards: [] | ||
}]) | ||
) as ScoreboardState; | ||
const initialState: ScoreboardState = { | ||
[OptimismLevel.normal]: { | ||
rows: [], | ||
ids: {} | ||
}, | ||
[OptimismLevel.optimistic]: { | ||
rows: [], | ||
ids: {} | ||
}, | ||
[OptimismLevel.pessimistic]: { | ||
rows: [], | ||
ids: {} | ||
} | ||
}; | ||
|
||
const scoreboardSlice = createSlice({ | ||
name: "scoreboard", | ||
initialState, | ||
reducers: { | ||
handleScoreboardDiff(state, action: PayloadAction<{ optimism: OptimismLevel, diff: ScoreboardDiff }>) { | ||
const { optimism, diff } = action.payload; | ||
state[optimism].awards = diff.awards; | ||
state[optimism].order = diff.order; | ||
state[optimism].ranks = diff.ranks; | ||
const rankMap = Object.fromEntries(_.zip(diff.order, diff.ranks)); | ||
for (const [id, newData] of Object.entries(diff.rows)) { | ||
state[optimism].ids[id] = { | ||
...newData, | ||
rank: rankMap[id] | ||
}; | ||
export const setScoreboard = (scoreboardType, rows) => { | ||
return async dispatch => { | ||
dispatch({ | ||
type: ActionTypes.SCOREBOARD_SET, | ||
payload: { | ||
type: scoreboardType, | ||
rows: rows | ||
} | ||
state[optimism].orderById = Object.fromEntries( | ||
diff.order.map((teamId, index) => [teamId, index]) | ||
); | ||
state[optimism].idAwards = {}; | ||
for (const award of diff.awards) { | ||
for (const teamId of award.teams) { | ||
if(state[optimism].idAwards[teamId] === undefined) { | ||
state[optimism].idAwards[teamId] = []; | ||
} | ||
state[optimism].idAwards[teamId].push({ ...award, teams: undefined }); | ||
} | ||
}); | ||
}; | ||
}; | ||
|
||
export function scoreboardReducer(state = initialState, action): ScoreboardState { | ||
switch (action.type) { | ||
case ActionTypes.SCOREBOARD_SET: | ||
return { | ||
...state, | ||
[action.payload.type]: { | ||
rows: action.payload.rows, | ||
ids: _.keyBy(action.payload.rows, "teamId") | ||
} | ||
} | ||
}; | ||
default: | ||
return state; | ||
} | ||
}); | ||
|
||
export const { handleScoreboardDiff } = scoreboardSlice.actions; | ||
|
||
export default scoreboardSlice.reducer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { handleScoreboardDiff } from "../../redux/contest/scoreboard"; | ||
import { OptimismLevel, ScoreboardDiff } from "@shared/api"; | ||
import { setScoreboard } from "../../redux/contest/scoreboard"; | ||
import { LegacyScoreboard } from "@shared/api"; | ||
|
||
export const handleMessage = (optimism: OptimismLevel) => (dispatch, e) => { | ||
const diff = JSON.parse(e.data) as ScoreboardDiff; | ||
dispatch(handleScoreboardDiff({ optimism, diff })); | ||
export const handleMessage = (type) => (dispatch, e) => { | ||
const message = JSON.parse(e.data) as LegacyScoreboard; | ||
dispatch(setScoreboard(type, message.rows)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters