Advertisement
NuAoA

Untitled

Aug 17th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.42 KB | None | 0 0
  1. --Amigo 0.11
  2. --NuAoA
  3.  
  4.  
  5. ------------------
  6. -----Settings-----
  7. ------------------
  8. local DataDir = "Data" -- Directory to store all playerdata
  9. local DEBUG = true -- Debug code
  10. local tickRate = 0 -- Update frequency on time dependent functions (ie, when scanning for players)
  11. local encryptData = false -- Toggle basic encryption of playerdata (player.data files)
  12. local MeBrideOutputDirection = 1 --0 for down, 1 for up, 2 for -Z, 3 for +Z, 4 for -X and 5 for +X.
  13. local cableFace = "right" -- The face that the RP or MF bundled cable is attached to.
  14. local command_prefix = "cc " -- Command prefex for chat commands.
  15. local tag = "§d[CC] " --Prefix tag for outgoing messages.
  16. local colors = {["IOport"] = 1,["ActivePlayer"]=2}
  17.  
  18. local AdminList = {
  19. "player1";
  20. "player2";
  21. }
  22.  
  23.  
  24.  
  25. ------------------
  26. ----Local Var-----
  27. ------------------
  28. local modem
  29. local meBridge
  30. local chatBox
  31. --local MySensor
  32. local mon
  33. local commandblock = false
  34. local enccode = os.getComputerID()
  35. local curr_player = false
  36. local playerData = {}
  37. local chatHelper = {}
  38. local player_temp = {}
  39. player_temp["items"] = {}
  40. player_temp["trades"] = {}
  41. local AE_Data = {}
  42. local hexnums = { [10] = "a", [11] = "b", [12] = "c", [13] = "d", [14] = "e" , [15] = "f" }
  43. local ValidChars = {
  44. "33","#","$","%","&","'","(",")","*","+",",","-",".","3","0","1","2","3",
  45. "4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","E","F","G",
  46. "H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[",
  47. "k(","]","^","_","`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o",
  48. "p","q","r","s","t","u","v","w","x","y","z","{","}","~"," "
  49. }
  50. local CharToCode = {}
  51. local CodeToChar = {}
  52. for _, c in pairs(ValidChars) do
  53. local code = #CodeToChar+1
  54. CharToCode[c] = code
  55. CodeToChar[code] = c
  56. end
  57. local tag = "§d[CC] "
  58. local help = {
  59. "§2------CC Trade Help menu-----";
  60. "§2|".."§b 'cc help' §fwill list commands ".."§2|";
  61. "§2|".."§b §f ".."§2|";
  62. "§2|".."§b §f ".."§2|";
  63. "§2-------&2--------------------";
  64. }
  65. ------------------
  66. -----Setup--------
  67. ------------------
  68. function setup()
  69. os.pullEvent = os.pullEventRaw
  70. if not fs.exists("wrap") then
  71. print("Wrap API not found, downloading from http://pastebin.com/g5tBarus")
  72. shell.run("pastebin get g5tBarus wrap")
  73. end
  74. if not fs.exists("ItemDB") then
  75. print("ItemDB API not found, downloading from http://pastebin.com/yjdEKRYY")
  76. shell.run("pastebin get yjdEKRYY ItemDB")
  77. end
  78. if not fs.exists("Draw") then
  79. print("ItemDB API not found, downloading from http://pastebin.com/3sAGiX7j")
  80. shell.run("pastebin get 3sAGiX7j Draw")
  81. end
  82. if not fs.exists("Chat") then
  83. print("ItemDB API not found, downloading from http://pastebin.com/i2s5Sr3N")
  84. shell.run("pastebin get i2s5Sr3N Chat")
  85. end
  86. if not fs.exists("wrap") or not fs.exists("Chat") or not fs.exists("Draw") or not fs.exists("ItemDB") then
  87. error("API's failed to download, maybe you need to enable HTTP api?")
  88. end
  89. os.loadAPI("wrap")
  90. os.loadAPI("ItemDB")
  91. os.loadAPI("ocs/apis/sensor")
  92. os.loadAPI("Chat")
  93. os.loadAPI("Draw")
  94. modem = wrap.attach("modem")
  95. meBridge = wrap.getRemoteName("meBridge")
  96. player = wrap.attach("playerDetector")
  97. --MySensor = sensor.wrap("top")
  98. --chatbox = wrap.getRemoteName("command")
  99. mon = wrap.getRemoteName("monitor")
  100. --[[
  101. if chatbox ~= nil then
  102. commandblock = true
  103. else
  104. print("Command block is not required")
  105. chatbox = wrap.getRemoteName("chat")
  106. end
  107. ]]--
  108. if modem == nil or meBridge == nil or MySensor == nil or mon == nil then
  109. print("Missing Peripherals!")
  110. --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.")
  111. end
  112. if not fs.isDir(DataDir) then
  113. fs.makeDir(DataDir)
  114. end
  115.  
  116. AE_Data = modem.callRemote(meBridge, "listItems")
  117. loadAll()
  118. Chat.setup(command_prefix,tag)
  119. os.queueEvent("setupDone")
  120. end
  121.  
  122.  
  123. -----------------
  124. ------Util-------
  125. -----------------
  126. if DEBUG and not fs.exists("debug") then
  127. local file = fs.open("debug","w")
  128. file.write("Amigo debug file \n")
  129. file:close()
  130. end
  131.  
  132. function Debug(text)
  133. if DEBUG then
  134. local file = fs.open("debug","r")
  135. local curr = file.readAll()
  136. file:close()
  137. file = fs.open("debug","w")
  138. file.write(curr.."\n"..tostring(text))
  139. file:close()
  140. end
  141. end
  142.  
  143.  
  144.  
  145. -- returns the event and parameters of up to 5 events passed to it. [borked]
  146. function returnEvent(...)
  147. local event,param1,param2,param3,param4,param5
  148. local funcList = {}
  149. local function zpullEvent(eventType)
  150. if eventType == nil then
  151. print("sleep")
  152. sleep(10^7.5) -- just over 1 year
  153. else
  154. print("else")
  155. event,param1,param2,param3,param4,param5 = os.pullEvent(eventType)
  156. end
  157. return
  158. end
  159. local function dummy(eve)
  160. return "hi"
  161. end
  162. for i=1,5 do
  163. funcList[i] = dummy
  164. end
  165. --parallel.waitForAny(zpullEvent,dummy)
  166. print("ASDAS")
  167. --parallel.waitForAny(funcList[1](arg[1]),funcList[2](arg[2]),funcList[3](arg[3]),funcList[4](arg[4]),funcList[5](arg[5]))
  168. return event,param1,param2,param3,param4,param5
  169. end
  170.  
  171. function cleanInput(curr_message,...)
  172.  
  173. local event,param1,param2,param3,param4,param5
  174. local function char()
  175. event,param1,param2,param3,param4,param5 = os.pullEventRaw("char")
  176.  
  177. end
  178. local function key()
  179. event,param1,param2,param3,param4,param5 = os.pullEventRaw("key")
  180. sleep(0) -- needed to make 'char' events win over 'key'
  181. end
  182. term.write(curr_message)
  183. parallel.waitForAny(key,char)
  184. return event,param1,param2,param3,param4,param5
  185. end
  186.  
  187. function getCleanInputAt(x,y,...)
  188. local maxLen = 40
  189. if arg[1] ~= nil then maxLen = arg[1] end
  190. local message = ""
  191. while true do
  192. term.setCursorPos(x,y)
  193. for i=1,maxLen do
  194. term.write(" ")
  195. end
  196. term.setCursorPos(x,y)
  197. local event,p1,p2 = cleanInput(message)
  198. if event == "char" then
  199. if string.len(message) < maxLen then
  200. term.write(p1)
  201. message = message..p1
  202. os.queueEvent("typing",message,string.len(message))
  203. end
  204. elseif event == "key" then
  205. if p1 == 14 then --backspace
  206. message = string.sub(message,0,string.len(message)-1)
  207. os.queueEvent("typing",message,string.len(message))
  208. elseif p1 == 28 then -- enter
  209. break
  210. end
  211. end
  212. end
  213. return message
  214. end
  215.  
  216. function textSearch(x,y,drawFunc,searchFunc)
  217. local notDone = true
  218. local final = ""
  219. local itemList = {}
  220. local totalFound = 0
  221. local function messageListener()
  222. while notDone do
  223. local event, message, len = os.pullEventRaw("typing")
  224. final = message
  225. if len>0 then
  226. itemList,totalFound = searchFunc(message)
  227. drawFunc(itemList,totalFound)
  228. else
  229. drawFunc(itemList,0)
  230. end
  231. end
  232. end
  233. local function getText()
  234. getCleanInputAt(x,y)
  235. notDone = false
  236. end
  237. parallel.waitForAll(messageListener,getText)
  238. return final,totalFound
  239. end
  240.  
  241. --remove
  242. function draw1(list,total)
  243. local maxID = 12
  244. for l=0,40 do
  245. term.setCursorPos(1,9+l)
  246. term.clearLine()
  247. --term.write(" ")
  248. end
  249. if total < 1 then
  250. sleep(0)
  251. else
  252. local iter =0
  253. for i=1,40 do
  254. if list[i] ~= nil then
  255. for k,v in pairs(list[i]) do
  256. term.setCursorPos(1,9+iter)
  257. local parsedID = k
  258. if string.match(k,":0") then
  259. parsedID = string.match(k,"(%d+):0")
  260. end
  261. term.write(parsedID)
  262. term.setCursorPos(maxID+1,9+iter)
  263. term.write(v)
  264. iter=iter+1
  265. end
  266. end
  267. if iter > 40 then break end
  268. end
  269. end
  270. end
  271.  
  272. --Converts a MiscPeripherals item # to a Item:damage value format
  273. function numToDamage(...)
  274. local damage = 0
  275. local num = arg[1]
  276. while num > 2^15 do
  277. num = num - 2^15
  278. damage = damage+1
  279. end
  280. return num..":"..damage
  281. end
  282.  
  283. --Converts a Item:damage labeled item to MiscPeripherals item format
  284. function damageToNum(...)
  285. local damage = tonumber(string.match(arg[1],":(%d+)"))
  286. local num = tonumber(string.match(arg[1],"(%d+):"))
  287. return num+(2^15)*damage
  288. end
  289.  
  290. -- Call to fully copy a table (table1=table2 will write changes to table1 back onto table2, deepcopy bypasses this)
  291. function deepcopy(orig)
  292. local orig_type = type(orig)
  293. local copy
  294. if orig_type == 'table' then
  295. copy = {}
  296. for orig_key, orig_value in next, orig, nil do
  297. copy[deepcopy(orig_key)] = deepcopy(orig_value)
  298. end
  299. setmetatable(copy, deepcopy(getmetatable(orig)))
  300. else -- number, string, boolean, etc
  301. copy = orig
  302. end
  303. return copy
  304. end
  305.  
  306. function enc(msg, key, ...)
  307. if not encryptData or arg[1] then return msg end
  308. math.randomseed(key)
  309. local out = ""
  310. for i = 1,#msg do
  311. local c = msg:sub(i,i)
  312. local code = CharToCode[c]
  313. code = code + math.random(0,#ValidChars)
  314. if code > #ValidChars then
  315. code = code - #ValidChars
  316. end
  317. out = out..CodeToChar[code]
  318. end
  319. return out
  320. end
  321.  
  322. function dec(msg, key, ...)
  323. if not encryptData or arg[1] then return msg end
  324. math.randomseed(key)
  325. local out = ""
  326. for i = 1,#msg do
  327. local c = msg:sub(i,i)
  328. local code = CharToCode[c]
  329. code = code - math.random(0,#ValidChars)
  330. if code < 1 then
  331. code = #ValidChars + code
  332. end
  333. out = out..CodeToChar[code]
  334. end
  335. return out
  336. end
  337.  
  338. function antiHack()
  339. print("[AntiHack] Enabled")
  340. while true do
  341. local event,side = os.pullEventRaw()
  342. if event == "peripheral" then
  343. if peripheral.getType(side) == "drive" then
  344. -- we got someone putting a drive next to the terminal
  345. elseif peripheral.getType(side) == "computer" then
  346.  
  347. end
  348. elseif event == "disk" then
  349. if fs.exists("disk/startup") then
  350. fs.delete("disk/startup")
  351. disk.setLabel(side,"Broken Floppy")
  352. end
  353. end
  354. end
  355. end
  356.  
  357.  
  358. ----------------
  359. -----Saving-----
  360. ----------------
  361. function addItem(player,ID,amount)
  362. if player == nil then player = "UnclaimedItems" end
  363. if playerData[player] == nil then
  364. playerData[player] = deepcopy(player_temp)
  365. end
  366. if playerData[player]["items"] == nil then
  367. local temp = {}
  368. temp[ID] = amount
  369. playerData[player]["items"] = temp
  370. else
  371. if playerData[player]["items"][ID] == nil or playerData[player]["items"][ID] == 0 then
  372. playerData[player]["items"][ID] = amount
  373. else
  374. playerData[player]["items"][ID] = amount + playerData[player]["items"][ID]
  375. end
  376. end
  377. if playerData[player]["items"][ID] == 0 then
  378. playerData[player]["items"][ID] = nil
  379. end
  380. end
  381.  
  382. function removeItem(player,ID,amount)
  383. if playerData[player] == nil or playerData[player]["items"] == nil then
  384. error("attempted to pull items that a player does not have! P:"..player.." id:"..ID.." a:"..amount)
  385. elseif playerData[player]["items"][ID] < amount then
  386. amount = playerData[player]["items"][ID]
  387. end
  388. addItem(player,ID,-1*amount)
  389. end
  390.  
  391. function saveTrade(player,ID,amount,ID2,amount2)
  392. --TODO
  393. error("Why you callign this? it doesnt work yet")
  394. end
  395.  
  396. function saveToFile(player)
  397. if player == nil or player == "" then player = "UnclaimedItems" end
  398. local file = fs.open(DataDir.."/"..enc(player,enccode)..".data","w")
  399. local function w(txt)
  400. file.write(enc(txt,enccode))
  401. file.write("\n")
  402. end
  403. if playerData[player]["items"] ~= nil then
  404. for k,v in pairs(playerData[player]["items"]) do
  405. if v >0 then
  406. w("i:"..k.." "..v)
  407. end
  408. end
  409. elseif playerData[player]["trades"] ~= nil then
  410. for k,v in pairs(playerData[player]["trades"]) do
  411. w("t:"..k.." "..v)
  412. end
  413. end
  414. file:close()
  415. os.queueEvent("updateDisplay")
  416. end
  417.  
  418. --all playerInfo should always be loaded.
  419. function loadAll()
  420. local tab = fs.list(DataDir)
  421. for k,v in pairs(tab) do
  422. if string.match(v,"%S+%.data") then
  423. local player = dec(string.match(v,"(%S+)%.data"),enccode)
  424. local file = io.open(DataDir.."/"..v)
  425. local data_temp = {}
  426. for line in file:lines() do
  427. local dec_line = dec(line,enccode)
  428. if string.match(dec_line,"i:%d+:%d+") then
  429. local item = string.match(dec_line,"i:(%d+:%d+)%s%d+")
  430. local quanity = tonumber(string.match(dec_line,"i:%d+:%d+%s(%d+)"))
  431. local temp = {}
  432. if data_temp["items"] ~= nil then
  433. temp = data_temp["items"]
  434. end
  435. temp[item] = quanity
  436. data_temp["items"] = temp
  437. end
  438. if string.match(dec_line,"t:%d+:%d+") then
  439. local item = string.match(dec_line,"i:(%d+:%d+)%s%d+")
  440. local quanity = tonumber(string.match(dec_line,"i:%d+:%d+%s(%d+)"))
  441. local temp = {}
  442. if data_temp["trades"] ~= nil then
  443. temp = trade_temp["trades"]
  444. end
  445. temp[item] = quanity
  446. data_temp["trades"] = temp
  447. end
  448. end
  449. playerData[player] = data_temp
  450. end
  451. end
  452.  
  453. end
  454. ----------------
  455. ------program---
  456. ----------------
  457.  
  458. function sensePlayers()
  459. while true do
  460. local str = os.pullEventRaw("player")
  461.  
  462. curr_player = string.match(str,"player(%S+)")
  463. print(curr_player)
  464. print(str)
  465. toggleDumping(false)
  466. sleep(3)
  467. curr_player = nil
  468. toggleDumping(true)
  469.  
  470. end
  471. end
  472.  
  473. --returns nil if tab1 = tab2 and returns the difference, Only compares k/v of table 1 to table2, and not vice versa.
  474. function compareAETables(tab1,tab2)
  475. local temp_tab = {}
  476. local ret = true
  477. if tab1 == nil then
  478. return true,{}
  479. end
  480. for k,v in pairs(tab1) do
  481. if tab2[k] == nil then
  482. temp_tab[k] = v
  483. ret = false
  484. elseif tab2[k] < v then
  485. temp_tab[k] = v - tab2[k]
  486. ret = false
  487. elseif tab2[k] > v then
  488. temp_tab[k] = tab2[k] - v
  489. ret = false
  490. end
  491. end
  492. return ret,temp_tab
  493. end
  494.  
  495. function senseItems(...)
  496. local curr_player_temp = false
  497. function UpdateItems(player)
  498. local AE_Data_temp = modem.callRemote(meBridge, "listItems")
  499. local bool,AE_Data_new = compareAETables(AE_Data_temp,AE_Data)
  500. if bool then
  501. sleep(tickRate)
  502. else
  503. -- we have items coming into the network
  504. AE_Data = deepcopy(AE_Data_temp)
  505. for k,v in pairs(AE_Data_new) do
  506. addItem(player,numToDamage(k),v)
  507. end
  508. saveToFile(player)
  509. end
  510. end
  511. if #arg>0 then
  512. UpdateItems(arg[1])
  513. else
  514. while true do
  515. if curr_player and not dumping then
  516. curr_player_temp = curr_player
  517. UpdateItems(curr_player)
  518. elseif dumping then
  519. local _ = os.pullEventRaw("DumpingDone")
  520. sleep(tickRate)
  521. elseif curr_player_temp and not dumping then
  522. sleep(tickRate)
  523. UpdateItems(curr_player_temp)
  524. else
  525. sleep(tickRate)
  526. --error("should never get here") --remove
  527. end
  528. end
  529. end
  530. end
  531.  
  532. -- Pulls [amount] of [ID:dam] from [player]'s cache
  533. function retrieveAE(player,ID,amount)
  534. --checks to ensure player owns item.
  535. local list = getAllOwnedBy(player)
  536. if list[ID] == nil then -- or amount > list[ID]
  537. return false
  538. else
  539. modem.callRemote(meBridge, "retrieve",damageToNum(ID),amount,MeBrideOutputDirection)
  540. removeItem(player,ID,amount)
  541. end
  542. AE_Data = modem.callRemote(meBridge, "listItems")
  543. return true
  544. end
  545.  
  546. --Safe way ti call the list of items belonging to [player] without crashing. If they don't exist, it creates a blank table.
  547. function getAllOwnedBy(player)
  548. local tab = {}
  549. if player == nil then player = "UnclaimedItems" end
  550. if playerData[player] == nil then
  551. playerData[player] = deepcopy(player_temp)
  552. end
  553. if playerData[player]["items"] == nil then
  554. playerData[player]["items"] = {}
  555. end
  556. return deepcopy(playerData[player]["items"] )
  557. end
  558.  
  559. -- function to call when items are going to be extracted from the network.
  560. function toggleDumping(bool)
  561. if bool then
  562. rs.setBundledOutput(cableFace,colors["IOport"])
  563. dumping = true
  564. sleep(0)
  565. elseif not bool then
  566. 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.
  567. local instasave = curr_player
  568. print("ITS DOING ITS JOB "..curr_player_temp)
  569. dumping = false
  570. rs.setBundledOutput(cableFace,0)
  571. sleep(1)
  572. senseItems(instasave)
  573. os.queueEvent("DumpingDone")
  574. sleep(0)
  575. end
  576. dumping = false
  577. os.queueEvent("DumpingDone")
  578. sleep(0)
  579. rs.setBundledOutput(cableFace,0)
  580.  
  581. else
  582. error("never get here")
  583. end
  584. return true
  585. end
  586.  
  587. function testSearch()
  588. while true do
  589. local func = draw1
  590. textSearch(1,7,func,ItemDB.search)
  591. sleep(2)
  592. end
  593. end
  594.  
  595. --saveItem("NuAoA","1:0",55)
  596. --saveToFile("NuAoA")
  597. function commandHelp(_,player)
  598. for i = 1,#help do
  599. Chat.addBuffer("all",help[i])
  600. end
  601. end
  602. function printMes(_,player)
  603. print(_)
  604. end
  605. function test()
  606. Chat.registerCommand("help",commandHelp)
  607. Chat.registerCommand("Help",commandHelp)
  608. Chat.registerCommand("h",commandHelp)
  609. Chat.registerCommand("H",commandHelp)
  610. Chat.registerCommand("s",textSearch2)
  611. Chat.rc("ok",printMes)
  612. Chat.rc("trade",textTrade)
  613. Chat.rc("t",textTrade)
  614.  
  615. end
  616.  
  617. function nullScreen()
  618. local t_c_clear,t_t_clear = Draw.emptyScreen(mon)
  619. Draw.draw(mon,t_c_clear,1,1,2^15,t_t_clear)
  620. return
  621. end
  622. function monitor()
  623. local selection = 1
  624. local debug_updates = 1
  625. local page = 1
  626. local t_c,t_t = Draw.emptyScreen(mon)
  627. local function drawScreen(selection)
  628. t_c,t_t = Draw.emptyScreen(mon)
  629. if curr_player then
  630. local player_items,total = getItemTables(curr_player)
  631. Draw.tc(t_c,1,1,16,#t_c[1],#t_c)
  632. Draw.tc(t_c,2,2,4096,27,1+3*5)
  633.  
  634. if dumping then
  635. Draw.tc(t_c,29,8,1024,#t_c[1]-1,13)
  636. Draw.tt(t_t,30,10," Import")
  637. else
  638. Draw.tc(t_c,29,8,512,#t_c[1]-1,13)
  639. Draw.tt(t_t,30,10," Export")
  640. end
  641. Draw.tt(t_t,30,11," All")
  642. if math.floor(selection/5) > 0 then
  643. Draw.tt(t_t,5,#t_c-1,"Page "..(math.floor(selection/5)))
  644. Draw.tc(t_c,3,#t_c-2,512,13,#t_c)
  645. end
  646. if selection+5<=total then
  647. Draw.tt(t_t,18,#t_c-1,"Page "..(math.floor(selection/5)+2))
  648. Draw.tc(t_c,16,#t_c-2,512,26,#t_c)
  649. end
  650. Draw.tt(t_t,28,2," Welcome",2^15)
  651. Draw.tt(t_t,34-math.floor(#curr_player)/2,3,curr_player,2^15)
  652.  
  653. Draw.draw(mon,t_c,1,1,2^15,t_t)
  654. local iter = 1
  655. for k,v in pairs(player_items) do
  656. if iter >= selection and iter< selection+5 then
  657. Draw.draw(mon,player_items[iter]["color"],2,3*(iter-1)-3*(selection)+5,2^15,player_items[iter]["text"])
  658. end
  659. iter=iter+1
  660. end
  661. else
  662. nullScreen()
  663. end
  664. end
  665.  
  666.  
  667. while true do
  668. local event, button, xpos, ypos
  669. local function touch()
  670. event, button, xpos, ypos = os.pullEvent("updateDisplay")
  671. end
  672. local function update()
  673. event, button, xpos, ypos = os.pullEvent("monitor_touch")
  674. end
  675. parallel.waitForAny(touch,update)
  676.  
  677. if event == "updateDisplay" then
  678. drawScreen(selection)
  679. if button ~= nil and button then
  680. selection = 1
  681. end
  682. elseif event == "monitor_touch" and curr_player then
  683. local player_items,total = getItemTables(curr_player)
  684. if ypos>=#t_c-2 and ypos<=#t_c and xpos >= 3 and xpos <=13 and math.floor(selection/5) > 0 then
  685. --pageback
  686. if selection < 5 then
  687. selection = 1+math.floor(total/5)
  688. else
  689. selection = selection-5
  690. end
  691. elseif ypos>=#t_c-2 and ypos<=#t_c and xpos >= 16 and xpos <=26 and selection+5<=total then
  692. --pageforward
  693. if selection+5>total then
  694. selection = 1
  695. else
  696. selection = selection+5
  697. end
  698. elseif ypos>=2 and ypos<=#t_c-3 and xpos >= 2 and xpos <=27 then
  699. for i=5,9 do
  700. if ypos <= 1+(i-4)*3 then
  701. toggleDumping(true)
  702. retrieveAE(curr_player,player_items[(i-4)+selection-1]["ID"],64)
  703. sleep(0)
  704. saveToFile(curr_player)
  705. print(player_items[(i-4)+selection-1]["ID"])
  706. break
  707. end
  708. end
  709. elseif ypos>=8 and ypos<=13 and xpos >= 29 and xpos <=#t_c[1]-1 then
  710. if not dumping then
  711. toggleDumping(true)
  712. local temp2 = getAllOwnedBy(curr_player)
  713. local count = 0
  714. for k,v in pairs(temp2) do
  715. sleep(0)
  716. local _ = retrieveAE(curr_player,k,v)
  717. count = count +1
  718.  
  719. end
  720. saveToFile(curr_player)
  721. else
  722. toggleDumping(false)
  723. end
  724. end
  725. --print(selection)
  726. drawScreen(selection)
  727. else
  728. --error("problem!") gets here on terminate command
  729. end
  730. debug_updates=debug_updates+1
  731. end
  732. end
  733. --returns a table of all items avabile for player formatted for a screen. table structure is tab[#]["text"/"color"] = drawable table.
  734. function getItemTables(player)
  735. local tab = {}
  736. local tab_2 = {}
  737. local player_tab = getAllOwnedBy(player)
  738. local toggle = true
  739. local bg_c = 128
  740. local iter = 1
  741. for id,amount in pairs(player_tab) do
  742. local item_c,item_t = Draw.emptyScreen(mon,25,3)
  743. if toggle then
  744. bg_c = bg_c*2
  745. else
  746. bg_c = bg_c/2
  747. end
  748. toggle = not toggle
  749. local str = ItemDB.getItemName(id)
  750. if #str > 25 then
  751. str = string.sub(str,0,23)..".."
  752. end
  753. Draw.tc(item_c,1,1,bg_c,#item_c[1],#item_c)
  754. Draw.tt(item_t,1,2, string.sub(" &e(x"..amount..") ",0,11).."&9ID:"..id,512)
  755. Draw.tt(item_t,1,1,str,2^14)
  756. tab_2["text"] = item_t
  757. tab_2["color"] = item_c
  758. tab_2["ID"] = id
  759. tab[iter] = deepcopy(tab_2)
  760. iter=iter+1
  761. end
  762. return tab,iter-1
  763. end
  764.  
  765.  
  766. function testzxz()
  767. sleep(5)
  768. while true do
  769. if curr_player then
  770. toggleDumping(true)
  771. local list = getAllOwnedBy(curr_player)
  772. for k,v in pairs(list) do
  773. retrieveAE(curr_player,k,v)
  774. sleep(0)
  775. end
  776. saveToFile(curr_player)
  777. sleep(10)
  778. toggleDumping(false)
  779. end
  780. sleep(10)
  781. end
  782. end
  783.  
  784. -----------------------
  785. -----ChatCommands------
  786. -----------------------
  787. function textSearch2(command,player)
  788. local str = string.match(command,"%S%s(.+)")
  789. --Chat.addBuffer(player,"Searching your items for ID/name: '"..str.."'")
  790. local id, count = ItemDB.search(str,true,playerData[player]["items"])
  791. if id then
  792. Chat.addBuffer(player,tag.." Closest match in your storage is: ["..id.."] "..ItemDB.getItemName(id).."(x"..playerData[player]["items"][id]..")")
  793. else
  794. Chat.addBuffer(player,tag.." No matches found for '"..str.."'")
  795. end
  796. end
  797.  
  798. -- "cc trade" # item1 to player2 for # item2
  799. function textTrade(command,player)
  800. local syntax = tag.." Incorect Syntax. Please follow: trade [#] [Item/ID] to [player] for [#] [Item/ID]"
  801. command = string.match(command,"%S%s(.+)")
  802. if command == nil then
  803. Chat.addBuffer(player,syntax)
  804. end
  805. --old string.match(command,"trade (%d+)%s.+%sto%S+%sfor%s%d+%s.+")
  806. local player2 = string.match(command,"%d+ .+ to (%S+) for %d+ .+")
  807. local amount1 = tonumber(string.match(command,"(%d+) .+ to %S+ for %d+ .+"))
  808. local item1 = string.match(command,"%d+ (.+) to %S+ for %d+ .+")
  809. local amount2 = tonumber(string.match(command,"%d+ .+ to %S+ for (%d+) .+"))
  810. local item2 = string.match(command,"%d+ .+ to %S+ for %d+ (.+)")
  811. if player2 == nil or amount1 ==nil or amount2 == nil or item2 == nil then
  812. Chat.addBuffer(player,syntax)
  813. return
  814. end
  815. local id1,_ = ItemDB.search(item1,true,playerData[player]["items"])
  816. if id1 then
  817. if playerData[player]["items"][id1] < amount1 then
  818. Chat.addBuffer(player,tag.." Trade Failed. Not enough items to complete trade: ["..id1.."] "..ItemDB.getItemName(id1).."(x"..playerData[player]["items"][id1]..") needs: "..amount1)
  819. return
  820. end
  821. else
  822. Chat.addBuffer(player,tag.." Trade Failed. No item/id found for term '"..item1.."'")
  823. return
  824. end
  825. local id2 = ItemDB.search(item2,true)
  826. if not id2 then
  827. Chat.addBuffer(player,tag.." Trade Failed. No item/id found for term '"..item2.."'")
  828. return
  829. end
  830. -- sucess?
  831.  
  832. chatHelper[player] = constructTrade(player2,id1,amount1,id2,amount2)
  833. Chat.addBuffer(player,tag.." Confirm Trade: "..amount1.." ["..id1.."] "..ItemDB.getItemName(id1).." to "..player2.." for "..amount2.." ["..id2.."] "..ItemDB.getItemName(id2))
  834. Chat.addBuffer(player,tag.." Type 'cc yes' to confirm.")
  835. end
  836. function constructTrade(player2,id1,amount1,id2,amount2)
  837. local ch_temp = {}
  838. local ch_temp2 = {}
  839. ch_temp2[id1] = amount1
  840. ch_temp["give"] = deepcopy(ch_temp2)
  841. ch_temp2 = {}
  842. ch_temp2[id2] = amount2
  843. ch_temp["take"] = deepcopy(ch_temp2)
  844. ch_temp["player2"] = player2
  845. ch_temp["type"] = "trade"
  846. return deepcopy(ch_temp)
  847. end
  848. function confirm(command,player)
  849. if chatHelper[player] == nil then
  850. return
  851. end
  852. if string.match(command,"yes") then
  853. if chatHelper[player]["type"] == "trade" then
  854. local player2 = chatHelper[player]["player2"]
  855. for k,v in pairs(chatHelper[player]["give"]) do
  856. id1 = k
  857. amount1 = v
  858. end
  859. if playerData[player2] ~= nil and playerData[player2]["items"] ~= nil then
  860.  
  861. else
  862. Chat.addBuffer(player,tag.." "..player2.." doesn't have enough items!")
  863. return
  864. end
  865. local tab_temp = {}
  866. tab_temp["take"] = chatHelper[player]["give"]
  867. tab_temp["give"] = chatHelper[player]["take"]
  868. tab_temp["player2"] = player
  869. tab_temp["type"] = "atrade"
  870. chatHelper[player2] = deepcopy(tab_temp)
  871. elseif chatHelper[player]["type"] == "atrade" then
  872.  
  873. end
  874. else
  875. if chatHelper[player]["type"] == "trade" then
  876. Chat.addBuffer(player,tag.." Trade Canceled.")
  877. end
  878. end
  879. end
  880.  
  881. -----------------------
  882. --------startup--------
  883. -----------------------
  884.  
  885. function run()
  886. parallel.waitForAny(antiHack,setup)
  887. parallel.waitForAll(antiHack,senseItems,sensePlayers,test,Chat.run,monitor)
  888. end
  889. local status,err = pcall(run)
  890. if not status then
  891. print("Error was caught. restarting..")
  892. print(err)
  893. io.read()
  894. Debug(err)
  895. end
  896. os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement