Skip to content

Commit 7656ff2

Browse files
committed
Refactor updatePlayTime worker with structured play time tracking
- Introduce type-safe data structures for play time tracking - Replace string-based map keys with structured objects - Improve type safety and readability of play time accumulation logic - Simplify database update loops by using structured data
1 parent 9d28c34 commit 7656ff2

File tree

1 file changed

+97
-62
lines changed

1 file changed

+97
-62
lines changed

apps/worker/src/workers/updatePlayTime.ts

Lines changed: 97 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -50,164 +50,199 @@ export async function updatePlayTime(snapshotId: number) {
5050
const deltaPlayTime = deltaSecond > 10 * 60 ? 5 * 60 : deltaSecond;
5151
const clients = removeDuplicatedClients(snapshot.clients);
5252

53-
// Create maps to store accumulated play times
54-
const playerMapPlayTimes = new Map<string, number>();
55-
const playerGameTypePlayTimes = new Map<string, number>();
56-
const playerPlayTimes = new Map<string, number>();
57-
const clanMapPlayTimes = new Map<string, number>();
58-
const clanGameTypePlayTimes = new Map<string, number>();
59-
const clanPlayTimes = new Map<string, number>();
60-
const clanPlayerPlayTimes = new Map<string, number>();
53+
// Create maps to store accumulated play times with structured values
54+
type PlayerMapPlayTime = { playerName: string; mapId: number; playTime: number };
55+
type PlayerGameTypePlayTime = { playerName: string; gameTypeName: string; playTime: number };
56+
type PlayerPlayTime = { playerName: string; playTime: number };
57+
type ClanMapPlayTime = { clanName: string; mapId: number; playTime: number };
58+
type ClanGameTypePlayTime = { clanName: string; gameTypeName: string; playTime: number };
59+
type ClanPlayTime = { clanName: string; playTime: number };
60+
type ClanPlayerPlayTime = { clanName: string; playerName: string; playTime: number };
61+
62+
const playerMapPlayTimes = new Map<string, PlayerMapPlayTime>();
63+
const playerGameTypePlayTimes = new Map<string, PlayerGameTypePlayTime>();
64+
const playerPlayTimes = new Map<string, PlayerPlayTime>();
65+
const clanMapPlayTimes = new Map<string, ClanMapPlayTime>();
66+
const clanGameTypePlayTimes = new Map<string, ClanGameTypePlayTime>();
67+
const clanPlayTimes = new Map<string, ClanPlayTime>();
68+
const clanPlayerPlayTimes = new Map<string, ClanPlayerPlayTime>();
6169

6270
// Process all clients and accumulate play times
6371
for (const client of clients) {
6472
// Player-Map play time
6573
const playerMapKey = `${client.playerName}_${snapshot.mapId}`;
66-
playerMapPlayTimes.set(playerMapKey, (playerMapPlayTimes.get(playerMapKey) || 0) + deltaPlayTime);
74+
const existingPlayerMapTime = playerMapPlayTimes.get(playerMapKey)?.playTime ?? 0;
75+
playerMapPlayTimes.set(playerMapKey, {
76+
playerName: client.playerName,
77+
mapId: snapshot.mapId,
78+
playTime: existingPlayerMapTime + deltaPlayTime
79+
});
6780

6881
// Player-GameType play time
6982
const playerGameTypeKey = `${client.playerName}_${snapshot.map.gameTypeName}`;
70-
playerGameTypePlayTimes.set(playerGameTypeKey, (playerGameTypePlayTimes.get(playerGameTypeKey) || 0) + deltaPlayTime);
83+
const existingPlayerGameTypeTime = playerGameTypePlayTimes.get(playerGameTypeKey)?.playTime ?? 0;
84+
playerGameTypePlayTimes.set(playerGameTypeKey, {
85+
playerName: client.playerName,
86+
gameTypeName: snapshot.map.gameTypeName,
87+
playTime: existingPlayerGameTypeTime + deltaPlayTime
88+
});
7189

7290
// Player total play time
73-
playerPlayTimes.set(client.playerName, (playerPlayTimes.get(client.playerName) || 0) + deltaPlayTime);
91+
const existingPlayerTime = playerPlayTimes.get(client.playerName)?.playTime ?? 0;
92+
playerPlayTimes.set(client.playerName, {
93+
playerName: client.playerName,
94+
playTime: existingPlayerTime + deltaPlayTime
95+
});
7496

7597
if (client.clanName !== null) {
7698
// Clan-Map play time
7799
const clanMapKey = `${client.clanName}_${snapshot.mapId}`;
78-
clanMapPlayTimes.set(clanMapKey, (clanMapPlayTimes.get(clanMapKey) || 0) + deltaPlayTime);
100+
const existingClanMapTime = clanMapPlayTimes.get(clanMapKey)?.playTime ?? 0;
101+
clanMapPlayTimes.set(clanMapKey, {
102+
clanName: client.clanName,
103+
mapId: snapshot.mapId,
104+
playTime: existingClanMapTime + deltaPlayTime
105+
});
79106

80107
// Clan-GameType play time
81108
const clanGameTypeKey = `${client.clanName}_${snapshot.map.gameTypeName}`;
82-
clanGameTypePlayTimes.set(clanGameTypeKey, (clanGameTypePlayTimes.get(clanGameTypeKey) || 0) + deltaPlayTime);
109+
const existingClanGameTypeTime = clanGameTypePlayTimes.get(clanGameTypeKey)?.playTime ?? 0;
110+
clanGameTypePlayTimes.set(clanGameTypeKey, {
111+
clanName: client.clanName,
112+
gameTypeName: snapshot.map.gameTypeName,
113+
playTime: existingClanGameTypeTime + deltaPlayTime
114+
});
83115

84116
// Clan total play time
85-
clanPlayTimes.set(client.clanName, (clanPlayTimes.get(client.clanName) || 0) + deltaPlayTime);
117+
const existingClanTime = clanPlayTimes.get(client.clanName)?.playTime ?? 0;
118+
clanPlayTimes.set(client.clanName, {
119+
clanName: client.clanName,
120+
playTime: existingClanTime + deltaPlayTime
121+
});
86122

87123
// Clan-Player play time
88124
const clanPlayerKey = `${client.clanName}_${client.playerName}`;
89-
clanPlayerPlayTimes.set(clanPlayerKey, (clanPlayerPlayTimes.get(clanPlayerKey) || 0) + deltaPlayTime);
125+
const existingClanPlayerTime = clanPlayerPlayTimes.get(clanPlayerKey)?.playTime ?? 0;
126+
clanPlayerPlayTimes.set(clanPlayerKey, {
127+
clanName: client.clanName,
128+
playerName: client.playerName,
129+
playTime: existingClanPlayerTime + deltaPlayTime
130+
});
90131
}
91132
}
92133

93-
// Batch update database sequentially
94134
// Update PlayerInfoMap records
95-
for (const [key, playTime] of playerMapPlayTimes.entries()) {
96-
const [playerName, mapId] = key.split('_');
135+
for (const data of playerMapPlayTimes.values()) {
97136
await prisma.playerInfoMap.upsert({
98137
where: {
99138
playerName_mapId: {
100-
mapId: parseInt(mapId),
101-
playerName,
139+
mapId: data.mapId,
140+
playerName: data.playerName,
102141
},
103142
},
104143
update: {
105-
playTime: { increment: playTime },
144+
playTime: { increment: data.playTime },
106145
},
107146
create: {
108-
player: { connect: { name: playerName } },
109-
map: { connect: { id: parseInt(mapId) } },
110-
playTime,
147+
player: { connect: { name: data.playerName } },
148+
map: { connect: { id: data.mapId } },
149+
playTime: data.playTime,
111150
},
112151
});
113152
}
114153

115154
// Update PlayerInfoGameType records
116-
for (const [key, playTime] of playerGameTypePlayTimes.entries()) {
117-
const [playerName, gameTypeName] = key.split('_');
155+
for (const data of playerGameTypePlayTimes.values()) {
118156
await prisma.playerInfoGameType.upsert({
119157
where: {
120158
playerName_gameTypeName: {
121-
gameTypeName,
122-
playerName,
159+
gameTypeName: data.gameTypeName,
160+
playerName: data.playerName,
123161
},
124162
},
125163
update: {
126-
playTime: { increment: playTime },
164+
playTime: { increment: data.playTime },
127165
},
128166
create: {
129-
player: { connect: { name: playerName } },
130-
gameType: { connect: { name: gameTypeName } },
131-
playTime,
167+
player: { connect: { name: data.playerName } },
168+
gameType: { connect: { name: data.gameTypeName } },
169+
playTime: data.playTime,
132170
},
133171
});
134172
}
135173

136174
// Update Player records
137-
for (const [playerName, playTime] of playerPlayTimes.entries()) {
175+
for (const data of playerPlayTimes.values()) {
138176
await prisma.player.update({
139-
where: { name: playerName },
140-
data: { playTime: { increment: playTime } },
177+
where: { name: data.playerName },
178+
data: { playTime: { increment: data.playTime } },
141179
});
142180
}
143181

144182
// Update ClanInfoMap records
145-
for (const [key, playTime] of clanMapPlayTimes.entries()) {
146-
const [clanName, mapId] = key.split('_');
183+
for (const data of clanMapPlayTimes.values()) {
147184
await prisma.clanInfoMap.upsert({
148185
where: {
149186
clanName_mapId: {
150-
mapId: parseInt(mapId),
151-
clanName,
187+
mapId: data.mapId,
188+
clanName: data.clanName,
152189
},
153190
},
154191
update: {
155-
playTime: { increment: playTime },
192+
playTime: { increment: data.playTime },
156193
},
157194
create: {
158-
clan: { connect: { name: clanName } },
159-
map: { connect: { id: parseInt(mapId) } },
160-
playTime,
195+
clan: { connect: { name: data.clanName } },
196+
map: { connect: { id: data.mapId } },
197+
playTime: data.playTime,
161198
},
162199
});
163200
}
164201

165202
// Update ClanInfoGameType records
166-
for (const [key, playTime] of clanGameTypePlayTimes.entries()) {
167-
const [clanName, gameTypeName] = key.split('_');
203+
for (const data of clanGameTypePlayTimes.values()) {
168204
await prisma.clanInfoGameType.upsert({
169205
where: {
170206
clanName_gameTypeName: {
171-
gameTypeName,
172-
clanName,
207+
gameTypeName: data.gameTypeName,
208+
clanName: data.clanName,
173209
},
174210
},
175211
update: {
176-
playTime: { increment: playTime },
212+
playTime: { increment: data.playTime },
177213
},
178214
create: {
179-
clan: { connect: { name: clanName } },
180-
gameType: { connect: { name: gameTypeName } },
181-
playTime,
215+
clan: { connect: { name: data.clanName } },
216+
gameType: { connect: { name: data.gameTypeName } },
217+
playTime: data.playTime,
182218
},
183219
});
184220
}
185221

186222
// Update Clan records
187-
for (const [clanName, playTime] of clanPlayTimes.entries()) {
223+
for (const data of clanPlayTimes.values()) {
188224
await prisma.clan.update({
189-
where: { name: clanName },
190-
data: { playTime: { increment: playTime } },
225+
where: { name: data.clanName },
226+
data: { playTime: { increment: data.playTime } },
191227
});
192228
}
193229

194230
// Update ClanPlayerInfo records
195-
for (const [key, playTime] of clanPlayerPlayTimes.entries()) {
196-
const [clanName, playerName] = key.split('_');
231+
for (const data of clanPlayerPlayTimes.values()) {
197232
await prisma.clanPlayerInfo.upsert({
198233
where: {
199234
clanName_playerName: {
200-
clanName,
201-
playerName,
235+
clanName: data.clanName,
236+
playerName: data.playerName,
202237
},
203238
},
204239
update: {
205-
playTime: { increment: playTime },
240+
playTime: { increment: data.playTime },
206241
},
207242
create: {
208-
clan: { connect: { name: clanName } },
209-
player: { connect: { name: playerName } },
210-
playTime,
243+
clan: { connect: { name: data.clanName } },
244+
player: { connect: { name: data.playerName } },
245+
playTime: data.playTime,
211246
},
212247
});
213248
}

0 commit comments

Comments
 (0)