Advertisement
ZNZNCOOP

ChestC_Test

Dec 4th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 KB | None | 0 0
  1. CHEST_SIDE= 'right'
  2. MODEM_SIDE= 'top'
  3. MAIN_SERVER_ID= 1
  4. function send(id,data,r)
  5.    c= 0
  6.    while true do
  7.       c= c+1
  8.       rednet.send(id,data)
  9.       ids,message= rednet.receive(1)
  10.       if (ids == id and message == #data..'') then
  11.          return true
  12.       end
  13.       if (c == r) then break end
  14.    end
  15. end
  16.  
  17. function receive(timeout)
  18.    while true do
  19.       id,message= rednet.receive(timeout)
  20.       if (id ~= nil and #message~= nil) then
  21.          rednet.send(id,#message..'')
  22.          return id,message
  23.       end
  24.    end
  25. end
  26.  
  27. function getID(NumberSlot)
  28.    info= chest.getStackInSlot(NumberSlot)
  29.    id= ''
  30.    if (info == nil) then return -1 end
  31.    for k,v in pairs(info) do
  32.       if (k == 'id') then id= v end
  33.       if (k == 'dmg') then if (v ~= 0) then return id..':'..v end return id end
  34.    end
  35.    return -1
  36. end
  37.  
  38. function getAmount(NumberSlot)
  39.    info= chest.getStackInSlot(NumberSlot)
  40.    if (info == nil) then return -1 end
  41.    for k,v in pairs(info) do
  42.        if (k == 'qty') then return v end
  43.    end
  44.    return -1
  45. end
  46.  
  47. function addA(arr1,arr2)
  48.    for i=1,#arr2 do
  49.       arr1[#arr1+1]= arr2[i]
  50.    end
  51.    return arr1
  52. end
  53.  
  54. function drawA(arr)
  55.    for i=1,#arr do
  56.     print(arr[i])
  57.    end
  58. end
  59.  
  60.  
  61. rednet.open(MODEM_SIDE)
  62. chest= peripheral.wrap(CHEST_SIDE)
  63. --ID_LIST= {}
  64. --AMOUNT_LIST= {}
  65. while true do
  66.    ID_LIST= {}
  67.    AMOUNT_LIST= {}
  68.    count= 0
  69.    InventorySize= chest.getInventorySize()
  70.    while true do
  71.       id,message= receive()
  72.       if (id == MAIN_SERVER_ID and message == 'start') then
  73.          break
  74.       end
  75.    end
  76.    for i=1,InventorySize do
  77.       d= chest.getAllStacks()
  78.       Rsize= #d
  79.       temp1= getID(i)
  80.       temp2= getAmount(i)
  81.       if (temp1 ~= -1 and temp2 ~= -1) then
  82.          count= count+1
  83.          stat= false
  84.          for d=1,#ID_LIST do
  85.           if (temp1 == ID_LIST[d]) then
  86.              stat= true
  87.              AMOUNT_LIST[d]= tonumber(AMOUNT_LIST[d])+temp2
  88.              break
  89.           end
  90.          end
  91.          if (stat == false) then
  92.           ID_LIST[#ID_LIST+1]= temp1
  93.             AMOUNT_LIST[#AMOUNT_LIST+1]= temp2
  94.          end
  95.       end
  96.    end
  97.    Rez= {}
  98.    Rez= addA(ID_LIST,AMOUNT_LIST)
  99.    table.insert(Rez,1,'DATA:'..#Rez..'')
  100.    data= textutils.serialize(Rez)
  101.    send(MAIN_SERVER_ID,data,10)
  102.    sleep(0)
  103. end
  104. rednet.close(MODEM_SIDE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement