gambling-bot-shared 0.1.54 → 1.0.1

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.
Files changed (166) hide show
  1. package/README.MD +4 -0
  2. package/dist/{mongoose/atmRequest.schema.d.ts → atm/atmRequest.mongoose.d.ts} +18 -28
  3. package/dist/atm/index.d.ts +3 -0
  4. package/dist/{types → atm}/index.js +5 -10
  5. package/dist/atm/schemas/atm.forms.d.ts +5 -0
  6. package/dist/atm/schemas/atm.forms.js +11 -0
  7. package/dist/blackjack/index.d.ts +3 -0
  8. package/dist/blackjack/index.js +21 -0
  9. package/dist/bonus/index.d.ts +7 -0
  10. package/dist/bonus/index.js +23 -0
  11. package/dist/bonus/schemas/bonus.forms.d.ts +16 -0
  12. package/dist/bonus/schemas/bonus.forms.js +39 -0
  13. package/dist/{types → bonus/types}/bonus.d.ts +1 -1
  14. package/dist/{utils → bonus/utils}/calculateBonusReward.d.ts +1 -1
  15. package/dist/{utils → bonus/utils}/generateBonusPreview.d.ts +1 -1
  16. package/dist/{utils → bonus/utils}/normalizeBonusSettings.d.ts +1 -1
  17. package/dist/{mongoose/blackjackGame.schema.d.ts → casino/blackjackGame.mongoose.d.ts} +19 -29
  18. package/dist/{constants → casino/constants}/index.d.ts +4 -9
  19. package/dist/{constants → casino/constants}/index.js +4 -9
  20. package/dist/{constants → casino/constants}/plinkoConfig.js +1 -1
  21. package/dist/casino/index.d.ts +12 -0
  22. package/dist/casino/index.js +30 -0
  23. package/dist/{schemas/forms.d.ts → casino/schemas/casino.forms.d.ts} +0 -103
  24. package/dist/casino/schemas/casino.forms.js +80 -0
  25. package/dist/casino/services/casinoBet.service.d.ts +41 -0
  26. package/dist/casino/services/casinoBet.service.js +146 -0
  27. package/dist/casino/types/casinoSettings.d.ts +2 -0
  28. package/dist/{utils → casino/utils}/calculateRTP.d.ts +1 -1
  29. package/dist/{utils → casino/utils}/calculateRTP.js +0 -2
  30. package/dist/{utils → casino/utils}/normalizeCasinoSettings.d.ts +1 -1
  31. package/dist/{utils → common}/formatters.d.ts +1 -1
  32. package/dist/{utils → common}/formatters.js +2 -2
  33. package/dist/common/index.d.ts +4 -0
  34. package/dist/{server.js → common/index.js} +4 -1
  35. package/dist/common/zod.d.ts +2 -0
  36. package/dist/common/zod.js +16 -0
  37. package/dist/{mongoose/guildConfiguration.schema.d.ts → guild/guildConfiguration.mongoose.d.ts} +16 -26
  38. package/dist/{mongoose/guildConfiguration.schema.js → guild/guildConfiguration.mongoose.js} +6 -5
  39. package/dist/guild/index.d.ts +8 -0
  40. package/dist/guild/index.js +26 -0
  41. package/dist/guild/schemas/guild.forms.d.ts +68 -0
  42. package/dist/guild/schemas/guild.forms.js +43 -0
  43. package/dist/{types → guild/types}/guildConfiguration.d.ts +2 -2
  44. package/dist/{utils → guild/utils}/normalizeGlobalSettings.d.ts +1 -1
  45. package/dist/guild/utils/normalizeGlobalSettings.js +46 -0
  46. package/dist/mongoose/index.d.ts +8 -8
  47. package/dist/mongoose/index.js +17 -22
  48. package/dist/predictions/index.d.ts +11 -0
  49. package/dist/predictions/index.js +29 -0
  50. package/dist/{mongoose/prediction.schema.d.ts → predictions/prediction.mongoose.d.ts} +14 -24
  51. package/dist/predictions/schemas/prediction.forms.d.ts +20 -0
  52. package/dist/predictions/schemas/prediction.forms.js +24 -0
  53. package/dist/predictions/services/normalizePredictionInput.d.ts +6 -0
  54. package/dist/predictions/services/normalizePredictionInput.js +53 -0
  55. package/dist/predictions/services/parsePredictionAutolock.d.ts +9 -0
  56. package/dist/predictions/services/parsePredictionAutolock.js +20 -0
  57. package/dist/predictions/services/parsePredictionChoices.d.ts +11 -0
  58. package/dist/predictions/services/parsePredictionChoices.js +26 -0
  59. package/dist/predictions/services/prediction.db.d.ts +55 -0
  60. package/dist/predictions/services/prediction.db.js +72 -0
  61. package/dist/predictions/services/predictionLifecycle.service.d.ts +25 -0
  62. package/dist/predictions/services/predictionLifecycle.service.js +133 -0
  63. package/dist/predictions/services/predictionSummary.d.ts +30 -0
  64. package/dist/predictions/services/predictionSummary.js +58 -0
  65. package/dist/predictions/services/types.d.ts +27 -0
  66. package/dist/raffle/index.d.ts +6 -0
  67. package/dist/raffle/index.js +24 -0
  68. package/dist/{mongoose/raffle.schema.d.ts → raffle/raffle.mongoose.d.ts} +19 -32
  69. package/dist/raffle/schemas/raffle.forms.d.ts +11 -0
  70. package/dist/raffle/schemas/raffle.forms.js +17 -0
  71. package/dist/raffle/services/raffle.db.d.ts +51 -0
  72. package/dist/raffle/services/raffle.db.js +153 -0
  73. package/dist/raffle/services/raffleLifecycle.service.d.ts +15 -0
  74. package/dist/raffle/services/raffleLifecycle.service.js +34 -0
  75. package/dist/raffle/services/types.d.ts +27 -0
  76. package/dist/{types → raffle/types}/raffle.d.ts +7 -5
  77. package/dist/transactions/constants/index.d.ts +2 -0
  78. package/dist/{schemas → transactions/constants}/index.js +2 -1
  79. package/dist/transactions/index.d.ts +4 -0
  80. package/dist/{index.js → transactions/index.js} +3 -2
  81. package/dist/{mongoose/transaction.schema.d.ts → transactions/transaction.mongoose.d.ts} +13 -23
  82. package/dist/{types → transactions/types}/transaction.d.ts +1 -1
  83. package/dist/transactions/types/transaction.js +2 -0
  84. package/dist/user/index.d.ts +2 -0
  85. package/dist/user/index.js +20 -0
  86. package/dist/user/types/user.js +2 -0
  87. package/dist/{mongoose/user.schema.d.ts → user/user.mongoose.d.ts} +12 -22
  88. package/dist/vip/index.d.ts +3 -0
  89. package/dist/vip/index.js +21 -0
  90. package/dist/vip/schemas/vip.forms.d.ts +10 -0
  91. package/dist/vip/schemas/vip.forms.js +16 -0
  92. package/dist/vip/types/vipRoom.js +2 -0
  93. package/dist/{mongoose/vipRoom.schema.d.ts → vip/vipRoom.mongoose.d.ts} +10 -20
  94. package/package.json +66 -26
  95. package/dist/index.d.ts +0 -3
  96. package/dist/schemas/forms.js +0 -173
  97. package/dist/schemas/index.d.ts +0 -1
  98. package/dist/server.d.ts +0 -1
  99. package/dist/types/index.d.ts +0 -11
  100. package/dist/utils/index.d.ts +0 -15
  101. package/dist/utils/index.js +0 -31
  102. package/dist/utils/normalizeGlobalSettings.js +0 -45
  103. /package/dist/{mongoose/atmRequest.schema.js → atm/atmRequest.mongoose.js} +0 -0
  104. /package/dist/{types → atm/types}/atmRequest.d.ts +0 -0
  105. /package/dist/{types → atm/types}/atmRequest.js +0 -0
  106. /package/dist/{constants → bonus/constants}/bonusLimits.d.ts +0 -0
  107. /package/dist/{constants → bonus/constants}/bonusLimits.js +0 -0
  108. /package/dist/{types → bonus/types}/bonus.js +0 -0
  109. /package/dist/{utils → bonus/utils}/bonusStreak.d.ts +0 -0
  110. /package/dist/{utils → bonus/utils}/bonusStreak.js +0 -0
  111. /package/dist/{utils → bonus/utils}/calculateBonusReward.js +0 -0
  112. /package/dist/{utils → bonus/utils}/generateBonusPreview.js +0 -0
  113. /package/dist/{utils → bonus/utils}/normalizeBonusSettings.js +0 -0
  114. /package/dist/{mongoose/blackjackGame.schema.js → casino/blackjackGame.mongoose.js} +0 -0
  115. /package/dist/{constants → casino/constants}/blackjack.d.ts +0 -0
  116. /package/dist/{constants → casino/constants}/blackjack.js +0 -0
  117. /package/dist/{constants → casino/constants}/casinoGames.d.ts +0 -0
  118. /package/dist/{constants → casino/constants}/casinoGames.js +0 -0
  119. /package/dist/{constants → casino/constants}/defaultConfig.d.ts +0 -0
  120. /package/dist/{constants → casino/constants}/defaultConfig.js +0 -0
  121. /package/dist/{constants → casino/constants}/lotteryConfig.d.ts +0 -0
  122. /package/dist/{constants → casino/constants}/lotteryConfig.js +0 -0
  123. /package/dist/{constants → casino/constants}/maxSimulations.d.ts +0 -0
  124. /package/dist/{constants → casino/constants}/maxSimulations.js +0 -0
  125. /package/dist/{constants → casino/constants}/plinkoConfig.d.ts +0 -0
  126. /package/dist/{constants → casino/constants}/rouletteConfig.d.ts +0 -0
  127. /package/dist/{constants → casino/constants}/rouletteConfig.js +0 -0
  128. /package/dist/{types → casino/types}/blackjackGame.d.ts +0 -0
  129. /package/dist/{types → casino/types}/blackjackGame.js +0 -0
  130. /package/dist/{types/guildConfiguration.js → casino/types/casinoSettings.js} +0 -0
  131. /package/dist/{utils → casino/utils}/normalizeCasinoSettings.js +0 -0
  132. /package/dist/{utils → casino/utils}/validateBetAmount.d.ts +0 -0
  133. /package/dist/{utils → casino/utils}/validateBetAmount.js +0 -0
  134. /package/dist/{utils → casino/utils}/winAnnouncements.d.ts +0 -0
  135. /package/dist/{utils → casino/utils}/winAnnouncements.js +0 -0
  136. /package/dist/{utils → common}/generateId.d.ts +0 -0
  137. /package/dist/{utils → common}/generateId.js +0 -0
  138. /package/dist/{utils → common}/parseTimeToSeconds.d.ts +0 -0
  139. /package/dist/{utils → common}/parseTimeToSeconds.js +0 -0
  140. /package/dist/{constants → guild/constants}/commonTimezones.d.ts +0 -0
  141. /package/dist/{constants → guild/constants}/commonTimezones.js +0 -0
  142. /package/dist/{constants → guild/constants}/defaultGlobalSettings.d.ts +0 -0
  143. /package/dist/{constants → guild/constants}/defaultGlobalSettings.js +0 -0
  144. /package/dist/{types/prediction.js → guild/types/guildConfiguration.js} +0 -0
  145. /package/dist/{utils → guild/utils}/globalSettings.d.ts +0 -0
  146. /package/dist/{utils → guild/utils}/globalSettings.js +0 -0
  147. /package/dist/{utils → guild/utils}/guildTimezone.d.ts +0 -0
  148. /package/dist/{utils → guild/utils}/guildTimezone.js +0 -0
  149. /package/dist/{mongoose/prediction.schema.js → predictions/prediction.mongoose.js} +0 -0
  150. /package/dist/{types/raffle.js → predictions/services/types.js} +0 -0
  151. /package/dist/{types → predictions/types}/prediction.d.ts +0 -0
  152. /package/dist/{types/transaction.js → predictions/types/prediction.js} +0 -0
  153. /package/dist/{utils → predictions/utils}/validatePredictionBet.d.ts +0 -0
  154. /package/dist/{utils → predictions/utils}/validatePredictionBet.js +0 -0
  155. /package/dist/{mongoose/raffle.schema.js → raffle/raffle.mongoose.js} +0 -0
  156. /package/dist/{types/user.js → raffle/services/types.js} +0 -0
  157. /package/dist/{types/vipRoom.js → raffle/types/raffle.js} +0 -0
  158. /package/dist/{constants → transactions/constants}/gameRecordFields.d.ts +0 -0
  159. /package/dist/{constants → transactions/constants}/gameRecordFields.js +0 -0
  160. /package/dist/{constants → transactions/constants}/transaction.d.ts +0 -0
  161. /package/dist/{constants → transactions/constants}/transaction.js +0 -0
  162. /package/dist/{mongoose/transaction.schema.js → transactions/transaction.mongoose.js} +0 -0
  163. /package/dist/{types → user/types}/user.d.ts +0 -0
  164. /package/dist/{mongoose/user.schema.js → user/user.mongoose.js} +0 -0
  165. /package/dist/{types → vip/types}/vipRoom.d.ts +0 -0
  166. /package/dist/{mongoose/vipRoom.schema.js → vip/vipRoom.mongoose.js} +0 -0
@@ -0,0 +1,55 @@
1
+ import type { Model } from 'mongoose';
2
+ import type { TPrediction } from '../types/prediction';
3
+ import type { TAddPredictionBet, TCreatePrediction, TGetOldPredictions, TGetPrediction, TUpdatePredictionStatus } from './types';
4
+ export declare function createPredictionDb(predictionModel: Model<TPrediction>): {
5
+ getPredictionById: ({ predictionId, guildId }: TGetPrediction) => Promise<(import("mongoose").Document<unknown, {}, TPrediction, {}, import("mongoose").DefaultSchemaOptions> & TPrediction & {
6
+ _id: import("mongoose").Types.ObjectId;
7
+ } & {
8
+ __v: number;
9
+ } & {
10
+ id: string;
11
+ }) | null>;
12
+ getPredictionToLock: ({ status, useAutolock }: {
13
+ status?: TPrediction["status"];
14
+ useAutolock?: boolean;
15
+ }) => Promise<(import("mongoose").Document<unknown, {}, TPrediction, {}, import("mongoose").DefaultSchemaOptions> & TPrediction & {
16
+ _id: import("mongoose").Types.ObjectId;
17
+ } & {
18
+ __v: number;
19
+ } & {
20
+ id: string;
21
+ })[]>;
22
+ getOldPredictions: ({ statuses, olderThanDays }: TGetOldPredictions) => Promise<(import("mongoose").Document<unknown, {}, TPrediction, {}, import("mongoose").DefaultSchemaOptions> & TPrediction & {
23
+ _id: import("mongoose").Types.ObjectId;
24
+ } & {
25
+ __v: number;
26
+ } & {
27
+ id: string;
28
+ })[]>;
29
+ createPrediction: ({ predictionId, guildId, channelId, creatorId, title, choices, autolock, status }: TCreatePrediction) => Promise<void>;
30
+ updatePredictionStatus: ({ predictionId, guildId, fromStatus, toStatus }: TUpdatePredictionStatus) => Promise<(import("mongoose").Document<unknown, {}, TPrediction, {}, import("mongoose").DefaultSchemaOptions> & TPrediction & {
31
+ _id: import("mongoose").Types.ObjectId;
32
+ } & {
33
+ __v: number;
34
+ } & {
35
+ id: string;
36
+ }) | null>;
37
+ deletePrediction: ({ predictionId }: {
38
+ predictionId: string;
39
+ }) => Promise<void>;
40
+ findPredictions: (query: Record<string, unknown>) => Promise<(import("mongoose").Document<unknown, {}, TPrediction, {}, import("mongoose").DefaultSchemaOptions> & TPrediction & {
41
+ _id: import("mongoose").Types.ObjectId;
42
+ } & {
43
+ __v: number;
44
+ } & {
45
+ id: string;
46
+ })[]>;
47
+ addPredictionBet: ({ predictionId, guildId, choiceName, userId, amount, betId }: TAddPredictionBet) => Promise<(import("mongoose").Document<unknown, {}, TPrediction, {}, import("mongoose").DefaultSchemaOptions> & TPrediction & {
48
+ _id: import("mongoose").Types.ObjectId;
49
+ } & {
50
+ __v: number;
51
+ } & {
52
+ id: string;
53
+ }) | null>;
54
+ };
55
+ export type PredictionDb = ReturnType<typeof createPredictionDb>;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createPredictionDb = createPredictionDb;
4
+ function createPredictionDb(predictionModel) {
5
+ const getPredictionById = async ({ predictionId, guildId }) => {
6
+ return predictionModel.findOne({ predictionId, guildId });
7
+ };
8
+ const getPredictionToLock = async ({ status = 'active', useAutolock = true }) => {
9
+ const now = new Date();
10
+ return predictionModel.find({
11
+ status,
12
+ ...(useAutolock ? { autolock: { $lte: now } } : {})
13
+ });
14
+ };
15
+ const getOldPredictions = async ({ statuses, olderThanDays }) => {
16
+ const cutoffDate = new Date(Date.now() - olderThanDays * 24 * 60 * 60 * 1000);
17
+ return predictionModel.find({
18
+ status: { $in: statuses },
19
+ updatedAt: { $lte: cutoffDate }
20
+ });
21
+ };
22
+ const createPrediction = async ({ predictionId, guildId, channelId, creatorId, title, choices, autolock, status }) => {
23
+ await predictionModel.create({
24
+ predictionId,
25
+ guildId,
26
+ channelId,
27
+ creatorId,
28
+ title,
29
+ choices,
30
+ autolock,
31
+ status
32
+ });
33
+ };
34
+ const updatePredictionStatus = async ({ predictionId, guildId, fromStatus, toStatus }) => {
35
+ return predictionModel.findOneAndUpdate({
36
+ predictionId,
37
+ guildId,
38
+ status: Array.isArray(fromStatus) ? { $in: fromStatus } : fromStatus
39
+ }, { $set: { status: toStatus } }, { returnDocument: 'after' });
40
+ };
41
+ const deletePrediction = async ({ predictionId }) => {
42
+ await predictionModel.deleteOne({ predictionId });
43
+ };
44
+ const findPredictions = async (query) => {
45
+ return predictionModel.find(query).limit(25);
46
+ };
47
+ const addPredictionBet = async ({ predictionId, guildId, choiceName, userId, amount, betId }) => {
48
+ return predictionModel.findOneAndUpdate({
49
+ predictionId,
50
+ guildId,
51
+ 'choices.choiceName': choiceName
52
+ }, {
53
+ $push: {
54
+ 'choices.$.bets': {
55
+ userId,
56
+ amount,
57
+ betId
58
+ }
59
+ }
60
+ });
61
+ };
62
+ return {
63
+ getPredictionById,
64
+ getPredictionToLock,
65
+ getOldPredictions,
66
+ createPrediction,
67
+ updatePredictionStatus,
68
+ deletePrediction,
69
+ findPredictions,
70
+ addPredictionBet
71
+ };
72
+ }
@@ -0,0 +1,25 @@
1
+ import type { CasinoBetService } from '../../casino/services/casinoBet.service';
2
+ import type { TPrediction } from '../types/prediction';
3
+ import type { PredictionDb } from './prediction.db';
4
+ import type { PayPredictionResult } from './types';
5
+ type PredictionLifecycleDeps = {
6
+ predictionDb: PredictionDb;
7
+ casinoBet: Pick<CasinoBetService, 'refundLockedBet' | 'settleCasinoWinnings'>;
8
+ };
9
+ export declare function createPredictionLifecycleService({ predictionDb, casinoBet }: PredictionLifecycleDeps): {
10
+ endPrediction: ({ predictionId, guildId }: {
11
+ predictionId: string;
12
+ guildId: string;
13
+ }) => Promise<TPrediction | null>;
14
+ cancelPrediction: ({ predictionId, guildId }: {
15
+ predictionId: string;
16
+ guildId: string;
17
+ }) => Promise<TPrediction | null>;
18
+ payoutPrediction: ({ predictionId, guildId, winnerChoice }: {
19
+ predictionId: string;
20
+ guildId: string;
21
+ winnerChoice: string;
22
+ }) => Promise<PayPredictionResult>;
23
+ };
24
+ export type PredictionLifecycleService = ReturnType<typeof createPredictionLifecycleService>;
25
+ export {};
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createPredictionLifecycleService = createPredictionLifecycleService;
4
+ const predictionSummary_1 = require("./predictionSummary");
5
+ function createPredictionLifecycleService({ predictionDb, casinoBet }) {
6
+ const { getPredictionById, updatePredictionStatus } = predictionDb;
7
+ const { refundLockedBet, settleCasinoWinnings } = casinoBet;
8
+ const finalizePaid = async (predictionId, guildId) => {
9
+ const paid = await updatePredictionStatus({
10
+ predictionId,
11
+ guildId,
12
+ fromStatus: 'paying',
13
+ toStatus: 'paid'
14
+ });
15
+ if (!paid)
16
+ throw new Error('FINALIZE_FAILED');
17
+ return paid;
18
+ };
19
+ const rollbackPaying = async (predictionId, guildId) => {
20
+ await updatePredictionStatus({
21
+ predictionId,
22
+ guildId,
23
+ fromStatus: 'paying',
24
+ toStatus: 'ended'
25
+ });
26
+ };
27
+ const endPrediction = async ({ predictionId, guildId }) => {
28
+ return updatePredictionStatus({
29
+ predictionId,
30
+ guildId,
31
+ fromStatus: 'active',
32
+ toStatus: 'ended'
33
+ });
34
+ };
35
+ const cancelPrediction = async ({ predictionId, guildId }) => {
36
+ const updatedPrediction = await updatePredictionStatus({
37
+ predictionId,
38
+ guildId,
39
+ fromStatus: ['active', 'ended'],
40
+ toStatus: 'canceled'
41
+ });
42
+ if (!updatedPrediction)
43
+ return null;
44
+ const allBets = updatedPrediction.choices.flatMap((c) => c.bets);
45
+ for (const bet of allBets) {
46
+ await refundLockedBet({
47
+ userId: bet.userId,
48
+ guildId,
49
+ amount: bet.amount,
50
+ betId: (0, predictionSummary_1.resolvePredictionBetId)(bet, predictionId),
51
+ game: 'prediction'
52
+ });
53
+ }
54
+ return updatedPrediction;
55
+ };
56
+ const payoutPrediction = async ({ predictionId, guildId, winnerChoice }) => {
57
+ const prediction = await getPredictionById({ predictionId, guildId });
58
+ if (!prediction)
59
+ return { ok: false, code: 'NOT_FOUND' };
60
+ if (prediction.status !== 'ended') {
61
+ if (prediction.status === 'paid' || prediction.status === 'paying') {
62
+ return { ok: false, code: 'ALREADY_HANDLED' };
63
+ }
64
+ return { ok: false, code: 'INVALID_STATUS' };
65
+ }
66
+ const winner = prediction.choices.find((c) => c.choiceName === winnerChoice);
67
+ if (!winner)
68
+ return { ok: false, code: 'INVALID_WINNER' };
69
+ const locked = await updatePredictionStatus({
70
+ predictionId,
71
+ guildId,
72
+ fromStatus: 'ended',
73
+ toStatus: 'paying'
74
+ });
75
+ if (!locked)
76
+ return { ok: false, code: 'ALREADY_HANDLED' };
77
+ try {
78
+ const allBets = prediction.choices.flatMap((c) => c.bets);
79
+ if (winner.bets.length === 0) {
80
+ for (const bet of allBets) {
81
+ await refundLockedBet({
82
+ userId: bet.userId,
83
+ guildId,
84
+ amount: bet.amount,
85
+ betId: (0, predictionSummary_1.resolvePredictionBetId)(bet, predictionId),
86
+ game: 'prediction'
87
+ });
88
+ }
89
+ const paid = await finalizePaid(predictionId, guildId);
90
+ return { ok: true, outcome: 'refunded', prediction: paid };
91
+ }
92
+ for (const bet of winner.bets) {
93
+ await settleCasinoWinnings({
94
+ userId: bet.userId,
95
+ guildId,
96
+ totalBet: bet.amount,
97
+ winnings: bet.amount * winner.odds,
98
+ betId: (0, predictionSummary_1.resolvePredictionBetId)(bet, predictionId),
99
+ game: 'prediction'
100
+ });
101
+ }
102
+ const losingChoices = prediction.choices.filter((c) => c.choiceName !== winnerChoice);
103
+ for (const choice of losingChoices) {
104
+ for (const bet of choice.bets) {
105
+ await settleCasinoWinnings({
106
+ userId: bet.userId,
107
+ guildId,
108
+ totalBet: bet.amount,
109
+ winnings: 0,
110
+ betId: (0, predictionSummary_1.resolvePredictionBetId)(bet, predictionId),
111
+ game: 'prediction'
112
+ });
113
+ }
114
+ }
115
+ const paid = await finalizePaid(predictionId, guildId);
116
+ return {
117
+ ok: true,
118
+ outcome: 'paid',
119
+ prediction: paid,
120
+ winnerChoice
121
+ };
122
+ }
123
+ catch (error) {
124
+ await rollbackPaying(predictionId, guildId);
125
+ throw error;
126
+ }
127
+ };
128
+ return {
129
+ endPrediction,
130
+ cancelPrediction,
131
+ payoutPrediction
132
+ };
133
+ }
@@ -0,0 +1,30 @@
1
+ import type { TPrediction } from '../types/prediction';
2
+ export declare function resolvePredictionBetId(bet: TPrediction['choices'][number]['bets'][number], predictionId: string): string;
3
+ export type PredictionChoiceSummary = {
4
+ choiceName: string;
5
+ odds: number;
6
+ betCount: number;
7
+ totalBetAmount: number;
8
+ payoutIfWins: number;
9
+ bets: TPrediction['choices'][number]['bets'];
10
+ };
11
+ export type PredictionCheckSummary = {
12
+ totalBetAmount: number;
13
+ bettorCount: number;
14
+ choices: PredictionChoiceSummary[];
15
+ };
16
+ export declare function getPredictionCheckSummary(prediction: TPrediction): PredictionCheckSummary;
17
+ export type PayoutBetSummary = {
18
+ userId: string;
19
+ betAmount: number;
20
+ winAmount: number;
21
+ };
22
+ export type PredictionPayoutSummary = {
23
+ participants: number;
24
+ winners: PayoutBetSummary[];
25
+ losers: PayoutBetSummary[];
26
+ totalWon: number;
27
+ totalLost: number;
28
+ casinoProfit: number;
29
+ };
30
+ export declare function calculatePredictionPayoutSummary(prediction: TPrediction, winnerChoice: string): PredictionPayoutSummary | null;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolvePredictionBetId = resolvePredictionBetId;
4
+ exports.getPredictionCheckSummary = getPredictionCheckSummary;
5
+ exports.calculatePredictionPayoutSummary = calculatePredictionPayoutSummary;
6
+ function resolvePredictionBetId(bet, predictionId) {
7
+ return bet.betId ?? `${predictionId}:${bet.userId}`;
8
+ }
9
+ function getPredictionCheckSummary(prediction) {
10
+ const allBets = prediction.choices.flatMap((c) => c.bets);
11
+ const bettorCount = new Set(allBets.map((b) => b.userId)).size;
12
+ const totalBetAmount = allBets.reduce((acc, b) => acc + b.amount, 0);
13
+ const choices = prediction.choices.map((choice) => {
14
+ const totalBet = choice.bets.reduce((acc, b) => acc + b.amount, 0);
15
+ const payoutIfWins = choice.bets.reduce((acc, b) => acc + b.amount * choice.odds, 0);
16
+ return {
17
+ choiceName: choice.choiceName,
18
+ odds: choice.odds,
19
+ betCount: choice.bets.length,
20
+ totalBetAmount: totalBet,
21
+ payoutIfWins,
22
+ bets: choice.bets
23
+ };
24
+ });
25
+ return {
26
+ totalBetAmount,
27
+ bettorCount,
28
+ choices
29
+ };
30
+ }
31
+ function calculatePredictionPayoutSummary(prediction, winnerChoice) {
32
+ const winner = prediction.choices.find((c) => c.choiceName === winnerChoice);
33
+ if (!winner)
34
+ return null;
35
+ const totalBets = prediction.choices.flatMap((c) => c.bets);
36
+ const winners = winner.bets.map((b) => ({
37
+ userId: b.userId,
38
+ betAmount: b.amount,
39
+ winAmount: b.amount * winner.odds
40
+ }));
41
+ const losers = prediction.choices
42
+ .filter((c) => c.choiceName !== winnerChoice)
43
+ .flatMap((c) => c.bets.map((b) => ({
44
+ userId: b.userId,
45
+ betAmount: b.amount,
46
+ winAmount: 0
47
+ })));
48
+ const totalWon = winners.reduce((acc, w) => acc + w.winAmount, 0);
49
+ const totalLost = losers.reduce((acc, l) => acc + l.betAmount, 0);
50
+ return {
51
+ participants: totalBets.length,
52
+ winners,
53
+ losers,
54
+ totalWon,
55
+ totalLost,
56
+ casinoProfit: totalLost - totalWon
57
+ };
58
+ }
@@ -0,0 +1,27 @@
1
+ import type { TPrediction, TPredictionOption } from '../types/prediction';
2
+ export type TCreatePrediction = Omit<TPrediction, 'createdAt' | 'updatedAt'>;
3
+ export type TGetPrediction = Pick<TPrediction, 'predictionId' | 'guildId'>;
4
+ export type TGetOldPredictions = {
5
+ statuses: TPrediction['status'][];
6
+ olderThanDays: number;
7
+ };
8
+ export type TUpdatePredictionStatus = Pick<TPrediction, 'predictionId' | 'guildId'> & {
9
+ fromStatus: TPrediction['status'] | TPrediction['status'][];
10
+ toStatus: TPrediction['status'];
11
+ };
12
+ export type TAddPredictionBet = Pick<TPrediction, 'predictionId' | 'guildId'> & {
13
+ choiceName: TPredictionOption['choiceName'];
14
+ userId: TPredictionOption['bets'][number]['userId'];
15
+ amount: TPredictionOption['bets'][number]['amount'];
16
+ betId: TPredictionOption['bets'][number]['betId'];
17
+ };
18
+ export type PayPredictionResult = {
19
+ ok: true;
20
+ outcome: 'paid' | 'refunded';
21
+ prediction: TPrediction;
22
+ winnerChoice?: string;
23
+ } | {
24
+ ok: false;
25
+ code: 'NOT_FOUND' | 'INVALID_STATUS' | 'ALREADY_HANDLED' | 'INVALID_WINNER';
26
+ };
27
+ export type PredictionLifecycleErrorCode = 'NOT_FOUND' | 'INVALID_STATUS' | 'ALREADY_HANDLED';
@@ -0,0 +1,6 @@
1
+ export * from './types/raffle';
2
+ export * from './services/types';
3
+ export * from './services/raffle.db';
4
+ export * from './services/raffleLifecycle.service';
5
+ export * from './schemas/raffle.forms';
6
+ export { RaffleSchema } from './raffle.mongoose';
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.RaffleSchema = void 0;
18
+ __exportStar(require("./types/raffle"), exports);
19
+ __exportStar(require("./services/types"), exports);
20
+ __exportStar(require("./services/raffle.db"), exports);
21
+ __exportStar(require("./services/raffleLifecycle.service"), exports);
22
+ __exportStar(require("./schemas/raffle.forms"), exports);
23
+ var raffle_mongoose_1 = require("./raffle.mongoose");
24
+ Object.defineProperty(exports, "RaffleSchema", { enumerable: true, get: function () { return raffle_mongoose_1.RaffleSchema; } });
@@ -1,18 +1,8 @@
1
1
  import { Schema } from 'mongoose';
