Advertisement
Arc13

Player Detector Beta

Sep 15th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 25.83 KB | None | 0 0
  1. local sVersion = "v0.9b3"
  2. local nBuild = 90
  3. local debugMode = false
  4.  
  5. -- Variables par défaut
  6.  
  7. local sLogFormat = "#y#m#d.log"
  8. local sLogDir = "/playerd_logs/"
  9.  
  10. local useChatInterface = true
  11. local printLog = true
  12. local disableTerminateEvent = false
  13. local disableAutomaticUpdates = false
  14.  
  15. --[[
  16. Liste des variables pour les messages du chat :
  17.  
  18. #p : Nom du joueur qui entre/sort
  19. #M : Minute réelle
  20. #h : Heure réelle
  21. #d : Jour réel
  22. #m : Mois réel
  23. #y : Année réelle
  24. --]]
  25.  
  26. local joinMessage = "#p joined"
  27. local leftMessage = "#p left"
  28. local chatTo = {"your_name"}
  29. local chatName = "Player Probe "..sVersion.." on "..os.computerID()
  30. local canUseCommands = {"your_name"}
  31.  
  32. local tWhitelist = {}
  33.  
  34. local joinProgram = ""
  35. local leftProgram = ""
  36.  
  37. -- Fin des variables par défaut
  38.  
  39. -- Déclaration de la fonction au centre du programme
  40.  
  41. local function getTableDifference(oldTable, newTable)
  42.   if not newTable then
  43.     return false
  44.   end
  45.  
  46.   local tDifference = {}
  47.  
  48.   --[[
  49.  
  50.   print("oldTable :")
  51.  
  52.   for i = 1, #oldTable do
  53.     print(oldTable[i])
  54.   end
  55.  
  56.   print("newTable :")
  57.  
  58.   for i = 1, #newTable do
  59.     print(newTable[i])
  60.   end
  61.  
  62.   print("Différences :")
  63.  
  64.   --]]
  65.  
  66.   for i = 1, #oldTable do
  67.     local isOK = false
  68.  
  69.     local selectedTable = oldTable[i]
  70.     --print("J'ai "..selectedTable.." actuellement")
  71.  
  72.     for i = 1, #newTable do
  73.       --print("Je compare "..selectedTable.." avec "..newTable[i])
  74.  
  75.       if selectedTable == newTable[i] then
  76.         --print("OK")
  77.         isOK = true
  78.         break
  79.       end
  80.     end
  81.  
  82.     if isOK == false then
  83.       --print(selectedTable.." n'est pas dans table2")
  84.       table.insert(tDifference, selectedTable)
  85.     end
  86.   end
  87.  
  88.   return tDifference
  89. end
  90.  
  91. -- Fin de la déclaration
  92.  
  93. -- Init Environment
  94.  
  95. term.setBackgroundColor(colors.black)
  96. term.setTextColor(colors.white)
  97. term.clear()
  98. term.setCursorPos(1, 1)
  99. print("Player Detector Init Environment")
  100. term.setCursorPos(1, 2)
  101.  
  102. for i = 1, term.getSize() do
  103.   write("-")
  104. end
  105.  
  106. term.setCursorPos(1, 4)
  107.  
  108. if not fs.exists("player_detector.conf") then
  109.   print("\nNo config file found, init setup...")
  110.   local configFile = fs.open("player_detector.conf", "w")
  111.   configFile.write("{\nLogFormat = \"#y#M#d.log\", \nLogDir = \"/playerd_logs/\", \nuseChatInterface = true, \nprintLog = true, \ndisableTerminateEvent = false, \ndisableAutomaticUpdates = false, \njoinMessage = \"#p joined\", \nleftMessage = \"#p left\", \nchatTo = {\"your_name\"}, \nchatName = \"".."Player Probe on "..os.computerID().."\", \ncanUseCommands = {\"your_name\"}, \ntWhitelist = {}, \njoinProgram = \"\", \nleftProgram = \"\", \n}")
  112.   configFile.close()
  113.   print("\nFile created, a text editor will be prompt to edit settings, please save before quit the editor.")
  114.   print("Press any key to continue...")
  115.   os.pullEvent("key")
  116.  
  117.   shell.run("edit player_detector.conf")
  118.  
  119.   term.setBackgroundColor(colors.black)
  120.   term.setTextColor(colors.white)
  121.   term.clear()
  122.   term.setCursorPos(1, 1)
  123.   print("Player Detector Init Environment")
  124.   term.setCursorPos(1, 2)
  125.  
  126.   for i = 1, term.getSize() do
  127.     write("-")
  128.   end
  129.  
  130.   term.setCursorPos(1, 4)
  131. else
  132.   print("\nConfig file found")
  133. end
  134.  
  135. print("Loading settings...")
  136. settings.load("player_detector.conf")
  137.  
  138. sLogFormat = settings.get("LogFormat", sLogFormat)
  139. sLogDir = settings.get("LogDir", sLogDir)
  140. useChatInterface = settings.get("useChatInterface", useChatInterface)
  141. printLog = settings.get("printLog", printLog)
  142. disableTerminateEvent = settings.get("disableTerminateEvent", disableTerminateEvent)
  143. disableAutomaticUpdates = settings.get("disableAutomaticUpdates", disableAutomaticUpdates)
  144. joinMessage = settings.get("joinMessage", joinMessage)
  145. leftMessage = settings.get("leftMessage", leftMessage)
  146. chatTo = settings.get("chatTo", chatTo)
  147. chatName = settings.get("chatName", chatName)
  148. canUseCommands = settings.get("canUseCommands", canUseCommands)
  149. tWhitelist = settings.get("tWhitelist", tWhitelist)
  150. joinProgram = settings.get("joinProgram", joinProgram)
  151. leftProgram = settings.get("leftProgram", leftProgram)
  152.  
  153. -- Check new version
  154.  
  155. if not disableAutomaticUpdates then
  156.   print("\nChecking for updates...")
  157.  
  158.   local sBuildNet = http.get("http://pastebin.com/raw/ZD8t8ZSK")
  159.   local nBuildNet = tonumber(sBuildNet.readAll())
  160.   sBuildNet.close()
  161.  
  162.   if nBuildNet > nBuild then
  163.     print("A new version is available ! (build "..nBuildNet..")")
  164.       --print("I'm running "..shell.getRunningProgram())
  165.       print("Downloading new version...")
  166.  
  167.       local sNewSoftware = http.get("http://pastebin.com/raw/7Jg670Ra")
  168.  
  169.       local sTempFile = fs.open(".temp", "w")
  170.       sTempFile.write(sNewSoftware.readAll())
  171.       sTempFile.close()
  172.  
  173.       sNewSoftware.close()
  174.  
  175.       if fs.exists(".temp") then
  176.         print("Downloaded succesfully, updating...")
  177.         fs.delete(shell.getRunningProgram())
  178.         fs.move(".temp", shell.getRunningProgram())
  179.         fs.delete(".temp")
  180.         write("Rebooting")
  181.       write(".")
  182.       sleep(1)
  183.       write(".")
  184.       sleep(1)
  185.       write(".")
  186.       sleep(1)
  187.       os.reboot()
  188.     else
  189.       print("Download fail...")
  190.     end
  191.   else
  192.     print("You are on the latest version")
  193.   end
  194. else
  195.   print("\nAutomatics updates are disabled.")
  196. end
  197.  
  198. print("")
  199.  
  200. print("Checking config file...")
  201. local tKeyAllowed = {"{", "LogFormat", "LogDir", "useChatInterface", "printLog", "disableTerminateEvent", "disableAutomaticUpdates", "joinMessage", "leftMessage", "chatTo", "chatName", "canUseCommands", "tWhitelist", "joinProgram", "leftProgram", "}"}
  202. local tPresentKeys = {}
  203. local tErroredKey = {}
  204. local tConfigLines = {}
  205. local sActualLine = ""
  206. local sScanningLine = ""
  207. local sScanningKey = ""
  208. local bScanResult = false
  209.  
  210. local configCheck = fs.open("player_detector.conf", "r")
  211.  
  212. while sActualLine ~= nil do
  213.   sActualLine = configCheck.readLine()
  214.   table.insert(tConfigLines, sActualLine)
  215. end
  216.  
  217. configCheck.close()
  218.  
  219. for i = 1, #tConfigLines do
  220.   sScanningLine = tConfigLines[i]
  221.   bScanResult = false
  222.  
  223.   for i = 1, #tKeyAllowed do
  224.     sScanningKey = sScanningLine:sub(1, #tKeyAllowed[i])
  225.     if sScanningKey == tKeyAllowed[i] then
  226.       bScanResult = true
  227.       break
  228.     end
  229.   end
  230.  
  231.   if bScanResult == false then
  232.     if not sScanningLine:sub(1, 7) == "LogFile" then
  233.       table.insert(tErroredKey, "L"..i.." > \""..sScanningLine.."\"")
  234.     end
  235.   else
  236.     table.insert(tPresentKeys, sScanningKey)
  237.   end
  238. end
  239.  
  240. if #tErroredKey > 0 then
  241.   if #tErroredKey == 1 then
  242.     printError("Errored key found :")
  243.   else
  244.     printError("Errored keys found :")
  245.   end
  246.   print("")
  247.  
  248.   textutils.pagedTabulate(tErroredKey)
  249.   print("\nPress any key to continue...")
  250.   os.pullEvent("key")
  251. else
  252.   print("No errored key found !")
  253. end
  254.  
  255. local tKeyDifference = getTableDifference(tKeyAllowed, tPresentKeys)
  256.  
  257. if #tKeyDifference > 0 then
  258.   if #tKeyDifference == 1 then
  259.     printError("Missing key found :")
  260.   else
  261.     printError("Missing keys found :")
  262.   end
  263.  
  264.   print("")
  265.   textutils.pagedTabulate(tKeyDifference)
  266.   print("")
  267.  
  268.   print("<          >")
  269.  
  270.   local nCurX, nCurY = term.getCursorPos()
  271.   term.setCursorPos(2, nCurY - 1)
  272.  
  273.   for i = 1, 10 do
  274.     sleep(1)
  275.     write("=")
  276.   end
  277.  
  278.   sleep(1)
  279. else
  280.   print("No missing key found !")
  281. end
  282.  
  283. --sleep(5)
  284.  
  285. -- End Init Environment
  286.  
  287. local bRun = true
  288.  
  289. local monX, monY = term.getSize()
  290. local oldMonX, oldMonY = term.getSize()
  291.  
  292. local tActualDate = {}
  293.  
  294. if monX < 29 then
  295.   printError("Resolution is too low !")
  296.   sleep(5)
  297. end
  298.  
  299. if disableTerminateEvent == true then
  300.   local oldPullEvent = os.pullEvent
  301.   os.pullEvent = os.pullEventRaw
  302. end
  303.  
  304. --LIP : EntityDetector / OpenCCSensor : sensor
  305. --LIP : WorldInterface / Peripherals++ : timeSensor
  306. --LIP : ChatInterface / Peripherals++ : chatBox
  307.  
  308. local t = peripheral.find("sensor")
  309. local p = peripheral.find("timeSensor")
  310. local c = peripheral.find("chatBox")
  311.  
  312. local tSide = ""
  313. local pSide = ""
  314. local cSide = ""
  315.  
  316. local bScrollEffectDone = false
  317.  
  318. local chatInterfaceConnected = true
  319.  
  320. if not t then
  321.   error("No sensor found !")
  322. end
  323.  
  324. if t.getSensorName() ~= "proximityCard" then
  325.   error("No proximityCard inserted !")
  326. end
  327.  
  328. if not p then
  329.   error("No timeSensor found !")
  330. end
  331.  
  332. if not c then
  333.   chatInterfaceConnected = false
  334. end
  335.  
  336. for i = 1, #peripheral.getNames() do
  337.   local sPeripheralType = peripheral.getType(peripheral.getNames()[i])
  338.  
  339.   if sPeripheralType == "EntityDetector" then
  340.     tSide = peripheral.getNames()[i]
  341.   elseif sPeripheralType == "WorldInterface" then
  342.     pSide = peripheral.getNames()[i]
  343.   elseif sPeripheralType == "ChatInterface" then
  344.     cSide = peripheral.getNames()[i]
  345.   end
  346. end
  347.  
  348. local tPlayers = {}
  349. local tOldPlayers = {}
  350.  
  351. tInventoryPlayers = {}
  352. tActualInventoryPlayers = {}
  353.  
  354. if not fs.exists(sLogDir) then
  355.   fs.makeDir(sLogDir)
  356. end
  357.  
  358. term.clear()
  359. term.setCursorPos(1, 1)
  360.  
  361. if monX >= 51 then
  362.   print("Player detector "..sVersion)
  363.   term.setCursorPos(monX - string.len(string.char(174).." arc13") + 1, 1)
  364.   term.write(string.char(174).." arc13")
  365.  
  366.   if term.isColor() then
  367.     term.setTextColor(colors.lightGray)
  368.   end
  369.  
  370.   term.setCursorPos(1, 3)
  371.   if useChatInterface and chatInterfaceConnected then
  372.     print("ChatBox enabled")
  373.   else
  374.     print("ChatBox disabled")
  375.   end
  376.  
  377.   term.setCursorPos(1, 4)
  378. elseif monX < 51 and monX >= 29 then
  379.   print("Player detector "..sVersion)
  380.   term.setCursorPos(monX - string.len(string.char(174).." arc13") + 1, 1)
  381.   term.write(string.char(174).." arc13")
  382.   term.setCursorPos(1, 3)
  383.   if useChatInterface and chatInterfaceConnected then
  384.     print("ChatBox enabled")
  385.   else
  386.     print("ChatBox disabled")
  387.   end
  388.  
  389.   term.setCursorPos(1, 4)
  390. end
  391.  
  392. if term.isColor() then
  393.   term.setTextColor(colors.gray)
  394. end
  395.  
  396. for i = 1, term.getSize() do
  397.   write("-")
  398. end
  399.  
  400. term.setTextColor(colors.white)
  401.  
  402. print("\n")
  403.  
  404. if not printLog then
  405.   print("Log here is disabled !")
  406. end
  407.  
  408. local function redrawHeader()
  409.   local oldCurX, oldCurY = term.getCursorPos()
  410.  
  411.   term.setCursorPos(1, 1)
  412.   term.clearLine()
  413.   print("Player detector "..sVersion)
  414.   term.setCursorPos(monX - string.len(string.char(174).." arc13") + 1, 1)
  415.   term.write(string.char(174).." arc13")
  416.  
  417.   if term.isColor() then
  418.     term.setTextColor(colors.gray)
  419.   end
  420.  
  421.   if bScrollEffectDone == true then
  422.     term.setCursorPos(1, 2)
  423.   else
  424.     if monX >= 51 then
  425.       term.setCursorPos(1, 4)
  426.     elseif monX < 51 and monX >= 29 then
  427.       term.setCursorPos(1, 4)
  428.     end
  429.   end
  430.  
  431.   for i = 1, term.getSize() do
  432.     write("-")
  433.   end
  434.  
  435.   term.setTextColor(colors.white)
  436.  
  437.   if monX < 29 then
  438.     term.clear()
  439.     term.setCursorPos(1, 1)
  440.     print("Resolution too low !")
  441.   end
  442.  
  443.   term.setCursorPos(oldCurX, oldCurY)
  444. end
  445.  
  446. local function scrollEffect()
  447.   sleep(5)
  448.  
  449.   local oldCurX, oldCurY = term.getCursorPos()
  450.  
  451.   for i = 1, 1 do
  452.     term.scroll(1)
  453.  
  454.     term.setCursorPos(1, 1)
  455.     print("Player detector "..sVersion)
  456.     term.setCursorPos(monX - string.len(string.char(174).." arc13") + 1, 1)
  457.     term.write(string.char(174).." arc13")
  458.     paintutils.drawLine(1, 2, monX, 2, colors.black)
  459.  
  460.     if monX < 51 and monX >= 29 and i == 2 then
  461.       term.setCursorPos(1, 2)
  462.  
  463.       if term.isColor() then
  464.         term.setTextColor(colors.gray)
  465.       end
  466.  
  467.       for i = 1, term.getSize() do
  468.         write("-")
  469.       end
  470.  
  471.       term.setTextColor(colors.white)
  472.     end
  473.  
  474.     sleep(0.2)
  475.   end
  476.   term.scroll(1)
  477.  
  478.   term.setCursorPos(1, 1)
  479.   print("Player detector "..sVersion)
  480.   term.setCursorPos(monX - string.len(string.char(174).." arc13") + 1, 1)
  481.   term.write(string.char(174).." arc13")
  482.  
  483.   if monX < 51 and monX >= 29 then
  484.     term.setCursorPos(1, 2)
  485.  
  486.     if term.isColor() then
  487.       term.setTextColor(colors.gray)
  488.     end
  489.  
  490.     for i = 1, term.getSize() do
  491.       write("-")
  492.     end
  493.  
  494.     term.setTextColor(colors.white)
  495.   end
  496.  
  497.   bScrollEffectDone = true
  498.  
  499.   redrawHeader()
  500.  
  501.   if monX >= 51 then
  502.     term.setCursorPos(oldCurX, oldCurY - 3)
  503.   elseif monX < 51 and monX >= 29 then
  504.     term.setCursorPos(oldCurX, oldCurY - 2)
  505.   end
  506. end
  507.  
  508. local function isInWhitelist(sPlayerName)
  509.   for i = 1, #tWhitelist do
  510.     if sPlayerName == tWhitelist[i] then
  511.       --c.sendPlayerMessage("arc13", "whitelisted")
  512.       return true
  513.     end
  514.   end
  515.  
  516.   --c.sendPlayerMessage("arc13", "not whitelisted")
  517.   return false
  518. end
  519.  
  520. local function getInventorySize(tInventory)
  521.   local i = 0
  522.  
  523.   for k, v in pairs(tInventory) do
  524.     i = i + 1
  525.   end
  526.  
  527.   return i
  528. end
  529.  
  530. local function getPlayers()
  531.   local playerList = {}
  532.   local f = t.getTargets()
  533.  
  534.   for k, v in pairs(f) do
  535.     if v.IsPlayer then
  536.       table.insert(playerList, k)
  537.     end
  538.   end
  539.  
  540.   for i = 1, #playerList do
  541.     --print(playerList[i])
  542.   end
  543.  
  544.   return playerList
  545. end
  546.  
  547. function logJoin(sPlayerJoined)
  548.   if printLog then
  549.     if term.isColor() then
  550.       term.setTextColor(colors.lightGray)
  551.     end
  552.  
  553.     local sFormattedPlayer = ""
  554.  
  555.     if #tPlayers > 1 then
  556.       sFormattedPlayer = "players"
  557.     else
  558.       sFormattedPlayer = "player"
  559.     end
  560.  
  561.     tActualDate = p.getDate()
  562.     write("["..string.format("%02i", tActualDate.hour)..":"..string.format("%02i", tActualDate.minute)..", "..#tPlayers.." "..sFormattedPlayer.."] ")
  563.     term.setTextColor(colors.white)
  564.     print(sPlayerJoined.." join")
  565.   end
  566.  
  567.   local sLogName = sLogFormat
  568.   sLogName = sLogName:gsub("#m", string.format("%02i", tActualDate.minute))
  569.   sLogName = sLogName:gsub("#h", string.format("%02i", tActualDate.hour))
  570.   sLogName = sLogName:gsub("#d", string.format("%02i", tActualDate.day))
  571.   sLogName = sLogName:gsub("#M", string.format("%02i", tActualDate.month))
  572.   sLogName = sLogName:gsub("#y", string.format("%02i", tActualDate.year))
  573.  
  574.   local file = fs.open(sLogDir..sLogName, fs.exists(sLogDir..sLogName) and "a" or "w")
  575.  
  576.   file.writeLine("["..string.format("%02i", tActualDate.hour)..":"..string.format("%02i", tActualDate.minute).."] "..sPlayerJoined.." join")
  577.   file.close()
  578.  
  579.   if useChatInterface == true and chatInterfaceConnected == true then
  580.     for i = 1, #chatTo do
  581.       local sMsg = string.gsub(joinMessage, "#p", sPlayerJoined)
  582.       sMsg = sMsg:gsub("#M", string.format("%02i", tActualDate.minute))
  583.       sMsg = sMsg:gsub("#h", string.format("%02i", tActualDate.hour))
  584.       sMsg = sMsg:gsub("#d", string.format("%02i", tActualDate.day))
  585.       sMsg = sMsg:gsub("#m", string.format("%02i", tActualDate.month))
  586.       sMsg = sMsg:gsub("#y", string.format("%02i", tActualDate.year))
  587.       c.tell(chatTo[i], sMsg)
  588.     end
  589.   end
  590. end
  591.  
  592. local function logLeft(sPlayerLeft)
  593.   if printLog then
  594.     if term.isColor() then
  595.       term.setTextColor(colors.lightGray)
  596.     end
  597.  
  598.     local sFormattedPlayer = ""
  599.  
  600.     if #tPlayers > 1 then
  601.       sFormattedPlayer = "players"
  602.     else
  603.       sFormattedPlayer = "player"
  604.     end
  605.  
  606.     tActualDate = p.getDate()
  607.     write("["..string.format("%02i", tActualDate.hour)..":"..string.format("%02i", tActualDate.minute)..", "..#tPlayers.." "..sFormattedPlayer.."] ")
  608.     term.setTextColor(colors.white)
  609.     print(sPlayerLeft.." left")
  610.   end
  611.  
  612.   local sLogName = sLogFormat
  613.   sLogName = sLogName:gsub("#m", string.format("%02i", tActualDate.minute))
  614.   sLogName = sLogName:gsub("#h", string.format("%02i", tActualDate.hour))
  615.   sLogName = sLogName:gsub("#d", string.format("%02i", tActualDate.day))
  616.   sLogName = sLogName:gsub("#M", string.format("%02i", tActualDate.month))
  617.   sLogName = sLogName:gsub("#y", string.format("%02i", tActualDate.year))
  618.  
  619.   local file = fs.open(sLogDir..sLogName, fs.exists(sLogDir..sLogName) and "a" or "w")
  620.  
  621.   file.writeLine("["..string.format("%02i", tActualDate.hour)..":"..string.format("%02i", tActualDate.minute).."] "..sPlayerLeft.." left")
  622.  
  623.   file.close()
  624.  
  625.   if useChatInterface == true and chatInterfaceConnected == true then
  626.     for i = 1, #chatTo do
  627.       local sMsg = string.gsub(leftMessage, "#p", sPlayerLeft)
  628.       sMsg = sMsg:gsub("#M", string.format("%02i", tActualDate.minute))
  629.       sMsg = sMsg:gsub("#h", string.format("%02i", tActualDate.hour))
  630.       sMsg = sMsg:gsub("#d", string.format("%02i", tActualDate.day))
  631.       sMsg = sMsg:gsub("#m", string.format("%02i", tActualDate.month))
  632.       sMsg = sMsg:gsub("#y", string.format("%02i", tActualDate.year))
  633.       c.tell(chatTo[i], sMsg)
  634.     end
  635.   end
  636. end
  637.  
  638. local function playerJoin(tPlayers, tOldPlayers)
  639.   local tDifference = getTableDifference(tPlayers, tOldPlayers)
  640.  
  641.   if joinProgram ~= "" and multishell then
  642.     local sTempPlayers = ""
  643.  
  644.     for i = 1, #tDifference do
  645.       sTempPlayers = sTempPlayers..tDifference[i]..","
  646.     end
  647.  
  648.     shell.openTab(joinProgram.." "..sTempPlayers)
  649.   end
  650.  
  651.   os.queueEvent("player_join", tDifference)
  652.  
  653.   for i = 1, #tDifference do
  654.     if not isInWhitelist(tDifference[i]) then
  655.       logJoin(tDifference[i])
  656.  
  657.       redrawHeader()
  658.     else
  659.       if printLog then
  660.         print("Whitelisted : "..tDifference[i])
  661.       end
  662.     end
  663.   end
  664. end
  665.  
  666. local function playerLeft(tPlayers, tOldPlayers)
  667.   local tDifference = getTableDifference(tOldPlayers, tPlayers)
  668.  
  669.   if debugMode then
  670.     print("Differences:")
  671.     for i = 1, #tDifference do
  672.       print(tDifference[i])
  673.     end
  674.   end
  675.  
  676.   if leftProgram ~= "" and multishell then
  677.     local sTempPlayers = ""
  678.  
  679.     for i = 1, #tDifference do
  680.       sTempPlayers = sTempPlayers..tDifference[i]..","
  681.     end
  682.  
  683.     shell.openTab(leftProgram.." "..sTempPlayers)
  684.   end
  685.  
  686.   os.queueEvent("player_left", tDifference)
  687.  
  688.   for i = 1, #tDifference do
  689.     if not isInWhitelist(tDifference[i]) then
  690.       logLeft(tDifference[i])
  691.  
  692.       redrawHeader()
  693.     else
  694.       if printLog then
  695.         print("Whitelisted : "..tDifference[i])
  696.       end
  697.     end
  698.   end
  699. end
  700.  
  701. local function main()
  702.   while bRun do
  703.     tOldPlayers = tPlayers
  704.     tPlayers = getPlayers()
  705.    
  706.     if debugMode then
  707.       print("tOldPlayers:")
  708.       for i = 1, #tOldPlayers do
  709.         print(tOldPlayers[i])
  710.       end
  711.      
  712.       print("tPlayers:")
  713.       for i = 1, #tPlayers do
  714.         print(tPlayers[i])
  715.       end
  716.      
  717.       --sleep(5)
  718.     end
  719.  
  720.     if #tPlayers ~= #tOldPlayers then
  721.       if debugMode then
  722.         print("New player")
  723.       end
  724.       if #tPlayers > #tOldPlayers then
  725.         if debugMode then
  726.           print("Player joined")
  727.         end
  728.  
  729.         threadJoin = coroutine.create(playerJoin)
  730.         coroutine.resume(threadJoin, tPlayers, tOldPlayers)
  731.       elseif #tPlayers < #tOldPlayers then
  732.         if debugMode then
  733.           print("Player left")
  734.         end
  735.  
  736.         threadLeft = coroutine.create(playerLeft)
  737.         coroutine.resume(threadLeft, tPlayers, tOldPlayers)
  738.       end
  739.     else
  740.       if debugMode then
  741.         --print("No new player")
  742.       end
  743.     end
  744.  
  745.     sleep(0.1)
  746.   end
  747. end
  748.  
  749. -- Inutilisable avant que les events soit fixés
  750. local function peripheralHandler()
  751.   while bRun do
  752.     redrawHeader()
  753.  
  754.     local sEvent, sSide = os.pullEvent()
  755.  
  756.     if sEvent == "peripheral" or sEvent == "peripheral_detach" then
  757.       print(sSide)
  758.       print(pSide)
  759.       print(tSide)
  760.       print(cSide)
  761.     end
  762.  
  763.     if sEvent == "peripheral" then
  764.       if peripheral.getType(sSide) == "ChatInterface" then
  765.         chatInterfaceConnected = true
  766.         print("[SYSTEM] ChatInterface connected")
  767.         cSide = sSide
  768.       end
  769.     elseif sEvent == "peripheral_detach" then
  770.       if sSide == pSide then
  771.         -- Le world interface à été détaché
  772.         error("[SYSTEM] WorldInterface disconnected")
  773.       elseif sSide == tSide then
  774.         -- L'entity detector à été détaché
  775.         error("[SYSTEM] EntityDetector disconnected")
  776.       elseif sSide == cSide then
  777.         -- Le chat interface à été détaché
  778.         chatInterfaceConnected = false
  779.         cSide = ""
  780.         print("[SYSTEM] ChatInterface disconnected")
  781.       end
  782.     end
  783.   end
  784. end
  785.  
  786. local function chatHandler()
  787.   while bRun do
  788.     local sEvent, sPlayer, sMessage = os.pullEvent("chat_message")
  789.  
  790.     for i = 1, #canUseCommands do
  791.       if sPlayer == canUseCommands[i] then
  792.         if sMessage == "##disable_chat" then
  793.           useChatInterface = false
  794.           c.sendPlayerMessage(sPlayer, "Chat disabled !")
  795.         elseif sMessage == "##enable_chat" then
  796.           useChatInterface = true
  797.           c.sendPlayerMessage(sPlayer, "Chat enabled !")
  798.         elseif sMessage == "##stop" then
  799.           c.sendPlayerMessage(sPlayer, "Terminated")
  800.           os.pullEvent = oldPullEvent
  801.           bRun = false
  802.         elseif sMessage == "##get_players" then
  803.           local tPlayersToSend = getPlayers()
  804.  
  805.           local sMsgToSend = ""
  806.  
  807.           if #tPlayersToSend == 0 then
  808.             c.sendPlayerMessage(sPlayer, "No players detected")
  809.           elseif #tPlayersToSend == 1 then
  810.             c.sendPlayerMessage(sPlayer, "Player : "..tPlayersToSend[1])
  811.           else
  812.             for i = 1, #tPlayersToSend - 1 do
  813.               sMsgToSend = sMsgToSend..tPlayersToSend[i]..", "
  814.             end
  815.  
  816.             sMsgToSend = sMsgToSend..tPlayersToSend[#tPlayersToSend]
  817.  
  818.             c.sendPlayerMessage(sPlayer, sMsgToSend)
  819.           end
  820.         end
  821.       end
  822.     end
  823.   end
  824. end
  825.  
  826. local function UIRefresh()
  827.   while bRun do
  828.     sleep(10)
  829.  
  830.     redrawHeader()
  831.   end
  832. end
  833.  
  834. local function resizeHandler()
  835.   while bRun do
  836.     event, side = os.pullEvent("monitor_resize")
  837.  
  838.     oldMonX, oldMonY = monX, monY
  839.  
  840.     monX, monY = term.getSize()
  841.  
  842.     if monX ~= oldMonX or monY ~= oldMonY then
  843.       -- Resolution has changed, the program run on a monitor
  844.  
  845.       if monX >= 29 then
  846.         redrawHeader()
  847.  
  848.         if bScrollEffectDone == true then
  849.           term.setCursorPos(1, 3)
  850.         else
  851.           if monX >= 51 then
  852.             term.setCursorPos(1, 6)
  853.           elseif monX < 51 and monX >= 29 then
  854.             term.setCursorPos(1, 5)
  855.           end
  856.         end
  857.       else
  858.         term.setCursorPos(1, 1)
  859.         print("Resolution too low !")
  860.       end
  861.     end
  862.   end
  863. end
  864.  
  865. local function getPlayerInventory(sPlayer)
  866.   return t.getTargetDetails(sPlayer).Inventory
  867. end
  868.  
  869. local function inventoryUpdate()
  870.   while bRun do
  871.     local tPlayersInRange = getPlayers()
  872.    
  873.     for i = 1, #tPlayersInRange do
  874.       local bSuccess, tTargetDetails = pcall(getPlayerInventory, tPlayersInRange[i])
  875.    
  876.       if bSuccess then
  877.         if not tActualInventoryPlayers[tPlayersInRange[i]] then
  878.           tActualInventoryPlayers[tPlayersInRange[i]] = {inventory = {}}
  879.         end
  880.      
  881.         local tActualInventory = {}
  882.  
  883.         tActualInventoryPlayers[tPlayersInRange[i]]["inventorySize"] = getInventorySize(tTargetDetails)
  884.  
  885.         for k, v in pairs(tTargetDetails) do
  886.           table.insert(tActualInventory, v.Name)
  887.         end
  888.      
  889.         tActualInventoryPlayers[tPlayersInRange[i]]["inventory"] = tActualInventory
  890.       else
  891.         --print("error")
  892.       end
  893.     end
  894.    
  895.     sleep(0.1)
  896.   end
  897. end
  898.  
  899. local function inventoryHandler()
  900.   while bRun do
  901.     local invHandlerEvent, invHandlerPlayer = os.pullEvent()
  902.    
  903.     if invHandlerEvent == "player_join" then
  904.       for i = 1, #invHandlerPlayer do
  905.         local bSuccess, tTargetDetails = pcall(getPlayerInventory, invHandlerPlayer[i])
  906.        
  907.         if bSuccess then
  908.           if not tInventoryPlayers[invHandlerPlayer[i]] then
  909.             tInventoryPlayers[invHandlerPlayer[i]] = {inventory = {}}
  910.           end
  911.      
  912.           local tActualInventory = {}
  913.  
  914.           tInventoryPlayers[invHandlerPlayer[i]]["inventorySize"] = getInventorySize(tTargetDetails)
  915.  
  916.           for k, v in pairs(tTargetDetails) do
  917.             table.insert(tActualInventory, v.Name)
  918.           end
  919.      
  920.           tInventoryPlayers[invHandlerPlayer[i]]["inventory"] = tActualInventory
  921.         else
  922.           print("error")
  923.         end
  924.       end
  925.     elseif invHandlerEvent == "player_left" then
  926.       for i = 1, #invHandlerPlayer do
  927.         if tInventoryPlayers[invHandlerPlayer[i]]["inventorySize"] ~= tActualInventoryPlayers[invHandlerPlayer[i]]["inventorySize"] then
  928.         local sLogName = sLogFormat
  929.         sLogName = sLogName:gsub("#m", string.format("%02i", tActualDate.minute))
  930.         sLogName = sLogName:gsub("#h", string.format("%02i", tActualDate.hour))
  931.         sLogName = sLogName:gsub("#d", string.format("%02i", tActualDate.day))
  932.         sLogName = sLogName:gsub("#M", string.format("%02i", tActualDate.month))
  933.         sLogName = sLogName:gsub("#y", string.format("%02i", tActualDate.year))
  934.  
  935.         local file = fs.open(sLogDir..sLogName, fs.exists(sLogDir..sLogName) and "a" or "w")
  936.        
  937.         print("Inventory has changed ! ("..tActualInventoryPlayers[invHandlerPlayer[i]]["inventorySize"].." items now, "..tInventoryPlayers[invHandlerPlayer[i]]["inventorySize"].." before)")
  938.  
  939.         local tCurrentInventory = {}
  940.  
  941.         for k, v in pairs(tActualInventoryPlayers[invHandlerPlayer[i]]["inventory"]) do
  942.           table.insert(tCurrentInventory, v.displayName)
  943.         end
  944.  
  945.         if tActualInventoryPlayers[invHandlerPlayer[i]]["inventorySize"] > tInventoryPlayers[invHandlerPlayer[i]]["inventorySize"] then
  946.           -- Un/plusieurs item(s) à/ont été(s) ajouté(s)
  947.           local tDifference = getTableDifference(tActualInventoryPlayers[invHandlerPlayer[i]]["inventory"], tInventoryPlayers[invHandlerPlayer[i]]["inventory"])
  948.  
  949.           file.writeLine("Inventory has changed (+) : ")
  950.  
  951.           file.write("/")
  952.  
  953.           for i = 1, #tDifference do
  954.             file.write(tDifference[i].."/")
  955.             print(tDifference[i])
  956.           end
  957.  
  958.           file.write("\n")
  959.         elseif tActualInventoryPlayers[invHandlerPlayer[i]]["inventorySize"] < tInventoryPlayers[invHandlerPlayer[i]]["inventorySize"] then
  960.           -- Un/plusieurs item(s) à/ont été(s) enlevée(s)
  961.           local tDifference = getTableDifference(tInventoryPlayers[invHandlerPlayer[i]]["inventory"], tActualInventoryPlayers[invHandlerPlayer[i]]["inventory"])
  962.  
  963.           file.writeLine("Inventory has changed (-) : ")
  964.  
  965.           file.write("/")
  966.  
  967.           for i = 1, #tDifference do
  968.             file.write(tDifference[i].."/")
  969.             print(tDifference[i])
  970.           end
  971.  
  972.           file.write("\n")
  973.           end
  974.          
  975.           file.close()
  976.         end
  977.       end
  978.     end
  979.   end
  980. end
  981.  
  982. parallel.waitForAll(main, chatHandler, scrollEffect, UIRefresh, resizeHandler, inventoryUpdate, inventoryHandler)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement