Advertisement
JereTheJuggler

Untitled

Dec 7th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.38 KB | None | 0 0
  1. local localDir = shell.resolve("")
  2. local configPath = localDir.."/settings.config"
  3. local listsFolder = localDir.."/lists"
  4. if not fs.exists(listsFolder) then fs.makeDir(listsFolder) end
  5. local controller = nil
  6. local trashCan = nil
  7. local modem = nil
  8. local clients = {
  9. -- [playerName]={
  10. -- chestAddress=[address],
  11. -- chest=[wrappedPlayerChest],
  12. -- active=[active],
  13. -- dumpList={
  14. -- [itemDisplayName]=true,
  15. -- ...
  16. -- },
  17. -- trashList={
  18. -- [itemDisplayName]=true,
  19. -- ...
  20. -- }
  21. -- },
  22. -- ...
  23. }
  24. local drawerIndex = {
  25. -- [itemDisplayName]=[slot],
  26. -- ...
  27. }
  28. local modemPort = 12
  29.  
  30. local running = true
  31.  
  32. function initialize()
  33. local self = {}
  34. function self:loadConfig()
  35. -- format:
  36. -- {
  37. -- [playerName]={
  38. -- chestAddress=[address],
  39. -- active=[active],
  40. -- dumpList={
  41. -- [itemDisplayName]=true,
  42. -- ...
  43. -- },
  44. -- trashList={
  45. -- [itemDisplayName]=true,
  46. -- ...
  47. -- }
  48. -- },
  49. -- ...
  50. -- }
  51. print("loading config...")
  52. if fs.exists(configPath) then
  53. local status,result = pcall(function()
  54. local file = fs.open(configPath,"r")
  55. local contents = file.readAll()
  56. file.close()
  57. return textutils.unserialize(contents)
  58. end)
  59. if status then
  60. self:handleConfigData(result)
  61. else
  62. term.setTextColor(colors.red)
  63. print("error loading config - "..result)
  64. term.setTextColor(colors.white)
  65. end
  66. end
  67. end
  68. function self:handleConfigData(data)
  69. local status,result = pcall(function()
  70. if type(data) ~= "table" then error("data is not a table") end
  71. for playerName,clientData in ipairs(data) do
  72. --validate client data
  73. local status2,result2 = pcall(function()
  74. if type(clientData) ~= "table" then error("client data is not a table") end
  75. if clientData.chestAddress ~= nil and
  76. type(clientData.chestAddress) == "string" and
  77. clientData.active ~= nil and
  78. type(clientData.active) == "boolean" then
  79. return true
  80. else error("invalid client data") end
  81. end)
  82. if status2 then
  83. --client data is valid
  84. clients[playerName] = clientData
  85. else
  86. term.setTextColor(colors.red)
  87. print("error loading config - "..result2)
  88. end
  89. end
  90. end)
  91. if not status then
  92. term.setTextColor(colors.red)
  93. print("error loading config - "..result)
  94. end
  95. end
  96. function self:setupPerips()
  97. print("setting up peripherals...")
  98. for _,address in ipairs(peripheral.getNames()) do
  99. local t = peripheral.getType(address)
  100. if t == "storagedrawers:controller" then
  101. controller = peripheral.wrap(address)
  102. elseif t == "xu2:tiletrashcan" then
  103. trashCan = peripheral.wrap(address)
  104. elseif t == "modem" and peripheral.call(address,"isWireless") then
  105. modem = peripheral.wrap(address)
  106. if not modem.isOpen(modemPort) then modem.open(modemPort) end
  107. end
  108. end
  109. for playerName,playerData in pairs(clients) do
  110. if peripheral.isPresent(playerData.chestAddress) then
  111. clients[playerName].chest = peripheral.wrap(playerData.chestAddress)
  112. else
  113. term.setTextColor(colors.red)
  114. print("chest not found for "..playerName)
  115. clients[playerName] = nil
  116. end
  117. end
  118. if controller == nil then error("Drawer controller not found") end
  119. if modem == nil then error("Wireless modem not found") end
  120. if trashCan == nil then error("Trash can not found") end
  121. end
  122. function self:loadLists()
  123. print("loading user lists...")
  124. local lists = {"dumpList","trashList"}
  125. for playerName,playerData in pairs(clients) do
  126. print("loading lists for "..playerName)
  127. --try to read in lists stored for each player
  128. local playerFolder = listsFolder.."/"..playerName
  129. if not fs.exists(playerFolder) then
  130. fs.makeDir(playerFolder)
  131. else
  132. for _,list in ipairs(lists) do
  133. local listFile = playerFolder.."/"..list..".list"
  134. if fs.exists(listFile) then
  135. local status,result = pcall(function()
  136. local file = fs.open(listFile,"r")
  137. local contents = file.readAll()
  138. file.close()
  139. return textutils.unserialize(contents)
  140. end)
  141. if status then
  142. clients[playerName][list] = result
  143. end
  144. end
  145. end
  146. end
  147. --populate tables for any lists that weren't read in
  148. for _,list in ipairs(lists) do
  149. if clients[playerName][list] == nil then
  150. clients[playerName][list] = {}
  151. end
  152. end
  153. end
  154. end
  155. term.clear()
  156. term.setCursorPos(1,1)
  157. print("initializing...")
  158. self:loadConfig()
  159. self:setupPerips()
  160. self:loadLists()
  161. drawerIndex = createDrawerIndex()
  162. end
  163.  
  164. function createDrawerIndex()
  165. print("indexing drawers...")
  166. local index = {}
  167. for i=1,controller.size() do
  168. local meta = controller.getItemMeta(i)
  169. if meta ~= nil then index[meta.displayName] = i end
  170. end
  171. return index
  172. end
  173.  
  174. function main()
  175. while true do
  176. for playerName,playerData in pairs(clients) do
  177. pcall(function()
  178. local chest = playerData.chest
  179. for i=1,i<chest.size() do
  180. local meta = chest.getItemMeta(i)
  181. if meta ~= nil then
  182. if playerData.dumpList[meta.displayName] ~= nil and drawerIndex[meta.displayName] ~= nil then
  183. controller.pullItems(playerData.chestAddress,i,meta.count,drawerIndex[meta.displayName])
  184. end
  185. end
  186. end
  187. end)
  188. end
  189. sleep(2.5)
  190. end
  191. end
  192.  
  193. local MessageFunctions = {
  194. ping=function(message)
  195. return {success=true}
  196. end,
  197. pause=function(message)
  198. clients[message.playerName].active = false
  199. return {active=false}
  200. end,
  201. resume=function(message)
  202. clients[message.playerName].active = true
  203. return {active=true}
  204. end,
  205. getActive=function(message)
  206. return {active=clients[message.playerName].active}
  207. end,
  208. addDump=function(message)
  209. if message.displayName ~= nil then
  210. if clients[message.playerName].dumpList[message.displayName] == nil then
  211. clients[message.playerName].dumpList[message.displayName] = 1
  212. saveConfig(
  213. listsFolder.."/"..message.playerName.."/dumpList.list",
  214. clients[message.playerName].dumpList
  215. )
  216. end
  217. return {success=true}
  218. end
  219. return {success=false}
  220. end,
  221. removeDump=function(message)
  222. if message.displayName ~= nil then
  223. if clients[message.playerName].dumpList[message.displayName] ~= nil then
  224. clients[message.playerName].dumpList[message.displayName] = nil
  225. saveConfig(
  226. listsFolder.."/"..message.playerName.."/dumpList.list",
  227. clients[message.playerName].dumpList
  228. )
  229. end
  230. return {success=true}
  231. end
  232. return {success=false}
  233. end,
  234. getDumpList=function(message)
  235. return clients[message.playerName].dumpList
  236. end,
  237. addTrash=function(message)
  238. if message.displayName ~= nil then
  239. if clients[message.playerName].trashList[message.displayName] == nil then
  240. clients[message.playerName].trashList[message.displayName] = 1
  241. saveConfig(
  242. listsFolder.."/"..message.playerName.."/trashList.list",
  243. clients[message.playerName].trashList
  244. )
  245. end
  246. return {success=true}
  247. end
  248. return {success=false}
  249. end,
  250. removeTrash=function(message)
  251. if message.displayName ~= nil then
  252. if clients[message.playerName].trashList[message.displayName] ~= nil then
  253. clients[message.playerName].trashList[message.displayName] = nil
  254. saveConfig(
  255. listsFolder.."/"..message.playerName.."/trashList.list",
  256. clients[message.playerName].trashList
  257. )
  258. end
  259. return {success=true}
  260. end
  261. return {success=false}
  262. end,
  263. getTrashList=function(message)
  264. return clients[message.playerName].trashList
  265. end
  266. }
  267.  
  268. function messageListener()
  269. while true do
  270. local event,side,freq,replyFreq,message,dist = os.pullEvent("modem_message")
  271. if type(message) == "table" then
  272. if message.playerName ~= nil and clients[message.playerName] ~= nil and
  273. message.operation ~= nil and MessageFunctions[message.operation] ~= nil then
  274. local response = MessageFunctions[message.operation](message)
  275. if response ~= nil then
  276. modem.transmit(replyFreq,modemPort,response)
  277. end
  278. end
  279. end
  280. end
  281. end
  282.  
  283. local TermFunctions = {
  284. { text="List Players",
  285. func=function()
  286. for playerName,playerData in pairs(clients) do
  287. print(playerName)
  288. end
  289. term.setTextColor(colors.yellow)
  290. print("Press any key to continue")
  291. os.pullEvent("key")
  292. term.setTextColor(colors.white)
  293. end
  294. },
  295. { text="Add Player",
  296. func=function()
  297. term.setTextColor(colors.yellow)
  298. print("Add who?")
  299. term.setTextColor(colors.white)
  300. local input = read()
  301. if clients[input] == nil then
  302. term.setTextColor(colors.yellow)
  303. print("Please connect an Extra Utilities 2 Player Chest")
  304. print("Press any key to cancel")
  305. local chestAddress = nil
  306. term.setTextColor(colors.red)
  307. parallel.waitForAny(function()
  308. --wait for player chest to be added
  309. while true do
  310. local event,address = os.pullEvent("peripheral")
  311. local t = peripheral.getType(address)
  312. if t ~= "xu2:tileplayerchest" then
  313. print("Invalid peripheral attached")
  314. term.setCursorPos(1,6)
  315. else
  316. chestAddress = address
  317. return
  318. end
  319. end
  320. end,function()
  321. --cancel function
  322. os.pullEvent("key")
  323. end)
  324. term.clearLine(6)
  325. if chestAddress == nil then
  326. print("Add Player operation aborted")
  327. else
  328. clients[input] = {
  329. active=false,
  330. chestAddress=chestAddress,
  331. chest=peripheral.wrap(chestAddress)
  332. }
  333. if not fs.exists(listsFolder.."/"..input) then fs.makeDir(listsFolder.."/"..input) end
  334. term.setTextColor(colors.lime)
  335. --try to read in any existing lists for the player
  336. local dumpList = loadConfig(listsFolder.."/"..input.."/dumpList.list")
  337. if dumpList == nil then dumpList = {} end
  338. local trashList = loadConfig(listsFolder.."/"..input.."/trashList.list")
  339. if trashList == nil then trashList = {} end
  340. clients[input].dumpList = dumpList
  341. clients[input].trashList = trashList
  342. print("Success")
  343. end
  344. else
  345. term.setTextColor(colors.red)
  346. print("Player already added")
  347. end
  348. term.setTextColor(colors.white)
  349. sleep(.5)
  350. end
  351. },
  352. { text="Remove Player",
  353. func=function()
  354. term.setTextColor(colors.yellow)
  355. print("Remove who?")
  356. term.setTextColor(colors.white)
  357. local input = read()
  358. if clients[input] ~= nil then
  359. clients[input] = nil
  360. term.setTextColor(colors.lime)
  361. print("Success")
  362. else
  363. term.setTextColor(colors.red)
  364. print("Player was not added")
  365. end
  366. term.setTextColor(colors.white)
  367. sleep(.5)
  368. end
  369. },
  370. { text="Re-index Drawers",
  371. func=function()
  372. drawerIndex = createDrawerIndex()
  373. end
  374. },
  375. { text="Terminate",
  376. func=function()
  377. running = false
  378. end
  379. }
  380. }
  381.  
  382. function termInteractions()
  383. term.clear()
  384. term.setCursorPos(1,1)
  385. for i,funcData in ipairs(TermFunctions) do
  386. print(i..") "..funcData.text)
  387. end
  388. term.setTextColor(colors.yellow)
  389. print("Enter number from 1 to "..(#TermFunctions))
  390. term.setTextColor(colors.white)
  391. local termWidth,termHeight = term.getSize()
  392. local inputWindow = window.create(term.current(),1,(#TermFunctions)+1,termWidth,termHeight-(#TermFunctions)-1,true)
  393. term.redirect(inputWindow)
  394. while running do
  395. term.clear()
  396. term.setCursorPos(1,1)
  397. local input = tonumber(read())
  398. if input ~= nil and input >= 1 and input <= #TermFunctions then
  399. TermFunctions[input].func()
  400. end
  401. end
  402. term.redirect(term.native())
  403. end
  404.  
  405. function writeAllConfigs()
  406. print("saving config...")
  407. local configData = {}
  408. for playerName,playerData in pairs(clients) do
  409. configData[playerName] = {
  410. chestAddress=playerData.chestAddress,
  411. active=playerData.active
  412. }
  413. end
  414. saveConfig(configPath,configData)
  415. print("saving user lists...")
  416. local lists = {"dumpList","trashList"}
  417. for playerName,playerData in pairs(clients) do
  418. for _,list in ipairs(lists) do
  419. if #playerData[list] ~= 0 then
  420. saveConfig(listsFolder.."/"..playerName.."/"..list..".list",playerData[list])
  421. end
  422. end
  423. end
  424. end
  425.  
  426. function saveConfig(configPath,configData)
  427. local file = fs.open(configPath,"w")
  428. file.write(textutils.serialize(configData))
  429. file.close()
  430. end
  431. function loadConfig(configPath)
  432. if not fs.exists(configPath) then return nil end
  433. local file = fs.open(configPath,"r")
  434. local contents = file.readAll()
  435. file.close()
  436. return textutils.unserialize(contents)
  437. end
  438.  
  439. function run()
  440. initialize()
  441. parallel.waitForAny(
  442. termInteractions,
  443. messageListener,
  444. main
  445. )
  446. writeAllConfigs()
  447. end
  448.  
  449. run()
  450. sleep(2.5)
  451. term.clear()
  452. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement