· 6 years ago · Mar 20, 2019, 12:02 AM
1local function savereserve(char)
2 nut.db.updateTable({
3 _reserve = char:getReserve()
4 }, nil, "reserve", "_charID = "..char:getID())
5end
6
7function SCHEMA:OnReserveChanged(char)
8 savereserve(char)
9end
10
11do
12 local MYSQL_CREATE_TABLES = [[
13CREATE TABLE IF NOT EXISTS `nut_reserve` (
14 `_charID` int(11) NOT NULL,
15 `_reserve` int(11) unsigned DEFAULT NULL,
16 PRIMARY KEY (`_charID`)
17);
18 ]]
19 local SQLITE_CREATE_TABLES = [[
20CREATE TABLE IF NOT EXISTS `nut_reserve` (
21 `_charID` INTEGER PRIMARY KEY,
22 `_reserve` INTEGER
23);
24 ]]
25
26 function SCHEMA:OnLoadTables()
27 if (nut.db.object) then
28 -- This is needed to perform multiple queries since the string is only 1 big query.
29 local queries = string.Explode(";", MYSQL_CREATE_TABLES)
30
31 nut.db.query(queries[1])
32 nut.db.query(queries[2])
33 else
34 nut.db.query(SQLITE_CREATE_TABLES)
35 end
36 end
37
38 function SCHEMA:CharacterPreSave(char)
39 savereserve(char)
40 end
41
42 function SCHEMA:CharacterLoaded(id)
43 -- legacy support
44 -- for modernRP users
45 local char = nut.char.loaded[id]
46 local legacy = false
47
48 if (char:getData("reserve")) then
49 local restore = char:getData("reserve", 0)
50
51 char:setReserve(tonumber(restore))
52 char:setData("reserve", nil)
53 legacy = true
54 end
55
56 nut.db.query("SELECT _reserve FROM nut_reserve WHERE _charID = "..id, function(data)
57 if (data and #data > 0) then
58 for k, v in ipairs(data) do
59 local money = tonumber(v._reserve)
60
61 if (!legacy) then
62 char:setReserve(money)
63 end
64 end
65 else
66 nut.db.insertTable({
67 _reserve = 0,
68 _charID = id,
69 }, function(data)
70 if (!legacy) then
71 char:setReserve(0)
72 end
73 end, "reserve")
74 end
75 end)
76 end
77
78 function SCHEMA:PreCharDelete(client, char)
79 nut.db.query("DELETE FROM nut_reserve WHERE _charID = "..char:getID())
80 end
81
82end