PaullyBeenis

CC: Bank

Mar 4th, 2021 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- VARIABLES --
  2. accountFile = "accounts.csv"
  3. modemSide = "right"
  4. logFile = "log.txt"
  5.  
  6. -- IMPORTS --
  7. --local inspect = require("/modules/inspect")
  8.  
  9.  
  10. -- UTILITY FUNCTIONS --
  11. function split (inputstr, sep)
  12.     if sep == nil then sep = "%s" end
  13.     local t={}
  14.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  15.         table.insert(t, str) end
  16.     return t
  17. end
  18.  
  19. function writeLog(msg,log)
  20.     time = split(os.date())
  21.     time = time[2]..time[3].." "..time[4]
  22.     local log = log or logFile
  23.     log = fs.open(log,"a")
  24.     logString = ("["..time.."] "..msg)
  25.     log.writeLine(logString)
  26.     print(logString)
  27.     log.close()
  28. end
  29.  
  30. function tableLength(T)
  31.     local count = 0
  32.     for _ in pairs(T) do count = count + 1 end
  33.     return count
  34. end
  35.  
  36. function tableCompare(a,b)
  37.     return tableToCSV(a) == tableToCSV(b)
  38. end
  39.  
  40. function tableToCSV(dict)
  41.     local csvData = ""
  42.     for _, a in ipairs(dict) do
  43.         csvData = (csvData..(tostring(dict[a].name..","..dict[a].balance..","..dict[a].pass..","..dict[a].enabled..",nil\n")))
  44.     end
  45.     return csvData
  46. end
  47.  
  48.  
  49. -- ACCOUNT CLASS --
  50. Account = {name,balance,password,enabled}
  51.  
  52. function Account:authenticate(input,src)
  53.     input = tostring(input)
  54.     assert(tostring(input),"Invalid password format")
  55.     assert(#input == 4,"Password must be 4 characters long")
  56.     if self.pass == input then
  57.         writeLog("["..src.."] "..self.name..":authenticate(SUCCESS)src=")
  58.         return true
  59.     else
  60.         writeLog("["..src.."] "..self.name..":authenticate(FAIL")
  61.         return false
  62.     end
  63. end
  64.  
  65. function Account:isEnabled(src)
  66.     if self.enabled == 1 then
  67.         return true else return false end
  68. end
  69.  
  70. function Account:getBalance(src)
  71.     writeLog("["..src.."] "..self.name..":getBalance($"..self.balance..")")
  72.     return self.balance
  73. end
  74.  
  75. function Account:send (recipient,amount,src)
  76.     assert(type(recipient) == type(self), "recipient is invalid for transaction")
  77.     self.balance = self.balance - amount
  78.     recipient.balance = recipient.balance + amount
  79.     writeLog("["..src.."] "..self.name..":send("..recipient.name..", $"..amount..")")
  80. end    
  81.  
  82.  
  83. -- ACCOUNT CREATION FUNCTION --
  84. function generateAccounts()
  85.  
  86.     -- parse account csv
  87.     local accountList = {}
  88.     for line in io.lines(accountFile) do
  89.        local name,balance,pass,enabled = line:match("%s*(.-),%s*(.-),%s*(.-),%s*(.-),%s*(.-)")
  90.        accountList[#accountList + 1] = {name=name,balance=balance,pass=pass,enabled=enabled}
  91.     end
  92.     writeLog("generateAccounts(success)")
  93.  
  94.     -- Instantiate objects into a dictionary
  95.     local accountListLen=tableLength(accountList)
  96.     local accountsDict = {}
  97.     for i=1, accountListLen do
  98.         local account = accountList[i]
  99.         local accountObj = {name=account.name,balance=account.balance,pass=account.pass,enabled=account.enabled}
  100.         setmetatable(accountObj, {__index=Account})
  101.         table.insert(accountsDict,account.name)
  102.         accountsDict[account.name] = accountObj
  103.     end  
  104.     writeLog("instantiateAccounts(success)")
  105.     return accountsDict
  106. end
  107.  
  108.  
  109. -- ACCOUNT FILE WRITEBACK --
  110. function databaseWriteback (accountsDict)
  111.     oldAccountsDict = generateAccounts()
  112.     tablesMatch = tableCompare(oldAccountsDict, accountsDict)
  113.     if not tablesMatch then
  114.         csvData = tableToCSV(accountsDict)
  115.         file = fs.open(accountFile, "w")
  116.         file.write(csvData)
  117.         writeLog("databaseWriteback(sucess)")
  118.         file.close()
  119.     else
  120.         writeLog("databaseWriteback(fail)")
  121.         writeLog("Current account table matches old table, no changes made")
  122.     end
  123. end
  124.  
  125.  
  126.  
  127.  
  128. -- Testing --
  129. ID=14
  130. port = "234265324534515762343289734906098437986128473623895734569823426324534512343289734906509843798612847362389573459823425676324534512343289734906098437986128473623895675734598"
  131. rednet.open(modemSide)
  132.  
  133. while true do
  134.     local id,msg,dist=rednet.receive(port)
  135.     writeLog("["..id.."] ".."Inbound Command: "..msg)
  136.     if msg == "auth" then
  137.         clientID=id
  138.         assert(loadfile("auth.lua"))(clientID)
  139.     elseif msg == "send" then
  140.        local a = generateAccounts()
  141.        a["Phizx"]:send((a["PaullyBeenis"]), 5,id)
  142.        databaseWriteback(a)
  143.        send = ("sent $5 to PaullyBeenis")
  144.        rednet.send(id, send, port)
  145.     elseif msg == "balance" then
  146.         local a = generateAccounts()
  147.         send = ("Phizx's Balance: $"..a["Phizx"]:getBalance(id))
  148.         rednet.send(id, send, port)
  149.     else
  150.         send = ("Invalid command, try again.")
  151.         rednet.send(id, send, port)
  152.     end
  153.        
  154. end
  155.  
  156.  
Add Comment
Please, Sign In to add comment