Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local monitor = peripheral.wrap("top")
- local modem = peripheral.wrap("bottom")
- modem.open(105)
- h = fs.open("Data","r")
- temp = h.readAll()
- h.close()
- Data = textutils.unserialize(temp)
- local Data_Size = 0
- if type(Data) == "table" then
- for k,v in pairs(Data) do
- Data_Size = Data_Size + 1
- end
- end
- sBoolean = false
- os.loadAPI("touch")
- page = 1
- List_Count = 14
- print(Data_Size)
- screen = {}
- function table.removeKey(array,key)
- local iter = 1
- for k,v in pairs(array) do
- if k == key then
- table.remove(array,iter)
- break
- end
- iter = iter +1
- end
- end
- function split(str,pat)
- local t = {}
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- function compTab(tab1,tab2)
- if type(tab1) ~= "table" or type(tab2) ~= "table" then
- error(" Arguments need to be tables.")
- end
- local sTab1 = {}
- local sTab2 = {}
- local s = false
- for i = 1,#tab1 do
- for j = 1,#tab2 do
- if tab1[i] == tab2[j] then
- s = true
- end
- end
- if not s then
- table.insert(sTab1,tab1[i])
- end
- s = false
- end
- for i = 1,#tab2 do
- for j = 1,#tab1 do
- if tab2[i] == tab1[j] then
- s = true
- end
- end
- if not s then
- table.insert(sTab2,tab2[i])
- end
- s = false
- end
- return sTab1,sTab2
- end
- function getCurrentTime()
- local time = http.get("http://www.timeapi.org/cet/now?format=%25H/%25M/%25S/%25D").readAll()
- local tab = split(time,"/")
- for inx=1,#tab do
- tab[inx] = tonumber(tab[inx])
- end
- return tab
- end
- function compareTime(time1,time2)
- local u = {6,5,4,1,2,3}
- for i=1,6 do
- if time1[u[i]] > time2[u[i]] then
- return 1
- elseif time1[u[i]] < time2[u[i]] then
- return 2
- end
- end
- end
- function chooseATarget(trg)
- local iter = 0
- currTime = getCurrentTime()
- local summ = 0
- for i = 1,Data_Size do
- summ = summ + Data[i]["bounty"]
- end
- person = math.random(1,summ)
- i = 0
- for k,v in pairs(Data) do
- i = i+1
- if iter <= person and iter+Data[i]["bounty"] > person then
- if compareTime(getCurrentTime(),Data[k]["time"]) == 1 and k ~= trg then
- return k
- else
- chooseATarget(trg)
- end
- end
- iter = iter + Data[i]["bounty"]
- end
- end
- function chooseAKill(trg)
- temp = math.random(1,List_Count)
- Data[trg]["WayOfKill"] = temp
- return temp
- end
- -- main programm
- function modemGestion()
- local oFlag = false
- while true do
- local event,side,channel,channelbis,msg = os.pullEvent("modem_message")
- if channelbis == 105 then
- print("yop")
- t = textutils.unserialize(msg)
- if Data_Size > 0 then
- local vTab = {}
- for k,v in pairs(Data) do
- table.insert(vTab,k)
- end
- local tb1,tb2 = compTab(t,vTab)
- if type(tb1) == "table" then
- for bla = 1,#tb1 do
- Data[tb1[bla]] = {}
- Data[tb1[bla]]["time"] = {99,99,99,99,99,9999}
- Data[tb1[bla]]["bounty"] = 10
- Data[tb1[bla]]["trg"] = ""
- Data[tb1[bla]]["WayOfKill"] = 0
- Data[tb1[bla]]["hunt"] = false
- Data[tb1[bla]]["isHunted"] = false
- end
- end
- if type(tb2) == "table" then
- for bla = 1,#tb2 do
- for key in pairs(Data) do
- if key == tb2[bla] then
- table.removeKey(Data,key)
- end
- end
- end
- end
- else
- Data = {}
- for i = 1,#t do
- Data[t[i]] = {}
- Data[t[i]]["time"] = {99,99,99,99,99,9999}
- Data[t[i]]["bounty"] = 10
- Data[t[i]]["trg"] = ""
- Data[t[i]]["WayOfKill"] = 0
- Data[t[i]]["hunt"] = false
- Data[t[i]]["isHunted"] = false
- end
- end
- fs.delete("Backup")
- fs.copy("Data","Backup")
- fs.delete("Data")
- h = fs.open("Data","w")
- h.write(textutils.serialize(Data))
- os.reboot()
- elseif channelbis == 104 then
- if Data[msg]["hunt"] == true then
- trg = Data[msg]["trg"]
- Data[trg]["isHunted"] = false
- fs.delete("Backup")
- fs.copy("Data","Backup")
- fs.delete("Data")
- h = fs.open("Data","w")
- h.write(textutils.serialize(Data))
- h.close()
- modem.transmit(105,107,textutils.serialize({"HuntDone",msg}))
- Data[msg]["hunt"] = false
- elseif Data[msg]["hunt"] == false then
- target = chooseATarget(msg)
- Data[target]["time"] = getCurrentTime()
- Data[target]["time"][4] = Data[target]["time"][4]+1
- Data[target]["isHunted"] = true
- modem.transmit(105,107,textutils.serialize({"HuntToGo",msg,target,chooseAKill(msg)}))
- Data[target]["bounty"] = Data[target]["bounty"]-2
- Data[msg]["bounty"] = Data[msg]["bounty"]+3
- Data[msg]["hunt"] = true
- fs.delete("Backup")
- fs.copy("Data","Backup")
- fs.delete("Data")
- h = fs.open("Data","w")
- h.write(textutils.serialize(Data))
- h.close()
- end
- end
- end
- end
- function getName(index)
- ind = 1
- for k,value in pairs(Data) do
- if ind == index then
- return k
- end
- ind = ind+1
- end
- error("Index is higher than Table size")
- end
- function panel()
- nbPage = math.ceil(Data_Size/27)
- xMin = {27,1,14}
- xMax = {39,13,26}
- yMin = {2,4,6,8,10,12,14,16,18}
- yMax = {3,5,7,9,11,13,15,17,19}
- for j=1,nbPage do
- screen[j] = touch.new("top")
- screen[j]:add("<<Page",function() PageMoins() end,6,23,17,25,colors.red,colors.lime)
- screen[j]:add("Page>>",function() PagePlus() end,22,23,33,25,colors.red,colors.lime)
- end
- lastPageCount = Data_Size%27
- if lastPageCount == 0 then
- lastPageCount = 27
- end
- lastLine = math.ceil(lastPageCount/3)
- lastNumber = lastPageCount%3
- if nbPage > 1 then
- for a=1,nbPage-1 do
- for b=1,27 do
- 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)
- end
- end
- end
- for j = 1,(lastPageCount-lastNumber) do
- 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)
- end
- if lastNumber == 2 then
- screen[nbPage]:add(getName(Data_Size-1),nil,6,yMin[lastLine],19,yMax[lastLine],colors.black,colors.black)
- screen[nbPage]:add(getName(Data_Size),nil,21,yMin[lastLine],34,yMax[lastLine],colors.black,colors.black)
- elseif lastNumber == 1 then
- screen[nbPage]:add(getName(Data_Size),nil,14,yMin[lastLine],26,yMax[lastLine],colors.black,colors.black)
- end
- while true do
- screen[page]:draw()
- monitor.setCursorPos(15,22)
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- monitor.write("Page "..page.." of "..nbPage)
- local event, p1 = screen[page]:handleEvents(os.pullEvent())
- if event == "button_click" then
- if type(screen[page].buttonList[p1].func) == "function" then
- screen[page].buttonList[p1].func()
- end
- end
- end
- end
- function handleColor()
- while true do
- sleep(5)
- for uol =1,Data_Size do
- local name_1 = getName(uol)
- timecompared = compareTime(Data[name_1]["time"],getCurrentTime())
- if timecompared == 2 and Data[name_1]["hunt"] == true and Data[name_1]["isHunted"] == false then
- screen[math.ceil(uol/27)].buttonList[name_1].inactiveText = colors.red
- screen[math.ceil(uol/27)].buttonList[name_1].activeText = colors.red
- elseif timecompared == 2 and Data[name_1]["hunt"] == false and Data[name_1]["isHunted"] == false then
- screen[math.ceil(uol/27)].buttonList[name_1].inactiveText = colors.green
- screen[math.ceil(uol/27)].buttonList[name_1].activeText = colors.green
- elseif timecompared == 1 and Data[name_1]["hunt"] == true then
- screen[math.ceil(uol/27)].buttonList[name_1].inactiveText = colors.orange
- screen[math.ceil(uol/27)].buttonList[name_1].activeText = colors.orange
- else
- screen[math.ceil(uol/27)].buttonList[name_1].inactiveText = colors.lightBlue
- screen[math.ceil(uol/27)].buttonList[name_1].activeText = colors.lightBlue
- end
- end
- screen[page]:draw()
- monitor.setCursorPos(15,22)
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- monitor.write("Page "..page.." of "..nbPage)
- sleep(5)
- end
- end
- PagePlus = function()
- page = page + 1
- if page>nbPage then
- page = nbPage
- end
- end
- PageMoins = function()
- page = page - 1
- if page<1 then
- page = 1
- end
- end
- if Data_Size > 0 then
- parallel.waitForAll(panel,modemGestion)
- else
- modemGestion()
- end
Advertisement
Add Comment
Please, Sign In to add comment