Skip to content

Commit

Permalink
Merge pull request spookybear0#73 from EboMike/cleanup
Browse files Browse the repository at this point in the history
Score tables sort during game replays.
  • Loading branch information
spookybear0 authored May 11, 2024
2 parents 0206b78 + 4484ee4 commit 2315609
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 7 deletions.
67 changes: 67 additions & 0 deletions assets/js/replay.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ current_starting_sound_playing = undefined;

started = false;

// Index of the columns to sort by, in order of significance.
sort_columns = [];

// All team <table> elements.
team_tables = [];

// This value increases with every restart or start skip so we know
// whether or not the current start sound is relevant.
playback_key = 1
Expand Down Expand Up @@ -109,6 +115,55 @@ function getCurrentGameTimeMillis() {
return base_game_time_millis + (now - base_timestamp) * get_playback_speed();
}

function setSortColumns(new_sort_columns) {
sort_columns = new_sort_columns;
}

// Compares two lists of values, in order of significance. 0 if they're both identical.
function compareValueList(a, b) {
for (let i = 0; i < a.length; i++) {
if (a[i] > b[i]) {
return -1;
}

if (a[i] < b[i]) {
return 1;
}
}

return 0;
}

function sortTable(table) {
if (sort_columns.length == 0) {
return;
}

let rows = Array.from(table.querySelectorAll("tr"));

rows = rows.slice(1);

rows.sort( (r1,r2) => {
let row_id1 = r1.id;
let row_id2 = r2.id;
let values1 = [];
let values2 = [];

sort_columns.forEach((column) => {
values1.push(parseInt(document.getElementById(`${row_id1}_${column}`).innerHTML));
values2.push(parseInt(document.getElementById(`${row_id2}_${column}`).innerHTML));
});

return compareValueList(values1, values2);
});

rows.forEach(row => table.appendChild(row));
}

function sortTables() {
team_tables.forEach((table) => sortTable(table));
}

function beginPlayback() {
play = true;
restarted = false;
Expand Down Expand Up @@ -211,6 +266,8 @@ function addTeam(team_name, team_id, team_css_class) {
team_table.appendChild(header_row);
team_div.appendChild(team_table);
teams.appendChild(team_div);

team_tables.push(team_table);
}

function registerSound(sound_id, asset_urls, priority, required) {
Expand Down Expand Up @@ -336,15 +393,25 @@ function playEvents() {
eventBox.innerHTML += `<div class="event">${message}</div>\n`;
}

let sortable_column_changed = false;

// Handle all cell changes.
event[2].forEach((cell_change) => {
row_id = cell_change[0];
column = cell_change[1];
new_value = cell_change[2];

document.getElementById(`${row_id}_${column}`).innerHTML = new_value;

if (sort_columns.includes(parseInt(column))) {
sortable_column_changed = true;
}
});

if (sortable_column_changed) {
sortTables();
}

// Handle all row changes.
event[3].forEach((row_change) => {
row_id = row_change[0];
Expand Down
5 changes: 5 additions & 0 deletions helpers/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ class Replay:
# Names of the columns in each team table.
column_headers: List[str]

# If not empty, the tables should be sorted by these column, in order of significance.
sort_columns_index: List[int]

# Export this entire replay to JavaScript code.
def export_to_js(self) -> str:
result = ""
Expand Down Expand Up @@ -160,6 +163,8 @@ def export_to_js(self) -> str:
resetPlayers();
"""

result += f"setSortColumns({self.sort_columns_index});\n"

if self.intro_sound:
result += f"setIntroSound({self.intro_sound.id});\n"

Expand Down
7 changes: 1 addition & 6 deletions helpers/replay_laserball.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ async def create_laserball_replay(game: LaserballGame) -> Replay:
sounds=[],
intro_sound=None,
start_sound=None,
sort_columns_index=[_GOALS_COLUMN, _ASSISTS_COLUMN, _STEALS_COLUMN, _BLOCKS_COLUMN],
)


Expand Down Expand Up @@ -331,12 +332,6 @@ def _add_assists(player: _Player, assists_to_add: int, cell_changes: List[Replay
ReplayCellChange(row_id=player.row_id, column=_ASSISTS_COLUMN, new_value=str(player.assists)))


def _add_assists(player: _Player, assists_to_add: int, cell_changes: List[ReplayCellChange]):
player.assists += assists_to_add
cell_changes.append(
ReplayCellChange(row_id=player.row_id, column=_ASSISTS_COLUMN, new_value=str(player.assists)))


def _create_entity_reference(argument: str, entity_id_to_player: dict, entity_id_to_nonplayer_name: dict) -> str:
if argument in entity_id_to_nonplayer_name:
return entity_id_to_nonplayer_name[argument]
Expand Down
1 change: 1 addition & 0 deletions helpers/replay_sm5.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ async def create_sm5_replay(game: SM5Game) -> Replay:
sounds=sound_assets,
intro_sound=start_audio,
start_sound=alarm_start_audio,
sort_columns_index=[_SCORE_COLUMN]
)


Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/replay_sm5_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async def test_create_sm5_replay(self):
start_sound=ReplaySound(asset_urls=['/assets/sm5/audio/Effect/General Quarters.wav'], id=1,
priority=1, required=False),
column_headers=['Role', 'Codename', 'Score', 'Lives', 'Shots', 'Missiles', 'Spec', 'Accuracy',
'K/D'])
'K/D'], sort_columns_index=[2])

self.assertEqual(expected, replay)

Expand Down

0 comments on commit 2315609

Please sign in to comment.