-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
124 lines (75 loc) · 2.93 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
-- main.lua
--[[
Implements the main logic for the plugin
--]]
--- The SQLite database used for the storage
-- Initialized in InitDB()
local gDB
--- If the player is not yet in the DB, create an entry for them
local function MakeSurePlayerIsPresent(aPlayerUuid)
assert(type(aPlayerUuid) == "string")
-- Insert the player; this will fail silently if the player already exists thanks to the UNIQUE constraint
gDB:exec('INSERT INTO PlayerBalances (UUID, Balance) VALUES ("' .. aPlayerUuid .. '", 0);')
end
--- Returns the player's current balance
function GetPlayerBalance(aPlayerUuid)
assert(type(aPlayerUuid) == "string")
MakeSurePlayerIsPresent(aPlayerUuid)
for aBal in gDB:urows("SELECT Balance FROM PlayerBalances WHERE UUID='" .. aPlayerUuid .. "';") do
return aBal
end
-- DB error?
return false
end
function SetPlayerBalance(aPlayerUuid, aNewBalance)
assert(type(aPlayerUuid) == "string")
assert(type(aNewBalance) == "number")
gDB:exec("BEGIN TRANSACTION")
MakeSurePlayerIsPresent(aPlayerUuid)
gDB:exec("UPDATE PlayerBalances SET Balance = " .. aNewBalance .. " WHERE UUID = '" .. aPlayerUuid .. "';")
gDB:exec("COMMIT;")
return true
end
--- Modifies the player's current balance by the specified amount
-- Returns the player's new balance on success, false on failure
function ChangePlayerBalance(aPlayerUuid, aDelta)
assert(type(aPlayerUuid) == "string")
assert(type(aDelta) == "number")
gDB:exec("BEGIN TRANSACTION")
MakeSurePlayerIsPresent(aPlayerUuid)
local NewBalance = GetPlayerBalance(aPlayerUuid) + aDelta
SetPlayerBalance(aPlayerUuid, NewBalance)
gDB:exec("COMMIT;")
return NewBalance
end
--- Transfers aAmount from aFromPlayer to aToPlayer, if aFromPlayer has enough funds
-- Returns true if transfer succeeds, false if not
function TransferPlayerBalance(aFromPlayerUuid, aToPlayerUuid, aAmount)
assert(type(aFromPlayerUuid) == "string")
assert(type(aToPlayerUuid) == "string")
assert(type(aAmount) == "number")
gDB:exec("BEGIN TRANSACTION")
MakeSurePlayerIsPresent(aFromPlayerUuid)
MakeSurePlayerIsPresent(aToPlayerUuid)
if (GetPlayerBalance(aFromPlayerUuid) < aAmount) then
return false
else
ChangePlayerBalance(aFromPlayerUuid, -aAmount)
ChangePlayerBalance(aToPlayerUuid, aAmount)
return true
end
gDB:exec("COMMIT;")
end
--- Opens the DB file and initializes the structure:
local function InitDB()
gDB = sqlite3.open('PlayerBalances.sqlite3')
-- TODO: We need a migration from when the table had no UNIQUE constraint
gDB:exec("CREATE TABLE IF NOT EXISTS PlayerBalances(UUID text(36) UNIQUE, Balance decimal);")
end
--- The main entrypoint, called by Cuberite upon plugin load
function Initialize(Plugin)
InitDB()
-- Allow the external API, callable by other plugins:
dofile(cPluginManager:GetCurrentPlugin():GetLocalFolder() .. "/API/manage.lua")
return true
end