Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- accounts = {}
- function save()
- local file = fs.open('accounts.txt',"w")
- file.write(textutils.serialize(accounts))
- file.close()
- end
- if fs.exists('accounts.txt') == false then
- accounts = {['1111'] = {'Test man', 0}}
- save()
- end
- file = fs.open('accounts.txt',"r")
- data = file.readAll()
- file.close()
- accounts = textutils.unserialise(data)
- valueTable = {['minecraft:emerald'] = 400, ['minecraft:diamond'] = 200, ['minecraft:gold_ingot'] = 50, ['minecraft:iron_ingot'] = 20}
- gameCost = 99
- function ServeRequest(operation, account, param)
- if operation == nil then
- return
- end
- if accounts[account] == nil then
- return
- end
- if account == nil then
- return
- end
- if operation == 'account' then
- rednet.send(sender, textutils.serialise(accounts[account]), "C_ACC")
- print('Served account request '..account)
- elseif operation == 'depo' then
- item = param[1]
- amount = param[2]
- if valueTable[item] ~= nil then
- accounts[account][2] = accounts[account][2] + valueTable[item] * amount
- print('Served depo request '..account)
- end
- elseif operation == 'with' then
- takeAmount = math.floor(accounts[account][2] / valueTable[param])
- accounts[account][2] = accounts[account][2] - valueTable[param] * takeAmount
- rednet.send(sender, textutils.serialise({'withR', takeAmount}), "C_ACC")
- print('Served withdraw request '..account)
- elseif operation == 'add' then
- if accounts[account][2] + param >= 0 then
- accounts[account][2] = accounts[account][2] + param
- rednet.send(sender, textutils.serialise(true), "C_ACC")
- print('Edited balance of account '..account..' by '..param)
- else
- rednet.send(sender, textutils.serialise(false), "C_ACC")
- print('Failed to edit balance of account '..account..' by '..param)
- end
- end
- save()
- end
- function ServeData()
- while true do
- sender,message,distance = rednet.receive("C_ACC")
- message = textutils.unserialise(message)
- ServeRequest(message[1], message[2], message[3])
- end
- end
- function ServeGames()
- while true do
- sender,message,distance = rednet.receive("C_GAME")
- message = textutils.unserialise(message)
- if message[1] == 'add' then
- accounts[message[2]][2] = accounts[message[2]][2] + message[3]
- print('Edited balance of account '..message[2]..' by '..message[3])
- end
- save()
- end
- end
- function Main()
- while true do
- print('new account: n')
- print('give/remove credit: c')
- print('view account: v')
- command = read()
- if command == 'n' then
- print('pin')
- pin = read()
- print('name')
- name = read()
- accounts[pin] = {name, 0}
- print('Created account '..name..' with pin '..pin)
- end
- if command == 'c' then
- print('pin')
- pin = read()
- print('amount')
- amount = read()
- print(accounts[pin][2])
- accounts[pin][2] = accounts[pin][2] + tonumber(amount)
- print('Added '..amount..' credits to account '..pin)
- print('Total credits '..accounts[pin][2])
- end
- if command == 'v' then
- print('pin or name')
- pin = read()
- amount = read()
- print('Name: '..accounts[pin][1])
- print('Pin: '..pin)
- print('Credits: '..accounts[pin][2])
- end
- save()
- end
- end
- rednet.open("top")
- rednet.host("C_ACC", "database")
- parallel.waitForAll(ServeData, ServeGames, Main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement