Advertisement
ZNZNCOOP

ChestS_Test

Dec 4th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.33 KB | None | 0 0
  1. MODEM_SIDE= 'top'
  2. function send(id,data,r)
  3.    c= 0
  4.    while true do
  5.       c= c+1
  6.       rednet.send(id,data)
  7.       ids,message= rednet.receive(1)
  8.       if (ids == id and message == #data..'') then
  9.          return true
  10.       end
  11.       if (c == r) then break end
  12.    end
  13. end
  14.  
  15. function parseD(filename)
  16.    lines= {}
  17.    tokens= {}
  18.    path= fs.open(filename,'r')
  19.    while true do
  20.       temp= path.readLine()
  21.       if (temp == nil) then break end
  22.       lines[#lines+1]= temp
  23.    end
  24.    s= {}
  25.    c= 0
  26.    for i=1,#lines do
  27.       for q=1,#lines[i] do
  28.          c= c+1
  29.          s[c]= string.sub(lines[i],q,q)
  30.       end
  31.    end
  32.     temp= ''
  33.     forS= false
  34.     i= 0
  35.     while i<#s do
  36.        i= i+1
  37.        forS= false
  38.        if (s[i] == ';') then
  39.           forS= true
  40.           tokens[#tokens+1]= temp
  41.           temp= ''
  42.        end
  43.        if (s[i] == '=') then
  44.           forS= true
  45.           tokens[#tokens+1]= temp
  46.           tokens[#tokens+1]= '='
  47.           temp= ''
  48.        end
  49.        if (forS~= true) then
  50.           if (s[i+1] == nil) then
  51.              temp= temp..s[i]
  52.              tokens[#tokens+1]= temp
  53.           end
  54.           temp= temp..s[i]
  55.        end
  56.    end
  57.  
  58.    name_list= {}
  59.    id_list= {}
  60.    i= 0
  61.    while i<#tokens do
  62.     i= i+1
  63.       if (tokens[i+1] == '=') then
  64.          name_list[#name_list+1]= tokens[i]
  65.          id_list[#id_list+1]= tokens[i+2]
  66.          i= i+2
  67.       end
  68.  
  69.    end
  70.    return name_list,id_list
  71. end
  72.  
  73. function receive(timeout)
  74.    while true do
  75.       id,message= rednet.receive(timeout)
  76.       if (id ~= nil and #message~= nil) then
  77.          rednet.send(id,#message..'')
  78.          return id,message
  79.       end
  80.    end
  81. end
  82.  
  83. function getData(arr)
  84.    arr1= {}
  85.    arr2= {}
  86.    length= #arr-1
  87.    
  88.    for i=2,#arr do
  89.       if (i-1 <= length/2) then
  90.          --print('1')
  91.          arr1[#arr1+1]= arr[i]
  92.       elseif (i-1 >= length/2) then
  93.          --print('2')
  94.          arr2[#arr2+1]= arr[i]
  95.       end
  96.    end
  97.    return arr1,arr2
  98. end
  99.  
  100. function drawA(arr1,arr2)
  101.   term.clear()
  102.   term.setCursorPos(1,1)
  103.    for i=1,#arr1 do
  104.     print(arr1[i],'=',arr2[i])
  105.    end
  106. end
  107.  
  108. function loadF(filename)
  109.    ids= {}
  110.    path= fs.open(filename,'r')
  111.    while true do
  112.       temp= path.readLine()
  113.       if (temp == nil) then break end
  114.       ids[#ids+1]= tonumber(temp)
  115.    end
  116.    return ids
  117. end
  118.  
  119. function addtoSys(arr1,arr2)
  120.    for i=1,#arr1 do
  121.     stat= false
  122.       for d=1,#ID_LIST do
  123.          if (ID_LIST[d] == arr1[i]) then
  124.             AMOUNT_LIST[d]= tonumber(AMOUNT_LIST[d])+tonumber(arr2[i])
  125.             stat= true
  126.             break
  127.          end
  128.       end
  129.       if (stat == false) then
  130.          ID_LIST[#ID_LIST+1]= arr1[i]
  131.          AMOUNT_LIST[#AMOUNT_LIST+1]= arr2[i]
  132.       end
  133.    end
  134. end
  135.  
  136. IDs_LIST= loadF('id')
  137. ID_LIST= {}
  138. AMOUNT_LIST= {}
  139. count= 1
  140. ID_NOW= IDs_LIST[count]
  141. rednet.open(MODEM_SIDE)
  142. while true do
  143.    send(ID_NOW+0,'start',100)
  144.    id,message= receive()
  145.    --print(id,message)
  146.    data= textutils.unserialize(message)
  147.    n= #data-1
  148.    temp= 'DATA:'..n..''
  149.    if (id == ID_NOW and data[1] == temp) then
  150.       il,al= getData(data)
  151.       addtoSys(il,al)
  152.       count= count+1
  153.       if (count > #IDs_LIST) then
  154.          drawA(ID_LIST,AMOUNT_LIST)
  155.          count= 1
  156.          AMOUNT_LIST= {}
  157.          ID_LIST= {}
  158.       end
  159.       ID_NOW= IDs_LIST[count]
  160.    end
  161. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement