Skip to content

Commit fd5855a

Browse files
author
Daniel Svensson
committed
Use react-table with sortable columns.
1 parent e94dcb1 commit fd5855a

File tree

5 files changed

+522
-84
lines changed

5 files changed

+522
-84
lines changed

gatsby-node.js

+115-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require(`path`)
22
const fs = require("fs")
3+
const { resourceLimits } = require("worker_threads")
34

45
exports.onCreateWebpackConfig = ({ actions }) => {
56
actions.setWebpackConfig({
@@ -45,9 +46,110 @@ exports.onCreateNode = ({ node, actions }) => {
4546
}
4647
}
4748

49+
const lookupTable = {
50+
0: "•",
51+
5: "•",
52+
14: "•",
53+
15: "•",
54+
16: "[",
55+
17: "]",
56+
18: "0",
57+
19: "1",
58+
20: "2",
59+
21: "3",
60+
22: "4",
61+
23: "5",
62+
24: "6",
63+
25: "7",
64+
26: "8",
65+
27: "9",
66+
28: "•"
67+
}
68+
69+
70+
function normalizeName(name) {
71+
let result = ""
72+
for (let i = 0; i < name.length; i++) {
73+
let code = name.charCodeAt(i)
74+
if (code > 127) {
75+
code -= 128
76+
}
77+
if (code >= 32 && code < 127) {
78+
result += String.fromCharCode(code)
79+
} else {
80+
result += lookupTable[code] || " "
81+
}
82+
}
83+
return result
84+
}
85+
86+
function prepareTableData(duration, stats) {
87+
const result = {
88+
players: [],
89+
teams: [{}, {}]
90+
}
91+
for (let i = 0; i < stats.length; i++) {
92+
result.players.push({
93+
name: normalizeName(stats[i].name),
94+
team: stats[i].team,
95+
frags: stats[i].stats.frags,
96+
kills: stats[i].stats.kills,
97+
deaths: stats[i].stats.deaths,
98+
quad_takes: (stats[i].items.q || { took: 0 }).took,
99+
pent_takes: (stats[i].items.p || { took: 0 }).took,
100+
ctf_points: stats[i].ctf.points || 0,
101+
flag_pickups: stats[i].ctf.pickups || 0,
102+
flag_captures: stats[i].ctf.caps || 0,
103+
flag_defends: stats[i].ctf.defends || 0,
104+
flag_returns: stats[i].ctf.returns || 0,
105+
flag_carrier_defends: stats[i].ctf.carrier_defends || 0,
106+
flag_carrier_frags: stats[i].ctf.carrier_frags || 0,
107+
rune_time_res: Math.floor((stats[i].ctf.runes[0] * 100.0) / duration).toString() + "%",
108+
rune_time_str: Math.floor((stats[i].ctf.runes[1] * 100.0) / duration).toString() + "%",
109+
rune_time_hst: Math.floor((stats[i].ctf.runes[2] * 100.0) / duration).toString() + "%",
110+
rune_time_reg: Math.floor((stats[i].ctf.runes[3] * 100.0) / duration).toString() + "%",
111+
weapon_rl_hits: ((stats[i].weapons.rl || { acc: null }).acc || { virtual: 0 }).virtual,
112+
weapon_rl_attacks: ((stats[i].weapons.rl || { acc: null }).acc || { attacks: 0 }).attacks,
113+
weapon_lg_hits: ((stats[i].weapons.lg || { acc: null }).acc || { hits: 0 }).hits,
114+
weapon_lg_attacks: ((stats[i].weapons.lg || { acc: null }).acc || { attacks: 0 }).attacks
115+
})
116+
const team = (stats[i].team == "red" ? result.teams[0] : result.teams[1])
117+
team.frags += (team.frags || 0) + result.players[i].frags
118+
team.kills += (team.kills || 0) + result.players[i].kills
119+
team.deaths += (team.deaths || 0) + result.players[i].deaths
120+
team.quad_takes += (team.quad_takes || 0) + result.players[i].quad_takes
121+
team.pent_takes += (team.pent_takes || 0) + result.players[i].pent_takes
122+
team.ctf_points += (team.ctf_points || 0) + result.players[i].ctf_points
123+
team.flag_pickups += (team.flag_pickups || 0) + result.players[i].flag_pickups
124+
team.flag_captures += (team.flag_captures || 0) + result.players[i].flag_captures
125+
team.flag_defends += (team.flag_defends || 0) + result.players[i].flag_defends
126+
team.flag_returns += (team.flag_returns || 0) + result.players[i].flag_returns
127+
team.flag_carrier_defends += (team.flag_carrier_defends || 0) + result.players[i].flag_carrier_defends
128+
team.flag_carrier_frags += (team.flag_carrier_frags || 0) + result.players[i].flag_carrier_frags
129+
team.rune_time_res += (team.rune_time_res || 0) + stats[i].ctf.runes[0]
130+
team.rune_time_str += (team.rune_time_str || 0) + stats[i].ctf.runes[1]
131+
team.rune_time_hst += (team.rune_time_hst || 0) + stats[i].ctf.runes[2]
132+
team.rune_time_reg += (team.rune_time_reg || 0) + stats[i].ctf.runes[3]
133+
team.weapon_rl_hits += (team.weapon_rl_hits || 0) + result.players[i].weapon_rl_hits
134+
team.weapon_rl_attacks += (team.weapon_rl_attacks || 0) + result.players[i].weapon_rl_attacks
135+
team.weapon_lg_hits += (team.weapon_lg_hits || 0) + result.players[i].weapon_lg_hits
136+
team.weapon_lg_attacks += (team.weapon_lg_attacks || 0) + result.players[i].weapon_lg_attacks
137+
}
138+
result.teams[0].rune_time_res = Math.floor((result.teams[0].rune_time_res * 100.0) / duration).toString() + "%"
139+
result.teams[1].rune_time_res = Math.floor((result.teams[1].rune_time_res * 100.0) / duration).toString() + "%"
140+
result.teams[0].rune_time_str = Math.floor((result.teams[0].rune_time_str * 100.0) / duration).toString() + "%"
141+
result.teams[1].rune_time_str = Math.floor((result.teams[1].rune_time_str * 100.0) / duration).toString() + "%"
142+
result.teams[0].rune_time_hst = Math.floor((result.teams[0].rune_time_hst * 100.0) / duration).toString() + "%"
143+
result.teams[1].rune_time_hst = Math.floor((result.teams[1].rune_time_hst * 100.0) / duration).toString() + "%"
144+
result.teams[0].rune_time_reg = Math.floor((result.teams[0].rune_time_reg * 100.0) / duration).toString() + "%"
145+
result.teams[1].rune_time_reg = Math.floor((result.teams[1].rune_time_reg * 100.0) / duration).toString() + "%"
146+
return result
147+
}
148+
149+
48150
exports.createPages = async ({ graphql, actions }) => {
49151
const { createPage } = actions
50-
const mapMetadata = JSON.parse(fs.readFileSync('data/map.metadata.json'))
152+
const mapMetadata = JSON.parse(fs.readFileSync("data/map.metadata.json"))
51153
const matchTemplate = path.resolve(`src/pages/match.js`)
52154
const result = await graphql(`
53155
query Pages {
@@ -81,12 +183,18 @@ exports.createPages = async ({ graphql, actions }) => {
81183
kills
82184
frags
83185
}
186+
items {
187+
p {
188+
took
189+
}
190+
q {
191+
took
192+
}
193+
}
84194
weapons {
85195
rl {
86196
acc {
87197
virtual
88-
real
89-
hits
90198
attacks
91199
}
92200
}
@@ -112,13 +220,13 @@ exports.createPages = async ({ graphql, actions }) => {
112220
context: {
113221
demo: edge.node.demo,
114222
map: {
115-
"name": edge.node.map,
116-
"metadata": mapMetadata[edge.node.map]
223+
name: edge.node.map,
224+
metadata: mapMetadata[edge.node.map],
117225
},
118226
directory: edge.node.parent.directory,
119227
duration: edge.node.duration,
120-
stats: edge.node.players
228+
matchStats: prepareTableData(edge.node.duration, edge.node.players),
121229
},
122230
})
123231
})
124-
}
232+
}

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@fortawesome/fontawesome-svg-core": "^6.1.1",
99
"@fortawesome/free-solid-svg-icons": "^6.1.1",
1010
"@fortawesome/react-fontawesome": "^0.1.18",
11+
"@tippy.js/react": "^3.1.1",
1112
"browser-monads": "^1.0.0",
1213
"gatsby": "^4.12.1",
1314
"gatsby-image": "^3.11.0",
@@ -24,6 +25,8 @@
2425
"react": "^17.0.1",
2526
"react-dom": "^17.0.1",
2627
"react-helmet": "^6.1.0",
28+
"react-table": "^7.8.0",
29+
"recharts": "^2.1.10",
2730
"sass": "^1.51.0"
2831
},
2932
"devDependencies": {

0 commit comments

Comments
 (0)