Advertisement
Guest User

KREDITAPI

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