-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bet payout hook #7
Open
skoriop
wants to merge
10
commits into
main
Choose a base branch
from
add-bet-hook
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+226
−6
Open
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
12ea11f
feat: add cron hook to make bet payouts
skoriop 42b0f52
feat: add `standingsUpdated` column
skoriop 4469b7b
feat: convert cron hook to update hook
skoriop 91a5eb0
feat: add bet pool collection
skoriop e134f44
fix: update collection types
skoriop f9dc558
refactor: use bet pool collections
skoriop 9c1a60a
fix: add some bug fixes
skoriop 88e7d04
refactor: remove variable
skoriop c4ad04d
feat: add validation with zod
skoriop 9051288
feat: validate team and event ids
skoriop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
onRecordUpdate((e) => { | ||
const event = e.record; | ||
if (!event.getBool("standingsUpdated")) { | ||
return e.next(); | ||
} | ||
|
||
const winners = $app | ||
.findAllRecords( | ||
"standings", | ||
$dbx.exp("position == 1 AND event == {:id}", { id: event.id }) | ||
) | ||
.map((standing) => standing.get("team")); | ||
|
||
const bets = $app.findAllRecords( | ||
"bets", | ||
$dbx.exp("event == {:id}", { id: event.id }) | ||
); | ||
|
||
const wonBets = bets.filter((bet) => winners.includes(bet.get("team"))); | ||
|
||
const betPools = $app.findAllRecords( | ||
"betPool", | ||
$dbx.exp("event == {:id}", { id: event.id }) | ||
); | ||
|
||
let totalPool = 0; | ||
let wonPool = 0; | ||
|
||
// one passTM | ||
for (const betPool of betPools) { | ||
totalPool += betPool.getInt("amount"); | ||
if (winners.includes(betPool.get("team"))) | ||
wonPool += betPool.getInt("amount"); | ||
} | ||
|
||
if (wonPool === 0) { | ||
return e.next(); | ||
} | ||
|
||
for (const bet of wonBets) { | ||
const amount = bet.getInt("amount"); | ||
const payout = Math.floor((amount * totalPool) / wonPool); | ||
|
||
// not using the user expands on bets since it might have stale data (which means money could be disappearing) | ||
// does this make things safe from race conditions? i hope so | ||
const user = $app.findRecordById("users", bet.get("user")); | ||
user.set("balance", user.getInt("balance") + payout); | ||
$app.save(user); | ||
} | ||
|
||
e.next(); | ||
}, "events"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the
amount
is a string here the bets amount gets added as a string (amount isany
rn not anumber
)so consecutive bets of 10 lead
betPool.amount + amount
to become1010
also same issue in line 38 for
bet.amount + amount
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I've added validation middleware using
zod
.