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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
|
-- Blacklists --
create table if not exists blacklist_mail ( mail text primary key collate nocase ) without rowid;
create table if not exists blacklist_name ( name text primary key collate nocase ) without rowid;
insert or ignore into blacklist_mail (mail) values
('%@example.com')
;
insert or ignore into blacklist_name (name) values
('None'),
('System'),
('Deleted'),
('null')
;
-- Titles --
create table if not exists titles (
title_id text primary key,
title_name text,
bgg integer,
is_symmetric boolean default 0,
is_hidden boolean default 0
) without rowid;
-- Users --
create table if not exists logins (
sid integer primary key,
user_id integer,
expires real -- julianday
);
create table if not exists users (
user_id integer primary key,
name text unique collate nocase,
mail text unique collate nocase,
notify integer default 0,
is_verified boolean default 0,
is_banned boolean default 0,
ctime datetime default current_timestamp,
password text,
salt text,
about text
);
insert or ignore into
users (user_id, name, mail, ctime)
values (0, 'Deleted', 'deleted@nowhere', null)
;
create table if not exists user_last_seen (
user_id integer primary key,
atime datetime,
ip text
);
create table if not exists tokens (
user_id integer primary key,
token text,
time datetime
);
create table if not exists webhooks (
user_id integer primary key,
url text,
format text,
prefix text,
error text
);
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, is_banned
from
users
left join user_last_seen using(user_id)
;
drop view if exists user_dynamic_view;
create view user_dynamic_view as
select
user_id,
name,
mail,
(
select
count(*)
from
messages
where
to_id = user_id
and is_read = 0
and is_deleted_from_inbox = 0
) as unread,
(
select
count(*)
from
players
join games using(game_id)
where
status = 1
and user_count = player_count
and players.user_id = users.user_id
and active in ( players.role, 'Both' )
) + (
select
count(*)
from
players
where
players.user_id = users.user_id and players.is_invite
) + (
select
count(*)
from
games
where
owner_id = users.user_id
and status = 0
and join_count = 0
) as waiting,
is_banned
from
users
;
-- Elo ratings & match making --
drop view if exists rated_games_view;
create view rated_games_view as
select
game_id, title_id, player_count, scenario, result, mtime
from
games
where
status > 1
and moves >= player_count * 3
and user_count = player_count
and player_count > 1
and not exists (
select 1 from players where players.game_id = games.game_id and user_id = 0
)
;
create table if not exists ratings (
title_id integer,
user_id integer,
rating integer,
count integer,
last datetime,
primary key (title_id, user_id)
) without rowid;
drop view if exists rating_view;
create view rating_view as
select
title_id, name, rating, count, last
from
ratings
left join users using(user_id)
order by
title_id,
rating desc
;
drop view if exists player_rating_view;
create view player_rating_view as
select
games.game_id,
players.user_id,
players.role,
coalesce(rating, 1500) as rating,
coalesce(count, 0) as count
from players
join games using(game_id)
left join ratings using(title_id, user_id)
;
-- Friend and Block Lists --
create table if not exists contacts (
me integer,
you integer,
relation integer,
primary key (me, you)
) without rowid;
drop view if exists contact_view;
create view contact_view as
select
contacts.me,
users.user_id,
users.name,
user_last_seen.atime,
contacts.relation
from
contacts
left join users on contacts.you = users.user_id
left join user_last_seen on contacts.you = user_last_seen.user_id
order by
users.name
;
-- Messages --
create table if not exists messages (
message_id integer primary key,
is_deleted_from_inbox boolean default 0,
is_deleted_from_outbox boolean default 0,
from_id integer,
to_id integer,
time datetime default current_timestamp,
is_read boolean default 0,
subject text,
body text
);
drop view if exists message_view;
create view message_view as
select
messages.*,
users_from.name as from_name,
users_to.name as to_name
from
messages
left join users as users_from
on messages.from_id = users_from.user_id
left join users as users_to
on messages.to_id = users_to.user_id
;
create index if not exists messages_inbox_idx
on
messages(to_id)
where
is_deleted_from_inbox = 0
;
create index if not exists messages_inbox_unread_idx
on
messages(to_id)
where
is_read = 0 and is_deleted_from_inbox = 0
;
-- Forum --
create table if not exists threads (
thread_id integer primary key,
author_id integer,
subject text,
is_locked boolean default 0
);
create table if not exists posts (
post_id integer primary key,
thread_id integer,
author_id integer,
ctime datetime default current_timestamp,
mtime datetime default current_timestamp,
body text
);
create table if not exists read_threads (
user_id integer,
thread_id integer,
primary key (user_id, thread_id)
) without rowid;
drop view if exists thread_view;
create view thread_view as
select
threads.*,
author.name as author_name,
(
select
count(*)
from
posts
where
posts.thread_id = threads.thread_id
) as count,
(
select
max(posts.mtime)
from
posts
where
posts.thread_id = threads.thread_id
) as mtime
from
threads
left join users as author
on threads.author_id = author.user_id
;
drop view if exists post_view;
create view post_view as
select
posts.*,
author.name as author_name
from
posts
left join users as author
on posts.author_id = author.user_id
;
create index if not exists posts_thread_idx on posts(thread_id);
-- Forum Search (FTS5) --
-- drop table if exists forum_search;
create virtual table if not exists forum_search using fts5(thread_id, post_id, text, tokenize='porter unicode61');
-- insert into forum_search(thread_id,post_id,text) select thread_id, 0, subject from threads;
-- insert into forum_search(thread_id,post_id,text) select thread_id, post_id, body from posts;
-- Games --
create table if not exists games (
game_id integer primary key,
status integer default 0,
title_id text,
scenario text,
options text,
player_count integer default 2,
join_count integer default 0,
invite_count integer default 0,
user_count integer default 0,
owner_id integer default 0,
notice text,
pace integer default 0,
is_private boolean default 0,
is_random boolean default 0,
is_match boolean default 0,
ctime datetime default current_timestamp,
mtime datetime default current_timestamp,
moves integer default 0,
active text,
result text,
is_opposed boolean generated as ( user_count = join_count and join_count > 1 ),
is_ready boolean generated as ( player_count = join_count and invite_count = 0 )
);
create index if not exists games_title_idx on games(title_id);
create index if not exists games_status_idx on games(status);
create table if not exists game_state (
game_id integer primary key,
state text
);
create table if not exists game_chat (
game_id integer,
chat_id integer,
user_id integer,
time datetime default current_timestamp,
message text,
primary key (game_id, chat_id)
) without rowid;
create table if not exists unread_chats (
user_id integer,
game_id integer,
primary key (user_id, game_id)
) without rowid;
drop view if exists game_chat_view;
create view game_chat_view as
select
game_id, chat_id, time, name, message
from
game_chat
left join users using(user_id)
;
create table if not exists game_replay (
game_id integer,
replay_id integer,
role text,
action text,
arguments json,
primary key (game_id, replay_id)
) without rowid;
create table if not exists game_snap (
game_id integer,
snap_id integer,
replay_id integer,
state text,
primary key (game_id, snap_id)
);
create table if not exists game_notes (
game_id integer,
role text,
note text,
primary key (game_id, role)
) without rowid;
create table if not exists players (
game_id integer,
role text,
user_id integer,
is_invite integer,
clock real,
score integer,
primary key (game_id, role)
) without rowid;
create index if not exists player_user_idx on players(user_id);
create index if not exists 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,
tm_pools.pool_name
from
games
join titles using(title_id)
left join users as owner
on owner.user_id = games.owner_id
left join tm_rounds using(game_id)
left join tm_pools using(pool_id)
;
drop view if exists game_view_public;
create view game_view_public as
select
*
from
game_view
where
not is_private
and join_count > 0
and join_count = user_count
;
drop view if exists player_view;
create view player_view as
select
game_id,
user_id,
name,
role,
is_invite,
(
case status
when 0 then
owner_id = user_id
when 1 then
active in ( 'Both', role )
else
0
end
) as is_active,
(
case when active in ( 'Both', role ) then
clock - (julianday() - julianday(mtime))
else
clock
end
) as time_left,
atime
from
games
join players using(game_id)
left join users using(user_id)
left join user_last_seen using(user_id)
;
drop view if exists time_control_view;
create view time_control_view as
select
game_id,
user_id,
role,
is_opposed,
is_match
from
games
join players using(game_id)
where
status = 1
and active in ( 'Both', role )
and clock - (julianday() - julianday(mtime)) < 0
;
-- Export game state as JSON
drop view if exists game_export_view;
create view game_export_view as
select
game_id,
json_object(
'setup', json_object(
'game_id', game_id,
'title_id', title_id,
'scenario', scenario,
'options', json(options),
'player_count', player_count,
'notice', notice
),
'players',
(select json_group_array(
json_object('role', role, 'name', name)
)
from players
left join users using(user_id)
where game_id = outer.game_id
),
'state',
(select json(state)
from game_state
where game_id = outer.game_id
),
'replay',
(select json_group_array(
case when arguments is null then
json_array(role, action)
else
json_array(role, action, json(arguments))
end
)
from game_replay
where game_id = outer.game_id
),
'snaps',
(select json_group_array(
json_array(replay_id, json(state))
)
from game_snap
where game_id = outer.game_id
)
) as export
from games as outer
;
-- Tournaments --
create table if not exists tm_seeds (
seed_id integer primary key,
seed_name text unique,
title_id text,
scenario text,
options text,
player_count integer,
pace integer default 2,
pool_size integer default 3,
round_count integer default 4,
is_concurrent boolean default 1,
level_count integer default 1,
is_open boolean default 1
);
create table if not exists tm_banned (
user_id integer primary key,
time datetime default current_timestamp
);
create table if not exists tm_queue (
user_id integer,
seed_id integer,
level integer,
is_paused boolean default 0,
time datetime default current_timestamp,
primary key (user_id, seed_id, level)
);
create table if not exists tm_pools (
pool_id integer primary key,
seed_id integer,
level integer,
is_finished boolean,
start_date datetime,
finish_date datetime,
pool_name text unique
);
create table if not exists tm_rounds (
game_id integer primary key,
pool_id integer,
round integer
);
create index if not exists tm_rounds_pool_idx on tm_rounds(pool_id);
create table if not exists tm_results (
pool_id integer,
user_id integer,
points integer,
son integer,
primary key (pool_id, user_id)
);
create table if not exists tm_winners (
pool_id integer,
user_id integer
);
create index if not exists tm_winners_pool_idx on tm_winners(pool_id);
drop view if exists tm_pool_active_view;
create view tm_pool_active_view as
select
tm_pools.*,
sum(status > 1) || ' / ' || count(1) as status
from
tm_pools
left join tm_rounds using(pool_id)
left join games using(game_id)
where
not is_finished
group by
pool_id
order by
pool_name
;
drop view if exists tm_pool_finished_view;
create view tm_pool_finished_view as
select
tm_pools.*,
group_concat(name) as status
from
tm_pools
left join tm_winners using(pool_id)
left join users using(user_id)
where
is_finished
group by
pool_id
order by
pool_name
;
drop view if exists tm_pool_view;
create view tm_pool_view as
select * from tm_pool_active_view
union all
select * from tm_pool_finished_view
;
drop trigger if exists tm_trigger_update_results;
create trigger tm_trigger_update_results after update of result on games when new.is_match
begin
-- each player scores
update players
set score = (
case
when new.result is null then null
when new.result = role then 2
when new.result = 'Draw' then 1
when instr(new.result, role) then 1
else 0
end
)
where
players.game_id = new.game_id
;
-- Neustadtl Sonneborn–Berger tie-breaker
insert or replace into
tm_results (pool_id, user_id, points, son)
with
pts_cte as (
select
pool_id,
user_id,
sum(coalesce(score, 0)) as points
from
tm_rounds
join games using(game_id)
join players using(game_id)
where
pool_id = ( select pool_id from tm_rounds where game_id = new.game_id )
group by
user_id
),
son_cte as (
select
rr.pool_id,
p1.user_id,
sum(
case
when p1.score > p2.score then
pp.points * 2
when p1.score = p2.score then
pp.points
else
0
end
) as son
from
tm_rounds as rr
join games using(game_id)
join players as p1 using(game_id)
join players as p2 using(game_id)
join pts_cte pp on rr.pool_id = pp.pool_id and p2.user_id = pp.user_id
where
rr.pool_id = ( select pool_id from tm_rounds where game_id = new.game_id )
and p1.user_id != p2.user_id
group by
p1.user_id
)
select
pool_id, user_id, points, son
from
pts_cte
join son_cte using(pool_id, user_id)
;
end;
drop trigger if exists tm_trigger_update_winners;
create trigger tm_trigger_update_winners after update of is_finished on tm_pools when new.is_finished
begin
delete from tm_winners where pool_id = new.pool_id;
insert into
tm_winners ( pool_id, user_id )
with
tt as (
select
round_count as threshold
from
tm_seeds
where
seed_id = ( select seed_id from tm_pools where pool_id = new.pool_id )
),
aa as (
select
max(points) as max_points
from
tm_results
where
pool_id = new.pool_id
),
bb as (
select
max_points,
max(son) as max_son
from
tm_results, aa
where
pool_id = new.pool_id and points = max_points
)
select
pool_id, user_id
from
tm_results, bb, tt
where
pool_id = new.pool_id and points > threshold and points = max_points and son = max_son
;
end;
-- Trigger to update player counts when players join and part games
drop trigger if exists trigger_join_game;
create trigger trigger_join_game after insert on players
begin
update
games
set
join_count = ( select count(1) from players where players.game_id = new.game_id ),
user_count = ( select count(distinct user_id) from players where players.game_id = new.game_id ),
invite_count = ( select count(1) from players where players.game_id = new.game_id and players.is_invite ),
mtime = datetime()
where
games.game_id = new.game_id;
end;
drop trigger if exists trigger_part_game;
create trigger trigger_part_game after delete on players
begin
update
games
set
join_count = ( select count(1) from players where players.game_id = old.game_id ),
user_count = ( select count(distinct user_id) from players where players.game_id = old.game_id ),
invite_count = ( select count(1) from players where players.game_id = old.game_id and players.is_invite ),
mtime = datetime()
where
games.game_id = old.game_id;
end;
drop trigger if exists trigger_accept_invite;
create trigger trigger_accept_invite after update of is_invite on players
when old.is_invite and not new.is_invite
begin
update
games
set
invite_count = ( select count(1) from players where players.game_id = new.game_id and players.is_invite ),
mtime = datetime()
where
games.game_id = old.game_id;
end;
-- Trigger to track time spent!
drop trigger if exists trigger_time_used_update;
create trigger trigger_time_used_update before update of active on games
begin
update players
set clock = clock - (julianday() - julianday(old.mtime))
where
players.game_id = old.game_id and players.role in ( 'Both', old.active );
end;
-- Trigger to remove game data when filing a game as archived
drop trigger if exists trigger_archive_game;
create trigger trigger_archive_game after update of status on games when new.status = 3
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 game_snap where game_id = old.game_id;
delete from game_notes where game_id = old.game_id;
delete from unread_chats where game_id = old.game_id;
end;
-- Triggers to clean up without relying on foreign key cascades
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 game_snap where game_id = old.game_id;
delete from game_notes where game_id = old.game_id;
delete from unread_chats where game_id = old.game_id;
delete from players where game_id = old.game_id;
end;
drop trigger if exists trigger_delete_on_users;
create trigger trigger_delete_on_users after delete on users
begin
delete from logins where user_id = old.user_id;
delete from tokens where user_id = old.user_id;
delete from webhooks where user_id = old.user_id;
delete from user_last_seen where user_id = old.user_id;
delete from read_threads where user_id = old.user_id;
delete from unread_chats where user_id = old.user_id;
delete from contacts where me = old.user_id or you = old.user_id;
delete from messages where from_id = old.user_id or to_id = old.user_id;
delete from posts where author_id = old.user_id;
delete from threads where author_id = old.user_id;
delete from game_chat where user_id = old.user_id;
delete from tm_queue where user_id = old.user_id;
delete from players where user_id = old.user_id and game_id in (select game_id from games where status = 0);
update games set owner_id = 0 where owner_id = old.user_id;
end;
drop trigger if exists trigger_delete_on_threads;
create trigger trigger_delete_on_threads after delete on threads
begin
delete from posts where thread_id = old.thread_id;
delete from read_threads where thread_id = old.thread_id;
end;
drop trigger if exists trigger_mark_threads_as_unread1;
create trigger trigger_mark_threads_as_unread1 after insert on posts
begin
delete from read_threads where user_id != new.author_id and thread_id = new.thread_id;
end;
drop trigger if exists trigger_mark_threads_as_unread2;
create trigger trigger_mark_threads_as_unread2 after update on posts
when new.body != old.body
begin
delete from read_threads where user_id != new.author_id and thread_id = new.thread_id;
end;
create table if not exists deleted_users (
user_id integer,
name text collate nocase,
mail text collate nocase,
time datetime default current_timestamp
);
drop trigger if exists trigger_log_deleted_users;
create trigger trigger_log_deleted_users before delete on users begin
insert into deleted_users (user_id, name, mail) values (old.user_id, old.name, old.mail);
end;
-- Triggers to keep FTS search index up to date
drop trigger if exists trigger_search_insert_thread;
create trigger trigger_search_insert_thread after insert on threads
begin
insert into forum_search(thread_id, post_id, text) values(new.thread_id, 0, new.subject);
end;
drop trigger if exists trigger_search_update_thread;
create trigger trigger_search_update_thread after update on threads
begin
delete from forum_search where thread_id=old.thread_id and post_id=0;
insert into forum_search(thread_id, post_id, text) values(new.thread_id, 0, new.subject);
end;
drop trigger if exists trigger_search_delete_thread;
create trigger trigger_search_delete_thread after delete on threads
begin
delete from forum_search where thread_id=old.thread_id and post_id=0;
end;
drop trigger if exists trigger_search_insert_post;
create trigger trigger_search_insert_post after insert on posts
begin
insert into forum_search(thread_id, post_id, text) values(new.thread_id, new.post_id, new.body);
end;
drop trigger if exists trigger_search_update_post;
create trigger trigger_search_update_post after update on posts
begin
delete from forum_search where post_id=old.post_id;
insert into forum_search(thread_id, post_id, text) values(new.thread_id, new.post_id, new.body);
end;
drop trigger if exists trigger_search_delete_post;
create trigger trigger_search_delete_post after delete on posts
begin
delete from forum_search where post_id=old.post_id;
end;
|