Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Amigo 0.11
- --NuAoA
- ------------------
- -----Settings-----
- ------------------
- local DataDir = "Data" -- Directory to store all playerdata
- local DEBUG = true -- Debug code
- local tickRate = 0 -- Update frequency on time dependent functions (ie, when scanning for players)
- local encryptData = false -- Toggle basic encryption of playerdata (player.data files)
- local MeBrideOutputDirection = 1 --0 for down, 1 for up, 2 for -Z, 3 for +Z, 4 for -X and 5 for +X.
- local cableFace = "right" -- The face that the RP or MF bundled cable is attached to.
- local command_prefix = "cc " -- Command prefex for chat commands.
- local tag = "§d[CC] " --Prefix tag for outgoing messages.
- local colors = {["IOport"] = 1,["ActivePlayer"]=2}
- local AdminList = {
- "player1";
- "player2";
- }
- ------------------
- ----Local Var-----
- ------------------
- local modem
- local meBridge
- local chatBox
- --local MySensor
- local mon
- local commandblock = false
- local enccode = os.getComputerID()
- local curr_player = false
- local playerData = {}
- local chatHelper = {}
- local player_temp = {}
- player_temp["items"] = {}
- player_temp["trades"] = {}
- local AE_Data = {}
- local hexnums = { [10] = "a", [11] = "b", [12] = "c", [13] = "d", [14] = "e" , [15] = "f" }
- local ValidChars = {
- "33","#","$","%","&","'","(",")","*","+",",","-",".","3","0","1","2","3",
- "4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","E","F","G",
- "H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[",
- "k(","]","^","_","`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o",
- "p","q","r","s","t","u","v","w","x","y","z","{","}","~"," "
- }
- local CharToCode = {}
- local CodeToChar = {}
- for _, c in pairs(ValidChars) do
- local code = #CodeToChar+1
- CharToCode[c] = code
- CodeToChar[code] = c
- end
- local tag = "§d[CC] "
- local help = {
- "§2------CC Trade Help menu-----";
- "§2|".."§b 'cc help' §fwill list commands ".."§2|";
- "§2|".."§b §f ".."§2|";
- "§2|".."§b §f ".."§2|";
- "§2-------&2--------------------";
- }
- ------------------
- -----Setup--------
- ------------------
- function setup()
- os.pullEvent = os.pullEventRaw
- if not fs.exists("wrap") then
- print("Wrap API not found, downloading from http://pastebin.com/g5tBarus")
- shell.run("pastebin get g5tBarus wrap")
- end
- if not fs.exists("ItemDB") then
- print("ItemDB API not found, downloading from http://pastebin.com/yjdEKRYY")
- shell.run("pastebin get yjdEKRYY ItemDB")
- end
- if not fs.exists("Draw") then
- print("ItemDB API not found, downloading from http://pastebin.com/3sAGiX7j")
- shell.run("pastebin get 3sAGiX7j Draw")
- end
- if not fs.exists("Chat") then
- print("ItemDB API not found, downloading from http://pastebin.com/i2s5Sr3N")
- shell.run("pastebin get i2s5Sr3N Chat")
- end
- if not fs.exists("wrap") or not fs.exists("Chat") or not fs.exists("Draw") or not fs.exists("ItemDB") then
- error("API's failed to download, maybe you need to enable HTTP api?")
- end
- os.loadAPI("wrap")
- os.loadAPI("ItemDB")
- os.loadAPI("ocs/apis/sensor")
- os.loadAPI("Chat")
- os.loadAPI("Draw")
- modem = wrap.attach("modem")
- meBridge = wrap.getRemoteName("meBridge")
- player = wrap.attach("playerDetector")
- --MySensor = sensor.wrap("top")
- --chatbox = wrap.getRemoteName("command")
- mon = wrap.getRemoteName("monitor")
- --[[
- if chatbox ~= nil then
- commandblock = true
- else
- print("Command block is not required")
- chatbox = wrap.getRemoteName("chat")
- end
- ]]--
- if modem == nil or meBridge == nil or MySensor == nil or mon == nil then
- print("Missing Peripherals!")
- --error("You are missing required peripherals for this program. OpenCCsensor goes ontop, chatbox+meBridge+monitor are attached via a modem, a command block can be placed next to the terminal (optional), and a rednet cable to the ME import bus out the back.")
- end
- if not fs.isDir(DataDir) then
- fs.makeDir(DataDir)
- end
- AE_Data = modem.callRemote(meBridge, "listItems")
- loadAll()
- Chat.setup(command_prefix,tag)
- os.queueEvent("setupDone")
- end
- -----------------
- ------Util-------
- -----------------
- if DEBUG and not fs.exists("debug") then
- local file = fs.open("debug","w")
- file.write("Amigo debug file \n")
- file:close()
- end
- function Debug(text)
- if DEBUG then
- local file = fs.open("debug","r")
- local curr = file.readAll()
- file:close()
- file = fs.open("debug","w")
- file.write(curr.."\n"..tostring(text))
- file:close()
- end
- end
- -- returns the event and parameters of up to 5 events passed to it. [borked]
- function returnEvent(...)
- local event,param1,param2,param3,param4,param5
- local funcList = {}
- local function zpullEvent(eventType)
- if eventType == nil then
- print("sleep")
- sleep(10^7.5) -- just over 1 year
- else
- print("else")
- event,param1,param2,param3,param4,param5 = os.pullEvent(eventType)
- end
- return
- end
- local function dummy(eve)
- return "hi"
- end
- for i=1,5 do
- funcList[i] = dummy
- end
- --parallel.waitForAny(zpullEvent,dummy)
- print("ASDAS")
- --parallel.waitForAny(funcList[1](arg[1]),funcList[2](arg[2]),funcList[3](arg[3]),funcList[4](arg[4]),funcList[5](arg[5]))
- return event,param1,param2,param3,param4,param5
- end
- function cleanInput(curr_message,...)
- local event,param1,param2,param3,param4,param5
- local function char()
- event,param1,param2,param3,param4,param5 = os.pullEventRaw("char")
- end
- local function key()
- event,param1,param2,param3,param4,param5 = os.pullEventRaw("key")
- sleep(0) -- needed to make 'char' events win over 'key'
- end
- term.write(curr_message)
- parallel.waitForAny(key,char)
- return event,param1,param2,param3,param4,param5
- end
- function getCleanInputAt(x,y,...)
- local maxLen = 40
- if arg[1] ~= nil then maxLen = arg[1] end
- local message = ""
- while true do
- term.setCursorPos(x,y)
- for i=1,maxLen do
- term.write(" ")
- end
- term.setCursorPos(x,y)
- local event,p1,p2 = cleanInput(message)
- if event == "char" then
- if string.len(message) < maxLen then
- term.write(p1)
- message = message..p1
- os.queueEvent("typing",message,string.len(message))
- end
- elseif event == "key" then
- if p1 == 14 then --backspace
- message = string.sub(message,0,string.len(message)-1)
- os.queueEvent("typing",message,string.len(message))
- elseif p1 == 28 then -- enter
- break
- end
- end
- end
- return message
- end
- function textSearch(x,y,drawFunc,searchFunc)
- local notDone = true
- local final = ""
- local itemList = {}
- local totalFound = 0
- local function messageListener()
- while notDone do
- local event, message, len = os.pullEventRaw("typing")
- final = message
- if len>0 then
- itemList,totalFound = searchFunc(message)
- drawFunc(itemList,totalFound)
- else
- drawFunc(itemList,0)
- end
- end
- end
- local function getText()
- getCleanInputAt(x,y)
- notDone = false
- end
- parallel.waitForAll(messageListener,getText)
- return final,totalFound
- end
- --remove
- function draw1(list,total)
- local maxID = 12
- for l=0,40 do
- term.setCursorPos(1,9+l)
- term.clearLine()
- --term.write(" ")
- end
- if total < 1 then
- sleep(0)
- else
- local iter =0
- for i=1,40 do
- if list[i] ~= nil then
- for k,v in pairs(list[i]) do
- term.setCursorPos(1,9+iter)
- local parsedID = k
- if string.match(k,":0") then
- parsedID = string.match(k,"(%d+):0")
- end
- term.write(parsedID)
- term.setCursorPos(maxID+1,9+iter)
- term.write(v)
- iter=iter+1
- end
- end
- if iter > 40 then break end
- end
- end
- end
- --Converts a MiscPeripherals item # to a Item:damage value format
- function numToDamage(...)
- local damage = 0
- local num = arg[1]
- while num > 2^15 do
- num = num - 2^15
- damage = damage+1
- end
- return num..":"..damage
- end
- --Converts a Item:damage labeled item to MiscPeripherals item format
- function damageToNum(...)
- local damage = tonumber(string.match(arg[1],":(%d+)"))
- local num = tonumber(string.match(arg[1],"(%d+):"))
- return num+(2^15)*damage
- end
- -- Call to fully copy a table (table1=table2 will write changes to table1 back onto table2, deepcopy bypasses this)
- function deepcopy(orig)
- local orig_type = type(orig)
- local copy
- if orig_type == 'table' then
- copy = {}
- for orig_key, orig_value in next, orig, nil do
- copy[deepcopy(orig_key)] = deepcopy(orig_value)
- end
- setmetatable(copy, deepcopy(getmetatable(orig)))
- else -- number, string, boolean, etc
- copy = orig
- end
- return copy
- end
- function enc(msg, key, ...)
- if not encryptData or arg[1] then return msg end
- math.randomseed(key)
- local out = ""
- for i = 1,#msg do
- local c = msg:sub(i,i)
- local code = CharToCode[c]
- code = code + math.random(0,#ValidChars)
- if code > #ValidChars then
- code = code - #ValidChars
- end
- out = out..CodeToChar[code]
- end
- return out
- end
- function dec(msg, key, ...)
- if not encryptData or arg[1] then return msg end
- math.randomseed(key)
- local out = ""
- for i = 1,#msg do
- local c = msg:sub(i,i)
- local code = CharToCode[c]
- code = code - math.random(0,#ValidChars)
- if code < 1 then
- code = #ValidChars + code
- end
- out = out..CodeToChar[code]
- end
- return out
- end
- function antiHack()
- print("[AntiHack] Enabled")
- while true do
- local event,side = os.pullEventRaw()
- if event == "peripheral" then
- if peripheral.getType(side) == "drive" then
- -- we got someone putting a drive next to the terminal
- elseif peripheral.getType(side) == "computer" then
- end
- elseif event == "disk" then
- if fs.exists("disk/startup") then
- fs.delete("disk/startup")
- disk.setLabel(side,"Broken Floppy")
- end
- end
- end
- end
- ----------------
- -----Saving-----
- ----------------
- function addItem(player,ID,amount)
- if player == nil then player = "UnclaimedItems" end
- if playerData[player] == nil then
- playerData[player] = deepcopy(player_temp)
- end
- if playerData[player]["items"] == nil then
- local temp = {}
- temp[ID] = amount
- playerData[player]["items"] = temp
- else
- if playerData[player]["items"][ID] == nil or playerData[player]["items"][ID] == 0 then
- playerData[player]["items"][ID] = amount
- else
- playerData[player]["items"][ID] = amount + playerData[player]["items"][ID]
- end
- end
- if playerData[player]["items"][ID] == 0 then
- playerData[player]["items"][ID] = nil
- end
- end
- function removeItem(player,ID,amount)
- if playerData[player] == nil or playerData[player]["items"] == nil then
- error("attempted to pull items that a player does not have! P:"..player.." id:"..ID.." a:"..amount)
- elseif playerData[player]["items"][ID] < amount then
- amount = playerData[player]["items"][ID]
- end
- addItem(player,ID,-1*amount)
- end
- function saveTrade(player,ID,amount,ID2,amount2)
- --TODO
- error("Why you callign this? it doesnt work yet")
- end
- function saveToFile(player)
- if player == nil or player == "" then player = "UnclaimedItems" end
- local file = fs.open(DataDir.."/"..enc(player,enccode)..".data","w")
- local function w(txt)
- file.write(enc(txt,enccode))
- file.write("\n")
- end
- if playerData[player]["items"] ~= nil then
- for k,v in pairs(playerData[player]["items"]) do
- if v >0 then
- w("i:"..k.." "..v)
- end
- end
- elseif playerData[player]["trades"] ~= nil then
- for k,v in pairs(playerData[player]["trades"]) do
- w("t:"..k.." "..v)
- end
- end
- file:close()
- os.queueEvent("updateDisplay")
- end
- --all playerInfo should always be loaded.
- function loadAll()
- local tab = fs.list(DataDir)
- for k,v in pairs(tab) do
- if string.match(v,"%S+%.data") then
- local player = dec(string.match(v,"(%S+)%.data"),enccode)
- local file = io.open(DataDir.."/"..v)
- local data_temp = {}
- for line in file:lines() do
- local dec_line = dec(line,enccode)
- if string.match(dec_line,"i:%d+:%d+") then
- local item = string.match(dec_line,"i:(%d+:%d+)%s%d+")
- local quanity = tonumber(string.match(dec_line,"i:%d+:%d+%s(%d+)"))
- local temp = {}
- if data_temp["items"] ~= nil then
- temp = data_temp["items"]
- end
- temp[item] = quanity
- data_temp["items"] = temp
- end
- if string.match(dec_line,"t:%d+:%d+") then
- local item = string.match(dec_line,"i:(%d+:%d+)%s%d+")
- local quanity = tonumber(string.match(dec_line,"i:%d+:%d+%s(%d+)"))
- local temp = {}
- if data_temp["trades"] ~= nil then
- temp = trade_temp["trades"]
- end
- temp[item] = quanity
- data_temp["trades"] = temp
- end
- end
- playerData[player] = data_temp
- end
- end
- end
- ----------------
- ------program---
- ----------------
- function sensePlayers()
- while true do
- local str = os.pullEventRaw("player")
- curr_player = string.match(str,"player(%S+)")
- print(curr_player)
- print(str)
- toggleDumping(false)
- sleep(3)
- curr_player = nil
- toggleDumping(true)
- end
- end
- --returns nil if tab1 = tab2 and returns the difference, Only compares k/v of table 1 to table2, and not vice versa.
- function compareAETables(tab1,tab2)
- local temp_tab = {}
- local ret = true
- if tab1 == nil then
- return true,{}
- end
- for k,v in pairs(tab1) do
- if tab2[k] == nil then
- temp_tab[k] = v
- ret = false
- elseif tab2[k] < v then
- temp_tab[k] = v - tab2[k]
- ret = false
- elseif tab2[k] > v then
- temp_tab[k] = tab2[k] - v
- ret = false
- end
- end
- return ret,temp_tab
- end
- function senseItems(...)
- local curr_player_temp = false
- function UpdateItems(player)
- local AE_Data_temp = modem.callRemote(meBridge, "listItems")
- local bool,AE_Data_new = compareAETables(AE_Data_temp,AE_Data)
- if bool then
- sleep(tickRate)
- else
- -- we have items coming into the network
- AE_Data = deepcopy(AE_Data_temp)
- for k,v in pairs(AE_Data_new) do
- addItem(player,numToDamage(k),v)
- end
- saveToFile(player)
- end
- end
- if #arg>0 then
- UpdateItems(arg[1])
- else
- while true do
- if curr_player and not dumping then
- curr_player_temp = curr_player
- UpdateItems(curr_player)
- elseif dumping then
- local _ = os.pullEventRaw("DumpingDone")
- sleep(tickRate)
- elseif curr_player_temp and not dumping then
- sleep(tickRate)
- UpdateItems(curr_player_temp)
- else
- sleep(tickRate)
- --error("should never get here") --remove
- end
- end
- end
- end
- -- Pulls [amount] of [ID:dam] from [player]'s cache
- function retrieveAE(player,ID,amount)
- --checks to ensure player owns item.
- local list = getAllOwnedBy(player)
- if list[ID] == nil then -- or amount > list[ID]
- return false
- else
- modem.callRemote(meBridge, "retrieve",damageToNum(ID),amount,MeBrideOutputDirection)
- removeItem(player,ID,amount)
- end
- AE_Data = modem.callRemote(meBridge, "listItems")
- return true
- end
- --Safe way ti call the list of items belonging to [player] without crashing. If they don't exist, it creates a blank table.
- function getAllOwnedBy(player)
- local tab = {}
- if player == nil then player = "UnclaimedItems" end
- if playerData[player] == nil then
- playerData[player] = deepcopy(player_temp)
- end
- if playerData[player]["items"] == nil then
- playerData[player]["items"] = {}
- end
- return deepcopy(playerData[player]["items"] )
- end
- -- function to call when items are going to be extracted from the network.
- function toggleDumping(bool)
- if bool then
- rs.setBundledOutput(cableFace,colors["IOport"])
- dumping = true
- sleep(0)
- elseif not bool then
- if curr_player ~= curr_player_temp and curr_player_temp ~= nil then --We may have items still in the chest, suck them as the player who left.
- local instasave = curr_player
- print("ITS DOING ITS JOB "..curr_player_temp)
- dumping = false
- rs.setBundledOutput(cableFace,0)
- sleep(1)
- senseItems(instasave)
- os.queueEvent("DumpingDone")
- sleep(0)
- end
- dumping = false
- os.queueEvent("DumpingDone")
- sleep(0)
- rs.setBundledOutput(cableFace,0)
- else
- error("never get here")
- end
- return true
- end
- function testSearch()
- while true do
- local func = draw1
- textSearch(1,7,func,ItemDB.search)
- sleep(2)
- end
- end
- --saveItem("NuAoA","1:0",55)
- --saveToFile("NuAoA")
- function commandHelp(_,player)
- for i = 1,#help do
- Chat.addBuffer("all",help[i])
- end
- end
- function printMes(_,player)
- print(_)
- end
- function test()
- Chat.registerCommand("help",commandHelp)
- Chat.registerCommand("Help",commandHelp)
- Chat.registerCommand("h",commandHelp)
- Chat.registerCommand("H",commandHelp)
- Chat.registerCommand("s",textSearch2)
- Chat.rc("ok",printMes)
- Chat.rc("trade",textTrade)
- Chat.rc("t",textTrade)
- end
- function nullScreen()
- local t_c_clear,t_t_clear = Draw.emptyScreen(mon)
- Draw.draw(mon,t_c_clear,1,1,2^15,t_t_clear)
- return
- end
- function monitor()
- local selection = 1
- local debug_updates = 1
- local page = 1
- local t_c,t_t = Draw.emptyScreen(mon)
- local function drawScreen(selection)
- t_c,t_t = Draw.emptyScreen(mon)
- if curr_player then
- local player_items,total = getItemTables(curr_player)
- Draw.tc(t_c,1,1,16,#t_c[1],#t_c)
- Draw.tc(t_c,2,2,4096,27,1+3*5)
- if dumping then
- Draw.tc(t_c,29,8,1024,#t_c[1]-1,13)
- Draw.tt(t_t,30,10," Import")
- else
- Draw.tc(t_c,29,8,512,#t_c[1]-1,13)
- Draw.tt(t_t,30,10," Export")
- end
- Draw.tt(t_t,30,11," All")
- if math.floor(selection/5) > 0 then
- Draw.tt(t_t,5,#t_c-1,"Page "..(math.floor(selection/5)))
- Draw.tc(t_c,3,#t_c-2,512,13,#t_c)
- end
- if selection+5<=total then
- Draw.tt(t_t,18,#t_c-1,"Page "..(math.floor(selection/5)+2))
- Draw.tc(t_c,16,#t_c-2,512,26,#t_c)
- end
- Draw.tt(t_t,28,2," Welcome",2^15)
- Draw.tt(t_t,34-math.floor(#curr_player)/2,3,curr_player,2^15)
- Draw.draw(mon,t_c,1,1,2^15,t_t)
- local iter = 1
- for k,v in pairs(player_items) do
- if iter >= selection and iter< selection+5 then
- Draw.draw(mon,player_items[iter]["color"],2,3*(iter-1)-3*(selection)+5,2^15,player_items[iter]["text"])
- end
- iter=iter+1
- end
- else
- nullScreen()
- end
- end
- while true do
- local event, button, xpos, ypos
- local function touch()
- event, button, xpos, ypos = os.pullEvent("updateDisplay")
- end
- local function update()
- event, button, xpos, ypos = os.pullEvent("monitor_touch")
- end
- parallel.waitForAny(touch,update)
- if event == "updateDisplay" then
- drawScreen(selection)
- if button ~= nil and button then
- selection = 1
- end
- elseif event == "monitor_touch" and curr_player then
- local player_items,total = getItemTables(curr_player)
- if ypos>=#t_c-2 and ypos<=#t_c and xpos >= 3 and xpos <=13 and math.floor(selection/5) > 0 then
- --pageback
- if selection < 5 then
- selection = 1+math.floor(total/5)
- else
- selection = selection-5
- end
- elseif ypos>=#t_c-2 and ypos<=#t_c and xpos >= 16 and xpos <=26 and selection+5<=total then
- --pageforward
- if selection+5>total then
- selection = 1
- else
- selection = selection+5
- end
- elseif ypos>=2 and ypos<=#t_c-3 and xpos >= 2 and xpos <=27 then
- for i=5,9 do
- if ypos <= 1+(i-4)*3 then
- toggleDumping(true)
- retrieveAE(curr_player,player_items[(i-4)+selection-1]["ID"],64)
- sleep(0)
- saveToFile(curr_player)
- print(player_items[(i-4)+selection-1]["ID"])
- break
- end
- end
- elseif ypos>=8 and ypos<=13 and xpos >= 29 and xpos <=#t_c[1]-1 then
- if not dumping then
- toggleDumping(true)
- local temp2 = getAllOwnedBy(curr_player)
- local count = 0
- for k,v in pairs(temp2) do
- sleep(0)
- local _ = retrieveAE(curr_player,k,v)
- count = count +1
- end
- saveToFile(curr_player)
- else
- toggleDumping(false)
- end
- end
- --print(selection)
- drawScreen(selection)
- else
- --error("problem!") gets here on terminate command
- end
- debug_updates=debug_updates+1
- end
- end
- --returns a table of all items avabile for player formatted for a screen. table structure is tab[#]["text"/"color"] = drawable table.
- function getItemTables(player)
- local tab = {}
- local tab_2 = {}
- local player_tab = getAllOwnedBy(player)
- local toggle = true
- local bg_c = 128
- local iter = 1
- for id,amount in pairs(player_tab) do
- local item_c,item_t = Draw.emptyScreen(mon,25,3)
- if toggle then
- bg_c = bg_c*2
- else
- bg_c = bg_c/2
- end
- toggle = not toggle
- local str = ItemDB.getItemName(id)
- if #str > 25 then
- str = string.sub(str,0,23)..".."
- end
- Draw.tc(item_c,1,1,bg_c,#item_c[1],#item_c)
- Draw.tt(item_t,1,2, string.sub(" &e(x"..amount..") ",0,11).."&9ID:"..id,512)
- Draw.tt(item_t,1,1,str,2^14)
- tab_2["text"] = item_t
- tab_2["color"] = item_c
- tab_2["ID"] = id
- tab[iter] = deepcopy(tab_2)
- iter=iter+1
- end
- return tab,iter-1
- end
- function testzxz()
- sleep(5)
- while true do
- if curr_player then
- toggleDumping(true)
- local list = getAllOwnedBy(curr_player)
- for k,v in pairs(list) do
- retrieveAE(curr_player,k,v)
- sleep(0)
- end
- saveToFile(curr_player)
- sleep(10)
- toggleDumping(false)
- end
- sleep(10)
- end
- end
- -----------------------
- -----ChatCommands------
- -----------------------
- function textSearch2(command,player)
- local str = string.match(command,"%S%s(.+)")
- --Chat.addBuffer(player,"Searching your items for ID/name: '"..str.."'")
- local id, count = ItemDB.search(str,true,playerData[player]["items"])
- if id then
- Chat.addBuffer(player,tag.." Closest match in your storage is: ["..id.."] "..ItemDB.getItemName(id).."(x"..playerData[player]["items"][id]..")")
- else
- Chat.addBuffer(player,tag.." No matches found for '"..str.."'")
- end
- end
- -- "cc trade" # item1 to player2 for # item2
- function textTrade(command,player)
- local syntax = tag.." Incorect Syntax. Please follow: trade [#] [Item/ID] to [player] for [#] [Item/ID]"
- command = string.match(command,"%S%s(.+)")
- if command == nil then
- Chat.addBuffer(player,syntax)
- end
- --old string.match(command,"trade (%d+)%s.+%sto%S+%sfor%s%d+%s.+")
- local player2 = string.match(command,"%d+ .+ to (%S+) for %d+ .+")
- local amount1 = tonumber(string.match(command,"(%d+) .+ to %S+ for %d+ .+"))
- local item1 = string.match(command,"%d+ (.+) to %S+ for %d+ .+")
- local amount2 = tonumber(string.match(command,"%d+ .+ to %S+ for (%d+) .+"))
- local item2 = string.match(command,"%d+ .+ to %S+ for %d+ (.+)")
- if player2 == nil or amount1 ==nil or amount2 == nil or item2 == nil then
- Chat.addBuffer(player,syntax)
- return
- end
- local id1,_ = ItemDB.search(item1,true,playerData[player]["items"])
- if id1 then
- if playerData[player]["items"][id1] < amount1 then
- Chat.addBuffer(player,tag.." Trade Failed. Not enough items to complete trade: ["..id1.."] "..ItemDB.getItemName(id1).."(x"..playerData[player]["items"][id1]..") needs: "..amount1)
- return
- end
- else
- Chat.addBuffer(player,tag.." Trade Failed. No item/id found for term '"..item1.."'")
- return
- end
- local id2 = ItemDB.search(item2,true)
- if not id2 then
- Chat.addBuffer(player,tag.." Trade Failed. No item/id found for term '"..item2.."'")
- return
- end
- -- sucess?
- chatHelper[player] = constructTrade(player2,id1,amount1,id2,amount2)
- Chat.addBuffer(player,tag.." Confirm Trade: "..amount1.." ["..id1.."] "..ItemDB.getItemName(id1).." to "..player2.." for "..amount2.." ["..id2.."] "..ItemDB.getItemName(id2))
- Chat.addBuffer(player,tag.." Type 'cc yes' to confirm.")
- end
- function constructTrade(player2,id1,amount1,id2,amount2)
- local ch_temp = {}
- local ch_temp2 = {}
- ch_temp2[id1] = amount1
- ch_temp["give"] = deepcopy(ch_temp2)
- ch_temp2 = {}
- ch_temp2[id2] = amount2
- ch_temp["take"] = deepcopy(ch_temp2)
- ch_temp["player2"] = player2
- ch_temp["type"] = "trade"
- return deepcopy(ch_temp)
- end
- function confirm(command,player)
- if chatHelper[player] == nil then
- return
- end
- if string.match(command,"yes") then
- if chatHelper[player]["type"] == "trade" then
- local player2 = chatHelper[player]["player2"]
- for k,v in pairs(chatHelper[player]["give"]) do
- id1 = k
- amount1 = v
- end
- if playerData[player2] ~= nil and playerData[player2]["items"] ~= nil then
- else
- Chat.addBuffer(player,tag.." "..player2.." doesn't have enough items!")
- return
- end
- local tab_temp = {}
- tab_temp["take"] = chatHelper[player]["give"]
- tab_temp["give"] = chatHelper[player]["take"]
- tab_temp["player2"] = player
- tab_temp["type"] = "atrade"
- chatHelper[player2] = deepcopy(tab_temp)
- elseif chatHelper[player]["type"] == "atrade" then
- end
- else
- if chatHelper[player]["type"] == "trade" then
- Chat.addBuffer(player,tag.." Trade Canceled.")
- end
- end
- end
- -----------------------
- --------startup--------
- -----------------------
- function run()
- parallel.waitForAny(antiHack,setup)
- parallel.waitForAll(antiHack,senseItems,sensePlayers,test,Chat.run,monitor)
- end
- local status,err = pcall(run)
- if not status then
- print("Error was caught. restarting..")
- print(err)
- io.read()
- Debug(err)
- end
- os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement