-
Notifications
You must be signed in to change notification settings - Fork 4
/
matches.ts
174 lines (153 loc) · 4.94 KB
/
matches.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import express from "express";
import winston from "winston";
import _ from 'lodash';
import sha1 from 'sha1';
import { Player, Server, Match, IMatchModel } from "./db";
import { getChatFor } from "./chat";
import { ITribesServerQueryResponse, IFullReportPlayer } from "./types";
let router = express.Router();
function getPlayersForTeam(data: IMatchModel, team: string) {
return data.fullReport.players
.filter(p => p.team === team)
.map(x => {
const ip = x.ip.split(':')[0];
return {
...x,
ip: undefined,
ipHash: ip && sha1(ip),
ipHashFirstTwo: ip && sha1(ip.split('.')[0] + ip.split('.')[1]),
ipHashFirstThree: ip && sha1(ip.split('.')[0] + ip.split('.')[1] + ip.split('.')[2]),
url: '/player/' + encodeURIComponent(x.name)
}
})
.sort((a, b) => b.score - a.score)
}
function handleItem(key: string, player: IFullReportPlayer) {
return {
value: player[key],
name: player.name,
team: player.team
}
}
function prepareStats(data: IMatchModel) {
const keys = Object.keys(data.fullReport.players[0]).filter(x => [
'style',
'defense',
'offense',
'deaths',
'kills',
'score',
'team',
'voice',
'starttime',
'ping',
'name',
'url',
'ip'
].indexOf(x) === -1);
function getDataForKey(k: string) {
return {
max: handleItem(k, _.maxBy(data.fullReport.players, x => x[k])!),
min: handleItem(k, _.minBy(data.fullReport.players, x => x[k])!),
sum: _.sumBy(data.fullReport.players, k),
avg: _.meanBy(data.fullReport.players, k),
key: k
};
}
const ret: Record<string, ReturnType<typeof getDataForKey>> = keys.reduce(
(o, k) => o[k] = getDataForKey(k),
{}
);
return _.sortBy(_.values(ret).filter(x => x.sum > 0), x => (require('../data/statorder.json'))[x.key] || "99" + x.key);
}
function generateResultInfo(data: ITribesServerQueryResponse) {
if (data.teamonescore > data.teamtwoscore) {
return {text: `${data.teamone} won the match!`, team: data.teamone};
} else if (data.teamonescore < data.teamtwoscore) {
return {text: `${data.teamtwo} won the match!`, team: data.teamtwo};
} else {
return {text: 'Match ended in a tie!'}
}
}
function getMatchData(id: string) {
return Match
.where('_id').equals(id)
.findOne()
.exec()
.then((data: IMatchModel) => ({
id: data._id,
when: data.when,
result: generateResultInfo(data.basicReport) ,
info: { ...data.basicReport, players: undefined },
team1: getPlayersForTeam(data, data.basicReport.teamone),
team2: getPlayersForTeam(data, data.basicReport.teamtwo),
stats: prepareStats(data),
}));
}
router.get('/matches/:id.json', function (req, res, next) {
return getMatchData(req.params.id).then(data => res.json(data))
});
router.get('/matches/:id', function (req, res, next) {
return getMatchData(req.params.id).then(data => res.render('match', data))
});
async function getMatchesData(page: number) {
const perPage = 50;
const [data, count] = await Promise.all<IMatchModel[], number>([
Match
.where('basicReport.numplayers').ne(0)
.sort('-when')
.select({
basicReport: true,
when: true
})
.skip((page - 1) * perPage)
.limit(perPage)
.exec(),
Match
.where('basicReport.numplayers').ne(0)
.count()
.exec()
]);
const lastPage = Math.floor(count / perPage) + 1;
return {
matches: data.map(m => ({
id: m._id,
when: m.when,
map: m.basicReport.mapname,
hostname: m.basicReport.hostname,
teamone: m.basicReport.teamone,
teamonescore: m.basicReport.teamonescore,
teamtwo: m.basicReport.teamtwo,
teamtwoscore: m.basicReport.teamtwoscore,
gametype: m.basicReport.gametype,
numplayers: m.basicReport.numplayers,
})),
pagination: {
page,
perPage,
count: count,
first: 1,
last: lastPage,
prev: page - 1 >= 1 ? page - 1 : null,
next: page + 1 <= lastPage ? page + 1 : null,
}
};
}
function parsePage(page: string) {
try {
return parseInt((page || 1) + '');
} catch (ex) {
return 1;
}
}
router.get('/matches.json', function (req, res, next) {
return getMatchesData(parsePage(req.query.page))
.then(data => res.json(data))
});
router.get('/matches', function (req, res, next) {
return getMatchesData(parsePage(req.query.page))
.then(data => res.render('matches', data))
});
module.exports = {
router
}