@opprs/db-prisma 0.5.2-canary.6782466
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +85 -0
- package/dist/index.cjs +573 -0
- package/dist/index.d.cts +608 -0
- package/dist/index.d.ts +608 -0
- package/dist/index.js +500 -0
- package/package.json +85 -0
- package/prisma/migrations/20251231075738_init/migration.sql +112 -0
- package/prisma/migrations/migration_lock.toml +3 -0
- package/prisma/schema.prisma +123 -0
- package/prisma/seed.ts +349 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Mitch McAffee
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# @opprs/db-prisma
|
|
2
|
+
|
|
3
|
+
PostgreSQL persistence layer for the Open Pinball Player Ranking System (OPPRS) using Prisma ORM.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @opprs/db-prisma @prisma/client
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
### 1. Configure database connection
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# Set DATABASE_URL in your environment
|
|
17
|
+
DATABASE_URL="postgresql://user:password@localhost:5432/opprs?schema=public"
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### 2. Generate Prisma client and run migrations
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pnpm --filter @opprs/db-prisma run db:generate
|
|
24
|
+
pnpm --filter @opprs/db-prisma run db:migrate
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### 3. Use in your code
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import {
|
|
31
|
+
prisma,
|
|
32
|
+
createPlayer,
|
|
33
|
+
createTournament,
|
|
34
|
+
createResult,
|
|
35
|
+
getPlayerStats,
|
|
36
|
+
} from '@opprs/db-prisma';
|
|
37
|
+
|
|
38
|
+
// Create a player
|
|
39
|
+
const player = await createPlayer({
|
|
40
|
+
name: 'Alice Champion',
|
|
41
|
+
email: 'alice@example.com',
|
|
42
|
+
rating: 1500,
|
|
43
|
+
isRated: true,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Create a tournament and record results
|
|
47
|
+
const tournament = await createTournament({
|
|
48
|
+
name: 'Spring Championship 2024',
|
|
49
|
+
date: new Date('2024-03-15'),
|
|
50
|
+
location: 'Portland, OR',
|
|
51
|
+
eventBooster: 'CERTIFIED',
|
|
52
|
+
tgpConfig: {
|
|
53
|
+
qualifying: { type: 'limited', meaningfulGames: 7 },
|
|
54
|
+
finals: { formatType: 'match-play', meaningfulGames: 15, fourPlayerGroups: true },
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
await createResult({
|
|
59
|
+
playerId: player.id,
|
|
60
|
+
tournamentId: tournament.id,
|
|
61
|
+
position: 1,
|
|
62
|
+
totalPoints: 87.5,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Get player statistics
|
|
66
|
+
const stats = await getPlayerStats(player.id);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Features
|
|
70
|
+
|
|
71
|
+
- **PostgreSQL + Prisma ORM** - Type-safe database queries and migrations
|
|
72
|
+
- **Query Helpers** - High-level functions for players, tournaments, and results
|
|
73
|
+
- **Time Decay** - Automatic point depreciation calculations
|
|
74
|
+
- **Player Statistics** - Comprehensive performance metrics
|
|
75
|
+
- **Seed Data** - Sample data for development and testing
|
|
76
|
+
|
|
77
|
+
## Documentation
|
|
78
|
+
|
|
79
|
+
For complete API reference, database schema, and integration examples, see the [Database (Prisma) documentation](../../docs/db-prisma.md).
|
|
80
|
+
|
|
81
|
+
Full documentation site: [OPPRS Documentation](https://thatguyinabeanie.github.io/OPPR/)
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,573 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
connect: () => connect,
|
|
24
|
+
countPlayers: () => countPlayers,
|
|
25
|
+
countResults: () => countResults,
|
|
26
|
+
countTournaments: () => countTournaments,
|
|
27
|
+
createManyResults: () => createManyResults,
|
|
28
|
+
createPlayer: () => createPlayer,
|
|
29
|
+
createResult: () => createResult,
|
|
30
|
+
createTournament: () => createTournament,
|
|
31
|
+
deletePlayer: () => deletePlayer,
|
|
32
|
+
deleteResult: () => deleteResult,
|
|
33
|
+
deleteResultsByTournament: () => deleteResultsByTournament,
|
|
34
|
+
deleteTournament: () => deleteTournament,
|
|
35
|
+
disconnect: () => disconnect,
|
|
36
|
+
findPlayerByEmail: () => findPlayerByEmail,
|
|
37
|
+
findPlayerByExternalId: () => findPlayerByExternalId,
|
|
38
|
+
findPlayerById: () => findPlayerById,
|
|
39
|
+
findPlayers: () => findPlayers,
|
|
40
|
+
findResultById: () => findResultById,
|
|
41
|
+
findResultByPlayerAndTournament: () => findResultByPlayerAndTournament,
|
|
42
|
+
findResults: () => findResults,
|
|
43
|
+
findTournamentByExternalId: () => findTournamentByExternalId,
|
|
44
|
+
findTournamentById: () => findTournamentById,
|
|
45
|
+
findTournaments: () => findTournaments,
|
|
46
|
+
getMajorTournaments: () => getMajorTournaments,
|
|
47
|
+
getPlayerResults: () => getPlayerResults,
|
|
48
|
+
getPlayerStats: () => getPlayerStats,
|
|
49
|
+
getPlayerTopFinishes: () => getPlayerTopFinishes,
|
|
50
|
+
getPlayerWithResults: () => getPlayerWithResults,
|
|
51
|
+
getRatedPlayers: () => getRatedPlayers,
|
|
52
|
+
getRecentTournaments: () => getRecentTournaments,
|
|
53
|
+
getTopPlayersByRanking: () => getTopPlayersByRanking,
|
|
54
|
+
getTopPlayersByRating: () => getTopPlayersByRating,
|
|
55
|
+
getTournamentResults: () => getTournamentResults,
|
|
56
|
+
getTournamentStats: () => getTournamentStats,
|
|
57
|
+
getTournamentWithResults: () => getTournamentWithResults,
|
|
58
|
+
getTournamentsByBoosterType: () => getTournamentsByBoosterType,
|
|
59
|
+
getTournamentsByDateRange: () => getTournamentsByDateRange,
|
|
60
|
+
prisma: () => prisma,
|
|
61
|
+
recalculateTimeDecay: () => recalculateTimeDecay,
|
|
62
|
+
searchPlayers: () => searchPlayers,
|
|
63
|
+
searchTournaments: () => searchTournaments,
|
|
64
|
+
testConnection: () => testConnection,
|
|
65
|
+
updatePlayer: () => updatePlayer,
|
|
66
|
+
updatePlayerRating: () => updatePlayerRating,
|
|
67
|
+
updateResult: () => updateResult,
|
|
68
|
+
updateResultPoints: () => updateResultPoints,
|
|
69
|
+
updateTournament: () => updateTournament
|
|
70
|
+
});
|
|
71
|
+
module.exports = __toCommonJS(index_exports);
|
|
72
|
+
|
|
73
|
+
// src/client.ts
|
|
74
|
+
var import_client = require("@prisma/client");
|
|
75
|
+
var globalForPrisma = globalThis;
|
|
76
|
+
var prisma = globalForPrisma.prisma ?? new import_client.PrismaClient({
|
|
77
|
+
log: process.env.NODE_ENV === "development" ? ["query", "error", "warn"] : ["error"]
|
|
78
|
+
});
|
|
79
|
+
if (process.env.NODE_ENV !== "production") {
|
|
80
|
+
globalForPrisma.prisma = prisma;
|
|
81
|
+
}
|
|
82
|
+
async function disconnect() {
|
|
83
|
+
await prisma.$disconnect();
|
|
84
|
+
}
|
|
85
|
+
async function connect() {
|
|
86
|
+
await prisma.$connect();
|
|
87
|
+
}
|
|
88
|
+
async function testConnection() {
|
|
89
|
+
try {
|
|
90
|
+
await prisma.$queryRaw`SELECT 1`;
|
|
91
|
+
return true;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
console.error("Database connection test failed:", error);
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// src/players.ts
|
|
99
|
+
async function createPlayer(data) {
|
|
100
|
+
return prisma.player.create({
|
|
101
|
+
data
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
async function findPlayerById(id, include) {
|
|
105
|
+
return prisma.player.findUnique({
|
|
106
|
+
where: { id },
|
|
107
|
+
include
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
async function findPlayerByExternalId(externalId, include) {
|
|
111
|
+
return prisma.player.findUnique({
|
|
112
|
+
where: { externalId },
|
|
113
|
+
include
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
async function findPlayerByEmail(email, include) {
|
|
117
|
+
return prisma.player.findUnique({
|
|
118
|
+
where: { email },
|
|
119
|
+
include
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
async function findPlayers(options = {}) {
|
|
123
|
+
return prisma.player.findMany({
|
|
124
|
+
take: options.take,
|
|
125
|
+
skip: options.skip,
|
|
126
|
+
where: options.where,
|
|
127
|
+
orderBy: options.orderBy,
|
|
128
|
+
include: options.include
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
async function getRatedPlayers(options = {}) {
|
|
132
|
+
return findPlayers({
|
|
133
|
+
...options,
|
|
134
|
+
where: { isRated: true }
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
async function getTopPlayersByRating(limit = 50) {
|
|
138
|
+
return findPlayers({
|
|
139
|
+
take: limit,
|
|
140
|
+
orderBy: { rating: "desc" },
|
|
141
|
+
where: { isRated: true }
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
async function getTopPlayersByRanking(limit = 50) {
|
|
145
|
+
return findPlayers({
|
|
146
|
+
take: limit,
|
|
147
|
+
orderBy: { ranking: "asc" },
|
|
148
|
+
where: {
|
|
149
|
+
isRated: true,
|
|
150
|
+
ranking: { not: null }
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
async function updatePlayer(id, data) {
|
|
155
|
+
return prisma.player.update({
|
|
156
|
+
where: { id },
|
|
157
|
+
data
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
async function updatePlayerRating(id, rating, ratingDeviation, eventCount) {
|
|
161
|
+
const updateData = {
|
|
162
|
+
rating,
|
|
163
|
+
ratingDeviation,
|
|
164
|
+
lastRatingUpdate: /* @__PURE__ */ new Date(),
|
|
165
|
+
lastEventDate: /* @__PURE__ */ new Date()
|
|
166
|
+
};
|
|
167
|
+
if (eventCount !== void 0) {
|
|
168
|
+
updateData.eventCount = eventCount;
|
|
169
|
+
updateData.isRated = eventCount >= 5;
|
|
170
|
+
}
|
|
171
|
+
return updatePlayer(id, updateData);
|
|
172
|
+
}
|
|
173
|
+
async function deletePlayer(id) {
|
|
174
|
+
return prisma.player.delete({
|
|
175
|
+
where: { id }
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
async function countPlayers(where) {
|
|
179
|
+
return prisma.player.count({ where });
|
|
180
|
+
}
|
|
181
|
+
async function getPlayerWithResults(id) {
|
|
182
|
+
const player = await prisma.player.findUnique({
|
|
183
|
+
where: { id },
|
|
184
|
+
include: {
|
|
185
|
+
tournamentResults: {
|
|
186
|
+
include: {
|
|
187
|
+
tournament: true
|
|
188
|
+
},
|
|
189
|
+
orderBy: {
|
|
190
|
+
tournament: {
|
|
191
|
+
date: "desc"
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
if (!player) {
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
return {
|
|
201
|
+
...player,
|
|
202
|
+
results: player.tournamentResults
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
async function searchPlayers(query, limit = 20) {
|
|
206
|
+
return findPlayers({
|
|
207
|
+
take: limit,
|
|
208
|
+
where: {
|
|
209
|
+
OR: [
|
|
210
|
+
{ name: { contains: query, mode: "insensitive" } },
|
|
211
|
+
{ email: { contains: query, mode: "insensitive" } }
|
|
212
|
+
]
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// src/tournaments.ts
|
|
218
|
+
async function createTournament(data) {
|
|
219
|
+
return prisma.tournament.create({
|
|
220
|
+
data: {
|
|
221
|
+
...data,
|
|
222
|
+
eventBooster: data.eventBooster ?? "NONE"
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
async function findTournamentById(id, include) {
|
|
227
|
+
return prisma.tournament.findUnique({
|
|
228
|
+
where: { id },
|
|
229
|
+
include
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
async function findTournamentByExternalId(externalId, include) {
|
|
233
|
+
return prisma.tournament.findUnique({
|
|
234
|
+
where: { externalId },
|
|
235
|
+
include
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
async function findTournaments(options = {}) {
|
|
239
|
+
return prisma.tournament.findMany({
|
|
240
|
+
take: options.take,
|
|
241
|
+
skip: options.skip,
|
|
242
|
+
where: options.where,
|
|
243
|
+
orderBy: options.orderBy,
|
|
244
|
+
include: options.include
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
async function getRecentTournaments(limit = 20, include) {
|
|
248
|
+
return findTournaments({
|
|
249
|
+
take: limit,
|
|
250
|
+
orderBy: { date: "desc" },
|
|
251
|
+
include
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
async function getTournamentsByDateRange(startDate, endDate, options = {}) {
|
|
255
|
+
return findTournaments({
|
|
256
|
+
...options,
|
|
257
|
+
where: {
|
|
258
|
+
date: {
|
|
259
|
+
gte: startDate,
|
|
260
|
+
lte: endDate
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
async function getTournamentsByBoosterType(boosterType, options = {}) {
|
|
266
|
+
return findTournaments({
|
|
267
|
+
...options,
|
|
268
|
+
where: { eventBooster: boosterType }
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
async function getMajorTournaments(limit) {
|
|
272
|
+
return findTournaments({
|
|
273
|
+
take: limit,
|
|
274
|
+
where: { eventBooster: "MAJOR" },
|
|
275
|
+
orderBy: { date: "desc" }
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
async function updateTournament(id, data) {
|
|
279
|
+
return prisma.tournament.update({
|
|
280
|
+
where: { id },
|
|
281
|
+
data
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
async function deleteTournament(id) {
|
|
285
|
+
return prisma.tournament.delete({
|
|
286
|
+
where: { id }
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
async function countTournaments(where) {
|
|
290
|
+
return prisma.tournament.count({ where });
|
|
291
|
+
}
|
|
292
|
+
async function getTournamentWithResults(id) {
|
|
293
|
+
return prisma.tournament.findUnique({
|
|
294
|
+
where: { id },
|
|
295
|
+
include: {
|
|
296
|
+
results: {
|
|
297
|
+
include: {
|
|
298
|
+
player: true
|
|
299
|
+
},
|
|
300
|
+
orderBy: {
|
|
301
|
+
position: "asc"
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
async function searchTournaments(query, limit = 20) {
|
|
308
|
+
return findTournaments({
|
|
309
|
+
take: limit,
|
|
310
|
+
where: {
|
|
311
|
+
OR: [
|
|
312
|
+
{ name: { contains: query, mode: "insensitive" } },
|
|
313
|
+
{ location: { contains: query, mode: "insensitive" } }
|
|
314
|
+
]
|
|
315
|
+
},
|
|
316
|
+
orderBy: { date: "desc" }
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
async function getTournamentStats(id) {
|
|
320
|
+
const tournament = await getTournamentWithResults(id);
|
|
321
|
+
if (!tournament) {
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
const playerCount = tournament.results.length;
|
|
325
|
+
if (playerCount === 0) {
|
|
326
|
+
return {
|
|
327
|
+
tournament,
|
|
328
|
+
playerCount: 0,
|
|
329
|
+
averagePoints: 0,
|
|
330
|
+
averageEfficiency: 0,
|
|
331
|
+
highestPoints: 0,
|
|
332
|
+
lowestPoints: 0
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
const totalPoints = tournament.results.reduce((sum, r) => sum + (r.totalPoints || 0), 0);
|
|
336
|
+
const totalEfficiency = tournament.results.reduce((sum, r) => sum + (r.efficiency || 0), 0);
|
|
337
|
+
const allPoints = tournament.results.map((r) => r.totalPoints || 0);
|
|
338
|
+
return {
|
|
339
|
+
tournament,
|
|
340
|
+
playerCount,
|
|
341
|
+
averagePoints: totalPoints / playerCount,
|
|
342
|
+
averageEfficiency: totalEfficiency / playerCount,
|
|
343
|
+
highestPoints: Math.max(...allPoints),
|
|
344
|
+
lowestPoints: Math.min(...allPoints)
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// src/results.ts
|
|
349
|
+
async function createResult(data) {
|
|
350
|
+
const resultData = {
|
|
351
|
+
...data,
|
|
352
|
+
decayedPoints: data.decayedPoints ?? data.totalPoints ?? 0
|
|
353
|
+
};
|
|
354
|
+
return prisma.tournamentResult.create({
|
|
355
|
+
data: resultData
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
async function createManyResults(data) {
|
|
359
|
+
const resultsData = data.map((item) => ({
|
|
360
|
+
...item,
|
|
361
|
+
decayedPoints: item.decayedPoints ?? item.totalPoints ?? 0
|
|
362
|
+
}));
|
|
363
|
+
return prisma.tournamentResult.createMany({
|
|
364
|
+
data: resultsData
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
async function findResultById(id, include) {
|
|
368
|
+
return prisma.tournamentResult.findUnique({
|
|
369
|
+
where: { id },
|
|
370
|
+
include
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
async function findResultByPlayerAndTournament(playerId, tournamentId, include) {
|
|
374
|
+
return prisma.tournamentResult.findUnique({
|
|
375
|
+
where: {
|
|
376
|
+
playerId_tournamentId: {
|
|
377
|
+
playerId,
|
|
378
|
+
tournamentId
|
|
379
|
+
}
|
|
380
|
+
},
|
|
381
|
+
include
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
async function findResults(options = {}) {
|
|
385
|
+
return prisma.tournamentResult.findMany({
|
|
386
|
+
take: options.take,
|
|
387
|
+
skip: options.skip,
|
|
388
|
+
where: options.where,
|
|
389
|
+
orderBy: options.orderBy,
|
|
390
|
+
include: options.include
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
async function getPlayerResults(playerId, options = {}) {
|
|
394
|
+
return findResults({
|
|
395
|
+
...options,
|
|
396
|
+
where: { playerId },
|
|
397
|
+
include: { tournament: true, ...options.include },
|
|
398
|
+
orderBy: { tournament: { date: "desc" } }
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
async function getTournamentResults(tournamentId, options = {}) {
|
|
402
|
+
return findResults({
|
|
403
|
+
...options,
|
|
404
|
+
where: { tournamentId },
|
|
405
|
+
include: { player: true, ...options.include },
|
|
406
|
+
orderBy: { position: "asc" }
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
async function getPlayerTopFinishes(playerId, limit = 15) {
|
|
410
|
+
return findResults({
|
|
411
|
+
where: { playerId },
|
|
412
|
+
take: limit,
|
|
413
|
+
include: { tournament: true },
|
|
414
|
+
orderBy: { decayedPoints: "desc" }
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
async function updateResult(id, data) {
|
|
418
|
+
return prisma.tournamentResult.update({
|
|
419
|
+
where: { id },
|
|
420
|
+
data
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
async function updateResultPoints(id, linearPoints, dynamicPoints, totalPoints) {
|
|
424
|
+
const result = await findResultById(id, {
|
|
425
|
+
tournament: true
|
|
426
|
+
});
|
|
427
|
+
if (!result) {
|
|
428
|
+
throw new Error(`Result with id ${id} not found`);
|
|
429
|
+
}
|
|
430
|
+
const now = /* @__PURE__ */ new Date();
|
|
431
|
+
const tournamentDate = result.tournament.date;
|
|
432
|
+
const ageInDays = Math.floor((now.getTime() - tournamentDate.getTime()) / (1e3 * 60 * 60 * 24));
|
|
433
|
+
const ageInYears = ageInDays / 365;
|
|
434
|
+
let decayMultiplier = 0;
|
|
435
|
+
if (ageInYears < 1) {
|
|
436
|
+
decayMultiplier = 1;
|
|
437
|
+
} else if (ageInYears < 2) {
|
|
438
|
+
decayMultiplier = 0.75;
|
|
439
|
+
} else if (ageInYears < 3) {
|
|
440
|
+
decayMultiplier = 0.5;
|
|
441
|
+
} else {
|
|
442
|
+
decayMultiplier = 0;
|
|
443
|
+
}
|
|
444
|
+
const decayedPoints = totalPoints * decayMultiplier;
|
|
445
|
+
return updateResult(id, {
|
|
446
|
+
linearPoints,
|
|
447
|
+
dynamicPoints,
|
|
448
|
+
totalPoints,
|
|
449
|
+
ageInDays,
|
|
450
|
+
decayMultiplier,
|
|
451
|
+
decayedPoints
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
async function deleteResult(id) {
|
|
455
|
+
return prisma.tournamentResult.delete({
|
|
456
|
+
where: { id }
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
async function deleteResultsByTournament(tournamentId) {
|
|
460
|
+
return prisma.tournamentResult.deleteMany({
|
|
461
|
+
where: { tournamentId }
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
async function countResults(where) {
|
|
465
|
+
return prisma.tournamentResult.count({ where });
|
|
466
|
+
}
|
|
467
|
+
async function getPlayerStats(playerId) {
|
|
468
|
+
const results = await getPlayerResults(playerId);
|
|
469
|
+
if (results.length === 0) {
|
|
470
|
+
return null;
|
|
471
|
+
}
|
|
472
|
+
const totalPoints = results.reduce((sum, r) => sum + (r.totalPoints || 0), 0);
|
|
473
|
+
const totalDecayedPoints = results.reduce((sum, r) => sum + (r.decayedPoints || 0), 0);
|
|
474
|
+
const averagePosition = results.reduce((sum, r) => sum + r.position, 0) / results.length;
|
|
475
|
+
const averageEfficiency = results.reduce((sum, r) => sum + (r.efficiency || 0), 0) / results.length;
|
|
476
|
+
const firstPlaceFinishes = results.filter((r) => r.position === 1).length;
|
|
477
|
+
const topThreeFinishes = results.filter((r) => r.position <= 3).length;
|
|
478
|
+
return {
|
|
479
|
+
totalEvents: results.length,
|
|
480
|
+
totalPoints,
|
|
481
|
+
totalDecayedPoints,
|
|
482
|
+
averagePoints: totalPoints / results.length,
|
|
483
|
+
averagePosition,
|
|
484
|
+
averageFinish: averagePosition,
|
|
485
|
+
averageEfficiency,
|
|
486
|
+
firstPlaceFinishes,
|
|
487
|
+
topThreeFinishes,
|
|
488
|
+
bestFinish: Math.min(...results.map((r) => r.position)),
|
|
489
|
+
highestPoints: Math.max(...results.map((r) => r.totalPoints || 0))
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
async function recalculateTimeDecay(referenceDate = /* @__PURE__ */ new Date()) {
|
|
493
|
+
const results = await findResults({
|
|
494
|
+
include: { tournament: true }
|
|
495
|
+
});
|
|
496
|
+
const updates = results.map((result) => {
|
|
497
|
+
const tournamentDate = result.tournament.date;
|
|
498
|
+
const ageInDays = Math.floor(
|
|
499
|
+
(referenceDate.getTime() - tournamentDate.getTime()) / (1e3 * 60 * 60 * 24)
|
|
500
|
+
);
|
|
501
|
+
const ageInYears = ageInDays / 365;
|
|
502
|
+
let decayMultiplier = 0;
|
|
503
|
+
if (ageInYears < 1) {
|
|
504
|
+
decayMultiplier = 1;
|
|
505
|
+
} else if (ageInYears < 2) {
|
|
506
|
+
decayMultiplier = 0.75;
|
|
507
|
+
} else if (ageInYears < 3) {
|
|
508
|
+
decayMultiplier = 0.5;
|
|
509
|
+
} else {
|
|
510
|
+
decayMultiplier = 0;
|
|
511
|
+
}
|
|
512
|
+
const decayedPoints = (result.totalPoints || 0) * decayMultiplier;
|
|
513
|
+
return prisma.tournamentResult.update({
|
|
514
|
+
where: { id: result.id },
|
|
515
|
+
data: {
|
|
516
|
+
ageInDays,
|
|
517
|
+
decayMultiplier,
|
|
518
|
+
decayedPoints
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
});
|
|
522
|
+
return Promise.all(updates);
|
|
523
|
+
}
|
|
524
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
525
|
+
0 && (module.exports = {
|
|
526
|
+
connect,
|
|
527
|
+
countPlayers,
|
|
528
|
+
countResults,
|
|
529
|
+
countTournaments,
|
|
530
|
+
createManyResults,
|
|
531
|
+
createPlayer,
|
|
532
|
+
createResult,
|
|
533
|
+
createTournament,
|
|
534
|
+
deletePlayer,
|
|
535
|
+
deleteResult,
|
|
536
|
+
deleteResultsByTournament,
|
|
537
|
+
deleteTournament,
|
|
538
|
+
disconnect,
|
|
539
|
+
findPlayerByEmail,
|
|
540
|
+
findPlayerByExternalId,
|
|
541
|
+
findPlayerById,
|
|
542
|
+
findPlayers,
|
|
543
|
+
findResultById,
|
|
544
|
+
findResultByPlayerAndTournament,
|
|
545
|
+
findResults,
|
|
546
|
+
findTournamentByExternalId,
|
|
547
|
+
findTournamentById,
|
|
548
|
+
findTournaments,
|
|
549
|
+
getMajorTournaments,
|
|
550
|
+
getPlayerResults,
|
|
551
|
+
getPlayerStats,
|
|
552
|
+
getPlayerTopFinishes,
|
|
553
|
+
getPlayerWithResults,
|
|
554
|
+
getRatedPlayers,
|
|
555
|
+
getRecentTournaments,
|
|
556
|
+
getTopPlayersByRanking,
|
|
557
|
+
getTopPlayersByRating,
|
|
558
|
+
getTournamentResults,
|
|
559
|
+
getTournamentStats,
|
|
560
|
+
getTournamentWithResults,
|
|
561
|
+
getTournamentsByBoosterType,
|
|
562
|
+
getTournamentsByDateRange,
|
|
563
|
+
prisma,
|
|
564
|
+
recalculateTimeDecay,
|
|
565
|
+
searchPlayers,
|
|
566
|
+
searchTournaments,
|
|
567
|
+
testConnection,
|
|
568
|
+
updatePlayer,
|
|
569
|
+
updatePlayerRating,
|
|
570
|
+
updateResult,
|
|
571
|
+
updateResultPoints,
|
|
572
|
+
updateTournament
|
|
573
|
+
});
|