Guest User

[ComputerCraft] KreditAPI

a guest
Sep 1st, 2014
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.37 KB | None | 0 0
  1. --Kredit API by JLW222 (James222)
  2. --Build V 1.0
  3.  
  4.  
  5. --Array Database
  6.  
  7. local name = {}
  8. local money = {}
  9. local password = {}
  10. local inTransactions = {}
  11. local outTransactions = {}
  12.  
  13.  
  14. --Enter a number from 0 to 100 (%)
  15. local transactionFee = 10
  16.  
  17. --The server can hold money because of transaction fees
  18. local serverMoney = 0
  19.  
  20. --The server's password. It is asked when using admin commands
  21. --CHANGE ME <=========================================================================
  22. local serverAdminPassword = "beta2"
  23. --The encryption's password. It is used to encrypt passwords and data
  24. --CHANGE ME <=========================================================================
  25. local encryptionPassword = "YEAH"
  26.  
  27. --Defines is KREDITAPI should automatically read the accounts file when api is ran
  28. local autoRead = true
  29.  
  30. --Function Variables. Don't modify these
  31. local database = ""
  32.  
  33. local completeDatabase = {}
  34.  
  35.  
  36. local foundAccountC = false
  37. local foundAccountCk = false
  38. local foundAccountE = false
  39. local foundAccountCr = false
  40.  
  41. local databases = {}
  42. local splitDatabases = {}
  43. --Functions
  44.  
  45. os.loadAPI("enc")
  46.  
  47. --Writes data from accounts file to tables
  48. function applyDatabase()
  49.   if database == "" then
  50.     print("Database must be loaded first or is empty")
  51.     return false
  52.   else
  53.    databases[1] = textutils.unserialize(database)
  54.    splitDatabases = split(databases[1], "}")
  55.    name = textutils.unserialize(splitDatabases[1])
  56.    money = textutils.unserialize(splitDatabases[2])
  57.    password = textutils.unserialize(splitDatabases[3])
  58.    inTransactions = textutils.unserialize(splitDatabases[4])
  59.    outTransactions = textutils.unserialize(splitDatabases[5])
  60.   end
  61. end
  62.  
  63. --Save the database's data
  64. function saveDatabase()
  65.   if fs.exists("KACCOUNTS") then
  66.     local h = fs.open("KACCOUNTS", "w")
  67.     completeDatabase[1] = textutils.serialize(name) .. textutils.serialize(money) .. textutils.serialize(password) .. textutils.serialize(inTransactions) .. textutils.serialize(outTransactions)
  68.     h.write(textutils.serialize(completeDatabase[1]))
  69.     h.close()
  70.    else
  71.    loadDatabase()
  72.    saveDatabase()
  73.   end
  74. end
  75.  
  76. --Loads the bank account file into the tables
  77. function loadDatabase()
  78.   if fs.exists("KACCOUNTS") then
  79.    local h = fs.open("KACCOUNTS", "r")
  80.    database = h.readAll()
  81.    h.close()
  82.    print("Accounts database loaded")
  83.    applyDatabase()
  84.   else
  85.    local h = fs.open("KACCOUNTS", "a")
  86.    h.close()
  87.    print("Accounts database created")
  88.   end
  89. end
  90.  
  91. --Lists all accounts names
  92. function listAccounts()
  93.   local accounts = ""
  94.   if count() == 0 then
  95.    return "There are no accounts in this database"
  96.   else
  97.   for i = 1, count() do
  98.     accounts = accounts .. " | " .. name[i]
  99.   end
  100.   return accounts
  101.   end
  102. end
  103.  
  104. --Used to create an account
  105. function createAccount(userName, userPassword)
  106.  for i = 0, count() do
  107.   if userName == name[i] then
  108.     print("Account already exists")
  109.     foundAccountCr = false
  110.     return false
  111.   end
  112.   if i == count() then
  113.     if foundAccountCr == false then
  114.      encryptedPassword = enc.encrypt(userPassword, encryptionPassword)
  115.      table.insert(name, userName)
  116.      table.insert(money, 0)
  117.      table.insert(password, encryptedPassword)
  118.      table.insert(inTransactions, "")
  119.      table.insert(outTransactions, "")
  120.   return true
  121.     end
  122.   end
  123.  end
  124. end
  125.  
  126. --Used to check account balance
  127. function check(userName, userPassword)
  128.   if checkCredentials(userName, userPassword) == true then
  129.      for i = 0, count() do
  130.       if userName == name[i] then
  131.         foundAccountCk = true
  132.         return money[i]
  133.       end
  134.       if i == count() then
  135.         if foundAccountCk == false then
  136.           print("Account does not exist")
  137.           foundAccountCk = false
  138.         end
  139.       end
  140.      end
  141.   end
  142. end
  143.  
  144. --Used to give money to certain account
  145. function give(userName, userPassword, amount, receiverName, escapesFee)
  146.  if type(amount) == "number" then
  147.   if checkCredentials(userName, userPassword) == true then
  148.     if (check(userName, userPassword) - amount) >= 0 then
  149.      if checkUserExists(receiverName) == true then
  150.       --Set the receiver's amount of money
  151.       for i = 0, count() do
  152.         if receiverName == name[i] then
  153.           local moneyResult = money[i] + amount
  154.           if escapesFee == true then
  155.             table.insert(money, i, moneyResult)
  156.           else
  157.             table.insert(money, i, transac(moneyResult))
  158.           end
  159.           local osdate = os.time()
  160.           local inTransac = inTransactions[i] .. "| From: " .. userName .. " | +" .. amount .. "$ | " .. osdate .. " |,"
  161.           table.insert(inTransactions, i, inTransac)
  162.         end
  163.       end
  164.       --Set the sender's amount of money
  165.       for i = 0, count() do
  166.         if userName == name[i] then
  167.           local moneyResult = money[i] - amount
  168.           table.insert(money, i, moneyResult)
  169.           local osdate = os.time()
  170.           local outTransac = outTransactions[i] .. "| To: " .. receiverName .. " | -" .. amount .. "$ | " .. osdate .. " |,"
  171.           table.insert(outTransactions, i, outTransac)
  172.         end
  173.       end
  174.       saveDatabase()
  175.       return true
  176.      else
  177.       print("Receiver's account does not exist")
  178.       return false
  179.      end
  180.     else
  181.      return false
  182.     end
  183.   end
  184.   else
  185.   print("Amount provided is not numeric")
  186.   return false
  187.  end
  188. end
  189.  
  190. --Verify credentials
  191. function checkCredentials(userName, userPassword)
  192.   local encryptedPassword = enc.decrypt(userPassword, encryptionPassword)
  193.    for i = 0, count() do
  194.      if userName == name[i] then
  195.       if encryptedPassword == password[i] then
  196.         foundAccountC = true
  197.         return true
  198.       else
  199.         foundAccountC = true
  200.       end
  201.      end
  202.      if i == count() then
  203.       if foundAccountC == false then
  204.        print("Account does not exist")
  205.        foundAccountC = false
  206.       else
  207.        print("Passwords do not match")
  208.        foundAccountC = false
  209.        return false
  210.       end
  211.      end
  212.    end
  213. end
  214. --Used to check if a user exists. Does not require password
  215. function checkUserExists(userName)
  216.  for i = 0, count() do
  217.    if userName == name[i] then
  218.      foundAccountE = true
  219.      return true
  220.    end
  221.    if i == count then
  222.     if foundAccountE == false then
  223.       print("Account does not exist")
  224.       return false
  225.     end
  226.    end
  227.  end
  228. end
  229.  
  230. --Used to calculate amount after transactionFee
  231. function transac(amount)
  232.  if transactionFee >= 101 then
  233.   print("No fee applied: Transaction fee is over than 100!")
  234.   return 0
  235.  end
  236.   local fee = (transactionFee / 100) * amount
  237.   local finalAmount = amount - fee
  238.   serverMoney = serverMoney + fee
  239.   return finalAmount
  240. end
  241.  
  242. --Used to count how many accounts there are
  243. function count()
  244.  local count = 0
  245.  for _ in pairs(name) do count = count + 1 end
  246.  return count
  247. end
  248.  
  249. --ADMIN FUNCTIONS, BE CAREFUL WITH THESE
  250.  
  251. --Used to set the money of a user's account
  252. function setMoney(userName, amount, serverPassword)
  253. if serverPassword == serverAdminPassword then
  254.  if checkUserExists(userName) == true then
  255.     for i = 0, count() do
  256.       if userName == name[i] then
  257.         if type(amount) == "number" then
  258.           table.insert(money, i, amount)
  259.           saveDatabase()
  260.           return true
  261.         else
  262.           print("Amount provided is not numeric")
  263.           return false
  264.         end
  265.       end
  266.     end
  267.   else
  268.    print("Account does not exist")
  269.    return false
  270.   end
  271.  else
  272.    print("Server passwords do not match")
  273.    return false
  274.  end
  275. end
  276.  
  277. --Used to show money of all users
  278. function listMoney(serverPassword)
  279. if serverPassword == serverAdminPassword then
  280.  local moneyList = ""
  281.   if count() == 0 then
  282.    return "There are no accounts in this database"
  283.   else
  284.   for i = 1, count() do
  285.     moneyList = moneyList .. " | " .. name[i] .. ": " .. money[i] .. "$"
  286.   end
  287.   return moneyList
  288.   end
  289.  else
  290.    print("Server passwords do not match")
  291.    return false
  292.  end
  293. end
  294.  
  295. --Used to show money of a specific user
  296. function showMoney(userName, serverPassword)
  297. if serverPassword == serverAdminPassword then
  298.  if checkUserExists(userName) == true then
  299.     for i = 0, count() do
  300.       if userName == name[i] then
  301.         return name[i] .. " has " .. money[i] .. "$"
  302.       end
  303.     end
  304.   else
  305.     print("Account does not exist")
  306.     return false
  307.   end
  308.  else
  309.    print("Server passwords do not match")
  310.    return false
  311.  end
  312. end
  313.  
  314.  
  315. --Used to give money to a user instead having to manually calculate amount and use setMoney()
  316. function addMoney(userName, amount, serverPassword)
  317. if serverPassword == serverAdminPassword then
  318.  if type(amount) == "number" then
  319.   if checkUserExists(userName) == true then
  320.     for i = 0, count() do
  321.       if userName == name[i] then
  322.          local moneyResult = money[i] + amount
  323.          table.insert(money, i, moneyResult)
  324.          saveDatabase()
  325.          return true
  326.       end
  327.     end
  328.   else
  329.     print("Account does not exist")
  330.     return false
  331.   end
  332.  else
  333.   print("Amount provided is not numeric")
  334.   return false
  335.  end
  336.  else
  337.    print("Server passwords do not match")
  338.    return false
  339.  end
  340. end
  341.  
  342. --Used to remove money from a user instead having to manually calculate amount and use setMoney()
  343. function removeMoney(userName, amount, serverPassword)
  344. if serverPassword == serverAdminPassword then
  345.  if type(amount) == "number" then
  346.   if checkUserExists(userName) == true then
  347.     for i = 0, count() do
  348.       if userName == name[i] then
  349.          local moneyResult = money[i] - amount
  350.          table.insert(money, i, moneyResult)
  351.          saveDatabase()
  352.          return true
  353.       end
  354.     end
  355.   else
  356.     print("Account does not exist")
  357.     return false
  358.   end
  359.  else
  360.   print("Amount provided is not numeric")
  361.   return false
  362.  end
  363.  else
  364.    print("Server passwords do not match")
  365.    return false
  366.  end
  367. end
  368.  
  369. --Deletes the user's account
  370. function deleteAccount(userName, serverPassword)
  371.  if serverPassword == serverAdminPassword then
  372.   if checkUserExists(userName) == true then
  373.     for i = 0, count() do
  374.       if userName == name[i] then
  375.         table.remove(name, i)
  376.         table.remove(money, i)
  377.         table.remove(password, i)
  378.         table.remove(inTransactions, i)
  379.         table.remove(outTransactions, i)
  380.         saveDatabase()
  381.       end
  382.     end
  383.     return true
  384.   end
  385.  else
  386.    print("Server passwords do not match")
  387.    return false
  388.  end
  389. end
  390.  
  391. --Lists the user's in transactions
  392. function showInTransaction(userName, serverPassword)
  393.  if serverPassword == serverAdminPassword then
  394.   if checkUserExists(userName) == true then
  395.     for i = 0, count() do
  396.       if userName == name[i] then
  397.         local transactions = ""
  398.         for word in string.gmatch(inTransactions[i], '([^,]+)') do
  399.          transactions = transactions .. "\n" .. word
  400.         end
  401.         return "Last In Transactions of " .. userName .. "\n" .. transactions
  402.       end
  403.     end
  404.   else
  405.    print("Account does not exist")
  406.    return false
  407.   end
  408.   else
  409.    print("Server passwords do not match")
  410.    return false
  411.  end
  412. end
  413.  
  414. --Lists the user's out transactions
  415. function showOutTransaction(userName, serverPassword)
  416. if serverPassword == serverAdminPassword then
  417.   if checkUserExists(userName) == true then
  418.     for i = 0, count() do
  419.       if userName == name[i] then
  420.         local transactions = ""
  421.         for word in string.gmatch(outTransactions[i], '([^,]+)') do
  422.          transactions = transactions .. "\n" .. word
  423.         end
  424.         return "Last Out Transactions of " .. userName .. "\n" .. transactions
  425.       end
  426.     end
  427.   else
  428.    print("Account does not exist")
  429.    return false
  430.   end
  431.     else
  432.    print("Server passwords do not match")
  433.    return false
  434.  end
  435. end
  436.  
  437. --Sets the user's password
  438. function setPassword(userName, newPassword, serverPassword)
  439.   if serverPassword == serverAdminPassword then
  440.       encryptedPassword = enc.encrypt(newPassword, encryptionPassword)
  441.       if checkUserExists(userName) == true then
  442.         for i = 0, count() do
  443.           if userName == name[i] then
  444.             table.insert(password, i, encryptedPassword)
  445.             return true
  446.           end
  447.         end
  448.       else
  449.         print("Account does not exist")
  450.         return false
  451.       end
  452.       else
  453.    print("Server passwords do not match")
  454.    return false
  455.   end
  456. end
  457.  
  458. --Other functions
  459.  
  460. --Splits the string
  461. function split(str, pat)
  462.    local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  463.    local fpat = "(.-)" .. pat
  464.    local last_end = 1
  465.    local s, e, cap = str:find(fpat, 1)
  466.    while s do
  467.       if s ~= 1 or cap ~= "" then
  468.      table.insert(t,cap .. "}")
  469.       end
  470.       last_end = e+1
  471.       s, e, cap = str:find(fpat, last_end)
  472.    end
  473.    if last_end <= #str then
  474.       cap = str:sub(last_end)
  475.       table.insert(t, cap .. "}")
  476.    end
  477.    return t
  478. end
  479.  
  480. if autoRead == true then
  481.   loadDatabase()
  482. end
Advertisement
Add Comment
Please, Sign In to add comment