2
- import { TRaffle } from '../types';
3
- export declare const RaffleSchema: Schema<TRaffle, import("mongoose").Model<TRaffle, any, any, any, (import("mongoose").Document<unknown, any, TRaffle, any, import("mongoose").DefaultSchemaOptions> & TRaffle & {
4
- _id: import("mongoose").Types.ObjectId;
5
- } & {
6
- __v: number;
7
- } & {
8
- id: string;
9
- }) | (import("mongoose").Document<unknown, any, TRaffle, any, import("mongoose").DefaultSchemaOptions> & TRaffle & {
10
- _id: import("mongoose").Types.ObjectId;
11
- } & {
12
- __v: number;
13
- }), any, TRaffle>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
2
+ import { TRaffle } from './types/raffle';
3
+ export declare const RaffleSchema: Schema<TRaffle, import("mongoose").Model<TRaffle, any, any, any, any, any, TRaffle>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
14
4
  id: string;
15
- }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<TRaffle & {
5
+ }, import("mongoose").DefaultSchemaOptions> & Omit<TRaffle & {
16
6
  _id: import("mongoose").Types.ObjectId;
17
7
  } & {
18
8
  __v: number;
@@ -21,7 +11,7 @@ export declare const RaffleSchema: Schema<TRaffle, import("mongoose").Model<TRaf
21
11
  }, {
22
12
  drawId?: import("mongoose").SchemaDefinitionProperty<string, TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
23
13
  id: string;
24
- }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<TRaffle & {
14
+ }, import("mongoose").DefaultSchemaOptions> & Omit<TRaffle & {
25
15
  _id: import("mongoose").Types.ObjectId;
26
16
  } & {
27
17
  __v: number;
@@ -30,7 +20,7 @@ export declare const RaffleSchema: Schema<TRaffle, import("mongoose").Model<TRaf
30
20
  }> | undefined;
