added good base for currency system

This commit is contained in:
snowman1711 2021-04-12 22:16:56 +05:30
parent 38cd45f379
commit b582230e7e
15 changed files with 161 additions and 0 deletions

View file

@ -0,0 +1,42 @@
""" store functions executed when chat command are called """
import ba, _ba, json, random
from _ba import chatmessage as send
from _ba import screenmessage as screenmsg
from Currency.Handlers.bank_handler import *
from Currency.Handlers.ba_get_player_data import *
from Currency.Handlers.cooldown_manager import *
from .fun import get_random_donator, get_random_cash
def balance_call(userid, clientid):
open_account(userid)
users = get_bank_data()
name = client_to_name(clientid)
cash_amt = users[str(userid)]["cash"]
bank_cash_amt = users[str(userid)]["bank_cash"]
bank_space = users[str(userid)]["bank_space"]
balance = '|| {} | Cash - {} | Bank- {}/{} ||'.format(str(name), str(cash_amt), str(bank_cash_amt), str(bank_space))
send(balance)
def beg_call(userid):
open_account(userid)
earned = get_random_cash()
users = get_bank_data()
user = users[str(userid)]
user["cash"] += earned
cash_amt = user["cash"]
donator = get_random_donator()
send(f'{donator} gave you {earned} now you have {cash_amt} coins')
commit(users)

View file

@ -0,0 +1,17 @@
import random
donators_list = [
'Eggs broke and',
'pranav',
'your mom',
'saitama',
'one simp',
'idiot',
'mr smoothy'
]
def get_random_donator():
return random.choice(donators_list)
def get_random_cash():
return random.randrange(80)

View file

@ -0,0 +1,13 @@
""" check given command and executive the the cmd function from data functions"""
import ba, _ba
from .Command_Objects.data_functions import *
def on_command(cmd, args, accountid, clientid):
if cmd in ['coins', 'bal', 'balance', 'me']:
balance_call(accountid, clientid)
elif cmd == 'beg':
beg_call(accountid)

View file

@ -0,0 +1,12 @@
{
"None": {
"cash": 1421,
"bank_space": 100,
"bank_cash": 0
},
"pb-IF4VAk4a": {
"cash": 297,
"bank_space": 100,
"bank_cash": 0
}
}

View file

@ -0,0 +1,24 @@
""" retruns information of given user using client_id """
import ba, _ba
def client_to_account(client_id):
rost = _ba.get_game_roster()
for i in rost:
if i['client_id'] == client_id:
return i['account_id']
return None
def client_to_name(client_id):
rost = _ba.get_game_roster()
for i in rost:
if i['client_id'] == client_id:
return i['players'][0]['name_full']
return None
def client_to_display_string(client_id):
rost = _ba.get_game_roster()
for i in rost:
if i['client_id'] == client_id:
return i['display_string']
return None

View file

@ -0,0 +1,38 @@
""" helperfunctions for save lot of lines of code """
import ba, _ba, json
bank_path = _ba.env()['python_directory_user']+'/Currency/Data/bank.json'
def open_account(accountid):
users = get_bank_data()
if str(accountid) in users:
return False
else:
users[str(accountid)] = {}
users[str(accountid)]["cash"] = 0
users[str(accountid)]["bank_space"] = 100
users[str(accountid)]["bank_cash"] = 0
commit(users)
return True
def get_bank_data():
with open(bank_path, 'r') as f:
users = json.load(f)
return users
def commit(data):
with open(bank_path, "w") as f:
json.dump(data, f, indent=2)

View file

@ -0,0 +1 @@
""" cooldown manager """"

14
dist/ba_root/mods/Currency/__init__.py vendored Normal file
View file

@ -0,0 +1,14 @@
from .Commands import chat_commands
from .Handlers.ba_get_player_data import client_to_account
def main(msg, client_id):
command = msg.split(" ")[0]
if command.startswith("."):
command = command.split(".")[1]
arguments = msg.split(" ")[1:]
accountid = client_to_account(client_id)
chat_commands.on_command(command, arguments, accountid, client_id)