Created
March 19, 2021 15:28
-
-
Save HonbraDev/a8250b355bec72e09b65a37bbb9633d9 to your computer and use it in GitHub Desktop.
CoinFlip
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
import { CommandInput } from "./CommandInput"; | |
import { getUser, setMonies } from "../totallyARealDB"; | |
import { addMessageToQueue } from "../queue"; | |
export async function coinflip({ wrapper, msg, userId }: CommandInput) { | |
if (msg.tokens[1]) { | |
const user = getUser(userId); | |
const amount = parseInt(msg.tokens[1].v as string, 10); | |
if (typeof amount === "number") { | |
if (amount > 0) { | |
if (user.monies >= amount) { | |
const flipResult = Math.random() >= 0.5; | |
if (flipResult) { | |
setMonies(userId, user.monies + amount); | |
const newUser = getUser(userId); | |
addMessageToQueue([ | |
{ | |
t: "text", | |
v: `${msg.displayName} has won the coin flip. Much wow. They now have ${newUser.monies} DodgyCoin.`, | |
}, | |
]); | |
} else { | |
setMonies(userId, user.monies - amount); | |
const newUser = getUser(userId); | |
addMessageToQueue([ | |
{ | |
t: "text", | |
v: `${msg.displayName} has lost the coin flip. Much sad. They now have ${newUser.monies} DodgyCoin.`, | |
}, | |
]); | |
} | |
} else { | |
addMessageToQueue([ | |
{ | |
t: "text", | |
v: "You do not have enough DodgyCoin for that.", | |
}, | |
]); | |
} | |
} else { | |
addMessageToQueue([ | |
{ | |
t: "text", | |
v: "Nice try forker.", | |
}, | |
]); | |
} | |
} else { | |
addMessageToQueue([ | |
{ | |
t: "text", | |
v: "Invalid command syntax.", | |
}, | |
]); | |
} | |
} else { | |
addMessageToQueue([ | |
{ | |
t: "text", | |
v: "Invalid command syntax.", | |
}, | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment