2020-07-13 00:59:42 +00:00
import asyncio
2020-11-23 21:27:27 +00:00
from bot import *
2020-07-13 00:59:42 +00:00
async def bal ( self ) :
2020-09-02 15:16:54 +00:00
bals = { }
2020-07-13 00:59:42 +00:00
for t in self . ledger :
await asyncio . sleep ( 0 ) # yeild control as this is a long operation
t [ ' amount ' ] = float ( t [ ' amount ' ] )
if t [ ' amount ' ] < 0.01 :
self . ledger . delete ( id = t [ ' id ' ] )
continue # dont send negative money lol
if t [ ' sender ' ] not in bals :
bals [ t [ ' sender ' ] ] = 0.00
if t [ ' to ' ] not in bals :
bals [ t [ ' to ' ] ] = 0.00
2020-09-04 13:09:49 +00:00
if t [ ' sender ' ] != ' bank ' and round ( bals [ t [ ' sender ' ] ] , 2 ) - t [ ' amount ' ] < 0.0 :
2020-07-13 00:59:42 +00:00
self . ledger . delete ( id = t [ ' id ' ] )
continue # no debt for you
bals [ t [ ' sender ' ] ] + = 0 - t [ ' amount ' ]
2020-09-02 15:16:54 +00:00
bals [ t [ ' to ' ] ] + = t [ ' amount ' ]
for i in bals :
bals [ ' bank ' ] + = bals [ i ] * 0.001
bals [ i ] - = bals [ i ] * 0.001
2020-09-02 16:02:01 +00:00
self . initfund = abs ( bals [ ' bank ' ] )
2020-07-13 00:59:42 +00:00
return bals
async def send ( self , c , n , m ) :
m = m . split ( ' ' )
if len ( m ) < 2 :
2020-07-13 01:24:24 +00:00
await self . message ( c , ' [ \x03 6coin \x0f ] invalid syntax ' )
2020-07-13 00:59:42 +00:00
return
2020-07-13 01:37:17 +00:00
try :
2020-11-23 21:27:27 +00:00
to = self . users [ m . pop ( 0 ) . lower ( ) ] . account
2020-07-13 01:40:41 +00:00
except :
2020-07-13 01:37:17 +00:00
await self . message ( c , ' [ \x03 6coin \x0f ] that user is not logged in. refusing so coins are not lost ' )
2020-11-24 00:49:55 +00:00
if to == ' ' :
await self . message ( c , ' [ \x03 6coin \x0f ] they must authenticate with nickserv. ' )
return
2020-07-13 01:40:41 +00:00
amount = round ( float ( m . pop ( 0 ) ) , 2 )
2020-07-13 00:59:42 +00:00
message = ' ' . join ( m )
2020-11-23 21:27:27 +00:00
sender = self . users [ n . lower ( ) ] . account
2020-07-13 00:59:42 +00:00
self . ledger . insert ( dict ( to = to , sender = sender , amount = amount , message = message ) )
2020-07-13 01:24:24 +00:00
await self . message ( c , ' [ \x03 6coin \x0f ] added transaction to ledger, check balances to verify ' )
2020-07-13 00:59:42 +00:00
async def balance ( self , c , n , m ) :
m = m . strip ( )
if len ( m ) < 1 :
m = n
try :
2020-11-23 21:27:27 +00:00
m = self . users [ m . lower ( ) ] . account
2020-07-13 00:59:42 +00:00
except :
m = m
2020-11-24 00:49:55 +00:00
if m == ' ' :
m = m
2020-07-13 00:59:42 +00:00
bals = await bal ( self )
if m in bals :
2020-07-13 01:24:24 +00:00
latest = self . ledger . find_one ( to = m , order_by = ' -id ' )
if latest :
2020-07-30 16:40:38 +00:00
await self . message ( c , ' [ \x03 6coin \x0f ] {} \u200c {} \' s balance is {} BUTT (Balun Useless Trading Tokens), {} % o f the total supply '
2020-07-15 20:27:14 +00:00
. format ( m [ : 1 ] , m [ 1 : ] , round ( bals [ m ] , 2 ) , int ( ( bals [ m ] / self . initfund ) * 100 ) ) +
2020-07-13 01:24:24 +00:00
' . last deposit: [ {} from {} , " {} " ] ' . format (
latest [ ' amount ' ] , latest [ ' sender ' ] , latest [ ' message ' ]
) )
else :
2020-09-04 13:09:49 +00:00
await self . message ( c , ' [ \x03 6coin \x0f ] {} \u200c {} \' s balance is {} BUTT (Balun Useless Trading Tokens), {} % o f the total supply '
2020-07-15 20:27:14 +00:00
. format ( m [ : 1 ] , m [ 1 : ] , round ( bals [ m ] , 2 ) , int ( ( bals [ m ] / self . initfund ) * 100 ) ) )
2020-07-13 00:59:42 +00:00
else :
await self . message ( c , ' [ \x03 6coin \x0f ] this user has never made a transaction ' )
async def richest ( self , c , n , m ) :
richest = sorted ( ( await bal ( self ) ) . items ( ) , key = lambda item : item [ 1 ] , reverse = True ) [ : 10 ]
await self . message ( c , ' [ \x03 6coin \x0f ] richest users: ' + ' , ' . join (
[
2020-07-15 20:27:14 +00:00
i [ 0 ] [ : 1 ] + " \u200c " + i [ 0 ] [ 1 : ] + " : " + str ( round ( i [ 1 ] , 2 ) )
2020-07-13 00:59:42 +00:00
for i in richest ]
) )
async def init ( self ) :
2020-11-23 21:27:27 +00:00
self . ledger = shared . db [ ' ledger ' ]
2020-09-02 16:05:15 +00:00
self . initfund = 1
2020-07-13 00:59:42 +00:00
2020-11-23 21:27:27 +00:00
shared . commands [ ' tipcoins ' ] = send
shared . commands [ ' sendcoins ' ] = send
shared . commands [ ' balance ' ] = balance
shared . commands [ ' richest ' ] = richest
2020-07-13 00:59:42 +00:00
2020-11-23 21:27:27 +00:00
return
2020-07-13 00:59:42 +00:00
self . help [ ' sendcoins ' ] = [ ' sendcoins <recipient> <amount> [message] - send someone coins. note (more) ' , ' this does NOT verify transactions went through!! check your balance after ' ]
self . help [ ' balance ' ] = [ ' balance [person] - check someone \' s balance ' , ' coins owo ' ]
self . help [ ' richest ' ] = [ ' richest - who has the most coins ' , ' coins owo ' ]