Clem105

Panel 2

Apr 8th, 2016
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.45 KB | None | 0 0
  1. local monitor = peripheral.wrap("top")
  2. local modem = peripheral.wrap("bottom")
  3. modem.open(105)
  4. h = fs.open("Data","r")
  5. temp = h.readAll()
  6. h.close()
  7. Data = textutils.unserialize(temp)
  8. local Data_Size = 0
  9. if type(Data) == "table" then
  10.     for k,v in pairs(Data) do
  11.         Data_Size = Data_Size + 1
  12.     end
  13. end
  14. sBoolean = false
  15. os.loadAPI("touch")
  16. page = 1
  17. List_Count = 14
  18. print(Data_Size)
  19. screen = {}
  20.  
  21.  
  22. function table.removeKey(array,key)
  23.     local iter = 1
  24.     for k,v in pairs(array) do
  25.         if k == key then
  26.             table.remove(array,iter)
  27.             break
  28.         end
  29.         iter = iter +1
  30.     end
  31. end
  32.  
  33.  
  34. function split(str,pat)
  35.     local t = {}
  36.     local fpat = "(.-)" .. pat
  37.     local last_end = 1
  38.     local s, e, cap = str:find(fpat, 1)
  39.     while s do
  40.         if s ~= 1 or cap ~= "" then
  41.             table.insert(t,cap)
  42.         end
  43.         last_end = e+1
  44.         s, e, cap = str:find(fpat, last_end)
  45.     end
  46.     if last_end <= #str then
  47.         cap = str:sub(last_end)
  48.         table.insert(t, cap)
  49.     end
  50.     return t
  51. end
  52.  
  53. function compTab(tab1,tab2)
  54.     if type(tab1) ~= "table" or type(tab2) ~= "table" then
  55.         error(" Arguments need to be tables.")
  56.     end
  57.     local sTab1 = {}
  58.     local sTab2 = {}
  59.     local s = false
  60.     for i = 1,#tab1 do
  61.         for j = 1,#tab2 do
  62.             if tab1[i] == tab2[j] then
  63.                 s = true
  64.             end
  65.         end
  66.         if not s then
  67.             table.insert(sTab1,tab1[i])
  68.         end
  69.         s = false
  70.     end
  71.     for i = 1,#tab2 do
  72.         for j = 1,#tab1 do
  73.             if tab2[i] == tab1[j] then
  74.                 s = true
  75.             end
  76.         end
  77.         if not s then
  78.             table.insert(sTab2,tab2[i])
  79.         end
  80.         s = false
  81.     end
  82.     return sTab1,sTab2
  83. end
  84.  
  85.  
  86. function getCurrentTime()
  87.     local time = http.get("http://www.timeapi.org/cet/now?format=%25H/%25M/%25S/%25D").readAll()
  88.     local tab = split(time,"/")
  89.     for inx=1,#tab do
  90.         tab[inx] = tonumber(tab[inx])
  91.     end
  92.     return tab
  93. end
  94.  
  95. function compareTime(time1,time2)
  96.     local u = {6,5,4,1,2,3}
  97.     for i=1,6 do   
  98.         if time1[u[i]] > time2[u[i]] then
  99.             return 1
  100.         elseif time1[u[i]] < time2[u[i]] then
  101.             return 2
  102.         end
  103.     end
  104. end
  105.  
  106. function chooseATarget(trg)
  107.     local iter = 0
  108.     currTime = getCurrentTime()
  109.     local summ = 0
  110.     for i = 1,Data_Size do
  111.         summ = summ + Data[i]["bounty"]
  112.     end
  113.     person = math.random(1,summ)
  114.     i = 0
  115.     for k,v in pairs(Data) do
  116.         i = i+1
  117.         if iter <= person and iter+Data[i]["bounty"] > person then
  118.             if compareTime(getCurrentTime(),Data[k]["time"]) == 1 and k ~= trg then
  119.                 return k
  120.             else
  121.                 chooseATarget(trg)
  122.             end
  123.         end
  124.         iter = iter + Data[i]["bounty"]
  125.     end
  126. end
  127.  
  128. function chooseAKill(trg)
  129.     temp = math.random(1,List_Count)
  130.     Data[trg]["WayOfKill"] = temp
  131.     return temp
  132. end
  133.  
  134.  
  135. -- main programm
  136. function modemGestion()
  137. local oFlag = false
  138. while true do
  139.     local event,side,channel,channelbis,msg = os.pullEvent("modem_message")
  140.     if channelbis == 105 then
  141.         print("yop")
  142.         t = textutils.unserialize(msg)
  143.         if Data_Size > 0 then
  144.             local vTab = {}
  145.             for k,v in pairs(Data) do
  146.                 table.insert(vTab,k)
  147.             end
  148.             local tb1,tb2 = compTab(t,vTab)
  149.             if type(tb1) == "table" then
  150.                 for bla = 1,#tb1 do
  151.                     Data[tb1[bla]] = {}
  152.                     Data[tb1[bla]]["time"] = {99,99,99,99,99,9999}
  153.                     Data[tb1[bla]]["bounty"] = 10
  154.                     Data[tb1[bla]]["trg"] = ""
  155.                     Data[tb1[bla]]["WayOfKill"] = 0
  156.                     Data[tb1[bla]]["hunt"] = false
  157.                     Data[tb1[bla]]["isHunted"] = false
  158.                 end
  159.             end
  160.             if type(tb2) == "table" then
  161.                 for bla = 1,#tb2 do
  162.                     for key in pairs(Data) do
  163.                         if key == tb2[bla] then
  164.                             table.removeKey(Data,key)
  165.                         end
  166.                     end
  167.                 end
  168.             end
  169.         else
  170.             Data = {}
  171.             for i = 1,#t do
  172.                 Data[t[i]] = {}
  173.                 Data[t[i]]["time"] = {99,99,99,99,99,9999}
  174.                 Data[t[i]]["bounty"] = 10
  175.                 Data[t[i]]["trg"] = ""
  176.                 Data[t[i]]["WayOfKill"] = 0
  177.                 Data[t[i]]["hunt"] = false
  178.                 Data[t[i]]["isHunted"] = false
  179.             end
  180.         end
  181.         fs.delete("Backup")
  182.         fs.copy("Data","Backup")
  183.         fs.delete("Data")
  184.          h = fs.open("Data","w")
  185.         h.write(textutils.serialize(Data))
  186.         os.reboot()
  187.        
  188.     elseif channelbis == 104 then
  189.         if Data[msg]["hunt"] == true then
  190.             trg = Data[msg]["trg"]
  191.             Data[trg]["isHunted"] = false
  192.             fs.delete("Backup")
  193.             fs.copy("Data","Backup")
  194.             fs.delete("Data")
  195.             h = fs.open("Data","w")
  196.             h.write(textutils.serialize(Data))
  197.             h.close()
  198.             modem.transmit(105,107,textutils.serialize({"HuntDone",msg}))
  199.             Data[msg]["hunt"] = false
  200.         elseif Data[msg]["hunt"] == false then
  201.             target = chooseATarget(msg)
  202.             Data[target]["time"] = getCurrentTime()
  203.             Data[target]["time"][4] = Data[target]["time"][4]+1
  204.             Data[target]["isHunted"] = true
  205.             modem.transmit(105,107,textutils.serialize({"HuntToGo",msg,target,chooseAKill(msg)}))
  206.             Data[target]["bounty"] = Data[target]["bounty"]-2
  207.             Data[msg]["bounty"] = Data[msg]["bounty"]+3
  208.             Data[msg]["hunt"] = true
  209.             fs.delete("Backup")
  210.             fs.copy("Data","Backup")
  211.             fs.delete("Data")
  212.             h = fs.open("Data","w")
  213.             h.write(textutils.serialize(Data))
  214.             h.close()
  215.         end
  216.     end
  217. end
  218. end
  219.  
  220.  
  221.  
  222. function getName(index)
  223.     ind = 1
  224.     for k,value in pairs(Data) do
  225.         if ind == index then
  226.             return k
  227.         end
  228.         ind = ind+1
  229.     end
  230.     error("Index is higher than Table size")
  231. end
  232.  
  233.  
  234. function panel()
  235.     nbPage = math.ceil(Data_Size/27)
  236.     xMin = {27,1,14}
  237.     xMax = {39,13,26}
  238.     yMin = {2,4,6,8,10,12,14,16,18}
  239.     yMax = {3,5,7,9,11,13,15,17,19}
  240.     for j=1,nbPage do
  241.         screen[j] = touch.new("top")
  242.         screen[j]:add("<<Page",function()  PageMoins() end,6,23,17,25,colors.red,colors.lime)
  243.         screen[j]:add("Page>>",function()  PagePlus() end,22,23,33,25,colors.red,colors.lime)
  244.     end
  245.     lastPageCount = Data_Size%27
  246.     if lastPageCount == 0 then
  247.         lastPageCount = 27
  248.     end
  249.     lastLine = math.ceil(lastPageCount/3)
  250.     lastNumber = lastPageCount%3
  251.     if nbPage > 1 then
  252.         for a=1,nbPage-1 do
  253.             for b=1,27 do
  254.                 screen[a]:add(getName((a-1)*27+b),nil,xMin[b%3+1],yMin[math.ceil(b/3)],xMax[b%3+1],yMax[math.ceil(b/3)],colors.black,colors.black)
  255.             end
  256.         end
  257.     end
  258.     for j = 1,(lastPageCount-lastNumber) do
  259.         screen[nbPage]:add(getName((nbPage-1)*27+j),nil,xMin[j%3+1],yMin[math.ceil(j/3)],xMax[j%3+1],yMax[math.ceil(j/3)],colors.black,colors.black)
  260.     end
  261.     if lastNumber == 2 then
  262.         screen[nbPage]:add(getName(Data_Size-1),nil,6,yMin[lastLine],19,yMax[lastLine],colors.black,colors.black)
  263.         screen[nbPage]:add(getName(Data_Size),nil,21,yMin[lastLine],34,yMax[lastLine],colors.black,colors.black)
  264.     elseif lastNumber == 1 then
  265.         screen[nbPage]:add(getName(Data_Size),nil,14,yMin[lastLine],26,yMax[lastLine],colors.black,colors.black)
  266.     end
  267.     while true do
  268.         screen[page]:draw()
  269.         monitor.setCursorPos(15,22)
  270.         monitor.setBackgroundColor(colors.black)
  271.         monitor.setTextColor(colors.white)
  272.         monitor.write("Page "..page.." of "..nbPage)
  273.         local event, p1 = screen[page]:handleEvents(os.pullEvent())
  274.         if event == "button_click" then
  275.             if type(screen[page].buttonList[p1].func) == "function" then
  276.                 screen[page].buttonList[p1].func()
  277.             end
  278.         end
  279.     end
  280. end
  281.  
  282. function handleColor()
  283.     while true do
  284.         sleep(5)
  285.         for uol =1,Data_Size do
  286.             local name_1 = getName(uol)
  287.             timecompared = compareTime(Data[name_1]["time"],getCurrentTime())
  288.             if timecompared == 2 and Data[name_1]["hunt"] == true and Data[name_1]["isHunted"] == false then
  289.                 screen[math.ceil(uol/27)].buttonList[name_1].inactiveText = colors.red
  290.                 screen[math.ceil(uol/27)].buttonList[name_1].activeText = colors.red
  291.             elseif timecompared == 2 and Data[name_1]["hunt"] == false and Data[name_1]["isHunted"] == false then
  292.                 screen[math.ceil(uol/27)].buttonList[name_1].inactiveText = colors.green
  293.                 screen[math.ceil(uol/27)].buttonList[name_1].activeText = colors.green
  294.             elseif timecompared == 1 and Data[name_1]["hunt"] == true then
  295.                 screen[math.ceil(uol/27)].buttonList[name_1].inactiveText = colors.orange
  296.                 screen[math.ceil(uol/27)].buttonList[name_1].activeText = colors.orange
  297.             else
  298.                 screen[math.ceil(uol/27)].buttonList[name_1].inactiveText = colors.lightBlue
  299.                 screen[math.ceil(uol/27)].buttonList[name_1].activeText = colors.lightBlue
  300.            
  301.             end
  302.         end
  303.         screen[page]:draw()
  304.         monitor.setCursorPos(15,22)
  305.         monitor.setBackgroundColor(colors.black)
  306.         monitor.setTextColor(colors.white)
  307.         monitor.write("Page "..page.." of "..nbPage)
  308.         sleep(5)
  309.     end
  310. end
  311.  
  312. PagePlus = function()
  313.     page = page + 1
  314.     if page>nbPage then
  315.         page = nbPage  
  316.     end
  317. end
  318.    
  319. PageMoins = function()
  320.     page = page - 1
  321.     if page<1 then
  322.         page = 1
  323.     end
  324. end
  325.  
  326.  
  327. if Data_Size > 0 then
  328.     parallel.waitForAll(panel,modemGestion)
  329. else
  330.     modemGestion()
  331. end
Advertisement
Add Comment
Please, Sign In to add comment