SHARE
TWEET

GCAPI

GravityCube Mar 2nd, 2018 (edited) 4,065 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.    
  3.             --*--*--*--*--*--*--*--*--*--*--*--*--*--*--   
  4.             --*                GCAPI                 *--
  5.             --*     https://pastebin.com/0uAaAcrW    *--
  6.             --*            by: GravityCube           *--
  7.             --*--*--*--*--*--*--*--*--*--*--*--*--*--*--
  8.  
  9.            
  10.     Changelog:
  11.         1.0.0 First release
  12.         1.1.0 Image displayer for monitors added and bugs in the economy fixed
  13.         1.2.0 Tracker of players and chatSpy added, a custom http.get was added and global peripheral.find.
  14.         1.3.0 New listener, chatEvents and local admins for selling programs!
  15.         1.4.0 Various bugs fixed (repited chatEvents, and rgb api changed). Added version changelog and getVersion()
  16.         1.5.0 table.contains added
  17.             1.5.1 ChatEvent Listener little fix
  18.             1.5.2 Split function modified (self) and getChar added
  19.             1.5.3 getCenter added and centered of monitors fixed
  20.             1.5.4 getCenter now supports number and string in the #2 argument
  21.             1.5.5 Added some default "URLs" to the images
  22.             1.5.6 URL Alias updated
  23.             1.5.7 Added password for unfiltered tracker (akaElite request)
  24.         1.6.0 New method for tracking and some minor modifications.
  25.             1.6.1 Some minor changes and bugs fixed
  26.         1.7.0 New method gcapi.customMonitorWrite()
  27.             1.7.1 Added table.isEmpty
  28.             1.7.2 New admin (Freecss).
  29.         1.8.0 New method printAvatar()
  30.             1.8.1 New function getTime added and seed for random numbers changed.
  31.             1.8.2 Added deepCopy function from http://lua-users.org/wiki/CopyTable
  32.             1.8.3 An issue with getCenter was solved.
  33.        
  34. --]]
  35. --------------------------------------------
  36. -->               Version                <--
  37. --------------------------------------------
  38. local debugAPI = false
  39.  
  40. local version = "1.8.3"
  41.  
  42. print("GCAPI Version: " .. version)
  43.  
  44. function getVersion()
  45.     return version
  46. end
  47. --------------------------------------------
  48. -->              URL ALIAS               <--
  49. --------------------------------------------
  50. local urlAlias = {
  51.     ["emx"] = "http://bmo.emx.cl/logo.jpg",
  52.     ["cl"] = "http://craftersland.net/images/logo.png",
  53.     ["deadpool"] = "http://pixelartmaker.com/art/de17950892c0026.png"
  54. }
  55. --------------------------------------------
  56. -->                Tools                 <--
  57. --------------------------------------------
  58.  
  59. function getTime()
  60.     local response = http.get("http://bmo.emx.cl/time.php")
  61.     if response then
  62.         local millis = tonumber(response.readAll())
  63.         if millis then
  64.             return millis, true
  65.         end
  66.     end
  67.     return os.time(), false
  68. end
  69.  
  70. local charToColor = {["0"]=0, ["1"]= 1,["2"]= 2, ["3"]=3, ["4"]=4, ["5"]=5, ["6"]=6, ["7"]=7, ["8"]=8, ["9"]=9, ["a"]=10 ,["b"]=11, ["c"]=12, ["d"]=13, ["e"]=14, ["f"]=15}
  71. function customMonitorWrite(mon, text)
  72.     local xi, yi = mon.getCursorPos()
  73.     local chars = {}
  74.     for i = 1, string.len(text) do
  75.         local c = text:sub(i,i)
  76.         table.insert(chars, c)
  77.     end
  78.    
  79.     --charToColor["g"] = mon.getBackgroundColor()
  80.     local isNumber = false
  81.     for i=1, #chars, 1 do
  82.         local c = chars[i]
  83.         if c == "&" and charToColor[chars[i+1]] then
  84.             color = 2^chars[i+1]
  85.             mon.setBackgroundColor(color)
  86.             isNumber = true
  87.         else
  88.             if not isNumber then
  89.                 mon.write(c)
  90.             end
  91.             isNumber = false
  92.         end
  93.     end
  94. end
  95.  
  96.  
  97. function verifyVar(rawVar, varType)
  98.     if type(rawVar) ~= varType then
  99.         error( "bad argument (expected " .. varType .. ", got " .. type( rawVar ) .. ")", 2 )
  100.     end
  101. end
  102.  
  103. -- RANDOM STRING
  104. local charset = {}
  105. -- qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890
  106. for i = 48,  57 do table.insert(charset, string.char(i)) end
  107. for i = 65,  90 do table.insert(charset, string.char(i)) end
  108. for i = 97, 122 do table.insert(charset, string.char(i)) end
  109.  
  110. math.randomseed(getTime())
  111. function randomString(length)
  112.     if length > 0 then
  113.         return string.random(length - 1) .. charset[math.random(1, #charset)]
  114.     else
  115.         return ""
  116.     end
  117. end
  118. string.random = randomString
  119. --END RANDOM STRING
  120.  
  121. function getRandom(minValue, maxValue)
  122.     return math.random(minValue, maxValue)
  123. end
  124.  
  125. --peripheral.find() in Tekkit
  126. function findPeripheral(pType)
  127.     local pList = {}
  128.     for _,pName in pairs(peripheral.getNames()) do
  129.         if peripheral.getType(pName) == pType then
  130.             table.insert(pList, peripheral.wrap(pName))
  131.         end
  132.     end
  133.     return unpack(pList)
  134. end
  135. peripheral.find = findPeripheral
  136.  
  137. --Wipe table "t"
  138. function wipeTable(t)
  139.     local listForDeletion = {}
  140.     for k,v in pairs(t) do
  141.         table.insert(listForDeletion, k)
  142.     end
  143.    
  144.     for _,i in pairs(listForDeletion) do
  145.         t[i] = nil
  146.     end
  147. end
  148. table.wipe = wipeTable
  149.  
  150. --Split string
  151. function split(self, delimiter)
  152.     result = {};
  153.     for match in (self..delimiter):gmatch("(.-)"..delimiter) do
  154.         table.insert(result, match);
  155.     end
  156.     return result;
  157. end
  158. string.split = split
  159.  
  160. string.getChar = function (self, nVar)
  161.     if type(nVar) ~= "number" then
  162.         error( "bad argument #2 (expected number, got " .. type( nVar ) .. ")")
  163.     else
  164.         return self:sub(nVar, nVar)
  165.     end
  166. end
  167.  
  168. table.isEmpty = function(self)
  169.     for k,v in pairs(self) do
  170.         return true
  171.     end
  172.     return false
  173. end
  174.  
  175. table.contains = function (self, eVar)
  176.     for k,v in pairs(self) do
  177.         if v == eVar then
  178.             return true
  179.         end
  180.     end
  181.     return false
  182. end
  183.  
  184. --Print to a file and term (message, dir(optional) )
  185. function printLog(line, dir)
  186.     if line == nil then
  187.         return nil
  188.     end
  189.     if dir == nil then
  190.         dir = "/log"
  191.     end
  192.     line = line
  193.     print(line)
  194.    
  195.     file = fs.open(dir,"a")
  196.     file.writeLine(line)
  197.     file.close()
  198. end
  199.  
  200. --Are items equal? Ignoring quantity
  201. function equalItems(item1, item2)
  202.     if item1 == nil or item2 == nil then
  203.         return false
  204.     end
  205.     for k,v in pairs(item1) do
  206.         if k ~= qty then
  207.             if item2[k] ~= v then
  208.                 return false
  209.             end
  210.         end
  211.     end
  212.     return true
  213. end
  214.  
  215. --Get number of lines file
  216. function getNumberOfLinesFile(filePath)
  217.     if not fs.exists(filePath) then
  218.         print("[GCAPI] File not found")
  219.         return 0
  220.     end
  221.     file = fs.open(filePath, "r")
  222.     if file then
  223.         local i = 0
  224.         while file.readLine() do
  225.             i = i + 1
  226.         end
  227.         file.close()
  228.         return i
  229.     end
  230.     return 0
  231. end
  232.  
  233. function getListFromFile(filePath)
  234.     local list = {}
  235.     if not fs.exists(filePath) then
  236.         saveListToFile(filePath, list)
  237.         return list
  238.     end
  239.     local file = fs.open(filePath,"r")
  240.     local data = file.readAll()
  241.     file.close()
  242.     list = textutils.unserialize(data)
  243.     if textutils.unserialize(data) == nil then
  244.         list = {}
  245.     end
  246.     return list
  247. end
  248.  
  249. function saveListToFile(filePath, list)
  250.     file = fs.open(filePath,"w")
  251.     file.write(textutils.serialize(list))
  252.     file.close()
  253. end
  254.  
  255. string.starts = function (self,Start)
  256.    return string.sub(self,1,string.len(Start))==Start
  257. end
  258.  
  259. function getCenter(f, varP)
  260.     if not varP then
  261.         varP = 0
  262.     end
  263.    
  264.     local lengthOfVar = 0
  265.     if type(varP) == "number" then
  266.         lengthOfVar = varP
  267.     elseif type(varP) == "string" then
  268.         lengthOfVar = string.len(varP)
  269.     else
  270.         error("For function gcapi.getCenter expected number or string in argument #2")
  271.     end
  272.     if ((f - string.len(varP)) % 2) == 0 then
  273.         return math.floor((f - lengthOfVar)/2)
  274.     end
  275.    
  276.     return math.floor((f - lengthOfVar)/2)+1
  277.    
  278. end
  279.  
  280. function deepCopy(orig)
  281.     local orig_type = type(orig)
  282.     local copy
  283.     if orig_type == 'table' then
  284.         copy = {}
  285.         for orig_key, orig_value in next, orig, nil do
  286.             copy[deepCopy(orig_key)] = deepCopy(orig_value)
  287.         end
  288.         setmetatable(copy, deepCopy(getmetatable(orig)))
  289.     else -- number, string, boolean, etc
  290.         copy = orig
  291.     end
  292.     return copy
  293. end
  294.  
  295. function numberBetween(v1, v2, v3)
  296.     if v1 >= v2 and v1 <= v3 then
  297.         return true
  298.     end
  299.     return false
  300. end
  301.  
  302. --------------------------------------------
  303. -->          Custom Http Request         <--
  304. -->    http.get with timeout variable    <--
  305. --------------------------------------------
  306.  
  307. function http.getStringWithTimeout(url, headers, timeout)
  308.     if timeout == nil then
  309.         local response = http.get(url, headers)
  310.         local responseString = response.readAll()
  311.         response.close()
  312.         return responseString
  313.     end
  314.    
  315.     --seconds to ticks
  316.     timeout = timeout*20
  317.    
  318.     http.request(url, nil, headers)
  319.    
  320.     local requesting = true
  321.    
  322.     local localReloj = 0
  323.     local nextTimeEventID = os.startTimer(0.1)
  324.     while requesting do
  325.        
  326.         --Wait for event.
  327.         tEvent = {os.pullEvent()}
  328.        
  329.         if "timer" == tEvent[1] then
  330.             if tEvent[2] == nextTimeEventID then
  331.                 if timeout < localReloj then
  332.                     return nil
  333.                 end
  334.                 localReloj = localReloj + 2
  335.                 nextTimeEventID = os.startTimer(0.1)
  336.             end
  337.         else
  338.             --Success.
  339.             if tEvent[1] == "http_success" and url == tEvent[2] then
  340.                 local response = tEvent[3]
  341.                 local responseString = response.readAll()
  342.                 response.close()
  343.                 return responseString
  344.             end
  345.             --Failure
  346.             if tEvent[1] == "http_failure" and url == tEvent[2] then
  347.                 return nil
  348.             end
  349.            
  350.             nextTimeEventID = os.startTimer(0.1)
  351.         end
  352.        
  353.     end
  354. end
  355.  
  356.  
  357. --------------------------------------------
  358. -->             ImageDisplay             <--
  359. -->                                      <--
  360. -->              IMPORTANT:              <--
  361. -->  The API server get bugged sometimes <--
  362. -->  so I had to make a timeout http.get <--
  363. --------------------------------------------
  364.  
  365. -- ** Display Image from URL **
  366.  
  367. -- Example:
  368. -- url = http://www.image.com/image.jpg
  369. -- glassesPeripheral = peripheral.wrap(glassesName) [Optional]
  370. -- maxSize = 10 (10 max of height and weight) [Optional]
  371.  
  372. -- NOTE: If you want the image to be send to only 1 player use
  373. -- peripheral.wrap(glassesName).getUserSurface(playerName)
  374. -- instead of peripheral.wrap(glassesName)
  375.  
  376. function displayImageFromURL(url, glassesPeripheral, maxSize, xo, yo)
  377.    
  378.     local filePath, ok, err = getImagePathFromURL(url, maxSize)
  379.     if not ok then
  380.         return false, err
  381.     end
  382.    
  383.     displayImageFromFile(filePath, glassesPeripheral, xo, yo)
  384.     fs.delete(filePath)
  385.    
  386.     return true
  387. end
  388.  
  389. -- ** Display Image from Rule34 **
  390.  
  391. -- Example:
  392. -- tag = random (just 1)
  393. -- glassesPeripheral = peripheral.wrap(glassesName) [Optional]
  394. -- maxSize = 10 (10 max of height and weight) [Optional]
  395.  
  396. -- NOTE: If you want the image to be send to only 1 player use
  397. -- peripheral.wrap(glassesName).getUserSurface(playerName)
  398. -- instead of peripheral.wrap(glassesName)
  399.  
  400. function displayImageFromRule34(tag, glassesPeripheral, maxSize, xo, yo)
  401.     url, err = getURLfromRule34(tag)
  402.     if url == nil then
  403.         return false, err
  404.     end
  405.     return displayImageFromURL(url, glassesPeripheral, maxSize, xo, yo)
  406.     end
  407.  
  408. function getURLfromRule34(tag)
  409.     if tag == nil then
  410.         tag = "random"
  411.     end
  412.    
  413.     local dataWeb = http.get("http://bmo.emx.cl/rule34CC.php?tag=" .. tag).readAll()
  414.     if not dataWeb then
  415.         err = "[GCAPI] Tag not found"
  416.         print(err)
  417.         return nil, err
  418.     end
  419.    
  420.     return dataWeb
  421. end
  422.  
  423. function getGlasses(glassesPeripheral)
  424.     if glassesPeripheral == nil then
  425.         return peripheral.find("openperipheral_glassesbridge")
  426.     end
  427.     return glassesPeripheral
  428. end
  429.  
  430. -- file = fs.open("imagen","r")
  431. -- glassesPeripheral = peripheral.wrap(glassesName) [Optional]
  432. function displayImageFromFile(filePath, glassesPeripheral, xo, yo)
  433.    
  434.     if xo == nil then
  435.         xo = 1
  436.     end
  437.     if yo == nil then
  438.         yo = 1
  439.     end
  440.    
  441.     numberLines = getNumberOfLinesFile(filePath)
  442.     file = fs.open(filePath, "r")
  443.    
  444.     gb = getGlasses(glassesPeripheral) 
  445.     if gb == nil then
  446.         error("[GCAPI] Glasses peripheral not found")
  447.     end
  448.    
  449.     for i=1,numberLines,1 do
  450.         linea = file.readLine(i)
  451.         lineaQ = {}
  452.         lineaQ = split(linea,",")
  453.         for x,color in pairs(lineaQ) do
  454.             hcolor = assert(loadstring("return "..color))()
  455.             gb.addBox(x+xo,i+yo,1,1,hcolor,1)
  456.         end
  457.     end
  458.     file.close()
  459. end
  460.  
  461. function getImagePathFromURL(url, maxSize, max_x, max_y, forMonitor, filePath)
  462.    
  463.     if filePath == nil then
  464.         filePath = 'gcAPIImage'
  465.     end
  466.  
  467.     if url == nil then
  468.         err = '[GCAPI] No URL for image'
  469.         print(err)
  470.         return nil, false, err
  471.     end
  472.    
  473.     --DELETE SAVED IMAGE
  474.     if url == "none" then
  475.         fs.delete(filePath)
  476.     end
  477.    
  478.     --URL ALIAS
  479.     if urlAlias[url] then
  480.         url = urlAlias[url]
  481.     end
  482.    
  483.     extraParameters = ""
  484.     if maxSize ~= nil and tonumber(maxSize) then
  485.         extraParameters = extraParameters .. '&max_size=' .. maxSize
  486.     end
  487.    
  488.     if max_x ~= nil and tonumber(max_x) then
  489.         extraParameters = extraParameters .. '&max_x=' .. max_x
  490.     end
  491.    
  492.     if max_y ~= nil and tonumber(max_y) then
  493.         extraParameters = extraParameters .. '&max_y=' .. max_y
  494.     end
  495.    
  496.     script = "imageCC"
  497.    
  498.     if forMonitor then
  499.         script = "monitorImageCC"
  500.     end
  501.    
  502.     local dataWeb = http.getStringWithTimeout("http://bmo.emx.cl/" .. script .. ".php?url=" .. url .. extraParameters, nil, 3)
  503.     if not dataWeb then
  504.         err = "[GCAPI] Server error or timeout"
  505.         print(err)
  506.         return nil, false, err
  507.     end
  508.    
  509.     if #dataWeb < 30 then
  510.         err = "[GCAPI] " .. dataWeb
  511.         print(err)
  512.         return nil, false, err
  513.     end
  514.    
  515.     fs.delete(filePath)
  516.    
  517.     f = fs.open(filePath, "w")
  518.     f.write(dataWeb)
  519.     f.close()
  520.    
  521.     return filePath, true, nil
  522.    
  523. end
  524.  
  525. --------------------------------------------
  526. -->             ImageDisplay for         <--
  527. -->                 monitors             <--
  528. --------------------------------------------
  529. function getMonitor(monSelected)
  530.     if monSelected == nil then
  531.         return peripheral.find("monitor")
  532.     end
  533.     return monSelected
  534. end
  535. function printImageFromURL(url, monSelected, centered)
  536.    
  537.     mon = getMonitor(monSelected)
  538.    
  539.     if mon == nil then
  540.         error("[GCAPI] Monitor not found")
  541.     end
  542.    
  543.     mon.setTextScale(0.5)
  544.     mon.clear()
  545.     local max_x, max_y = mon.getSize()
  546.    
  547.     local filePath, ok, err = getImagePathFromURL(url, nil, max_x, max_y, true)
  548.     if not ok then
  549.         return false, err
  550.     end
  551.  
  552.     return printImageFromFile(filePath, mon, centered, true)
  553. end
  554.  
  555. function printAvatar(mon, player_name, x, y, max_side)
  556.     local xo = x
  557.     local yo = y
  558.    
  559.     local filePath, ok, err = getImagePathFromURL("https://minotar.net/avatar/" .. player_name .. "/"..max_side, nil, max_side, max_side, true)
  560.     if not ok then
  561.         return false, err
  562.     end
  563.    
  564.     numberLines = getNumberOfLinesFile(filePath)
  565.     local file = fs.open(filePath, "r")
  566.     for i=1,numberLines,1 do
  567.         linea = file.readLine(i)
  568.         lineaQ = {}
  569.         lineaQ = split(linea,",")
  570.         for x,colorSet in pairs(lineaQ) do
  571.             colors0 = split(colorSet,"-")
  572.             r = tonumber(colors0[1])
  573.             g = tonumber(colors0[2])
  574.             b = tonumber(colors0[3])
  575.            
  576.             if r and g and b then
  577.                 hcolor = colors.fromRGB(r, g, b)
  578.                 mon.setBackgroundColour(hcolor)
  579.                 mon.setCursorPos(x+xo,i+yo)
  580.                 mon.write(" ")
  581.             end
  582.         end
  583.     end
  584.     file.close()
  585.    
  586.     return true
  587. end
  588.  
  589. function printImageFromFile(filePath, monSelected, centered, monSetup)
  590.    
  591.     mon = monSelected
  592.    
  593.     if monSetup == nil or monSetup == false then
  594.         mon = getMonitor(monSelected)
  595.         if mon == nil then
  596.             error("[GCAPI] Monitor not found")
  597.         end
  598.     end
  599.    
  600.     mon.setTextScale(0.5)
  601.     mon.clear()
  602.     local max_x, max_y = mon.getSize()
  603.  
  604.     numberLines = getNumberOfLinesFile(filePath)
  605.    
  606.     xo = 0
  607.     yo = 0
  608.    
  609.     if centered and numberLines > 0 then
  610.         file = fs.open(filePath, "r")
  611.         real_x = #(split(file.readLine(1), ","))
  612.         file.close()
  613.         xo = getCenter(max_x, real_x)
  614.         yo = getCenter(max_y, numberLines)
  615.     end
  616.    
  617.    
  618.     file = fs.open(filePath, "r")
  619.     for i=1,numberLines,1 do
  620.         linea = file.readLine(i)
  621.         lineaQ = {}
  622.         lineaQ = split(linea,",")
  623.         for x,colorSet in pairs(lineaQ) do
  624.             colors0 = split(colorSet,"-")
  625.             r = tonumber(colors0[1])
  626.             g = tonumber(colors0[2])
  627.             b = tonumber(colors0[3])
  628.            
  629.             if r and g and b then
  630.                 hcolor = colors.fromRGB(r, g, b)
  631.                 mon.setBackgroundColour(hcolor)
  632.                 mon.setCursorPos(x+xo,i+yo)
  633.                 mon.write(" ")
  634.             end
  635.         end
  636.     end
  637.     file.close()
  638.    
  639.     return true
  640. end
  641.  
  642. --------------------------------------------
  643. -->             ImageDisplay for         <--
  644. -->            chunks of monitors        <--
  645. --------------------------------------------
  646. local chunksHeight = 0
  647. local chunksWidth = 0
  648. monitors = {}
  649.  
  650. function printPixel(x,y,color)
  651.    
  652. end
  653.  
  654.  
  655. --------------------------------------------
  656. -->            Tracker of players        <--
  657. -->          Just for craftersland       <--
  658. -->                  DYNMAP              <--
  659. --------------------------------------------
  660. function getPosPlayer(player, dataPlayers)
  661.     if player == nil or player == "" then
  662.         err = "User not found"
  663.         print("[GCAPI] " .. err)
  664.         return nil, false, err
  665.     end
  666.    
  667.     player = string.lower(player)
  668.    
  669.     if not dataPlayers then
  670.         dataPlayers, ok, err = getPosPlayers()
  671.     end
  672.    
  673.     for i,dataPlayer in pairs(dataPlayers) do
  674.         user = string.lower(dataPlayer["name"])
  675.         if string.match(user, player) then
  676.             return dataPlayer, true, nil
  677.         end
  678.     end
  679.    
  680.     err = "User not found"
  681.     return nil, false, err
  682. end
  683.  
  684. function getPosPlayers(password)
  685.     local url = "http://bmo.emx.cl/dynmapPos.php"
  686.     if password then
  687.         url = url .. "?password=" .. password
  688.     end
  689.     local dataWeb = http.get(url)
  690.     local dataPlayers = textutils.unserialize(dataWeb.readAll())
  691.     dataWeb.close()
  692.    
  693.     if not dataWeb then
  694.         return nil, false, "[GCAPI] Server Error"
  695.     end
  696.    
  697.     if dataPlayers["error"] ~= nil then
  698.         return nil, false, dataPlayers["error"]
  699.     end
  700.    
  701.     return dataPlayers, true, nil
  702. end
  703.  
  704. function getClosePlayers(user, range)
  705.     local url = "http://bmo.emx.cl/closePlayers.php?user=" .. user .. "&range=" .. range
  706.    
  707.     local dataWeb = http.get(url)
  708.     local closePlayers = textutils.unserialize(dataWeb.readAll())
  709.     dataWeb.close()
  710.    
  711.     if not dataWeb then
  712.         return nil, false, "[GCAPI] Server Error"
  713.     end
  714.    
  715.     if closePlayers["error"] ~= nil then
  716.         return nil, false, closePlayers["error"]
  717.     end
  718.    
  719.     return closePlayers, true, nil
  720. end
  721. --------------------------------------------
  722. -->            Chat tracker for          <--
  723. -->              Craftersland            <--
  724. --------------------------------------------
  725.  
  726. function getChat()
  727.     dataWeb = http.get("http://bmo.emx.cl/chatSpy.php")
  728.     dataChat = textutils.unserialize(dataWeb.readAll())
  729.     dataWeb.close()
  730.    
  731.     if not dataWeb then
  732.         return nil, false, "[GCAPI] Server Error"
  733.     end
  734.    
  735.     return dataChat, true, nil
  736. end
  737.  
  738.  
  739. --------------------------------------------
  740. -->                Economy               <--
  741. -->                                      <--
  742. -->         This is saved every use      <--
  743. -->           for safety purposes.       <--
  744. --------------------------------------------
  745.  
  746. function getMoney(player)
  747.     list = getListFromFile("/disk/economy")
  748.    
  749.     player = string.lower(player)
  750.    
  751.     for name,amount in pairs(list) do
  752.         if name == player then
  753.             return amount
  754.         end
  755.     end
  756.     setMoney(player, 0)
  757.     return 0
  758. end
  759.  
  760. function setMoney(player, amount)
  761.    
  762.     player = string.lower(player)
  763.    
  764.     list = getListFromFile("/disk/economy")
  765.    
  766.     fs.delete("/disk/economy")
  767.    
  768.     list[player]=amount
  769.    
  770.     saveListToFile("/disk/economy", list)
  771. end
  772.  
  773. function withdraw(player, amount)  
  774.     player = string.lower(player)
  775.    
  776.     if getMoney(player) < amount then
  777.         return false
  778.     end
  779.    
  780.     setMoney(player, (getMoney(player)-amount))
  781.     return true
  782. end
  783.  
  784. function addMoney(player, money)
  785.    
  786.     player = string.lower(player)
  787.    
  788.     setMoney(player,getMoney(player)+money)
  789. end
  790. --------------------------------------------
  791. -->          Admins for selling          <--
  792. --------------------------------------------
  793. admins = {"GravityCube", "Archmaestro", "Lancellot", "Freecss"}
  794. function hasPermissions(player, globalAdmins)
  795.     if globalAdmins == nil then
  796.         globalAdmins = true
  797.     end
  798.     for _,user in pairs(getAdmins(globalAdmins)) do
  799.         if user == player then
  800.             return true
  801.         end
  802.     end
  803.     return false
  804. end
  805.  
  806. function getAdmins(globalAdmins)
  807.     localAdmins = {}
  808.     if fs.exists("admins") then
  809.         localAdmins = getListFromFile("admins")
  810.     end
  811.    
  812.     if globalAdmins then
  813.         for _,admin in pairs(admins) do
  814.             table.insert(localAdmins, admin)
  815.         end
  816.     end
  817.    
  818.     return localAdmins
  819. end
  820. --------------------------------------------
  821. -->              EventHanlder            <--
  822. --------------------------------------------
  823. if not lastID then
  824.     lastID = 0
  825. end
  826. function startChatEventQueue()
  827.     while true do
  828.         chatData, ok, err = getChat()
  829.         if ok then
  830.             lastIDTemp = lastID
  831.             for i=1, 10, 1 do
  832.                 data = chatData[i]
  833.                 if data ~= nil then
  834.                     id = data["id"]
  835.                     if id > lastID then
  836.                         if id > lastIDTemp then
  837.                             lastIDTemp = id
  838.                         end
  839.                        
  840.                         player = data["name"]
  841.                         command = data["message"]
  842.                        
  843.                         if command ~= nil and string.starts(command, "!") then
  844.                             command = string.sub(command, 2)
  845.                             lastID = lastIDTemp
  846.                             os.queueEvent("chatEvent", player, command)
  847.                         end
  848.                     end    
  849.                 end
  850.             end
  851.             lastID = lastIDTemp
  852.         end
  853.         sleep(1)
  854.     end
  855. end
  856.  
  857.  
  858. --*--*--*--*--*--*--*--*--*--*--*--*--*--*--
  859. --*             RBG TO COLOUR            *--
  860. --*          by CrazedProgrammer         *--
  861. --*    (Used in the printImageFromURL)   *--
  862. --*   https://pastebin.com/BCSWghjR/rgb  *--
  863. --*--*--*--*--*--*--*--*--*--*--*--*--*--*--
  864. local hex = {"F0F0F0", "F2B233", "E57FD8", "99B2F2", "DEDE6C", "7FCC19", "F2B2CC", "4C4C4C", "999999", "4C99B2", "B266E5", "3366CC", "7F664C", "57A64E", "CC4C4C", "191919"}
  865. local rgb = {}
  866. for i=1,16,1 do
  867.   rgb[i] = {tonumber(string.sub(hex[i],1, 2), 16), tonumber(string.sub(hex[i],3, 4), 16), tonumber(string.sub(hex[i], 5, 6), 16)}
  868. end
  869.  
  870. colors.fromRGB = function (r, g, b)
  871.   local dist = 1e100
  872.   local d = 1e100
  873.   local color = -1
  874.   for i=1,16,1 do
  875.     d = math.sqrt((math.max(rgb[i][1], r) - math.min(rgb[i][1], r)) ^ 2 + (math.max(rgb[i][2], g) - math.min(rgb[i][2], g)) ^ 2 + (math.max(rgb[i][3], b) - math.min(rgb[i][3], b)) ^ 2)
  876.     if d < dist then
  877.       dist = d
  878.       color = i - 1
  879.     end
  880.   end
  881.   return 2 ^ color
  882. end
  883.  
  884. colours.fromRGB = colors.fromRGB
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top