31
21
  raffleId?: import("mongoose").SchemaDefinitionProperty<string, TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
32
22
  id: string;
33
- }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<TRaffle & {
23
+ }, import("mongoose").DefaultSchemaOptions> & Omit<TRaffle & {
34
24
  _id: import("mongoose").Types.ObjectId;
35
25
  } & {
36
26
  __v: number;
@@ -39,7 +29,7 @@ export declare const RaffleSchema: Schema<TRaffle, import("mongoose").Model<TRaf
39
29
  }> | undefined;
40
30
  guildId?: import("mongoose").SchemaDefinitionProperty<string, TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
41
31
  id: string;
42
- }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<TRaffle & {
32
+ }, import("mongoose").DefaultSchemaOptions> & Omit<TRaffle & {
43
33
  _id: import("mongoose").Types.ObjectId;
44
34
  } & {
45
35
  __v: number;
@@ -48,7 +38,7 @@ export declare const RaffleSchema: Schema<TRaffle, import("mongoose").Model<TRaf
48
38
  }> | undefined;
49
39
  channelId?: import("mongoose").SchemaDefinitionProperty<string, TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
50
40
  id: string;
51
- }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<TRaffle & {
41
+ }, import("mongoose").DefaultSchemaOptions> & Omit<TRaffle & {
52
42
  _id: import("mongoose").Types.ObjectId;
53
43
  } & {
54
44
  __v: number;
@@ -57,7 +47,7 @@ export declare const RaffleSchema: Schema<TRaffle, import("mongoose").Model<TRaf
57
47
  }> | undefined;
58
48
  creatorId?: import("mongoose").SchemaDefinitionProperty<string, TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
59
49
  id: string;
60
- }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<TRaffle & {
50
+ }, import("mongoose").DefaultSchemaOptions> & Omit<TRaffle & {
61
51
  _id: import("mongoose").Types.ObjectId;
62
52
  } & {
63
53
  __v: number;
@@ -66,7 +56,7 @@ export declare const RaffleSchema: Schema<TRaffle, import("mongoose").Model<TRaf
66
56
  }> | undefined;
67
57
  ticketPrice?: import("mongoose").SchemaDefinitionProperty<number, TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
68
58
  id: string;
69
- }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<TRaffle & {
59
+ }, import("mongoose").DefaultSchemaOptions> & Omit<TRaffle & {
70
60
  _id: import("mongoose").Types.ObjectId;
71
61
  } & {
72
62
  __v: number;
@@ -75,7 +65,7 @@ export declare const RaffleSchema: Schema<TRaffle, import("mongoose").Model<TRaf
75
65
  }> | undefined;
76
66
  maxTicketsPerUser?: import("mongoose").SchemaDefinitionProperty<number, TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
77
67
  id: string;
78
- }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<TRaffle & {
68
+ }, import("mongoose").DefaultSchemaOptions> & Omit<TRaffle & {
79
69
  _id: import("mongoose").Types.ObjectId;
80
70
  } & {
81
71
  __v: number;
@@ -84,7 +74,7 @@ export declare const RaffleSchema: Schema<TRaffle, import("mongoose").Model<TRaf
84
74
  }> | undefined;
85
75
  nextDrawAt?: import("mongoose").SchemaDefinitionProperty<Date, TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
86
76
  id: string;
87
- }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<TRaffle & {
77
+ }, import("mongoose").DefaultSchemaOptions> & Omit<TRaffle & {
88
78
  _id: import("mongoose").Types.ObjectId;
89
79
  } & {
90
80
  __v: number;
@@ -93,7 +83,7 @@ export declare const RaffleSchema: Schema<TRaffle, import("mongoose").Model<TRaf
93
83
  }> | undefined;
94
84
  lastDrawAt?: import("mongoose").SchemaDefinitionProperty<Date | undefined, TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
95
85
  id: string;
96
- }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<TRaffle & {
86
+ }, import("mongoose").DefaultSchemaOptions> & Omit<TRaffle & {
97
87
  _id: import("mongoose").Types.ObjectId;
98
88
  } & {
99
89
  __v: number;
@@ -102,28 +92,25 @@ export declare const RaffleSchema: Schema<TRaffle, import("mongoose").Model<TRaf
102
92
  }> | undefined;
103
93
  drawIntervalMs?: import("mongoose").SchemaDefinitionProperty<number, TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
104
94
  id: string;
105
- }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<TRaffle & {
95
+ }, import("mongoose").DefaultSchemaOptions> & Omit<TRaffle & {
106
96
  _id: import("mongoose").Types.ObjectId;
107
97
  } & {
108
98
  __v: number;
109
99
  }, "id"> & {
110
100
  id: string;
111
101
  }> | undefined;
112
- status?: import("mongoose").SchemaDefinitionProperty<"active" | "canceled", TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
102
+ status?: import("mongoose").SchemaDefinitionProperty<import("./types/raffle").TRaffleStatus, TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
113
103
  id: string;
114
- }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<TRaffle & {
104
+ }, import("mongoose").DefaultSchemaOptions> & Omit<TRaffle & {
115
105
  _id: import("mongoose").Types.ObjectId;
116
106
  } & {
117
107
  __v: number;
118
108
  }, "id"> & {
119
109
  id: string;
120
110
  }> | undefined;
121
- participants?: import("mongoose").SchemaDefinitionProperty<{
122
- userId: string;
123
- tickets: number;
124
- }[], TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
111
+ participants?: import("mongoose").SchemaDefinitionProperty<import("./types/raffle").TRaffleParticipant[], TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
125
112
  id: string;
126
- }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<TRaffle & {
113
+ }, import("mongoose").DefaultSchemaOptions> & Omit<TRaffle & {
127
114
  _id: import("mongoose").Types.ObjectId;
128
115
  } & {
129
116
  __v: number;
@@ -132,7 +119,7 @@ export declare const RaffleSchema: Schema<TRaffle, import("mongoose").Model<TRaf
132
119
  }> | undefined;
133
120
  createdAt?: import("mongoose").SchemaDefinitionProperty<Date, TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
