View difference between Paste ID: PUeRnkrS and mqGsvHT0
SHOW: | | - or go back to the newest paste.
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)
7+
8
end
9
10
function jsonFileToTable(fileName)
11
    aTable = {}
12
    jsonFile = io.open(fileName, "r")
13
    jsonString = jsonFile:read("*all")
14
    if jsonString:len() > 0 then
15
        aTable =  json.decode(jsonString)
16
    end
17
    io.close(jsonFile)
18
    return aTable
19
end
20
21
function tableToJSONFile(dbTable, fileName)
22
    jsonString = json.encode(dbTable)
23
    jsonFile = io.open(fileName, "w")
24
    io.output(jsonFile)
25
    io.write(jsonString)
26
    io.close(jsonFile)
27
end
28
29
-- yoinked from user Michal Kottman on stackoverflow, iterator to sort a table
30
function spairs(t)
31
    -- collect the keys
32
    local keys = {}
33
    for k in pairs(t) do keys[#keys+1] = k end
34
35
    -- sort the keys
36
    table.sort(keys)
37
38
    -- return the iterator function
39
    local i = 0
40
    return function()
41
        i = i + 1
42
        if keys[i] then
43
            return keys[i], t[keys[i]]
44
        end
45
    end
46
end
47
48
function printDatabase(db)
49
    for k, v in spairs(db) do
50
        print(k..":")
51
        for x, y in ipairs(v) do
52
            print("    "..y["Level"]..": "..y["Amount"])
53
        end
54
        print("")
55
    end
56
end
57
58
function stripLevel(enchantName)
59
    s = enchantName:find("%sI") or enchantName:find("%sV")
60
    if s then
61
        enchantName = enchantName:sub(0, s-1)
62
    end
63
    return enchantName
64
end
65
66
function insertEnchant(db, enchant)
67
    noLevel = stripLevel(enchant)
68
    if db[noLevel] then
69
        for k, v in ipairs(db[noLevel]) do
70
            if v["Level"] == enchant then
71
                v["Amount"] = v["Amount"] + 1
72
                return
73
            end
74
        end
75
        table.insert(db[noLevel], {Level = enchant, Amount = 1})
76
    else
77
        db[noLevel] = {{Level = enchant, Amount = 1}}
78
    end
79
end
80
81
function removeEnchant(db, enchantLevel)
82
    noLevel = stripLevel(enchantLevel)
83
    if db[noLevel] then
84
        for k, v in ipairs(db[noLevel]) do
85
            if v["Level"] == enchantLevel then
86
                if v["Amount"] > 0 then
87
                    v["Amount"] = v["Amount"] - 1
88
                else
89
                    print("No "..enchantLevel.." books remaining.")
90
                end
91
                return
92
            end
93
        end
94
    end
95
end
96
97
createDatabase("database.json")
98
local database = jsonFileToTable("database.json")
99
100
insertEnchant(database, "Sharpness V")
101
insertEnchant(database, "Sharpness V")
102
103
insertEnchant(database, "Soul Stealer III")
104
removeEnchant(database, "Sharpness V")
105
insertEnchant(database, "Sharpness IV")
106
insertEnchant(database, "Sharpness I")
107
insertEnchant(database, "Sharpness III")
108
insertEnchant(database, "Efficiency IV")
109
insertEnchant(database, "Silk Touch")
110
removeEnchant(database, "LOL III")
111
112
tableToJSONFile(database, "database.json")
113
local database = jsonFileToTable("database.json")
114
printDatabase(database)