Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Casino MainFrame
- recieves message as
- "command arg[1] arg[2] arg[3]"
- commands
- get playername - returns player
- modify playername amount type- adds/removes credits to an account
- types:
- "credit" - for when a player cashes in or buys more credits
- "payoutSlots" - for when player wins at a reel
- "pull" - for when a player is charged for using a slot
- ]]--
- -- Checks if a player exists in a database
- -- returns true / false
- function inDatabase(p)
- print("checking database for " .. p)
- if fs.exists("players/" .. p) then
- print(p .. " exists in the database")
- return true
- else
- print(p .. " doesn't exist in the database")
- return false
- end
- end
- -- Returns a list of all players in the database
- function updateDatabase()
- pFiles = fs.list("players/")
- if debugger then
- print("Players in Database:")
- for _, pFile in ipairs(pFiles) do
- print(pFile)
- end
- end
- return pFiles
- end
- -- Creates a new player files with 20 credits
- function newPlayer(p)
- print("Creating new player file for " .. p)
- np = fs.open("players/" .. p, "w")
- if np then
- local newPlayer = {}
- newPlayer.name = p
- newPlayer.balance = 0
- newPlayer.payIn = 0
- newPlayer.payOut = 0
- newPlayer.pulls = 0
- newPlayer.slotWins = 0
- pSerial = textutils.serialize(newPlayer)
- np.writeLine(pSerial)
- np.close()
- else
- print("Error Creating file for " .. p)
- end
- end
- function saveCasinoStats()
- if not fs.exists("casinoStats") then
- end
- fs.open("casinoStats",w)
- end
- -- Returns a table containing the requested players information
- function getPlayer(p)
- print("Getting Player information on " .. p)
- local playerFile = fs.open("players/" .. p, "r")
- local line = playerFile.readAll()
- print(line)
- local player = textutils.unserialize(line)
- playerFile.close()
- print("W/L: ".. math.floor((player.slotWins/player.pulls) *100) .. "% PayIn/PayOut: " .. math.floor((player.payIn/player.payOut)*100) .."%")
- return player
- end
- function modifyPlayer(p,num,_Type)
- print("modifying player " .. p)
- local playerFile = fs.open("players/" .. p, "r")
- if playerFile then
- for i=1,#playerFile do
- print(playerFile[i])
- end
- local line = playerFile.readAll()
- playerFile.close()
- print(line)
- local player = textutils.unserialize(line)
- player.balance = player.balance + num
- -- If player wins at game
- if _Type == "payoutSlots" then
- player.slotWins = player.slotWins + 1
- player.payOut = player.payOut + num
- casinoProfits = casinoProfits - num
- -- If Player Buys Credits
- elseif _Type == "credit" then
- casinoProfits = casinoProfits + num
- elseif _Type == "pull" then
- player.pulls = player.pulls + 1
- player.payIn = player.payIn + num * -1
- end
- pFile = fs.open("players/" .. p, "w")
- pFile.writeLine(textutils.serialize(player))
- pFile.close()
- else
- print("Could not open file for player")
- end
- end
- -- Checks if an id is on a trusted list
- function isTrusted(num)
- print("checking if " .. num .. " is trusted")
- local trusted = false
- for i=1,#terminals do
- print("checking against " .. terminals[i])
- if terminals[i] == num then
- print("found match")
- trusted = true
- end
- end
- if trusted then
- print(num .. " is trusted")
- else
- print(num .. " is not trusted")
- end
- return trusted
- end
- casinoProfits = 0
- terminals = {164,272,166,295,325,329,328,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350}
- rednet.open("left")
- print(textutils.serialize(terminals))
- while true do
- id,msg,proto = rednet.receive()
- if isTrusted(id) then
- -- Split msg into arguments
- args = {}
- for arg in msg:gmatch("%S+") do
- table.insert(args, arg)
- end
- command = args[1]
- playerName = args[2]
- amount = args[3]
- pType = args[4]
- if amount == nil then
- amount = 0
- end
- if pType == nil then
- pType = "none"
- end
- print(command .. " " .. playerName .. " " .. amount .. " " .. pType)
- if command == "get" then
- print("received get command for " .. playerName)
- if not inDatabase(playerName) then
- print("player not in database")
- newPlayer(playerName)
- end
- rednet.send(id,textutils.serialize(getPlayer(playerName)))
- print("sending player info")
- elseif command == "modify" then
- print("received modify command for " .. playerName .. " for " .. amount .. " type of " .. pType)
- modifyPlayer(playerName,amount,pType)
- rednet.send(id,textutils.serialize(getPlayer(playerName)))
- end
- end
- end
- rednet.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement