import es import playerlib import time import math import sqlite3 import langlib import re import popuplib info = es.AddonInfo() info.name = 'FGBank' info.version = '1.0.0' info.url = 'http://addons.eventscripts.com/addons/view/fgbank' info.basename = 'fgbank' info.author = 'Freigeist' ########### # Globals # ########### conn = sqlite3.connect(es.getAddonPath('fgbank') + '/fgbank.sqlitedb') cur = conn.cursor() ########## # Config # ########## fgbank_inactivity = es.ServerVar("fgbank_inactivity", 14, "How long a play can be inactiv before removed from Database (in Days)") fgbank_interestrate = es.ServerVar("fgbank_interestrate", 0.005, "This is the interest rate which will be distributed every round.") #fgbank_maxinterests = es.ServerVar("fgbank_maxinterests", 0, "How many interest a player can earn at begin of a round. 0 = unlimited") def load(): es.set('fgbank_version', info.version) es.makepublic('fgbank_version') if not es.exists('saycommand', '/deposit'): es.regsaycmd('/deposit', 'fgbank/deposit') if not es.exists('saycommand', '/autodeposit'): es.regsaycmd('/autodeposit', 'fgbank/autodeposit') if not es.exists('saycommand', '/withdraw'): es.regsaycmd('/withdraw', 'fgbank/withdraw') if not es.exists('saycommand', '/autowithdraw'): es.regsaycmd('/autowithdraw', 'fgbank/autowithdraw') if not es.exists('saycommand', '/balance'): es.regsaycmd('/balance', 'fgbank/balance') if not es.exists('saycommand', '/bank'): es.regsaycmd('/bank', 'fgbank/bank') #es.regclientcmd('command', 'script/function', 'Comment') cur.execute('CREATE TABLE IF NOT EXISTS bank (steamid VARCHAR PRIMARY KEY NOT NULL UNIQUE, name VARCHAR NOT NULL, money INTEGER NOT NULL DEFAULT 0, withdraw INTEGER NOT NULL DEFAULT 0, deposit INTEGER NOT NULL DEFAULT 0, hidden INTEGER NOT NULL DEFAULT 0, protected INTEGER NOT NULL DEFAULT 0, timestamp INTEGER NOT NULL DEFAULT 0);') #remove inactive players from database timestamp = getts() querytimestamp = timestamp-(fgbank_inactivity*86400) vars = (querytimestamp,) cur.execute("DELETE FROM bank WHERE timestamp < ? AND protected = 0;", vars) cur.execute("VACUUM;") conn.commit() PlayerList = playerlib.getPlayerList('#human') for ply in PlayerList: timestamp = getts() vars = (ply.steamid,) cur.execute("SELECT steamid FROM bank WHERE steamid = ?;", vars) result = cur.fetchone() if result is None: vars = (ply.steamid,ply.name,timestamp) cur.execute("INSERT INTO bank (steamid,name,timestamp) VALUES (?,?,?);", vars) conn.commit() log("Added new player \"" + ply.name + "\" (" + ply.steamid + ") to database") else: vars = (ply.name,timestamp,ply.steamid) cur.execute("UPDATE bank SET name = ?, timestamp = ? WHERE steamid = ?;", vars) conn.commit() def unload(): es.unregsaycmd('/deposit') es.unregsaycmd('/withdraw') es.unregsaycmd('/balance') es.unregsaycmd('/bank') conn.commit() conn.close() def log(msg): es.log("[FGBank] %s" % msg) def es_player_validated(event_var): timestamp = getts() vars = (event_var['networkid'],) cur.execute("SELECT steamid FROM bank WHERE steamid = ?;", vars) result = cur.fetchone() if result is None: vars = (event_var['networkid'],event_var["name"],timestamp) cur.execute("INSERT INTO bank (steamid,name,timestamp) VALUES (?,?,?);", vars) conn.commit() log("Added new player \"" + event_var["name"] + "\" (" + event_var['networkid'] + ") to database") else: vars = (event_var["name"],timestamp,event_var['networkid']) cur.execute("UPDATE bank SET name = ?, timestamp = ? WHERE steamid = ?;", vars) conn.commit() def round_start(event_var): PlayerList = playerlib.getPlayerList('#ct') for ply in PlayerList: interest = float(getbalance(ply.steamid)) interest *= fgbank_interestrate interest = str(int(math.ceil(interest))) dodeposit(interest, ply.userid, ply.steamid, 1) PlayerList = playerlib.getPlayerList('#t') for ply in PlayerList: interest = float(getbalance(ply.steamid)) interest *= fgbank_interestrate interest = str(int(math.ceil(interest))) dodeposit(interest, ply.userid, ply.steamid, 1) def player_spawn(event_var): PlayerList = playerlib.getPlayerList('#ct') for ply in PlayerList: playercash = playerlib.getPlayer(ply.userid).get("cash") withdraw = getsql("withdraw", ply.steamid) deposit = getsql("deposit", ply.steamid) if (withdraw > playercash) and (withdraw != 0): withdraw -= playercash withdraw = str(withdraw) dowithdraw(withdraw, ply.userid, ply.steamid) elif (deposit < playercash) and (deposit != 0): deposit = playercash - deposit deposit = str(deposit) dodeposit(deposit, ply.userid, ply.steamid) PlayerList = playerlib.getPlayerList('#t') for ply in PlayerList: playercash = playerlib.getPlayer(ply.userid).get("cash") withdraw = getsql("withdraw", ply.steamid) deposit = getsql("deposit", ply.steamid) if (withdraw > playercash) and (withdraw != 0): withdraw -= playercash withdraw = str(withdraw) dowithdraw(withdraw, ply.userid, ply.steamid) elif (deposit < playercash) and (deposit != 0): deposit = playercash - deposit deposit = str(deposit) dodeposit(deposit, ply.userid, ply.steamid) def getts(): ts = int(math.ceil(time.time())) #log(ts) return ts def deposit(): args = es.getargc() userid = es.getcmduserid() steamid = es.getplayersteamid(userid) if args == 1: show_depositmenu() elif args == 2: deposittemp = es.getargv(1) dodeposit(deposittemp, userid, steamid) else: es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have passed to many parameters.") def withdraw(): args = es.getargc() userid = es.getcmduserid() steamid = es.getplayersteamid(userid) if args == 1: show_withdrawmenu() elif args == 2: withdrawtemp = es.getargv(1) dowithdraw(withdrawtemp, userid, steamid) else: es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have passed to many parameters.") def balance(): userid = es.getcmduserid() steamid = es.getplayersteamid(userid) balance = getbalance(steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen Your ballance is #green%s#lightgreen$." % balance) def autowithdraw(): args = es.getargc() userid = es.getcmduserid() steamid = es.getplayersteamid(userid) if args == 1: show_autowithdrawmenu() elif args == 2: awithdrawtemp = es.getargv(1) if re.match("[0-9-]*$", awithdrawtemp): awithdrawtemp = int(awithdrawtemp) if awithdrawtemp < 0: awithdrawtemp = 0 if awithdrawtemp > 16000: awithdrawtemp = 16000 setsql("withdraw", awithdrawtemp, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic withdraw to #green%s#lightgreen$." % awithdrawtemp) else: es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have not entered a valid number.") else: es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have passed to many parameters.") def autodeposit(): args = es.getargc() userid = es.getcmduserid() steamid = es.getplayersteamid(userid) if args == 1: show_autodepositmenu() elif args == 2: adeposittemp = es.getargv(1) if re.match("[0-9-]*$", adeposittemp): adeposittemp = int(adeposittemp) if adeposittemp < 0: adeposittemp = 0 if adeposittemp > 16000: adeposittemp = 16000 setsql("deposit", adeposittemp, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic deposit to #green%s#lightgreen$." % adeposittemp) else: es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have not entered a valid number.") else: es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have passed to many parameters.") def dodeposit(deposittemp, userid, steamid, interest=0): if re.match("[0-9-]*$", deposittemp): deposittemp = int(deposittemp) if interest == 0: if deposittemp < 0: deposittemp = 0 if deposittemp > 16000: deposittemp = 16000 else: if deposittemp != 0 and deposittemp < fgbank_maxinterests: deposittemp = fgbank_maxinterests balance = getbalance(steamid) test = 2000000000 - balance if test == 0: deposittemp = 0 if interest == 0: es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have reached the maxium of #green2000000000#lightgreen$.") else: es.tell(userid, "#multi", "#green[FGBank]#lightgreen You can not earn any interests because, you have reached the maxium of #green2000000000#lightgreen$.") elif test <= deposittemp: deposittemp = test if interest == 0: dplayer = playerlib.getPlayer(userid) dplayercash = dplayer.get("cash") if dplayercash < deposittemp: deposittemp = dplayercash dplayercash = dplayercash - deposittemp dplayer.set("cash", dplayercash) timestamp = getts() vars = (deposittemp, timestamp, steamid) cur.execute("UPDATE bank SET money = (money + ?), timestamp = ? WHERE steamid = ?;", vars) conn.commit() if interest == 0: es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have deposited #green%s#lightgreen$." % deposittemp) else: es.tell(userid, "#multi", "#green[FGBank]#lightgreen You earned #green%s#lightgreen$ interests." % deposittemp) deposittemp = None else: es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have not entered a valid number.") def dowithdraw(withdrawtemp, userid, steamid): if re.match("[0-9-]*$", withdrawtemp): withdrawtemp = int(withdrawtemp) if withdrawtemp < 0: withdrawtemp = 0 if withdrawtemp > 16000: withdrawtemp = 16000 wplayer = playerlib.getPlayer(userid) wplayercash = wplayer.get("cash") bankmoney = getbalance(steamid) if bankmoney <= withdrawtemp: withdrawtemp = bankmoney test = withdrawtemp + wplayercash if test > 16000: withdrawtemp = test - 16000 - withdrawtemp if withdrawtemp < 0: test = -1 withdrawtemp *= test timestamp = getts() vars = (withdrawtemp, timestamp, steamid) cur.execute("UPDATE bank SET money = (money - ?), timestamp = ? WHERE steamid = ?;", vars) conn.commit() wplayercash = wplayercash + withdrawtemp wplayer.set("cash", wplayercash) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have withdrawed #green%s#lightgreen$." % withdrawtemp) withdrawtemp = None else: es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have not entered a valid number.") def getbalance(steamid): vars = (steamid,) cur.execute("SELECT money FROM bank WHERE steamid=?;", vars) result = cur.fetchone() return int(result[0]) def getsql(opt, steamid): vars = (steamid,) if opt == "withdraw": return getsqlquery("SELECT withdraw FROM bank WHERE steamid=?;", vars) elif opt == "deposit": return getsqlquery("SELECT deposit FROM bank WHERE steamid=?;", vars) elif opt == "hidden": return getsqlquery("SELECT hidden FROM bank WHERE steamid=?;", vars) elif opt == "protected": return getsqlquery("SELECT protected FROM bank WHERE steamid=?;", vars) else: return "error-50" # An insider def getsqlquery(sql, vars): cur.execute(sql, vars) result = cur.fetchone() return int(result[0]) def setsql(opt, value, steamid): vars = (value, steamid) if opt == "withdraw": cur.execute("UPDATE bank SET withdraw = ? WHERE steamid=?;", vars) conn.commit() elif opt == "deposit": cur.execute("UPDATE bank SET deposit = ? WHERE steamid=?;", vars) conn.commit() elif opt == "hidden": cur.execute("UPDATE bank SET hidden = ? WHERE steamid=?;", vars) conn.commit() elif opt == "protected": cur.execute("UPDATE bank SET protected = ? WHERE steamid=?;", vars) conn.commit() def gettop10(): cur.execute("SELECT name,money FROM bank WHERE hidden = 0 ORDER BY money DESC LIMIT 10;") i = 0 for row in cur: name,money = row if i == 0: vars = (name, money) else: vars = vars + (name, money) i += 1 while i < 10: vars = vars + ("-", "0") i += 1 return vars ##################### # Menus # ##################### def bank(): show_mainmenu() def from_mainmenu(): userid = es.getcmduserid() popupchoice = es.getInt('_popup_choice') if popupchoice == 1: show_depositmenu() elif popupchoice == 2: show_withdrawmenu() elif popupchoice == 3: show_top10menu() elif popupchoice == 4: show_autodepositmenu() elif popupchoice == 5: show_autowithdrawmenu() elif popupchoice == 6: show_optionhidemenu() else: if popupchoice != 10: show_mainmenu() def from_depositmenu(): userid = es.getcmduserid() steamid = es.getplayersteamid(userid) popupchoice = es.getInt('_popup_choice') if popupchoice == 1: dodeposit("2000", userid, steamid) elif popupchoice == 2: dodeposit("4000", userid, steamid) elif popupchoice == 3: dodeposit("6000", userid, steamid) elif popupchoice == 4: dodeposit("8000", userid, steamid) elif popupchoice == 5: dodeposit("10000", userid, steamid) elif popupchoice == 6: dodeposit("12000", userid, steamid) elif popupchoice == 7: dodeposit("14000", userid, steamid) elif popupchoice == 8: dodeposit("16000", userid, steamid) elif popupchoice == 10: show_mainmenu() else: show_depositmenu() def from_autodepositmenu(): userid = es.getcmduserid() steamid = es.getplayersteamid(userid) popupchoice = es.getInt('_popup_choice') if popupchoice == 1: setsql("deposit", 2000, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic deposit to #green2000#lightgreen.") elif popupchoice == 2: setsql("deposit", 4000, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic deposit to #green4000#lightgreen.") elif popupchoice == 3: setsql("deposit", 6000, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic deposit to #green6000#lightgreen.") elif popupchoice == 4: setsql("deposit", 8000, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic deposit to #green8000#lightgreen.") elif popupchoice == 5: setsql("deposit", 10000, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic deposit to #green10000#lightgreen.") elif popupchoice == 6: setsql("deposit", 12000, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic deposit to #green12000#lightgreen.") elif popupchoice == 7: setsql("deposit", 14000, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic deposit to #green14000#lightgreen.") elif popupchoice == 8: setsql("deposit", 16000, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic deposit to #green16000#lightgreen.") elif popupchoice == 10: show_mainmenu() else: show_autodepositmenu() def from_withdrawmenu(): userid = es.getcmduserid() steamid = es.getplayersteamid(userid) popupchoice = es.getInt('_popup_choice') if popupchoice == 1: dowithdraw("2000", userid, steamid) elif popupchoice == 2: dowithdraw("4000", userid, steamid) elif popupchoice == 3: dowithdraw("6000", userid, steamid) elif popupchoice == 4: dowithdraw("8000", userid, steamid) elif popupchoice == 5: dowithdraw("10000", userid, steamid) elif popupchoice == 6: dowithdraw("12000", userid, steamid) elif popupchoice == 7: dowithdraw("14000", userid, steamid) elif popupchoice == 8: dowithdraw("16000", userid, steamid) elif popupchoice == 10: show_mainmenu() else: show_withdrawmenu() def from_autowithdrawmenu(): userid = es.getcmduserid() steamid = es.getplayersteamid(userid) popupchoice = es.getInt('_popup_choice') if popupchoice == 1: setsql("withdraw", 2000, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic withdraw to #green2000#lightgreen.") elif popupchoice == 2: setsql("withdraw", 4000, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic withdraw to #green4000#lightgreen.") elif popupchoice == 3: setsql("withdraw", 6000, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic withdraw to #green6000#lightgreen.") elif popupchoice == 4: setsql("withdraw", 8000, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic withdraw to #green8000#lightgreen.") elif popupchoice == 5: setsql("withdraw", 10000, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic withdraw to #green10000#lightgreen.") elif popupchoice == 6: setsql("withdraw", 12000, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic withdraw to #green12000#lightgreen.") elif popupchoice == 7: setsql("withdraw", 14000, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic withdraw to #green14000#lightgreen.") elif popupchoice == 8: setsql("withdraw", 16000, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You have set automatic withdraw to #green16000#lightgreen.") elif popupchoice == 10: show_mainmenu() else: show_autowithdrawmenu() def from_top10menu(): userid = es.getcmduserid() steamid = es.getplayersteamid(userid) popupchoice = es.getInt('_popup_choice') if popupchoice == 10: show_mainmenu() else: show_top10menu() def from_optionhidemenu(): userid = es.getcmduserid() steamid = es.getplayersteamid(userid) popupchoice = es.getInt('_popup_choice') if popupchoice == 1: setsql("hidden", 0, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You are now #greenvisible#lightgreen in Top10.") elif popupchoice == 2: setsql("hidden", 1, steamid) es.tell(userid, "#multi", "#green[FGBank]#lightgreen You are now #greenhidden#lightgreen in Top10.") elif popupchoice == 10: show_mainmenu() else: show_optionhidemenu() def show_mainmenu(): userid = es.getcmduserid() version = info.version balance = getbalance(es.getplayersteamid(userid)) vars = (version,balance) popuplib.quicksend(0, userid, "FGBank %s\n\nMain Menu\n--------\nCurrent Balance: %s$\n--------\n->1. Deposit\n->2. Withdraw\n->3. Top 10\n->4. Configure Automatic Deposit\n->5. Configure Automatic Withdraw\n->6. Hide/Show in Top10\n--------\n->0. Close" % vars, 'fgbank/from_mainmenu') def show_depositmenu(): userid = es.getcmduserid() version = info.version balance = getbalance(es.getplayersteamid(userid)) vars = (version,balance) popuplib.quicksend(0, userid, 'FGBank %s\nDeposit\n--------\nCurrent Balance: %s$\n--------\n->1. 2000$\n->2. 4000$\n->3. 6000$\n->4. 8000$\n->5. 10000$\n->6. 12000$\n->7. 14000$\n->8. 16000$\n--------\n->0. Back' % vars, 'fgbank/from_depositmenu') def show_withdrawmenu(): userid = es.getcmduserid() version = info.version balance = getbalance(es.getplayersteamid(userid)) vars = (version,balance) popuplib.quicksend(0, userid, 'FGBank %s\nWithdraw\n--------\nCurrent Balance: %s$\n--------\n->1. 2000$\n->2. 4000$\n->3. 6000$\n->4. 8000$\n->5. 10000$\n->6. 12000$\n->7. 14000$\n->8. 16000$\n--------\n->0. Back' % vars, 'fgbank/from_withdrawmenu') def show_autodepositmenu(): userid = es.getcmduserid() version = info.version option = getsql("deposit", es.getplayersteamid(userid)) vars = (version,option) popuplib.quicksend(0, userid, 'FGBank %s\nConfigure Automatic Deposit\n--------\nCurrent Setting: %s$\n--------\n->1. 2000$\n->2. 4000$\n->3. 6000$\n->4. 8000$\n->5. 10000$\n->6. 12000$\n->7. 14000$\n->8. 16000$\n--------\n->0. Back' % vars, 'fgbank/from_autodepositmenu') def show_autowithdrawmenu(): userid = es.getcmduserid() version = info.version option = getsql("withdraw", es.getplayersteamid(userid)) vars = (version,option) popuplib.quicksend(0, userid, 'FGBank %s\nConfigure Automatic Withdraw\n--------\nCurrent Setting: %s$\n--------\n->1. 2000$\n->2. 4000$\n->3. 6000$\n->4. 8000$\n->5. 10000$\n->6. 12000$\n->7. 14000$\n->8. 16000$\n--------\n->0. Back' % vars, 'fgbank/from_autowithdrawmenu') def show_top10menu(): userid = es.getcmduserid() version = info.version top10 = gettop10() vars = (version,) + top10 popuplib.quicksend(0, userid, 'FGBank %s\nTop 10\n--------\n1. %s %s$\n2. %s %s$\n3. %s %s$\n4. %s %s$\n5. %s %s$\n6. %s %s$\n7. %s %s$\n8. %s %s$\n9. %s %s$\n10. %s %s$\n--------\n->0. Back' % vars, 'fgbank/from_top10menu') def show_optionhidemenu(): userid = es.getcmduserid() version = info.version option = getsql("hidden", es.getplayersteamid(userid)) if option == 0: option = "Not hidden" else: option = "Hidden" vars = (version,option) popuplib.quicksend(0, userid, 'FGBank %s\nHide from Top10\n--------\nCurrent Setting: %s$\n--------\n->1. Dont hide me from Top10\n->2. Hide me from Top10\n--------\n->0. Back' % vars, 'fgbank/from_optionhidemenu')