Advertisement
jared314

dev sensor

Aug 2nd, 2015
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. -- Global Variables
  2. --allowedPlayerArray={["Topher"]=true,["nunley21"]=true,["Demethan"]=true,["waerloga"]=true}
  3. allowedPlayerArray={}
  4. snapShot={}
  5. snapShot2={}
  6. itemString={}
  7. flag={}
  8. heart = 0
  9. time = 0
  10. token = '0'
  11. scanner = ''
  12. username = ''
  13.  
  14. -- write text to the terminal screen
  15. function draw_text_term(x, y, text, text_color, bg_color)
  16. term.setTextColor(text_color)
  17. term.setBackgroundColor(bg_color)
  18. term.setCursorPos(x,y)
  19. write(text)
  20. end
  21.  
  22. -- draw a line on the terminal screen
  23. function draw_line_term(x, y, length, color)
  24. term.setBackgroundColor(color)
  25. term.setCursorPos(x,y)
  26. term.write(string.rep(" ", length))
  27. end
  28.  
  29. function bars()
  30. draw_line_term(1, 1, 51, colors.lime)
  31. draw_line_term(1, 19, 51, colors.lime)
  32. draw_text_term(20, 1, 'Base Scanner', colors.gray, colors.lime)
  33. draw_text_term(13, 19, 'jaredeverett.ca/base_logger', colors.gray, colors.lime)
  34. end
  35.  
  36. function scanner_screen()
  37. term.clear()
  38.  
  39. -- scanner info
  40. bars()
  41. draw_text_term(1, 2, 'Scanner: ', colors.lime, colors.black)
  42. draw_text_term(10, 2, scanner, colors.white, colors.black)
  43. draw_text_term(1, 3, 'Owner: ', colors.lime, colors.black)
  44. draw_text_term(8, 3, username, colors.white, colors.black)
  45. draw_text_term(1, 4 , string.rep("-", 51), colors.lime, colors.black)
  46.  
  47. -- scanning graphuc
  48. heart = heart + 1
  49. draw_text_term(3, 10, 'Scanning ', colors.red, colors.black)
  50. draw_text_term(12, 10 , string.rep(".", heart), colors.red, colors.black)
  51. if heart == 15 then
  52. heart = 1
  53. end
  54. end
  55.  
  56. -- saves current token variable to local text file
  57. function save_config()
  58. sw = fs.open("config.txt", "w")
  59. sw.writeLine(token)
  60. sw.writeLine(scanner)
  61. sw.writeLine(username)
  62. sw.close()
  63. end
  64.  
  65. -- retrieves token from local text file
  66. -- called at startup if config.txt exists
  67. -- token is used to authorize the scanner to post to users log
  68. function load_config()
  69. sr = fs.open("config.txt", "r")
  70. token = sr.readLine()
  71. scanner = sr.readLine()
  72. username = sr.readLine()
  73. sr.close()
  74. end
  75.  
  76. -- called every 30 seconds when scanner is running
  77. -- tells the server that the scanner is online
  78. function ping()
  79. http.post("http://jaredeverett.ca/base_logger/code/ping.php",
  80. "token="..token.."&id="..os.getComputerID())
  81. end
  82.  
  83. function round(what, precision)
  84. if what==nil then return 0 end
  85. return math.floor(what*math.pow(10,precision)+0.5) / math.pow(10,precision)
  86. end
  87.  
  88. function record()
  89. term.setCursorPos(1,1)
  90. players=s.getPlayers()
  91. for num,player in pairs(players) do
  92. for p,ign in pairs(player) do
  93. if p=="name" then
  94. playerData=s.getPlayerByName(ign)
  95. data = playerData.all()
  96. inventory=data.player.inventory
  97. if not allowedPlayerArray[ign] then -- check the allow array
  98. --print(ign.." is not on the allowed list")
  99. invArray={} -- fresh copy
  100. for a,b in pairs(inventory) do --getting player inventory
  101. slot=b.all()
  102. --print(slot.name,slot.qty) -- debug
  103. invArray[a]=slot.name,slot.qty
  104. end
  105. if flag[ign]==nil or not flag[ign] then --recording first inventory scan
  106. itemString[ign]=""
  107. snapShot[ign]= invArray
  108. --print("Initial snapshot")
  109. post(ign,1," has entered sensor range")
  110. flag[ign]=true -- set player flag for later processing
  111. else
  112. snapShot2[ign]= invArray
  113. --print("updated snapshot")
  114. --print("comparing Inventory")
  115. guilty,Items=compare(snapShot[ign],snapShot2[ign])
  116. --redempt,Items=compare(snapShot2[ign],snapShot[ign]) --not sure how to work the dropped items
  117.  
  118. if guilty then
  119. for i,v in pairs(Items) do --building string
  120. if v~="" or v~=nil then
  121. itemString[ign]=","..v..itemString[ign]
  122.  
  123. end
  124. end
  125. post(ign, 3, itemString[ign])
  126. snapShot[ign]=snapShot2[ign]--inventory has been logged, can now reset the initial snapshot
  127. itemString[ign]=""
  128. else
  129. --print(ign," - No item found")
  130. end
  131. sleep(1)
  132. leaveCheck()
  133. if snapShot2[ign]~= nil then --recoding newer inventory until player leaves.
  134. snapShot2[ign]= nil
  135. end
  136. end
  137.  
  138. end
  139.  
  140.  
  141. end
  142.  
  143. end
  144. end
  145. --Cleanup
  146. playerData={}
  147. data={}
  148. inventory={}
  149. guilty=nil
  150.  
  151. end
  152.  
  153. -- iterate through all players with an active flag
  154. -- see if they're still in range of the scanner
  155. function leaveCheck()
  156. for ign,v in pairs(flag) do
  157. -- print("Did ",ign," leave?")
  158. local ok,msg=pcall(function ()s.getPlayerByName(ign) end)
  159. --print(msg) --debug
  160. if not ok and flag[ign] then
  161. --print(ign," has left.")
  162. flag[ign]=false
  163. snapShot[ign]=nil
  164. snapShot2[ign]=nil
  165. post(ign,2," has left sensor range")
  166. end
  167. end
  168. end
  169.  
  170.  
  171.  
  172. -- records a log entry on the server
  173. -- passes to server:
  174. -- token and computer ID (used to verify source)
  175. -- event type: 1 = player entering, 2 = player leaving, 3 = inventory change
  176. -- ign, players name
  177. -- discription of event
  178. function post(ign, event, discription)
  179. http.post("http://jaredeverett.ca/base_logger/code/log.php",
  180. "token="..token.."&ign="..ign.."&id="..os.getComputerID().."&event="..event.."&discription="..discription)
  181. end
  182.  
  183. function tablelength(T) --get real count of table
  184. local count = 0
  185. for _ in pairs(T) do count = count + 1 end
  186. return count
  187. end
  188.  
  189. function compare(t1,t2)
  190.  
  191. local ai = {}
  192. local r = {}
  193. table.sort(t1) --sorting by name
  194. table.sort(t2) --sorting by name
  195.  
  196. for k,v in pairs(t2) do
  197. r[k] = v; ai[v]=true
  198. end
  199. for k,v in pairs(t1) do
  200. if ai[v]~=nil then --if item match, remove it from temp table r
  201. r[k]=nil
  202. end
  203. end
  204.  
  205. if tablelength(r) > 0 then --if there are items left in r
  206. return true,r
  207. else
  208. return false,nil
  209. end
  210. end
  211.  
  212. -- Setup a new scanner
  213. function newInstall()
  214. -- collect information. Scanner name, account user/pass
  215. term.clear()
  216. bars()
  217. draw_text_term(1, 2, 'Setup Your Scanner', colors.lime, colors.black)
  218. draw_text_term(1, 3, 'Before you can register this scanner, please create an account at jaredeverett.ca/base_logger', colors.lightGray, colors.black)
  219.  
  220. draw_text_term(1, 6, 'Enter a unique name for this scanner:', colors.lime, colors.black)
  221. term.setCursorPos(1,7)
  222. term.setTextColor(colors.white)
  223. name = read()
  224.  
  225. term.clear()
  226. bars()
  227. draw_text_term(1, 2, 'Register '..name, colors.lime, colors.black)
  228.  
  229. draw_text_term(1, 4, 'Username: ', colors.lime, colors.black)
  230. term.setTextColor(colors.white)
  231. user = read()
  232. draw_text_term(1, 5, 'Password: ', colors.lime, colors.black)
  233. term.setTextColor(colors.white)
  234. pass = read("*")
  235.  
  236. -- attempt to verify login
  237. response = http.post(
  238. "http://jaredeverett.ca/base_logger/code/signin.php",
  239. "user="..user.."&pass="..pass.."&id="..os.getComputerID().."&name="..name)
  240. token = response.readAll()
  241.  
  242. if token == 'error' then
  243. draw_text_term(1, 6, 'login failed', colors.red, colors.black)
  244. sleep(2)
  245. newInstall()
  246. else
  247. username = user
  248. scanner = name
  249. save_config()
  250. start_recording()
  251. end
  252. end
  253.  
  254. function start_recording()
  255. -- main loop
  256. while true do
  257. -- run scan
  258. ok,msg=pcall(record)
  259.  
  260. -- animate screen and delay
  261. scanner_screen()
  262. sleep(0.5)
  263. scanner_screen()
  264. sleep(0.5)
  265.  
  266. -- main active status with server
  267. time = time + 1
  268. if time > 30 then
  269. time=0
  270. ping()
  271. end
  272. end
  273. end
  274.  
  275. function start()
  276. s=peripheral.wrap("top")
  277. heart=0
  278. term.clear()
  279. term.setCursorPos(1,1)
  280.  
  281. if fs.exists("config.txt") then
  282. load_config()
  283. start_recording()
  284. else
  285. newInstall()
  286. end
  287. end
  288.  
  289. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement