The3vilM0nk3y

StatTimer

Oct 11th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.74 KB | None | 0 0
  1. -- Simple timer to update stats on screen.
  2.  
  3. function getWireless()
  4.   local peripherals = peripheral.getNames()
  5.   local wirelessSide = nil
  6.   local gotWireless = false
  7.   for i=1, #peripherals do
  8.     print("Checking " .. peripherals[i])
  9.     if peripheral.getType(peripherals[i]) == "modem" then
  10.       print("It is a Modem")
  11.       if peripheral.call(peripherals[i],"isWireless") then
  12.         print("It is also Wireless")
  13.         wirelessSide = peripherals[i]
  14.         gotWireless = true
  15.       end
  16.     end
  17.     if gotWireless then
  18.       break
  19.     end
  20.   end
  21.   return wirelessSide
  22. end
  23. function getPlayer(p)
  24.   print("getting player info for " .. p)
  25.   rednet.send(server,"get " ..  p)
  26.   local msg = nil
  27.   local attempts = 1
  28.   -- wait for message for 10 ticks retry 3 times
  29.   repeat
  30.     print("attempt " .. attempts)
  31.     id,msg,proto = rednet.receive(.5)
  32.     attempts = attempts + 1
  33.   until msg or (attempts > 3 )
  34.   if msg and id == server then
  35.     print(msg)
  36.     return msg
  37.   else
  38.     return nil
  39.   end
  40. end
  41. function showStats(p)
  42.   stats.setBackgroundColor(colors.black)
  43.   stats.clear()
  44.   stats.setCursorPos(2,3)
  45.   stats.write("Conversion Rate: $1 equals " .. conversionRate .. " Credits")
  46.   stats.setCursorPos(2,4)
  47.   stats.write("Stand On The PIM Then Click Buttons To Use")
  48.   if p then
  49.     stats.setCursorPos(2,1)
  50.     stats.write("Balance in Money: $" .. math.floor(p.balance/conversionRate) .. " Remaining Credits: " .. p.balance%conversionRate)
  51.     stats.setCursorPos(2,2)
  52.     stats.write("Player: " .. p.name)
  53.     stats.setCursorPos(mW - string.len("Credits: " .. p.balance)- 1,2)
  54.     stats.write("Credits: " .. p.balance)
  55.   end
  56. end
  57. pim = peripheral.find("pim")
  58. m = peripheral.find("monitor")
  59. m.setTextScale(.5)
  60. mW,mH = m.getSize()
  61. stats = window.create(m,2,mH-3,mW-2,4)
  62. lastPlayerName = "pim"
  63. player = nil
  64. conversionRate = 10
  65. rednet.open(getWireless())
  66. server = 267
  67. showStats(nil)
  68. while true do
  69.   timer = os.startTimer(.5)
  70.   while true do
  71.     _,eTimer = os.pullEvent("timer")
  72.     if eTimer == timer then
  73.       break
  74.     end
  75.   end
  76.   print("got timer, getting player standing on the pim")
  77.   -- Get player standing on pim and update monitor to display name and credits
  78.   playerName = assert(pim.getInventoryName(),"PIM Seems to be missing or not wrapped")
  79.   if playerName ~= "pim" then
  80.     if lastPlayerName ~= playerName then
  81.       lastPlayerName = playerName
  82.       msg = getPlayer(playerName)
  83.         -- continue if message was recieved back from server
  84.       if msg ~=nil then
  85.         player = textutils.unserialize(msg)
  86.         print(player)
  87.         showStats(player)
  88.       end
  89.     end
  90.   else
  91.     if lastPlayerName ~= "pim" then
  92.       lastPlayerName = "pim"
  93.       player = nil
  94.       showStats(player)
  95.     end
  96.   end
  97. end
Advertisement
Add Comment
Please, Sign In to add comment