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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
CREATE TABLE IF NOT EXISTS blacklist_ip ( ip TEXT NOT NULL PRIMARY KEY ) WITHOUT ROWID;
CREATE TABLE IF NOT EXISTS blacklist_mail ( mail TEXT NOT NULL PRIMARY KEY ) WITHOUT ROWID;
CREATE TABLE IF NOT EXISTS titles (
title_id TEXT NOT NULL PRIMARY KEY,
title_name TEXT NOT NULL,
bgg INTEGER,
hidden BOOLEAN
) WITHOUT ROWID;
CREATE TABLE IF NOT EXISTS roles (
title_id TEXT NOT NULL REFERENCES titles(title_id) ON DELETE CASCADE,
role TEXT NOT NULL,
UNIQUE ( title_id, role )
);
-- Users --
CREATE TABLE IF NOT EXISTS users (
user_id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL UNIQUE COLLATE NOCASE,
mail TEXT NOT NULL UNIQUE COLLATE NOCASE,
notify BOOLEAN NOT NULL DEFAULT 0,
banned BOOLEAN NOT NULL DEFAULT 0,
ctime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
password TEXT NOT NULL,
salt TEXT NOT NULL,
about TEXT
);
CREATE TABLE IF NOT EXISTS user_last_seen (
user_id INTEGER NOT NULL PRIMARY KEY REFERENCES users(user_id) ON DELETE CASCADE,
atime TIMESTAMP NOT NULL,
aip TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS tokens (
user_id INTEGER NOT NULL PRIMARY KEY REFERENCES users(user_id) ON DELETE CASCADE,
token TEXT NOT NULL,
time TIMESTAMP NOT NULL
);
CREATE TABLE IF NOT EXISTS last_notified (
game_id INTEGER NOT NULL REFERENCES games(game_id) ON DELETE CASCADE,
user_id INTEGER NOT NULL REFERENCES users(user_id) ON DELETE CASCADE,
time TIMESTAMP NOT NULL,
PRIMARY KEY ( game_id, user_id )
) WITHOUT ROWID;
DROP VIEW IF EXISTS user_view;
CREATE VIEW user_view AS
SELECT
user_id, name, mail, notify
FROM users
;
DROP VIEW IF EXISTS user_login_view;
CREATE VIEW user_login_view AS
SELECT
user_id, name, mail, notify, password, salt
FROM users
;
DROP VIEW IF EXISTS user_profile_view;
CREATE VIEW user_profile_view AS
SELECT
user_id, name, mail, notify, ctime, atime, about
FROM users
NATURAL LEFT JOIN user_last_seen
;
-- Messages --
CREATE TABLE IF NOT EXISTS messages (
message_id INTEGER NOT NULL PRIMARY KEY,
deleted_from_inbox BOOLEAN NOT NULL DEFAULT 0,
deleted_from_outbox BOOLEAN NOT NULL DEFAULT 0,
from_id INTEGER NOT NULL DEFAULT 0 REFERENCES users(user_id) ON DELETE SET DEFAULT,
to_id INTEGER NOT NULL DEFAULT 0 REFERENCES users(user_id) ON DELETE SET DEFAULT,
time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
read BOOLEAN NOT NULL DEFAULT 0,
subject TEXT NOT NULL,
body TEXT NOT NULL
);
DROP VIEW IF EXISTS message_view;
CREATE VIEW message_view AS
SELECT *
, users_from.name AS from_name
, users_to.name AS to_name
FROM messages
JOIN users AS users_from ON messages.from_id = users_from.user_id
JOIN users AS users_to ON messages.to_id = users_to.user_id
;
DROP INDEX IF EXISTS messages_inbox_idx;
CREATE INDEX messages_inbox_idx ON messages(to_id) WHERE deleted_from_inbox=0;
DROP INDEX IF EXISTS messages_inbox_unread_idx;
CREATE INDEX messages_inbox_unread_idx ON messages(to_id) WHERE read=0 AND deleted_from_inbox=0;
-- Forum --
CREATE TABLE IF NOT EXISTS threads (
thread_id INTEGER NOT NULL PRIMARY KEY,
author_id INTEGER NOT NULL DEFAULT 0 REFERENCES users(user_id) ON DELETE SET DEFAULT,
subject TEXT NOT NULL,
locked BOOLEAN NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS posts (
post_id INTEGER NOT NULL PRIMARY KEY,
thread_id INTEGER NOT NULL REFERENCES threads(thread_id) ON DELETE CASCADE,
author_id INTEGER NOT NULL DEFAULT 0 REFERENCES users(user_id) ON DELETE SET DEFAULT,
ctime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
mtime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
body TEXT NOT NULL
);
DROP VIEW IF EXISTS thread_view;
CREATE VIEW thread_view AS
SELECT *
, author.name AS author_name
, ( SELECT COUNT(*) FROM posts WHERE posts.thread_id = threads.thread_id ) - 1 AS replies
, ( SELECT MAX(posts.mtime) FROM posts WHERE posts.thread_id = threads.thread_id ) AS mtime
FROM threads
JOIN users AS author ON threads.author_id = author.user_id
;
DROP VIEW IF EXISTS post_view;
CREATE VIEW post_view AS
SELECT *
, author.name AS author_name
FROM posts
JOIN users AS author ON posts.author_id = author.user_id
;
DROP INDEX IF EXISTS posts_thread_idx;
CREATE INDEX posts_thread_idx ON posts(thread_id);
-- Games --
CREATE TABLE IF NOT EXISTS games (
game_id INTEGER NOT NULL PRIMARY KEY,
title_id TEXT NOT NULL REFERENCES titles(title_id) ON DELETE CASCADE,
scenario TEXT,
options TEXT,
owner_id INTEGER DEFAULT 0 REFERENCES users(user_id) ON DELETE SET NULL,
ctime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
private BOOLEAN NOT NULL DEFAULT 0,
random BOOLEAN NOT NULL DEFAULT 0,
description TEXT NOT NULL DEFAULT '',
status INTEGER NOT NULL DEFAULT 0,
result TEXT
);
DROP INDEX IF EXISTS games_title_idx;
CREATE INDEX games_title_idx ON games(title_id);
DROP INDEX IF EXISTS games_status_idx;
CREATE INDEX games_status_idx ON games(status);
CREATE TABLE IF NOT EXISTS game_state (
game_id INTEGER NOT NULL PRIMARY KEY REFERENCES games(game_id) ON DELETE CASCADE,
mtime TIMESTAMP NOT NULL,
active TEXT,
state TEXT
);
CREATE TABLE IF NOT EXISTS game_chat (
chat_id INTEGER NOT NULL PRIMARY KEY,
game_id INTEGER NOT NULL REFERENCES games(game_id) ON DELETE CASCADE,
time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
user_id INTEGER NOT NULL DEFAULT 0 REFERENCES users(user_id) ON DELETE SET DEFAULT,
message TEXT
);
DROP VIEW IF EXISTS game_chat_view;
CREATE VIEW game_chat_view AS
SELECT chat_id, game_id, time, name, message
FROM game_chat
NATURAL JOIN users
;
DROP INDEX IF EXISTS game_chat_idx;
CREATE INDEX game_chat_idx ON game_chat(game_id);
CREATE TABLE IF NOT EXISTS game_replay (
game_id INTEGER NOT NULL REFERENCES games(game_id) ON DELETE CASCADE,
time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
role TEXT,
action TEXT,
arguments TEXT
);
CREATE TABLE IF NOT EXISTS players (
game_id INTEGER NOT NULL REFERENCES games(game_id) ON DELETE CASCADE,
role TEXT NOT NULL,
user_id INTEGER NOT NULL DEFAULT 0 REFERENCES users(user_id) ON DELETE SET DEFAULT,
PRIMARY KEY ( game_id, role )
) WITHOUT ROWID;
DROP INDEX IF EXISTS player_user_idx;
CREATE INDEX player_user_idx ON players(user_id);
DROP INDEX IF EXISTS player_game_user_idx;
CREATE INDEX player_game_user_idx ON players(game_id,user_id);
DROP VIEW IF EXISTS game_view;
CREATE VIEW game_view AS
SELECT games.*
, titles.title_name
, owner.name AS owner_name
, game_state.mtime
, game_state.active
FROM games
NATURAL LEFT JOIN game_state
NATURAL JOIN titles
JOIN users AS owner ON owner.user_id=games.owner_id
;
DROP VIEW IF EXISTS game_full_view;
CREATE VIEW game_full_view AS
SELECT *,
( SELECT group_concat(name, ', ')
FROM players
NATURAL JOIN users
WHERE players.game_id=game_view.game_id
) AS player_names,
( SELECT COUNT(DISTINCT user_id) = 1
FROM players
WHERE players.game_id=game_view.game_id
) AS is_solo
FROM game_view
;
DROP VIEW IF EXISTS your_turn_reminder;
CREATE VIEW your_turn_reminder AS
SELECT
game_id, role, user_id, name, mail, notify
FROM game_full_view
JOIN players USING(game_id)
JOIN users USING(user_id)
WHERE status = 1 AND is_solo = 0 AND notify = 1
AND active IN ( 'All', 'Both', role )
AND datetime('now') > datetime(mtime, '+1 hour')
;
DROP TRIGGER IF EXISTS no_part_on_active_game;
CREATE TRIGGER no_part_on_active_game BEFORE DELETE ON players
BEGIN
SELECT RAISE(ABORT, "Cannot remove players from started games.")
WHERE ( SELECT status FROM games WHERE games.game_id = old.game_id ) > 0 ;
END;
DROP TRIGGER IF EXISTS no_join_on_active_game;
CREATE TRIGGER no_join_on_active_game BEFORE INSERT ON players
BEGIN
SELECT RAISE(ABORT, "Cannot add players to started games.")
WHERE ( SELECT status FROM games WHERE games.game_id = new.game_id ) > 0 ;
END;
DROP TRIGGER IF EXISTS must_be_valid_role;
CREATE TRIGGER must_be_valid_role BEFORE INSERT ON players
BEGIN
SELECT
RAISE(ABORT, "Invalid role.")
WHERE
NOT EXISTS (
SELECT 1
FROM roles
WHERE roles.title_id = ( SELECT title_id FROM games WHERE games.game_id = new.game_id )
AND roles.role = new.role
)
AND new.role <> 'Random 1'
AND new.role <> 'Random 2'
AND new.role <> 'Random 3'
AND new.role <> 'Random 4'
AND new.role <> 'Random 5'
AND new.role <> 'Random 6'
;
END;
-- Manual key management if pragma foreign_keys = OFF
DROP TRIGGER IF EXISTS trigger_delete_on_games;
CREATE TRIGGER trigger_delete_on_games AFTER DELETE ON games
BEGIN
DELETE FROM game_state WHERE game_id = old.game_id;
DELETE FROM game_chat WHERE game_id = old.game_id;
DELETE FROM game_replay WHERE game_id = old.game_id;
DELETE FROM last_notified WHERE game_id = old.game_id;
DELETE FROM players WHERE game_id = old.game_id;
END;
DROP VIEW IF EXISTS game_stat_view;
CREATE VIEW game_stat_view AS
SELECT title_id, scenario, result, count(*) AS count
FROM game_full_view
NATURAL JOIN titles
WHERE hidden=0 AND is_solo=0 AND status=2
GROUP BY title_name, scenario, result
;
|