Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- version 1.1
- --- INIT ---
- local function loadAPIs()
- if not fs.exists("lib/datetime") then
- error("[lib/datetime] file not found.")
- end
- os.loadAPI("lib/datetime")
- datetime.init()
- if not fs.exists("lib/objectJSON") then
- error("[lib/objectJSON] file not found")
- end
- os.loadAPI("lib/objectJSON")
- objectJSON.init()
- end
- --- UTILS ----
- local function split(inputstr, sep)
- if sep == nil then sep = "%s" end
- local t={}
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- table.insert(t, str)
- end
- return t
- end
- -- merge an array split from p-q, q-r
- local function merge(A, p, q, r)
- local n1 = q-p+1
- local n2 = r-q
- local left = {}
- local right = {}
- for i=1, n1 do
- left[i] = A[p+i-1]
- end
- for i=1, n2 do
- right[i] = A[q+i]
- end
- left[n1+1] = math.huge
- right[n2+1] = math.huge
- local i=1
- local j=1
- for k=p, r do
- if left[i]<=right[j] then
- A[k] = left[i]
- i=i+1
- else
- A[k] = right[j]
- j=j+1
- end
- end
- end
- -- main mergesort algorithm
- local function mergeSort(A, p, r)
- if p == nil then
- p = 1
- end
- if r == nil then
- r = #A
- end
- if p < r then
- local q = math.floor((p + r)/2)
- mergeSort(A, p, q)
- mergeSort(A, q+1, r)
- merge(A, p, q, r)
- end
- end
- --- FUNCTION ---
- local function getPlayers()
- while true do
- local players = objectJSON.decodeFromFile("players")
- local obj = objectJSON.decodeHTTP("http://api.mineaurion.com/v1/serveurs")
- if obj == nil or next(obj) == nil then
- return nil
- end
- for _,v in pairs(obj) do
- if v["info"] == "Infinity" then
- if type(v["joueurs"]) ~= "table" then
- return nil
- end
- local date = datetime.getDatetime2()
- if date == nil then
- print("Was not able to get the date.")
- return nil
- end
- local date_str = date["year"] .. "/" .. date["month"] .. "/" .. date["day"] ..
- " - " .. date["hour"] .. ":" .. date["min"]
- for _,p in pairs(v["joueurs"]) do players[p] = date_str end
- objectJSON.encodeAndSavePretty("players", players)
- end
- end
- sleep(60)
- end
- end
- local function getTime(datetime)
- local date = split(datetime, " - ")[1]
- local time = split(datetime, " - ")[3]
- local year = split(date, "/")[1]
- local month = split(date, "/")[2]
- local day = split(date, "/")[3]
- local hour = split(time, ":")[1]
- local min = split(time, ":")[2]
- return tonumber(year .. month .. day .. hour .. min)
- end
- local function getSortedPlayers(players)
- local keyTimePlayers = {}
- for p,t in pairs(players) do
- local player = {["name"] = p, ["lastCo"] = t}
- keyTimePlayers[getTime(t)] = player
- end
- local keys = {}
- for timeID,player in pairs(keyTimePlayers) do
- table.insert(keys, timeID)
- end
- mergeSort(keys)
- local sortedPlayers = {}
- for _,timeID in ipairs(keys) do
- table.insert(sortedPlayers, keyTimePlayers[timeID])
- end
- return sortedPlayers
- end
- local function printPlayers()
- local players = objectJSON.decodeFromFile("players")
- local mon = peripheral.find("monitor")
- if mon ~= nil then
- term.redirect(mon)
- end
- local _,y = term.getSize()
- term.clear()
- local n = 0
- for _,player in ipairs(getSortedPlayers(players)) do
- local escape = " "
- for i=0,(20 - string.len(player.name)) do
- escape = escape .. " "
- end
- if n < y - 1 then
- print(player.name .. escape .. player.lastCo)
- n = n + 1
- else
- print("Press enter to continue...")
- io.read()
- n = 0
- term.clear()
- end
- end
- end
- local function getRedstone()
- while true do
- for _,v in pairs(redstone.getSides()) do
- if redstone.getInput(v) then
- printPlayers()
- end
- end
- sleep(1)
- end
- end
- --- MAIN ---
- local function main()
- loadAPIs()
- while true do
- parallel.waitForAny(getRedstone, getPlayers)
- sleep(0)
- end
- end
- main()
Add Comment
Please, Sign In to add comment