134
121
  id: string;
135
- }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<TRaffle & {
122
+ }, import("mongoose").DefaultSchemaOptions> & Omit<TRaffle & {
136
123
  _id: import("mongoose").Types.ObjectId;
137
124
  } & {
138
125
  __v: number;
@@ -141,7 +128,7 @@ export declare const RaffleSchema: Schema<TRaffle, import("mongoose").Model<TRaf
141
128
  }> | undefined;
142
129
  updatedAt?: import("mongoose").SchemaDefinitionProperty<Date, TRaffle, import("mongoose").Document<unknown, {}, TRaffle, {
143
130
  id: string;
144
- }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<TRaffle & {
131
+ }, import("mongoose").DefaultSchemaOptions> & Omit<TRaffle & {
145
132
  _id: import("mongoose").Types.ObjectId;
146
133
  } & {
147
134
  __v: number;
@@ -0,0 +1,11 @@
1
+ import z from 'zod';
2
+ export declare const raffleChannelsFormSchema: z.ZodObject<{
3
+ actions: z.ZodString;
4
+ logs: z.ZodString;
5
+ }, z.core.$strip>;
6
+ export declare const raffleCreateFormSchema: z.ZodObject<{
7
+ ticketPrice: z.ZodString;
8
+ maxTickets: z.ZodNumber;
9
+ drawTime: z.ZodString;
10
+ interval: z.ZodString;
11
+ }, z.core.$strip>;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.raffleCreateFormSchema = exports.raffleChannelsFormSchema = void 0;
7
+ const zod_1 = __importDefault(require("zod"));
8
+ exports.raffleChannelsFormSchema = zod_1.default.object({
9
+ actions: zod_1.default.string(),
10
+ logs: zod_1.default.string()
11
+ });
12
+ exports.raffleCreateFormSchema = zod_1.default.object({
13
+ ticketPrice: zod_1.default.string().min(1),
14
+ maxTickets: zod_1.default.number().int().min(1).max(100),
15
+ drawTime: zod_1.default.string().min(1),
16
+ interval: zod_1.default.string().min(2)
17
+ });