Repository navigation
Expand file tree
/
Copy pathdb.lua
More file actions
69 lines (56 loc) · 1.81 KB
/
Copy pathdb.lua
File metadata and controls
69 lines (56 loc) · 1.81 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
local ie = minetest.request_insecure_environment() or
error("Mod requires decreased security settings in minetest.conf")
local driver = ie.require("luasql.postgres")
local db = nil
local env = nil
local u = pmutils
local function prep_db()
env = assert (driver.postgres())
-- connect to data source
db = assert (env:connect("citadella", "mt"))
-- group table, named ctgroup because heck quoted table names
local res = assert(u.prepare(db, [[
CREATE TABLE IF NOT EXISTS ctgroup (
id VARCHAR(16) NOT NULL,
name VARCHAR(16) NOT NULL,
description VARCHAR(128),
creation_date TIMESTAMP NOT NULL,
PRIMARY KEY (id),
UNIQUE (name)
)]]))
-- create reinforcements table
res = assert(u.prepare(db, [[
CREATE TABLE IF NOT EXISTS reinforcement (
x INTEGER NOT NULL,
y INTEGER NOT NULL,
z INTEGER NOT NULL,
value INTEGER NOT NULL,
material VARCHAR(50) NOT NULL,
ctgroup_id VARCHAR(32) REFERENCES ctgroup(id),
PRIMARY KEY (x, y, z)
)]]))
-- player table
res = assert(u.prepare(db, [[
CREATE TABLE IF NOT EXISTS player (
id VARCHAR(16) NOT NULL,
name VARCHAR(16) NOT NULL,
join_date TIMESTAMP NOT NULL,
PRIMARY KEY (id),
UNIQUE (name)
)]]))
-- maps players to groups/groups to players
-- TODO: sort of permissions, keep basic for now
res = assert(u.prepare(db, [[
CREATE TABLE IF NOT EXISTS player_ctgroup (
player_id varchar(16) REFERENCES player(id),
ctgroup_id varchar(16) REFERENCES ctgroup(id),
permission varchar(32) NOT NULL,
PRIMARY KEY (player_id, ctgroup_id)
)]]))
end
prep_db()
minetest.register_on_shutdown(function()
db:close()
env:close()
end)
return db