@name BMD Orderer
@inputs Scr:wirelink UserTrigger:entity
@persist [Data Order]:table User:entity Menu Draw Use Using Current
interval(100)
runOnHTTP(1)
runOnChat(1)
if (first()) {
    timer("ld_data",1000)
    function number pIR(Num:number) {
        local O=Scr:egpCursor(User)
        local Diff=Scr:egpSize(Num)/2
        local Pos=Scr:egpPos(Num)
        if (inrange(O,Pos-Diff,Pos+Diff)) {
            return 1
        } else {
            return 0
        }
    }
    Menu=-1
}
if (changed(Menu)) {Scr:egpClear() Draw=1}
if (changed(Scr)) {Scr:egpClear() Draw=1 entity():createWire(Scr:entity(),"UserTrigger","User") printColor(vec(0,255,255),"Wired UserTrigger.")}
if (clk("ld_data")) {
    if (httpCanRequest()) {
        printColor(vec(0,255,255),"Loading data...")
        httpRequest("http://voxxe.pw/request.php?filename=ByB_WeaponData")
    } else {
        timer("ld_data",1000)
    }
}
if (httpClk() & !Data) {
    local Raw_Data=httpData()
    local Raw_Entries=Raw_Data:explode("
")
     #[
        RAW
        1. Entity name
        2. Name
        3. Damage
        4. Price per Unit
        5. Model
        6. Icon
        7. Description
    ]#
    local Entries=0
    for (I=1, Raw_Entries:count()) {
        local Raw=Raw_Entries[I,string]:explode(";;;")
        local EntName=Raw[1,string]
        local Name=Raw[2,string]
        local Damage=Raw[3,string]
        local Price=Raw[4,string]
        local Model=Raw[5,string]
        local Icon=Raw[6,string]
        local Desc=Raw[7,string]
        Data[Name,array]=array(Price,Icon)
        Entries++
    }
    printColor(vec(0,255,0),Entries,vec(255)," entries loaded.")
    Menu=0
}
if (User:keyUse() & !Using) {
    Use=1
    Using=1
}
if (!User:keyUse()) {
    Using=0
    Use=0
}

if (Menu==0) {
    if (Draw) {
        Scr:egpBox(1,vec2(256),vec2(512,70))
        Scr:egpColor(1,vec(128))
        Scr:egpText(2,"Press anywhere to begin",vec2(256))
        Scr:egpSize(2,40)
        Scr:egpAlign(2,1,1)
    }
    if (UserTrigger) {
        User=UserTrigger
        Menu=1
    }
}
if (Menu==1) {
    if (Draw) {
        Scr:egpBox(100,vec2(256),vec2(512))
        Scr:egpMaterial(100,"gui/center_gradient")
        Scr:egpColor(100,vec(64))
        
        Scr:egpBox(1,vec2(256,512-25),vec2(512,70))
        Scr:egpColor(1,vec(128))
        
        Scr:egpRoundedBox(2,vec2(512-85,512-30),vec2(150,50))
        Scr:egpColor(2,vec(172))
        Scr:egpText(3,"Checkout",Scr:egpPos(2))
        Scr:egpSize(3,30)
        Scr:egpAlign(3,1,1)
        
        Scr:egpText(4,"In chat: order (Name) [Amount]",vec2(256,10))
        Scr:egpAlign(4,1,0)
        
        Scr:egpText(5,"Total: $0",vec2(10,512-25))
        Scr:egpAlign(5,0,1)
        Scr:egpSize(5,30)
    }
    if (chatClk(User)) {
        local Ex=User:lastSaid():lower():explode(" ")
        #print(Ex:concat(", "))
        if (Ex[1,string]=="order") {
            local Order_ID=""
            local AMT=Ex[3,string]:toNumber()
            if (!AMT) {AMT=1}
            foreach(I,V:array=Data) {
                if (I:lower():matchFirst(Ex[2,string])) {
                    Order_ID=I
                    break
                }
            }
            if (Order_ID) {
                Order[Order_ID,number]=Order[Order_ID,number]+AMT
            }
        }
        if (Ex[1,string]=="clear") {
            print("Wiped order.")
            Order:clear()
            for (I=101,300,1) {
                Scr:egpRemove(I)
            }
        }
    }
}

Draw=0
Use=0
