2022-01-16 23:18:56 +05:00
const Command = require ( "../../base/Command" ) ;
const games = { } ;
class Horserace extends Command {
constructor ( client ) {
super ( client , {
name : "horserace" ,
dirname : _ _dirname ,
2022-01-19 18:38:53 +05:00
enabled : true ,
2022-01-19 18:37:54 +05:00
guildOnly : true ,
2022-01-16 23:18:56 +05:00
aliases : [ "hr" ] ,
memberPermissions : [ ] ,
botPermissions : [ "SEND_MESSAGES" , "EMBED_LINKS" ] ,
nsfw : false ,
ownerOnly : false ,
cooldown : 2000
} ) ;
}
async run ( message , args , data ) {
2022-02-03 13:40:00 +05:00
if ( ! args [ 0 ] ) return message . error ( "economy/horserace:MISSING_STATUS" ) ;
2022-01-16 23:18:56 +05:00
const author = message . author ;
if ( args [ 0 ] === "create" ) {
let thisGame = games [ message . channel . id ] ;
2022-01-19 19:42:49 +05:00
2022-02-03 13:40:00 +05:00
if ( thisGame ) return message . error ( "economy/horserace:GAME_RUNNING" ) ;
2022-01-16 23:18:56 +05:00
else {
games [ message . channel . id ] = {
horseSpeeds : [ ] ,
bets : [ ]
} ;
thisGame = games [ message . channel . id ] ;
const f = [ ] ;
for ( let i = 0 ; i < 5 ; i ++ ) {
2022-01-19 20:57:44 +05:00
const speed = this . client . functions . randomNum ( 1 , 5 ) ;
2022-01-20 18:27:39 +05:00
const profit = {
1 : 3.5 ,
2 : 3 ,
3 : 2.5 ,
4 : 2 ,
5 : 1.5 ,
} ;
// const profit = Math.floor((((8.9 / 9) * (6 - speed)) + 1.1) * 10) / 10;
2022-01-16 23:18:56 +05:00
thisGame . horseSpeeds . push ( speed ) ;
f . push ( {
2022-02-03 13:40:00 +05:00
name : message . translate ( "economy/horserace:HORSE_NAME" , {
2022-01-16 23:18:56 +05:00
number : i + 1
} ) ,
2022-02-03 13:40:00 +05:00
value : message . translate ( "economy/horserace:HORSE_VALUE" , {
2022-01-16 23:18:56 +05:00
speed ,
2022-01-20 18:27:39 +05:00
profit : profit [ speed ]
2022-01-16 23:18:56 +05:00
} )
} ) ;
}
2022-02-13 17:26:12 +05:00
message . reply ( {
2022-01-16 23:18:56 +05:00
embeds : [ {
color : data . config . embed . color ,
2022-02-03 13:40:00 +05:00
title : message . translate ( "economy/horserace:EMBED_T" ) ,
2022-01-16 23:18:56 +05:00
fields : f
} ]
} ) ;
}
} else if ( args [ 0 ] === "bet" ) {
2022-01-19 19:42:49 +05:00
const thisGame = games [ message . channel . id ] ;
2022-01-19 18:37:54 +05:00
const horse = parseInt ( args [ 1 ] ) ;
const amount = parseInt ( args [ 2 ] ) ;
2022-01-16 23:18:56 +05:00
2022-02-03 13:40:00 +05:00
if ( horse > 5 ) return message . error ( "economy/horserace:HORSE_NUM" ) ;
if ( ! thisGame ) return message . error ( "economy/horserace:NO_GAME_RUNNING" ) ;
2022-01-19 19:42:49 +05:00
2022-01-16 23:18:56 +05:00
if ( ! amount || isNaN ( amount ) || parseInt ( amount , 10 ) <= 0 ) return message . error ( "economy/pay:INVALID_AMOUNT" ) ;
if ( amount > data . memberData . money ) return message . error ( "economy/pay:ENOUGH_MONEY" , {
amount : ` ** ${ amount } ** ${ message . getNoun ( amount , message . translate ( "misc:NOUNS:CREDITS:1" ) , message . translate ( "misc:NOUNS:CREDITS:2" ) , message . translate ( "misc:NOUNS:CREDITS:5" ) ) } `
} ) ;
2022-01-19 19:42:49 +05:00
thisGame . bets [ author . id ] = {
2022-01-19 20:23:40 +05:00
amount ,
horse
2022-01-16 23:18:56 +05:00
} ;
2022-02-03 13:40:00 +05:00
message . sendT ( "economy/horserace:BET" , {
2022-01-16 23:18:56 +05:00
user : author . username ,
2022-01-19 19:28:13 +05:00
amount : ` ** ${ Math . floor ( amount ) } ** ${ message . getNoun ( Math . floor ( amount ) , message . translate ( "misc:NOUNS:CREDITS:1" ) , message . translate ( "misc:NOUNS:CREDITS:2" ) , message . translate ( "misc:NOUNS:CREDITS:5" ) ) } ` ,
2022-01-19 20:23:40 +05:00
horse
2022-01-16 23:18:56 +05:00
} ) ;
} else if ( args [ 0 ] === "go" ) {
const thisGame = games [ message . channel . id ] ;
const horsePositions = [ 0 , 0 , 0 , 0 , 0 ] ;
2022-02-03 13:40:00 +05:00
if ( ! thisGame ) return message . error ( "economy/horserace:NO_GAME_RUNNING" ) ;
2022-01-19 19:42:49 +05:00
2022-01-16 23:18:56 +05:00
// eslint-disable-next-line no-constant-condition
while ( true ) {
for ( let i = 0 ; i < 5 ; i ++ ) {
2022-01-19 20:57:44 +05:00
if ( thisGame . horseSpeeds [ i ] >= Math . floor ( Math . random ( ) * 15 ) ) {
2022-01-16 23:18:56 +05:00
horsePositions [ i ] += 1 ;
2022-01-19 19:28:13 +05:00
if ( horsePositions [ i ] === 3 ) {
2022-01-16 23:18:56 +05:00
const winnings = [ ] ;
2022-01-20 18:27:39 +05:00
const profit = {
1 : 3.5 ,
2 : 3 ,
3 : 2.5 ,
4 : 2 ,
5 : 1.5 ,
} ;
// const profit = Math.floor((((8.9 / 9) * (6 - thisGame.horseSpeeds[i])) + 1.1) * 10) / 10;
2022-01-16 23:18:56 +05:00
for ( let j = 0 ; j < Object . keys ( thisGame . bets ) . length ; j ++ ) {
if ( Object . values ( thisGame . bets ) [ j ] . horse === i + 1 ) {
2022-01-20 18:27:39 +05:00
winnings . push ( [ Object . keys ( thisGame . bets ) [ j ] , Object . values ( thisGame . bets ) [ j ] . amount * profit [ thisGame . horseSpeeds [ i ] ] ] ) ;
2022-01-16 23:18:56 +05:00
}
}
if ( winnings . length === 0 ) {
2022-01-19 19:28:13 +05:00
for ( let j = 0 ; j < Object . keys ( thisGame . bets ) . length ; j ++ ) {
if ( Object . values ( thisGame . bets ) [ j ] . horse !== i + 1 ) {
2022-01-19 20:02:15 +05:00
const memberData = await this . client . findOrCreateMember ( {
2022-01-19 20:04:32 +05:00
id : Object . keys ( thisGame . bets ) [ j ] ,
2022-01-19 20:02:15 +05:00
guildID : message . guild . id
} ) ;
2022-01-19 19:28:13 +05:00
const info = {
user : message . translate ( "economy/transactions:HORSERACE" ) ,
amount : Object . values ( thisGame . bets ) [ j ] . amount ,
date : Date . now ( ) ,
2022-01-19 19:29:59 +05:00
type : "send"
2022-01-19 19:28:13 +05:00
} ;
2022-01-19 20:02:15 +05:00
memberData . transactions . push ( info ) ;
memberData . money -= Object . values ( thisGame . bets ) [ j ] . amount ;
2022-01-19 19:28:13 +05:00
}
}
2022-02-03 13:40:00 +05:00
message . sendT ( "economy/horserace:NO_WINNERS" , {
2022-01-16 23:18:56 +05:00
horse : i + 1
} ) ;
} else {
let winners = "" ;
for ( let j = 0 ; j < winnings . length ; j ++ ) {
2022-01-19 18:48:21 +05:00
winners += ` \n <@ ${ winnings [ j ] [ 0 ] } > выиграл ** ${ Math . floor ( winnings [ j ] [ 1 ] ) } ** ${ message . getNoun ( Math . floor ( winnings [ j ] [ 1 ] ) , message . translate ( "misc:NOUNS:CREDITS:1" ) , message . translate ( "misc:NOUNS:CREDITS:2" ) , message . translate ( "misc:NOUNS:CREDITS:5" ) ) } ! ` ;
2022-01-16 23:18:56 +05:00
const memberData = await this . client . findOrCreateMember ( {
id : winnings [ j ] [ 0 ] ,
guildID : message . guild . id
} ) ;
2022-01-19 20:29:24 +05:00
const toAdd = Math . floor ( winnings [ j ] [ 1 ] ) - Object . values ( thisGame . bets ) [ j ] . amount ;
2022-01-16 23:18:56 +05:00
const info = {
user : message . translate ( "economy/transactions:HORSERACE" ) ,
2022-01-19 20:29:24 +05:00
amount : toAdd ,
2022-01-16 23:18:56 +05:00
date : Date . now ( ) ,
type : "got"
} ;
2022-01-19 19:28:13 +05:00
memberData . transactions . push ( info ) ;
2022-01-19 20:29:24 +05:00
memberData . money += toAdd ;
2022-01-16 23:18:56 +05:00
memberData . save ( ) ;
}
2022-02-03 13:40:00 +05:00
message . sendT ( "economy/horserace:WINNERS" , {
2022-01-16 23:18:56 +05:00
horse : i + 1 ,
winners
} ) ;
}
delete games [ message . channel . id ] ;
return ;
}
}
}
}
}
}
}
module . exports = Horserace ;