Advertisement
Guest User

KREDITAPI

a guest
Apr 16th, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.52 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. -- check if banned
  194. function checkIfBanned(userName)
  195.   for i = 0, count() do
  196.     if userName == name[i] then
  197.       return isBanned[i] == true
  198.     end
  199.   end
  200. end
  201.  
  202.  
  203. --Verify credentials
  204. function checkCredentials(userName, userPassword)
  205. --  local encryptedPassword = enc.decrypt(userPassword, encryptionPassword)
  206.    for i = 0, count() do
  207.      if userName == name[i] then
  208.       if userPassword == password[i] then
  209.         foundAccountC = true
  210.         return true
  211.       else
  212.         foundAccountC = true
  213.       end
  214.      end
  215.      if i == count() then
  216.       if foundAccountC == false then
  217.        print("Account does not exist")
  218.        foundAccountC = false
  219.       else
  220.        print("Passwords do not match")
  221.        foundAccountC = false
  222.        return false
  223.       end
  224.      end
  225.    end
  226. end
  227. --Used to check if a user exists. Does not require password
  228. function checkUserExists(userName)
  229.  for i = 0, count() do
  230.    if userName == name[i] then
  231.      foundAccountE = true
  232.      return true
  233.    end
  234.    if i == count() then
  235.     if foundAccountE == false then
  236.       print("Account does not exist")
  237.       return false
  238.     end
  239.    end
  240.  end
  241. end
  242.  
  243. --Used to calculate amount after transactionFee
  244. function transac(amount)
  245.  if transactionFee >= 101 then
  246.   print("No fee applied: Transaction fee is over than 100!")
  247.   return 0
  248.  end
  249.   local fee = (transactionFee / 100) * amount
  250.   local finalAmount = amount - fee
  251.   serverMoney = serverMoney + fee
  252.   return finalAmount
  253. end
  254.  
  255. --Used to count how many accounts there are
  256. function count()
  257.  local count = 0
  258.  for _ in pairs(name) do count = count + 1 end
  259.  return count
  260. end
  261.  
  262. --ADMIN FUNCTIONS, BE CAREFUL WITH THESE
  263.  
  264. --Used to set the money of a user's account
  265. function setMoney(userName, amount, serverPassword)
  266. if serverPassword == serverAdminPassword then
  267.  if checkUserExists(userName) == true then
  268.     for i = 0, count() do
  269.       if userName == name[i] then
  270.         if type(amount) == "number" then
  271.           table.insert(money, i, amount)
  272.           saveDatabase()
  273.           return true
  274.         else
  275.           print("Amount provided is not numeric")
  276.           return false
  277.         end
  278.       end
  279.     end
  280.   else
  281.    print("Account does not exist")
  282.    return false
  283.   end
  284.  else
  285.    print("Server passwords do not match")
  286.    return false
  287.  end
  288. end
  289.  
  290. --Used to show money of all users
  291. function listMoney(serverPassword)
  292. if serverPassword == serverAdminPassword then
  293.  local moneyList = ""
  294.   if count() == 0 then
  295.    return "There are no accounts in this database"
  296.   else
  297.   for i = 1, count() do
  298.     moneyList = moneyList .. " | " .. name[i] .. ": " .. money[i] .. "$"
  299.   end
  300.   return moneyList
  301.   end
  302.  else
  303.    print("Server passwords do not match")
  304.    return false
  305.  end
  306. end
  307.  
  308. -- Used to ban a user
  309. function ban(userName)
  310.   loadDatabase()
  311.   for i = 0, count() do
  312.     if userName == name[i] then
  313.       table.insert(isBanned, i, true)
  314.       saveDatabase()
  315.       return true
  316.     end
  317.   end
  318. end
  319.  
  320. -- Used to un ban a user
  321. function unban(userName)
  322.   loadDatabase()
  323.   for i = 0, count() do
  324.     if userName == name[i] then
  325.       table.insert(isBanned, i, false)
  326.       saveDatabase()
  327.       return true
  328.     else
  329.       return false
  330.     end
  331.   end
  332. end
  333.  
  334.  
  335. --Used to show money of a specific user
  336. function showMoney(userName, serverPassword)
  337. if serverPassword == serverAdminPassword then
  338.  if checkUserExists(userName) == true then
  339.     for i = 0, count() do
  340.       if userName == name[i] then
  341.         return name[i] .. " has " .. money[i] .. "$"
  342.       end
  343.     end
  344.   else
  345.     print("Account does not exist")
  346.     return false
  347.   end
  348.  else
  349.    print("Server passwords do not match")
  350.    return false
  351.  end
  352. end
  353.  
  354.  
  355. --Used to give money to a user instead having to manually calculate amount and use setMoney()
  356. function addMoney(userName, amount, serverPassword)
  357. if serverPassword == serverAdminPassword then
  358.  if type(amount) == "number" then
  359.   if checkUserExists(userName) == true then
  360.     for i = 0, count() do
  361.       if userName == name[i] then
  362.          local moneyResult = money[i] + amount
  363.          table.insert(money, i, moneyResult)
  364.          saveDatabase()
  365.          return true
  366.       end
  367.     end
  368.   else
  369.     print("Account does not exist")
  370.     return false
  371.   end
  372.  else
  373.   print("Amount provided is not numeric")
  374.   return false
  375.  end
  376.  else
  377.    print("Server passwords do not match")
  378.    return false
  379.  end
  380. end
  381.  
  382. --Used to remove money from a user instead having to manually calculate amount and use setMoney()
  383. function removeMoney(userName, amount, serverPassword)
  384. if serverPassword == serverAdminPassword then
  385.  if type(amount) == "number" then
  386.   if checkUserExists(userName) == true then
  387.     for i = 0, count() do
  388.       if userName == name[i] then
  389.          local moneyResult = money[i] - amount
  390.          table.insert(money, i, moneyResult)
  391.          saveDatabase()
  392.          return true
  393.       end
  394.     end
  395.   else
  396.     print("Account does not exist")
  397.     return false
  398.   end
  399.  else
  400.   print("Amount provided is not numeric")
  401.   return false
  402.  end
  403.  else
  404.    print("Server passwords do not match")
  405.    return false
  406.  end
  407. end
  408.  
  409. --Deletes the user's account
  410. function deleteAccount(userName, serverPassword)
  411.  if serverPassword == serverAdminPassword then
  412.   if checkUserExists(userName) == true then
  413.     for i = 0, count() do
  414.       if userName == name[i] then
  415.   table.remove(diskID, i)
  416.      table.remove(name, i)
  417.         table.remove(money, i)
  418.         table.remove(password, i)
  419.         table.remove(inTransactions, i)
  420.         table.remove(outTransactions, i)
  421.         saveDatabase()
  422.       end
  423.     end
  424.     return true
  425.   end
  426.  else
  427.    print("Server passwords do not match")
  428.    return false
  429.  end
  430. end
  431.  
  432. --Lists the user's in transactions
  433. function showInTransaction(userName, serverPassword)
  434.  if serverPassword == serverAdminPassword then
  435.   if checkUserExists(userName) == true then
  436.     for i = 0, count() do
  437.       if userName == name[i] then
  438.         local transactions = ""
  439.         for word in string.gmatch(inTransactions[i], '([^,]+)') do
  440.          transactions = transactions .. "\n" .. word
  441.         end
  442.         return "Last In Transactions of " .. userName .. "\n" .. transactions
  443.       end
  444.     end
  445.   else
  446.    print("Account does not exist")
  447.    return false
  448.   end
  449.   else
  450.    print("Server passwords do not match")
  451.    return false
  452.  end
  453. end
  454.  
  455. --Lists the user's out transactions
  456. function showOutTransaction(userName, serverPassword)
  457. if serverPassword == serverAdminPassword then
  458.   if checkUserExists(userName) == true then
  459.     for i = 0, count() do
  460.       if userName == name[i] then
  461.         local transactions = ""
  462.         for word in string.gmatch(outTransactions[i], '([^,]+)') do
  463.          transactions = transactions .. "\n" .. word
  464.         end
  465.         return "Last Out Transactions of " .. userName .. "\n" .. transactions
  466.       end
  467.     end
  468.   else
  469.    print("Account does not exist")
  470.    return false
  471.   end
  472.     else
  473. --   print("Server passwords do not match")
  474.    return false
  475.  end
  476. end
  477.  
  478. --change user password
  479. function changePassword(userName, oldPassword, newPassword)
  480.   if (checkCredentials(userName, oldPassword) == true) then
  481.     for i = 0, count() do
  482.       if userName == name[i] then
  483.         table.insert(password, i, newPassword)
  484.         return true
  485.       end
  486.     end
  487.   else
  488.     return false
  489.   end
  490. end    
  491.  
  492.  
  493.  
  494. --Sets the user's password
  495. function setPassword(userName, newPassword, serverPassword)
  496.   if serverPassword == serverAdminPassword then
  497. --      encryptedPassword = enc.encrypt(newPassword, encryptionPassword)
  498.       if checkUserExists(userName) == true then
  499.         for i = 0, count() do
  500.           if userName == name[i] then
  501.             table.insert(password, i, userPassword)
  502.             return true
  503.           end
  504.         end
  505.       else
  506.         print("Account does not exist")
  507.         return false
  508.       end
  509.       else
  510.    print("Server passwords do not match")
  511.    return false
  512.   end
  513. end
  514.  
  515. --Other functions
  516.  
  517. --Splits the string
  518. function split(str, pat)
  519.    local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  520.    local fpat = "(.-)" .. pat
  521.    local last_end = 1
  522.    local s, e, cap = str:find(fpat, 1)
  523.    while s do
  524.       if s ~= 1 or cap ~= "" then
  525.      table.insert(t,cap .. "}")
  526.       end
  527.       last_end = e+1
  528.       s, e, cap = str:find(fpat, last_end)
  529.    end
  530.    if last_end <= #str then
  531.       cap = str:sub(last_end)
  532.       table.insert(t, cap .. "}")
  533.    end
  534.    return t
  535. end
  536.  
  537. if autoRead == true then
  538.   loadDatabase()
  539. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement