-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMatch.js
140 lines (111 loc) · 2.89 KB
/
Match.js
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
import { TEAM_DIRE, TEAM_RADIANT } from '../constants.js';
import patchesWithMap from '../definitions/patchesWithMap.js';
let db = null;
const table = 'matches';
class Match {
constructor() {
// Automatically populated UUID by dexie-observable
this.uuid = undefined;
this.replay = null;
this.matchID = null;
this.leagueID = null;
this.duration = null;
this.createdAt = new Date();
this.endedAt = null;
this.winningTeamID = null;
this.teams = {};
}
get id() {
return this.matchID || this.uuid;
}
get label() {
return this.matchID || this.replay.filename;
}
get radiant() {
return this.teams[TEAM_RADIANT];
}
get dire() {
return this.teams[TEAM_DIRE];
}
get patch() {
return patchesWithMap.find((patch) => this.endedAt >= patch.date) || patchesWithMap[0];
}
get winner() {
return this.teams[this.winningTeamID];
}
delete() {
return db[table].delete(this.uuid);
}
async update(blob) {
const { default: Parser } = await import(
/* webpackChunkName: 'parser+viewer' */ '../parser/Parser.js'
);
this.replay = {
filename: blob.name,
size: blob.size,
blob,
};
const buffer = await blob.arrayBuffer();
const parser = new Parser(buffer);
const { summary } = parser;
const gameInfo = summary.gameInfo?.dota;
this.duration = summary.playbackTime;
if (gameInfo) {
this.matchID = gameInfo.matchId;
this.leagueID = gameInfo.leagueid;
this.endedAt = new Date(gameInfo.endTime * 1000);
this.gameMode = gameInfo.gameMode;
this.winningTeamID = gameInfo.gameWinner;
this.teams = {};
for (const player of gameInfo.playerInfo) {
const teamID = player.gameTeam;
let team = this.teams[teamID];
if (!team) {
team = {
name: null,
proID: null,
players: [],
};
this.teams[teamID] = team;
}
team.players.push({
hero: player.heroName,
});
}
if (this.radiant) {
this.radiant.name = gameInfo.radiantTeamTag;
this.radiant.proID = gameInfo.radiantTeamId;
}
if (this.dire) {
this.dire.name = gameInfo.direTeamTag;
this.dire.proID = gameInfo.direTeamId;
}
} else {
// Locally recorded replays lack the lineup information
this.matchID = null;
this.leagueID = null;
this.endedAt = new Date(blob.lastModified);
this.gameMode = -1;
this.winningTeamID = null;
}
// TODO: Dispose of parser
}
async refresh() {
if (this.replay) {
await this.update(this.replay.blob);
}
return this.save();
}
save() {
return db[table].put(this);
}
static get query() {
return db[table];
}
static connect(root) {
db = root;
db[table].mapToClass(this);
return this;
}
}
export default Match;