@name Money system
@inputs 
@outputs MoneyInCirculation CircClk
@persist Msg:array MsgWho:entity Name:entity Players:array SalaryInt Salary

##VARIABLES

#Interval of salary payout to everyone. (Default is 600000, or 10 minutes)
SalaryInt = 600000 

#Salary to give to everyone.
Salary = 500




if(first()) {
    Players:clear()
    findByClass("player")
    Players = findToArray()
    timer("salary",SalaryInt)
    timer("circcheck",50)
}

interval(100)

MsgWho=lastSpoke()
Msg=lastSpoke():lastSaid():explode(" ")

if(changed(numPlayers())) {
    Players:clear()
    findByClass("player")
    Players = findToArray()
    
    timer("circcheck",50)
}


#######COMMANDS###############


if(Msg:string(1)=="!setmoney" & MsgWho:isAdmin()==1 & Msg:string(2) != "all"){ 
    hideChat(1)
    Name=findPlayerByName(Msg:string(2))
    Name["money",number]=Msg:string(3):toNumber()
    concmd("say " + Name:toString() + " now has " + Name["money",number]:toString())
    timer("circcheck",50)
}
if(Msg:string(1)=="!setmoney" & MsgWho:isAdmin()==1 & Msg:string(2) == "all"){ ##set EVERYONE'S money to x
    hideChat(1)
    local Money = Msg:string(3)
    for(I=1,numPlayers()) {
        Players[I,entity]["money",number] = Money:toNumber()
    }
    concmd("say " + MsgWho:toString() + " set everyone's balance to " + Money)
    timer("circcheck",50)
}
if(Msg:string(1)=="!setmoney" & MsgWho:isAdmin()!=1){
    hideChat(1)
  concmd("say " + MsgWho:toString() + ": !setmoney is for admins only!")
}




if(Msg:string(1)=="!addmoney" & MsgWho:isAdmin()==1){ ##give someone x money
    hideChat(1)
    Name=findPlayerByName(Msg:string(2))
    Name["money",number]= Name["money",number] + Msg:string(3):toNumber()
    concmd("say " + Name:toString() + " now has " + Name["money",number]:toString())
    timer("circcheck",50)
}
if(Msg:string(1)=="!addmoney" & MsgWho:isAdmin()==1 & Msg:string(2) == "everyone"){ ##give EVERYONE x money
    hideChat(1)
    local Money = Msg:string(3)
    for(I=1,numPlayers()) {
        Players[I,entity]["money",number] = Players[I,entity]["money",number] + Money:toNumber()
    }
    concmd("say " + MsgWho:toString() + " gave everyone " + Money)
    timer("circcheck",50)
}
if(Msg:string(1)=="!addmoney" & MsgWho:isAdmin()!=1){
    hideChat(1)
  concmd("say " + MsgWho:toString() + ": !addmoney is for admins only!")
}



if(Msg:string(1)=="!takemoney" & MsgWho:isAdmin()==1){ ##give someone x money
    hideChat(1)
    Name=findPlayerByName(Msg:string(2))
    Name["money",number]= Name["money",number] - Msg:string(3):toNumber()
    concmd("say " + Name:toString() + " now has " + Name["money",number]:toString())
    timer("circcheck",50)
}
if(Msg:string(1)=="!takemoney" & MsgWho:isAdmin()==1 & Msg:string(2) == "everyone"){ ##give EVERYONE x money
    hideChat(1)
    local Money = Msg:string(3)
    for(I=1,numPlayers()) {
        Players[I,entity]["money",number] = Players[I,entity]["money",number] - Money:toNumber()
    }
    concmd("say " + MsgWho:toString() + " revoked everyone in money of " + Money)
    timer("circcheck",50)
}
if(Msg:string(1)=="!takemoney" & MsgWho:isAdmin()!=1){
    hideChat(1)
  concmd("say " + MsgWho:toString() + ": !takemoney is for admins only!")
}



if(Msg:string(1)=="!showmoney"){
    hideChat(1)
    Name=findPlayerByName(Msg:string(2))
    concmd("say " + Name:toString() + " has " + (Name["money",number]:toString()))
}  



if(Msg:string(1)=="!pay"){ #!pay
    hideChat(1)
    Name=MsgWho
    MoneyTo=findPlayerByName(Msg:string(2))
    Num = Msg:string(3):toNumber()
    Money = Name["money",number]
    if(Money - Num < 0) {Ret = 1}
    if(Name:toString()=="") {Ret = 2}
    if(Num:toString()=="") {concmd("say " + MsgWho:toString() + ": You didn't specify how much money!")}
    if(Money - Num > 0) {
        MoneyTo["money",number] = MoneyTo["money",number] + Num
        Name["money",number] = Name["money",number] - Num
        concmd("say " + MsgWho:toString() + " has paid " + MoneyTo:toString() + " " + Num:toString())
        Ret = 0
    } elseif(Money - Num < 0 && Ret != 0) {
        concmd("say " + MsgWho:toString() + ": You don't have enough money or the payment wasn't understood! Type !money_help for help.")
    }
    
}


if(Msg:string(1)=="!moneyhelp") {
    hideChat(1)
    concmd("say " + MsgWho:toString() + ": HELP: !setmoney <user> <money> *ADMIN* || !add|takemoney <user> <money> *ADMIN*")
    timer("1",1000)
    if(clk("1")) {concmd("say " + MsgWho:toString() + ": HELP: !showmoney <user> || !pay <user> <money>")}
}





#Salaries
if(clk("salary")){
    for(I=1,numPlayers()) {
        Players[I,entity]["money",number] = Players[I,entity]["money",number] + Salary
        concmd("say The salary of 500 has been paid to everyone.")
    }
    timer("salary",SalaryInt)
    timer("circcheck",10)
}

#Money in circulation
if(clk("circcheck")){
    MoneyInCirculation=0
    CircClk=1
    CircClk=0
    for(I=1,numPlayers()) {
        MoneyInCirculation = MoneyInCirculation + Players[I,entity]["money",number]
    }
}



#[
function void entity:setMoney(Str:string) {
    This[Money,number]=Str:toNumber()
}

function string entity:getMoney() {
    return This[Money,number]:toString()
}

function normal entity:pay(Amt:string,MoneyTo:entity) {
    Num = Amt:toNumber()
    if(This[Money,number] - Amt < 0) {
        MoneyTo[Money,number] = MoneyTo[Money,number] + Num
        This[Money,number] = This[Money,number] - Amt
        return 0
    } else {
        return 1
    }
}
]#
