Advertisement
The3vilM0nk3y

CasinoController

Sep 26th, 2015
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.70 KB | None | 0 0
  1. --[[ Casino MainFrame
  2.  
  3.  
  4.  
  5. recieves message as
  6.   "command arg[1] arg[2] arg[3]"
  7.  
  8.   commands
  9.     get playername - returns player
  10.     modify playername amount type- adds/removes credits to an account
  11.    
  12.     types:
  13.     "credit" - for when a player cashes in or buys more credits
  14.     "payoutSlots" - for when player wins at a reel
  15.     "pull" - for when a player is charged for using a slot
  16.    
  17.  
  18. ]]--
  19. -- Checks if a player exists in a database
  20. -- returns true / false
  21. function inDatabase(p)
  22.   print("checking database for " .. p)
  23.   if fs.exists("players/" .. p) then
  24.     print(p .. " exists in the database")
  25.     return true
  26.   else
  27.     print(p .. " doesn't exist in the database")
  28.     return false
  29.   end
  30. end
  31.  
  32. -- Returns a list of all players in the database
  33. function updateDatabase()
  34.   pFiles = fs.list("players/")
  35.   if debugger then
  36.     print("Players in Database:")
  37.     for _, pFile in ipairs(pFiles) do
  38.       print(pFile)
  39.     end
  40.   end
  41.   return pFiles
  42.    
  43. end
  44. -- Creates a new player files with 20 credits
  45. function newPlayer(p)
  46.   print("Creating new player file for " .. p)
  47.   np = fs.open("players/" .. p, "w")
  48.   if np then
  49.     local newPlayer = {}
  50.     newPlayer.name = p
  51.     newPlayer.balance = 0
  52.     newPlayer.payIn = 0
  53.     newPlayer.payOut = 0
  54.     newPlayer.pulls = 0
  55.     newPlayer.slotWins = 0
  56.     pSerial = textutils.serialize(newPlayer)
  57.     np.writeLine(pSerial)
  58.     np.close()
  59.   else
  60.     print("Error Creating file for " .. p)
  61.   end
  62. end
  63. function saveCasinoStats()
  64.   if not fs.exists("casinoStats") then
  65.  
  66.   end
  67.  
  68.   fs.open("casinoStats",w)
  69.  
  70. end
  71. -- Returns a table containing the requested players information
  72. function getPlayer(p)
  73.   print("Getting Player information on " .. p)
  74.   local playerFile = fs.open("players/" .. p, "r")
  75.   local line = playerFile.readAll()
  76.   print(line)
  77.   local player = textutils.unserialize(line)
  78.   playerFile.close()
  79.   print("W/L: ".. math.floor((player.slotWins/player.pulls) *100) .. "%  PayIn/PayOut: " .. math.floor((player.payIn/player.payOut)*100) .."%")
  80.   return player
  81. end
  82. function modifyPlayer(p,num,_Type)
  83.   print("modifying player " .. p)
  84.   local playerFile = fs.open("players/" .. p, "r")
  85.   if playerFile then
  86.     for i=1,#playerFile do
  87.       print(playerFile[i])
  88.     end
  89.     local line = playerFile.readAll()
  90.     playerFile.close()
  91.     print(line)
  92.     local player = textutils.unserialize(line)
  93.     player.balance = player.balance + num
  94.     -- If player wins at game
  95.     if _Type == "payoutSlots" then
  96.       player.slotWins = player.slotWins + 1
  97.       player.payOut = player.payOut + num
  98.       casinoProfits = casinoProfits - num
  99.     -- If Player Buys Credits
  100.     elseif _Type == "credit" then
  101.       casinoProfits = casinoProfits + num
  102.     elseif _Type == "pull" then
  103.       player.pulls = player.pulls + 1
  104.       player.payIn = player.payIn + num * -1
  105.     end
  106.     pFile = fs.open("players/" .. p, "w")
  107.     pFile.writeLine(textutils.serialize(player))
  108.     pFile.close()
  109.   else
  110.     print("Could not open file for player")
  111.   end
  112.  
  113. end
  114. -- Checks if an id is on a trusted list
  115. function isTrusted(num)
  116.   print("checking if " .. num .. " is trusted")
  117.   local trusted = false
  118.   for i=1,#terminals do
  119.     print("checking against " .. terminals[i])
  120.     if terminals[i] == num then
  121.       print("found match")
  122.       trusted = true
  123.     end
  124.   end
  125.   if trusted then
  126.     print(num .. " is trusted")
  127.   else
  128.     print(num .. " is not trusted")
  129.   end
  130.   return trusted
  131. end
  132.  
  133. casinoProfits = 0
  134. terminals = {164,272,166,295,325,329,328,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350}
  135. rednet.open("left")
  136. print(textutils.serialize(terminals))
  137. while true do
  138.   id,msg,proto = rednet.receive()
  139.   if isTrusted(id) then
  140.     -- Split msg into arguments
  141.    
  142.     args = {}
  143.     for arg in msg:gmatch("%S+") do
  144.       table.insert(args, arg)
  145.     end
  146.     command = args[1]
  147.     playerName = args[2]
  148.     amount = args[3]
  149.     pType = args[4]
  150.     if amount == nil then
  151.       amount = 0
  152.     end
  153.     if pType == nil then
  154.       pType = "none"
  155.     end
  156.     print(command .. " " .. playerName .. " " .. amount .. " " .. pType)
  157.     if command == "get" then
  158.       print("received get command for " .. playerName)
  159.       if not inDatabase(playerName) then
  160.         print("player not in database")
  161.         newPlayer(playerName)
  162.       end
  163.       rednet.send(id,textutils.serialize(getPlayer(playerName)))
  164.       print("sending player info")
  165.     elseif command == "modify" then
  166.       print("received modify command for " .. playerName .. " for " .. amount .. " type of " .. pType)
  167.       modifyPlayer(playerName,amount,pType)
  168.       rednet.send(id,textutils.serialize(getPlayer(playerName)))
  169.     end
  170.   end
  171. end
  172. rednet.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement