Guest User

enchantDatabase.lua

a guest
May 9th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.96 KB | None | 0 0
  1. json = require("/json")
  2.  
  3. function createDatabase(dbName)
  4.     if not io.open(dbName, "r") then
  5.         db = io.open(dbName, "w")
  6.         io.close(db)
  7. --        sleep(0.1)
  8.     end
  9. end
  10.  
  11. function jsonFileToTable(fileName)
  12.     aTable = {}
  13.     jsonFile = io.open(fileName, "r")
  14.     jsonString = jsonFile:read("*all")
  15.     if jsonString:len() > 0 then
  16.         aTable =  json.decode(jsonString)
  17.     end
  18.     io.close(jsonFile)
  19.     return aTable
  20. end
  21.  
  22. function tableToJSONFile(dbTable, fileName)
  23.     jsonString = json.encode(dbTable)
  24.     jsonFile = io.open(fileName, "w")
  25.     io.output(jsonFile)
  26.     io.write(jsonString)
  27.     io.close(jsonFile)
  28. end
  29.  
  30. -- yoinked from user Michal Kottman on stackoverflow, iterator to sort a table
  31. function spairs(t)
  32.     -- collect the keys
  33.     local keys = {}
  34.     for k in pairs(t) do keys[#keys+1] = k end
  35.  
  36.     -- sort the keys
  37.     table.sort(keys)
  38.  
  39.     -- return the iterator function
  40.     local i = 0
  41.     return function()
  42.         i = i + 1
  43.         if keys[i] then
  44.             return keys[i], t[keys[i]]
  45.         end
  46.     end
  47. end
  48.  
  49. function printDatabase(db)
  50.     for k, v in spairs(db) do
  51.         print(k..":")
  52.         for x, y in ipairs(v) do
  53.             print("    "..y["Level"]..": "..y["Amount"])
  54.         end
  55.         print("")
  56.     end
  57. end
  58.  
  59. function stripLevel(enchantName)
  60.     s = enchantName:find("%sI") or enchantName:find("%sV")
  61.     if s then
  62.         enchantName = enchantName:sub(0, s-1)
  63.     end
  64.     return enchantName
  65. end
  66.  
  67. function insertEnchant(db, enchant)
  68.     noLevel = stripLevel(enchant)
  69.     if db[noLevel] then
  70.         for k, v in ipairs(db[noLevel]) do
  71.             if v["Level"] == enchant then
  72.                 v["Amount"] = v["Amount"] + 1
  73.                 return
  74.             end
  75.         end
  76.         table.insert(db[noLevel], {Level = enchant, Amount = 1})
  77.     else
  78.         db[noLevel] = {{Level = enchant, Amount = 1}}
  79.     end
  80. end
  81.  
  82. function removeEnchant(db, enchantLevel)
  83.     noLevel = stripLevel(enchantLevel)
  84.     if db[noLevel] then
  85.         for k, v in ipairs(db[noLevel]) do
  86.             if v["Level"] == enchantLevel then
  87.                 if v["Amount"] > 0 then
  88.                     v["Amount"] = v["Amount"] - 1
  89.                 else
  90.                     print("No "..enchantLevel.." books remaining.")
  91.                 end
  92.                 return
  93.             end
  94.         end
  95.     end
  96. end
  97.  
  98. createDatabase("database.json")
  99. local database = jsonFileToTable("database.json")
  100.  
  101. insertEnchant(database, "Sharpness V")
  102. insertEnchant(database, "Sharpness V")
  103.  
  104. insertEnchant(database, "Soul Stealer III")
  105. removeEnchant(database, "Sharpness V")
  106. insertEnchant(database, "Sharpness IV")
  107. insertEnchant(database, "Sharpness I")
  108. insertEnchant(database, "Sharpness III")
  109. insertEnchant(database, "Efficiency IV")
  110. insertEnchant(database, "Silk Touch")
  111. removeEnchant(database, "LOL III")
  112.  
  113. tableToJSONFile(database, "database.json")
  114. local database = jsonFileToTable("database.json")
  115. printDatabase(database)
Advertisement
Add Comment
Please, Sign In to add comment