-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.lua
More file actions
186 lines (164 loc) · 5.05 KB
/
database.lua
File metadata and controls
186 lines (164 loc) · 5.05 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
-- database.lua
-----------------
-- Handles db operations on comptuer
----------------
local db = {}
--TODO: Uncomment in production
local log4cc = require "lib.log4cc"
log4cc.config.file.enabled = true
log4cc.config.file.fileName = "/log/db.txt"
local DATA_DIR = "/data/"
local DEFAULT = {
balance = 100
}
------------------------
-- Database functions --
------------------------
function db.authorize(wallet, secret)
if not wallet_exists(wallet) then
return nil
end -- if not wallet_exists
local wallet = db.load(wallet)
if wallet.secret == secret then
return wallet
else
return nil
end -- if wallet.secret == secret
end -- function db.authorize
function db.create(wallet, secret)
if wallet_exists(wallet) then
-- log4cc.error("Attempted CREATE for existing wallet ("..wallet..")")
return nil
end -- if wallet_exists
log4cc.info("CREATE ("..wallet..")")
return db.commit(wallet, {name=wallet, secret=secret, balance=DEFAULT.balance})
end -- function db.create
function db.select(wallet)
if not wallet_exists(wallet) then
return nil
end -- if not wallet_exists
return db.load(wallet)
end -- funcion db.select
function db.query(key)
local wallets = db.load_all()
if key == "all" or key == "*" then
return wallets
elseif key == "users" or key == "names" then
return db.query_names(wallets)
elseif key == "balances" then
return db.query_balances(wallets)
end -- if key == "str"
end -- function db.query
function db.query_names(wallets)
if not wallets then
wallets = db.load_all()
end -- if not wallets
for i=1, #wallets do
wallets[i] = wallets[i]["name"]
end -- for i=1, #wallets
return wallets
end -- function db.query_wallets
--> Aliases
db.query_wallets = db.query_names
function db.query_balances(wallets)
if not wallets then
wallets = db.load_all()
end -- if not wallets
for i=1, #wallets do
wallets[i] = wallets[i]["balance"]
end -- for i=1, #wallets
return wallets
end -- function db.query_balances
function db.update(wallet, key, value, quiet)
if not wallet_exists(wallet) then
return nil
end -- if not wallet_exists
local data = db.load(wallet)
data[key] = value
local _ = not quiet and log4cc.info("UPDATE ("..wallet..") with ("..key..","..value..")")
return db.commit(wallet, data)
end -- function db.update
function db.deposit(wallet, value, quiet)
if not wallet_exists(wallet) then
return nil
end -- if not wallet_exists
local data = db.load(wallet)
data.balance = data.balance + value
local _ = not quiet and log4cc.info("DEPOSIT $"..value.." into ("..wallet..")")
return db.commit(wallet, data)
end -- function db.deposit
function db.withdraw(wallet, value, quiet)
if not wallet_exists(wallet) then
return nil
end -- if not wallet_exists
local data = db.load(wallet)
data.balance = data.balance - value
local _ = not quiet and log4cc.info("WITHDRAW $"..value.." from ("..wallet..")")
return db.commit(wallet, data)
end -- function db.deposit
function db.transfer(wallet_from, wallet_to, value, quiet)
-- Check that wallets exists
if not wallet_exists(wallet_from) or
not wallet_exists(wallet_to) then
return nil
end -- if not wallet_exists()
-- Access both wallets
local _ = not quiet and log4cc.info("TRANSFER $"..value.." from ("..wallet_from..") to ("..wallet_to..")")
return db.deposit(wallet_to, value, true) and db.withdraw(wallet_from, value, true)
end -- function db.transfer
-- function db.delete(wallet)
-- return nil
-- end -- function db.delete
----------------------
-- Helper functions --
----------------------
function to_path(wallet)
return DATA_DIR..wallet..".dat"
end -- function to_path
function to_wallet(path)
return path:sub(1, -#".dat"-1)
end -- function to_wallet
function wallet_exists(wallet)
local path = to_path(wallet)
return fs.exists(path)
end -- function wallet_exists
---------------------
-- File operations --
---------------------
-- Open user with name wallet
function db.load(wallet)
local path = to_path(wallet)
local file = fs.open(path, "r")
-- File does not exist
if not file then
return nil
else
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end -- if not file
end -- function db.unpack
function db.load_all()
local wallets = fs.list(DATA_DIR)
for i=1, #wallets do
-- remove file type
wallets[i] = db.load(to_wallet(wallets[i]))
end -- for i=1, #wallets
return wallets
end -- function load_all
-- Save the user with table of data
function db.commit(wallet, data)
local path = to_path(wallet)
local file = fs.open(path, "w")
-- File does not exist
if not file then
return nil
else
local text = textutils.serialize(data)
file.write(text)
file.close()
return true
end -- if not file
end -- function db.pack
-- Module end
return db