1
1
import { SqlAttribute , TableSchema } from './tableSchema' ;
2
2
import { createTableDB , queryDB , updateDB } from './storage' ;
3
+ import { update } from './match' ;
3
4
4
5
export const PLAYERS_TABLE = 'players' ;
5
6
export const MATCHES_TABLE = 'matches' ;
@@ -31,14 +32,17 @@ export const setup = async () => {
31
32
// Create matches table
32
33
const matchesAttributes = [
33
34
{ name : 'matchId' , type : 'TEXT' } ,
35
+ { name : 'map' , type : 'TEXT' } ,
34
36
{ name : 'teamA' , type : 'TEXT' } ,
35
37
{ name : 'teamAScore' , type : 'TEXT' } ,
36
38
{ name : 'teamB' , type : 'TEXT' } ,
37
39
{ name : 'teamBScore' , type : 'TEXT' } ,
38
- { name : 'map' , type : 'TEXT' } ,
39
40
{ name : 'winner' , type : 'TEXT' } ,
40
41
] as SqlAttribute [ ] ;
41
- const matchesTableSchema = new TableSchema ( MATCHES_TABLE , matchesAttributes , [ 'matchId' ] ) ;
42
+ const matchesTableSchema = new TableSchema ( MATCHES_TABLE , matchesAttributes , [
43
+ 'matchId' ,
44
+ 'map' ,
45
+ ] ) ;
42
46
await createTableDB ( matchesTableSchema ) ;
43
47
44
48
// Create player match stats table
@@ -53,6 +57,11 @@ export const setup = async () => {
53
57
type : 'TEXT' ,
54
58
constraints : `REFERENCES ${ MATCHES_TABLE } (matchId)` ,
55
59
} ,
60
+ {
61
+ name : 'map' ,
62
+ type : 'TEXT' ,
63
+ constraints : `REFERENCES ${ MATCHES_TABLE } (map)` ,
64
+ } ,
56
65
{ name : 'kills' , type : 'INTEGER' } ,
57
66
{ name : 'deaths' , type : 'INTEGER' } ,
58
67
{ name : 'assists' , type : 'INTEGER' } ,
@@ -71,35 +80,65 @@ export const setup = async () => {
71
80
const playerMatchStatsTableSchema = new TableSchema (
72
81
PLAYER_MATCH_STATS_TABLE ,
73
82
playerMatchStatsAttributes ,
74
- [ 'steamId' , 'matchId' ]
83
+ [ 'steamId' , 'matchId' , 'map' ]
75
84
) ;
76
85
await createTableDB ( playerMatchStatsTableSchema ) ;
77
86
} ;
78
87
79
88
export const onDamage = async (
80
89
matchId : string ,
90
+ map : string ,
81
91
attackerId : string ,
82
- victimId : string ,
83
92
damage : number ,
84
93
damageArmor : number ,
85
94
headshot : boolean
86
95
) => {
87
- // TODO
96
+ const currentAttackerMatchStats = ( await queryDB (
97
+ `SELECT hits,headshots,damages FROM ${ PLAYER_MATCH_STATS_TABLE } WHERE steamId = '${ attackerId } ' AND matchId = '${ matchId } '`
98
+ ) ) as number [ ] | undefined ;
99
+ const currentAttackerGlobalStats = ( await queryDB (
100
+ `SELECT tHits,tHeadshots,tDamages FROM ${ PLAYERS_TABLE } WHERE steamId = '${ attackerId } '`
101
+ ) ) as number [ ] | undefined ;
102
+
103
+ if ( currentAttackerMatchStats && currentAttackerGlobalStats ) {
104
+ await updateDB (
105
+ PLAYER_MATCH_STATS_TABLE ,
106
+ new Map < string , number > ( [
107
+ [ 'hits' , ( currentAttackerMatchStats [ 0 ] ?? 0 ) + 1 ] ,
108
+ [ 'headshots' , ( currentAttackerMatchStats [ 1 ] ?? 0 ) + ( headshot ? 1 : 0 ) ] ,
109
+ [ 'damages' , ( currentAttackerMatchStats [ 2 ] ?? 0 ) + damage + damageArmor ] ,
110
+ ] ) ,
111
+ `steamId = '${ attackerId } ' AND matchId = '${ matchId } '`
112
+ ) ;
113
+ await updateDB (
114
+ PLAYERS_TABLE ,
115
+ new Map < string , number > ( [
116
+ [ 'tHits' , ( currentAttackerGlobalStats [ 0 ] ?? 0 ) + 1 ] ,
117
+ [ 'tHeadshots' , ( currentAttackerGlobalStats [ 1 ] ?? 0 ) + ( headshot ? 1 : 0 ) ] ,
118
+ [ 'tDamages' , ( currentAttackerGlobalStats [ 2 ] ?? 0 ) + damage + damageArmor ] ,
119
+ ] ) ,
120
+ `steamId = '${ attackerId } '`
121
+ ) ;
122
+ }
123
+
124
+ await updateAdr ( matchId , map , attackerId ) ;
125
+ await updateHsPct ( matchId , map , attackerId ) ;
88
126
} ;
89
127
90
- export const onKill = async ( matchId : string , killerId : string , victimId : string ) => {
128
+ export const onKill = async ( matchId : string , map : string , killerId : string , victimId : string ) => {
91
129
const currentKillerMatchStats = ( await queryDB (
92
- `SELECT kills FROM ${ PLAYER_MATCH_STATS_TABLE } WHERE steamId = '${ killerId } ' AND matchId = '${ matchId } '`
130
+ `SELECT kills FROM ${ PLAYER_MATCH_STATS_TABLE } WHERE steamId = '${ killerId } ' AND matchId = '${ matchId } ' AND map = ' ${ map } ' `
93
131
) ) as number ;
94
132
const currentVictimMatchStats = ( await queryDB (
95
- `SELECT deaths FROM ${ PLAYER_MATCH_STATS_TABLE } WHERE steamId = '${ victimId } ' AND matchId = '${ matchId } '`
133
+ `SELECT deaths FROM ${ PLAYER_MATCH_STATS_TABLE } WHERE steamId = '${ victimId } ' AND matchId = '${ matchId } ' AND map = ' ${ map } ' `
96
134
) ) as number ;
97
135
const currentKillerGlobalStats = ( await queryDB (
98
136
`SELECT tKills FROM ${ PLAYERS_TABLE } WHERE steamId = '${ killerId } '`
99
137
) ) as number ;
100
138
const currentVictimGlobalStats = ( await queryDB (
101
139
`SELECT tDeaths FROM ${ PLAYERS_TABLE } WHERE steamId = '${ victimId } '`
102
140
) ) as number ;
141
+
103
142
await updateDB (
104
143
PLAYER_MATCH_STATS_TABLE ,
105
144
new Map < string , number > ( [ [ 'kills' , currentKillerMatchStats + 1 ] ] ) ,
@@ -120,15 +159,18 @@ export const onKill = async (matchId: string, killerId: string, victimId: string
120
159
new Map < string , number > ( [ [ 'tDeaths' , currentVictimGlobalStats + 1 ] ] ) ,
121
160
`steamId = '${ victimId } '`
122
161
) ;
162
+
163
+ await updateDiff ( matchId , map , killerId ) ;
123
164
} ;
124
165
125
- export const onAssist = async ( matchId : string , attackerId : string ) => {
166
+ export const onAssist = async ( matchId : string , map : string , attackerId : string ) => {
126
167
const currentAttackerMatchStats = ( await queryDB (
127
- `SELECT assists FROM ${ PLAYER_MATCH_STATS_TABLE } WHERE steamId = '${ attackerId } ' AND matchId = '${ matchId } '`
168
+ `SELECT assists FROM ${ PLAYER_MATCH_STATS_TABLE } WHERE steamId = '${ attackerId } ' AND matchId = '${ matchId } ' AND map = ' ${ map } ' `
128
169
) ) as number ;
129
170
const currentAttackerGlobalStats = ( await queryDB (
130
171
`SELECT tAssists FROM ${ PLAYERS_TABLE } WHERE steamId = '${ attackerId } '`
131
172
) ) as number ;
173
+
132
174
await updateDB (
133
175
PLAYER_MATCH_STATS_TABLE ,
134
176
new Map < string , number > ( [ [ 'assists' , currentAttackerMatchStats + 1 ] ] ) ,
@@ -141,13 +183,14 @@ export const onAssist = async (matchId: string, attackerId: string) => {
141
183
) ;
142
184
} ;
143
185
144
- export const onOtherDeath = async ( matchId : string , victimId : string ) => {
186
+ export const onOtherDeath = async ( matchId : string , map : string , victimId : string ) => {
145
187
const currentVictimMatchStats = ( await queryDB (
146
- `SELECT deaths FROM ${ PLAYER_MATCH_STATS_TABLE } WHERE steamId = '${ victimId } ' AND matchId = '${ matchId } '`
188
+ `SELECT deaths FROM ${ PLAYER_MATCH_STATS_TABLE } WHERE steamId = '${ victimId } ' AND matchId = '${ matchId } ' AND map = ' ${ map } ' `
147
189
) ) as number ;
148
190
const currentVictimGlobalStats = ( await queryDB (
149
191
`SELECT tDeaths FROM ${ PLAYERS_TABLE } WHERE steamId = '${ victimId } '`
150
192
) ) as number ;
193
+
151
194
await updateDB (
152
195
PLAYER_MATCH_STATS_TABLE ,
153
196
new Map < string , number > ( [ [ 'deaths' , currentVictimMatchStats + 1 ] ] ) ,
@@ -158,8 +201,82 @@ export const onOtherDeath = async (matchId: string, victimId: string) => {
158
201
new Map < string , number > ( [ [ 'tDeaths' , currentVictimGlobalStats + 1 ] ] ) ,
159
202
`steamId = '${ victimId } '`
160
203
) ;
204
+
205
+ await updateDiff ( matchId , map , victimId ) ;
206
+ } ;
207
+
208
+ export const updateRoundCount = async ( matchId : string , map : string ) => {
209
+ // TODO
210
+ } ;
211
+
212
+ const updateDiff = async ( matchId : string , map : string , steamId : string ) => {
213
+ const matchStats = ( await queryDB (
214
+ `SELECT kills,deaths FROM ${ PLAYER_MATCH_STATS_TABLE } WHERE steamId = '${ steamId } ' AND matchId = '${ matchId } ' AND map = '${ map } '`
215
+ ) ) as number [ ] | undefined ;
216
+ const globalStats = ( await queryDB (
217
+ `SELECT tKills,tDeaths FROM ${ PLAYERS_TABLE } WHERE steamId = '${ steamId } '`
218
+ ) ) as number [ ] | undefined ;
219
+
220
+ if ( matchStats && globalStats ) {
221
+ let diff = ( matchStats [ 1 ] ?? 0 ) - ( matchStats [ 0 ] ?? 0 ) ;
222
+ await updateDB (
223
+ PLAYER_MATCH_STATS_TABLE ,
224
+ new Map < string , number > ( [ [ 'diff' , diff ] ] ) ,
225
+ `steamId = '${ steamId } ' AND matchId = '${ matchId } ' AND map = '${ map } '`
226
+ ) ;
227
+ diff = ( globalStats [ 1 ] ?? 0 ) - ( globalStats [ 0 ] ?? 0 ) ;
228
+ await updateDB (
229
+ PLAYERS_TABLE ,
230
+ new Map < string , number > ( [ [ 'diff' , diff ] ] ) ,
231
+ `steamId = '${ steamId } '`
232
+ ) ;
233
+ }
234
+ } ;
235
+
236
+ const updateHsPct = async ( matchId : string , map : string , steamId : string ) => {
237
+ const matchStats = ( await queryDB (
238
+ `SELECT hits,headshots FROM ${ PLAYER_MATCH_STATS_TABLE } WHERE steamId = '${ steamId } ' AND matchId = '${ matchId } ' AND map = '${ map } '`
239
+ ) ) as number [ ] | undefined ;
240
+ const globalStats = ( await queryDB (
241
+ `SELECT tHits,tHeadshots FROM ${ PLAYERS_TABLE } WHERE steamId = '${ steamId } '`
242
+ ) ) as number [ ] | undefined ;
243
+
244
+ if ( matchStats && globalStats ) {
245
+ let hsPct = ( 100 * ( matchStats [ 1 ] ?? 0 ) ) / ( matchStats [ 0 ] ?? 1 ) ;
246
+ await updateDB (
247
+ PLAYER_MATCH_STATS_TABLE ,
248
+ new Map < string , number > ( [ [ 'hsPct' , hsPct ] ] ) ,
249
+ `steamId = '${ steamId } ' AND matchId = '${ matchId } ' AND map = '${ map } '`
250
+ ) ;
251
+ hsPct = ( 100 * ( globalStats [ 1 ] ?? 0 ) ) / ( globalStats [ 0 ] ?? 1 ) ;
252
+ await updateDB (
253
+ PLAYERS_TABLE ,
254
+ new Map < string , number > ( [ [ 'tHsPct' , hsPct ] ] ) ,
255
+ `steamId = '${ steamId } '`
256
+ ) ;
257
+ }
161
258
} ;
162
259
163
- export const updateRoundCount = async ( matchId : string ) => {
164
- //TODO
260
+ const updateAdr = async ( matchId : string , map : string , steamId : string ) => {
261
+ const matchStats = ( await queryDB (
262
+ `SELECT rounds,damages FROM ${ PLAYER_MATCH_STATS_TABLE } WHERE steamId = '${ steamId } ' AND matchId = '${ matchId } ' AND map = '${ map } '`
263
+ ) ) as number [ ] | undefined ;
264
+ const globalStats = ( await queryDB (
265
+ `SELECT tRounds,tDamages FROM ${ PLAYERS_TABLE } WHERE steamId = '${ steamId } '`
266
+ ) ) as number [ ] | undefined ;
267
+
268
+ if ( matchStats && globalStats ) {
269
+ let adr = ( matchStats [ 1 ] ?? 0 ) / ( matchStats [ 0 ] ?? 1 ) ;
270
+ await updateDB (
271
+ PLAYER_MATCH_STATS_TABLE ,
272
+ new Map < string , number > ( [ [ 'adr' , adr ] ] ) ,
273
+ `steamId = '${ steamId } ' AND matchId = '${ matchId } ' AND map = '${ map } '`
274
+ ) ;
275
+ adr = ( globalStats [ 1 ] ?? 0 ) / ( globalStats [ 0 ] ?? 1 ) ;
276
+ await updateDB (
277
+ PLAYERS_TABLE ,
278
+ new Map < string , number > ( [ [ 'tAdr' , adr ] ] ) ,
279
+ `steamId = '${ steamId } '`
280
+ ) ;
281
+ }
165
282
} ;
0 commit comments