Skip to content

Commit ecfbd16

Browse files
sphericlestadust
andauthored
Sort all stats viewer sections (#239)
* move dropdown into statsviewer class, write sort function * ok its mostly done i think * i forgot to remove this 😭😭 * this should work now... * remove this too --------- Co-authored-by: Patrick <43299462+stadust@users.noreply.github.com>
1 parent 38b486c commit ecfbd16

File tree

3 files changed

+93
-19
lines changed

3 files changed

+93
-19
lines changed

pointercrate-demonlist-pages/static/js/modules/statsviewer.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ export class StatsViewer extends FilteredPaginator {
6565
});
6666
}
6767

68-
let demonSortingModeDropdown = new Dropdown(
68+
this.demonSortingModeDropdown = new Dropdown(
6969
document.getElementById("demon-sorting-mode-dropdown")
7070
);
7171

72-
demonSortingModeDropdown.addEventListener((selected) => {
72+
this.demonSortingModeDropdown.addEventListener((selected) => {
7373
window.localStorage.setItem("demon_sorting_mode", selected);
7474

7575
if (selected === "Alphabetical") {
@@ -87,7 +87,7 @@ export class StatsViewer extends FilteredPaginator {
8787
}
8888
});
8989

90-
demonSortingModeDropdown.select(
90+
this.demonSortingModeDropdown.select(
9191
localStorage.getItem("demon_sorting_mode") ?? "Alphabetical",
9292
true
9393
); // default to alphabetical
@@ -182,6 +182,35 @@ export class StatsViewer extends FilteredPaginator {
182182

183183
return element;
184184
}
185+
186+
/**
187+
* Sort demons by the selected sorting option
188+
* @param {string} sortOption - The sorting option ("Alphabetical" or "Position")
189+
* @param data - Demon data to sort
190+
* @returns Sorted data
191+
*/
192+
sortStatsViewerRow(sortOption, data) {
193+
if (sortOption === "Alphabetical") {
194+
data.sort((r1, r2) => {
195+
if (r1.demon) {
196+
return r1.demon.name.localeCompare(r2.demon.name)
197+
} else {
198+
// nation unbeaten section does not have "demon" key
199+
return r1.name.localeCompare(r2.name)
200+
}
201+
});
202+
} else if (sortOption === "Position") {
203+
data.sort((r1, r2) => {
204+
if (r1.demon) {
205+
return r1.demon.position - r2.demon.position
206+
} else {
207+
// nation unbeaten section does not have "demon" key
208+
return r1.position - r2.position
209+
}
210+
});
211+
}
212+
return data;
213+
}
185214
}
186215

187216
export function formatInto(parent, childs) {

pointercrate-demonlist-pages/static/js/statsviewer/individual.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ class IndividualStatsViewer extends StatsViewer {
2525

2626
this.setName(playerData.name, playerData.nationality);
2727

28-
this.formatDemonsInto(this._created, playerData.created);
29-
this.formatDemonsInto(this._published, playerData.published);
30-
this.formatDemonsInto(this._verified, playerData.verified);
28+
const selectedSort = this.demonSortingModeDropdown.selected
29+
30+
this.formatDemonsInto(this._created, this.sortStatsViewerRow(selectedSort, playerData.created));
31+
this.formatDemonsInto(this._published, this.sortStatsViewerRow(selectedSort, playerData.published));
32+
this.formatDemonsInto(this._verified, this.sortStatsViewerRow(selectedSort, playerData.verified));
3133

3234
let beaten = playerData.records.filter((record) => record.progress === 100);
3335

@@ -80,10 +82,16 @@ class IndividualStatsViewer extends StatsViewer {
8082
this.setHardest(hardest.name === "None" ? undefined : hardest);
8183

8284
let non100Records = playerData.records
83-
.filter((record) => record.progress !== 100)
84-
.sort((r1, r2) => r1.progress - r2.progress);
85+
.filter((record) => record.progress !== 100);
86+
87+
this.formatRecordsInto(this._progress, this.sortStatsViewerRow(selectedSort, non100Records));
8588

86-
this.formatRecordsInto(this._progress, non100Records);
89+
this.demonSortingModeDropdown.addEventListener((selected) => {
90+
this.formatDemonsInto(this._created, this.sortStatsViewerRow(selected, playerData.created))
91+
this.formatDemonsInto(this._published, this.sortStatsViewerRow(selected, playerData.published))
92+
this.formatDemonsInto(this._verified, this.sortStatsViewerRow(selected, playerData.verified))
93+
this.formatRecordsInto(this._progress, this.sortStatsViewerRow(selected, non100Records))
94+
});
8795
}
8896

8997
formatDemonsInto(element, demons) {

pointercrate-demonlist-pages/static/js/statsviewer/nation.js

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class NationStatsViewer extends StatsViewer {
2323

2424
let nationData = response.data.data;
2525

26+
let selectedSort = this.demonSortingModeDropdown.selected;
27+
2628
this.setName(nationData.nation, nationData);
2729

2830
let beaten = [];
@@ -105,23 +107,18 @@ class NationStatsViewer extends StatsViewer {
105107
.map((record) => this.formatDemonFromRecord(record, true))
106108
);
107109

108-
nationData.unbeaten.sort((r1, r2) => r1.name.localeCompare(r2.name));
109-
progress.sort((r1, r2) => r2.progress - r1.progress);
110-
nationData.created.sort((r1, r2) =>
111-
r1.demon.name.localeCompare(r2.demon.name)
112-
);
113110

114111
formatInto(
115112
this._unbeaten,
116-
nationData.unbeaten.map((demon) => this.formatDemon(demon))
113+
this.sortStatsViewerRow(selectedSort, nationData.unbeaten).map((demon) => this.formatDemon(demon))
117114
);
118115
formatInto(
119116
this._progress,
120-
progress.map((record) => this.formatDemonFromRecord(record))
117+
this.sortStatsViewerRow(selectedSort, progress).map((record) => this.formatDemonFromRecord(record))
121118
);
122119
formatInto(
123120
this._created,
124-
nationData.created.map((creation) => {
121+
this.sortStatsViewerRow(selectedSort, nationData.created).map((creation) => {
125122
return this.makeTooltip(
126123
this.formatDemon(creation.demon),
127124
"(Co)created&nbsp;by&nbsp;" +
@@ -135,7 +132,7 @@ class NationStatsViewer extends StatsViewer {
135132
);
136133
formatInto(
137134
this._verified,
138-
nationData.verified.map((verification) => {
135+
this.sortStatsViewerRow(selectedSort, nationData.verified).map((verification) => {
139136
return this.makeTooltip(
140137
this.formatDemon(verification.demon),
141138
"Verified&nbsp;by: ",
@@ -145,14 +142,54 @@ class NationStatsViewer extends StatsViewer {
145142
);
146143
formatInto(
147144
this._published,
148-
nationData.published.map((publication) => {
145+
this.sortStatsViewerRow(selectedSort, nationData.published).map((publication) => {
149146
return this.makeTooltip(
150147
this.formatDemon(publication.demon),
151148
"Published&nbsp;by: ",
152149
publication.players.join(", ")
153150
);
154151
})
155152
);
153+
154+
this.demonSortingModeDropdown.addEventListener((selected) => {
155+
if (nationData.created.length > 0) {
156+
formatInto(this._created, this.sortStatsViewerRow(selected, nationData.created).map((creation) => {
157+
return this.makeTooltip(
158+
this.formatDemon(creation.demon),
159+
"(Co)created&nbsp;by&nbsp;" +
160+
creation.players.length +
161+
"&nbsp;player" +
162+
(creation.players.length === 1 ? "" : "s") +
163+
"&nbsp;in&nbsp;this&nbsp;country: ",
164+
creation.players.join(", ")
165+
);
166+
}));
167+
}
168+
169+
if (nationData.published.length > 0) {
170+
formatInto(this._published, this.sortStatsViewerRow(selected, nationData.published).map((publication) => {
171+
return this.makeTooltip(
172+
this.formatDemon(publication.demon),
173+
"Published&nbsp;by: ",
174+
publication.players.join(", ")
175+
);
176+
}));
177+
}
178+
if (nationData.verified.length > 0) {
179+
formatInto(this._verified, this.sortStatsViewerRow(selected, nationData.verified).map((verification) => {
180+
return this.makeTooltip(
181+
this.formatDemon(verification.demon),
182+
"Verified&nbsp;by: ",
183+
verification.players.join(", ")
184+
);
185+
}));
186+
}
187+
if (progress.length > 0)
188+
formatInto(this._progress, this.sortStatsViewerRow(selected, progress).map((record) => this.formatDemonFromRecord(record)));
189+
190+
if (nationData.unbeaten.length > 0)
191+
formatInto(this._unbeaten, this.sortStatsViewerRow(selected, nationData.unbeaten).map((demon) => this.formatDemon(demon)));
192+
});
156193
}
157194

158195
makeTooltip(hoverElement, title, content) {

0 commit comments

Comments
 (0)