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 ) {
if ( ! args [ 0 ] ) return message . sendT ( "fun/horserace:MISSING_STATUS" ) ;
const author = message . author ;
if ( args [ 0 ] === "create" ) {
let thisGame = games [ message . channel . id ] ;
if ( thisGame !== undefined ) message . sendT ( "fun/horserace:GAME_RUNNING" ) ;
else {
games [ message . channel . id ] = {
horseSpeeds : [ ] ,
bets : [ ]
} ;
thisGame = games [ message . channel . id ] ;
const f = [ ] ;
for ( let i = 0 ; i < 5 ; i ++ ) {
const speed = Math . ceil ( Math . random ( ) * 10 ) ;
const profit = Math . floor ( ( ( ( 8.9 / 9 ) * ( 10 - speed ) ) + 1.1 ) * 10 ) / 10 ;
thisGame . horseSpeeds . push ( speed ) ;
f . push ( {
name : message . translate ( "fun/horserace:HORSE_NAME" , {
number : i + 1
} ) ,
value : message . translate ( "fun/horserace:HORSE_VALUE" , {
speed ,
profit : Math . round ( profit * 100 )
} )
} ) ;
}
message . channel . send ( {
embeds : [ {
color : data . config . embed . color ,
title : message . translate ( "fun/horserace:EMBED_T" ) ,
fields : f
} ]
} ) ;
}
} else if ( args [ 0 ] === "bet" ) {
2022-01-19 18:37:54 +05:00
const horse = parseInt ( args [ 1 ] ) ;
2022-01-16 23:18:56 +05:00
if ( horse > 5 ) return message . sendT ( "fun/horserace:HORSE_NUM" ) ;
2022-01-19 18:37:54 +05:00
const amount = parseInt ( args [ 2 ] ) ;
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" ) ) } `
} ) ;
games [ message . channel . id ] . bets [ author . id ] = {
amount : amount ,
horse : horse
} ;
data . memberData . money -= amount ;
message . sendT ( "fun/horserace:BET" , {
user : author . username ,
amount : ` ** ${ amount } ** ${ message . getNoun ( amount , message . translate ( "misc:NOUNS:CREDITS:1" ) , message . translate ( "misc:NOUNS:CREDITS:2" ) , message . translate ( "misc:NOUNS:CREDITS:5" ) ) } ` ,
horse : horse
} ) ;
} else if ( args [ 0 ] === "go" ) {
const thisGame = games [ message . channel . id ] ;
const horsePositions = [ 0 , 0 , 0 , 0 , 0 ] ;
// eslint-disable-next-line no-constant-condition
while ( true ) {
for ( let i = 0 ; i < 5 ; i ++ ) {
if ( thisGame . horseSpeeds [ i ] >= Math . random ( ) * 15 ) {
horsePositions [ i ] += 1 ;
if ( horsePositions [ i ] == 3 ) {
const winnings = [ ] ;
const profit = Math . floor ( ( ( ( 8.9 / 9 ) * ( 10 - thisGame . horseSpeeds [ i ] ) ) + 1.1 ) * 10 ) / 10 ;
for ( let j = 0 ; j < Object . keys ( thisGame . bets ) . length ; j ++ ) {
if ( Object . values ( thisGame . bets ) [ j ] . horse === i + 1 ) {
winnings . push ( [ Object . keys ( thisGame . bets ) [ j ] , Object . values ( thisGame . bets ) [ j ] . amount * profit ] ) ;
}
}
if ( winnings . length === 0 ) {
message . sendT ( "fun/horserace:NO_WINNERS" , {
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
} ) ;
const info = {
user : message . translate ( "economy/transactions:HORSERACE" ) ,
2022-01-19 18:48:21 +05:00
amount : Math . floor ( winnings [ j ] [ 1 ] ) ,
2022-01-16 23:18:56 +05:00
date : Date . now ( ) ,
type : "got"
} ;
data . memberData . transactions . push ( info ) ;
2022-01-19 18:48:21 +05:00
memberData . money += Math . floor ( winnings [ j ] [ 1 ] ) ;
2022-01-16 23:18:56 +05:00
memberData . save ( ) ;
}
message . sendT ( "fun/horserace:WINNERS" , {
horse : i + 1 ,
winners
} ) ;
}
delete games [ message . channel . id ] ;
return ;
}
}
}
}
}
}
}
module . exports = Horserace ;