@@ -11,7 +11,7 @@ import {
11
11
12
12
import { ONE , ZERO } from '../games/consts/global' ;
13
13
14
- import { User , TowerGame } from '.' ;
14
+ import { User , TowerGame , Perk , PerkInventory } from '.' ;
15
15
16
16
interface TowerStatisticsAttributes {
17
17
attempts : number ;
@@ -31,7 +31,7 @@ interface TowerStatisticsCreationAttributes {
31
31
indexes : [
32
32
{
33
33
unique : true ,
34
- fields : [ '_userId' , '_gameId ' ] ,
34
+ fields : [ '_userId' , '_towerGameId ' ] ,
35
35
} ,
36
36
] ,
37
37
} )
@@ -57,7 +57,7 @@ export class TowerStatistics
57
57
declare _towerGameId : number ;
58
58
59
59
@BelongsTo ( ( ) => TowerGame , {
60
- foreignKey : '_gameId ' ,
60
+ foreignKey : '_towerGameId ' ,
61
61
onUpdate : 'CASCADE' ,
62
62
onDelete : 'CASCADE' ,
63
63
} )
@@ -69,6 +69,15 @@ export class TowerStatistics
69
69
@Column ( DataType . INTEGER )
70
70
declare completed : number ;
71
71
72
+ @Column ( DataType . INTEGER )
73
+ declare lastFloorVisited : number ;
74
+
75
+ @Column ( DataType . INTEGER )
76
+ declare lastHealth : number ;
77
+
78
+ @Column ( DataType . INTEGER )
79
+ declare perks : number ;
80
+
72
81
static associations : {
73
82
_user : Association < TowerStatistics , User > ;
74
83
_towerGame : Association < TowerStatistics , TowerGame > ;
@@ -109,9 +118,18 @@ export async function findOrCreateTowerStatistics(
109
118
return towerStatistics . reload ( { transaction } ) ;
110
119
}
111
120
121
+ function calculatePerkNumber ( perks : Array < Perk & { PerkInventory : PerkInventory } > ) {
122
+ return perks . reduce (
123
+ ( totalPerks , { PerkInventory : { quantity } } ) => totalPerks + ( quantity ?? ZERO ) ,
124
+ ZERO
125
+ ) ;
126
+ }
127
+
112
128
export async function updateTowerAsCompleted (
113
129
gameId : number ,
114
130
userId : number ,
131
+ lastFloorVisited : number ,
132
+ perks : Array < Perk & { PerkInventory : PerkInventory } > ,
115
133
transaction ?: Transaction
116
134
) {
117
135
const towerStats = await TowerStatistics . findOne ( {
@@ -122,7 +140,11 @@ export async function updateTowerAsCompleted(
122
140
transaction,
123
141
} ) ;
124
142
if ( towerStats ) {
143
+ const perksNumber = calculatePerkNumber ( perks ) ;
125
144
await towerStats . increment ( { completed : 1 } , { transaction } ) ;
145
+ towerStats . lastFloorVisited = lastFloorVisited ;
146
+ towerStats . perks = perksNumber ;
147
+ await towerStats . save ( { transaction } ) ;
126
148
}
127
149
}
128
150
@@ -132,6 +154,9 @@ export function findTowerStatisticsByGame(gameId: number, transaction: Transacti
132
154
_towerGameId : gameId ,
133
155
} ,
134
156
order : [
157
+ [ 'lastFloorVisited' , 'DESC' ] ,
158
+ [ 'lastHealth' , 'DESC' ] ,
159
+ [ 'perks' , 'ASC' ] ,
135
160
[ 'completed' , 'DESC' ] ,
136
161
[ 'attempts' , 'ASC' ] ,
137
162
] ,
@@ -147,6 +172,8 @@ export function findTowerStatisticsByGame(gameId: number, transaction: Transacti
147
172
export async function updateTowerAttempts (
148
173
gameId : number ,
149
174
userId : number ,
175
+ lastFloorVisited : number ,
176
+ perks : Array < Perk & { PerkInventory : PerkInventory } > ,
150
177
transaction ?: Transaction
151
178
) {
152
179
const towerStats = await TowerStatistics . findOne ( {
@@ -157,6 +184,31 @@ export async function updateTowerAttempts(
157
184
transaction,
158
185
} ) ;
159
186
if ( towerStats ) {
187
+ const perksNumber = calculatePerkNumber ( perks ) ;
160
188
await towerStats . increment ( { attempts : 1 } , { transaction } ) ;
189
+ towerStats . lastFloorVisited = lastFloorVisited ;
190
+ towerStats . perks = perksNumber ;
191
+ await towerStats . save ( { transaction } ) ;
192
+ }
193
+ }
194
+
195
+ export async function updateLastHealth (
196
+ gameId : number ,
197
+ userId : number ,
198
+ lastHealth : number ,
199
+ transaction ?: Transaction
200
+ ) {
201
+ const towerStats = await TowerStatistics . findOne ( {
202
+ where : {
203
+ _userId : userId ,
204
+ _towerGameId : gameId ,
205
+ } ,
206
+ transaction,
207
+ } ) ;
208
+ if ( towerStats ) {
209
+ console . log ( '============================================' ) ;
210
+ console . log ( 'Here ' , lastHealth ) ;
211
+ towerStats . lastHealth = lastHealth ;
212
+ await towerStats . save ( { transaction } ) ;
161
213
}
162
214
}
0 commit comments