-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathschema.sql
More file actions
137 lines (124 loc) · 4.4 KB
/
Copy pathschema.sql
File metadata and controls
137 lines (124 loc) · 4.4 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
CREATE TABLE IF NOT EXISTS announce (
announce_id SERIAL PRIMARY KEY,
contents text NOT NULL,
create_date timestamp without time zone DEFAULT now()
);
CREATE TABLE IF NOT EXISTS channel_profile (
channel_id UUID PRIMARY KEY,
platform smallint NOT NULL,
given_id bigint NOT NULL,
name varchar NOT NULL,
command_revision int NOT NULL DEFAULT 0,
register_date timestamp without time zone DEFAULT now(),
UNIQUE (given_id, platform)
);
CREATE TABLE IF NOT EXISTS channel_config (
channel_id UUID PRIMARY KEY REFERENCES channel_profile (channel_id),
language smallint NOT NULL,
board_style smallint NOT NULL,
focus_type smallint NOT NULL,
swap_type smallint NOT NULL,
archive_policy smallint NOT NULL,
hint_type smallint NOT NULL,
mark_type smallint NOT NULL
);
CREATE TABLE IF NOT EXISTS user_profile (
user_id UUID PRIMARY KEY,
platform smallint NOT NULL,
given_id bigint NOT NULL,
name varchar NOT NULL,
unique_name varchar NOT NULL,
profile_url varchar,
announce_id int REFERENCES announce (announce_id),
register_date timestamp without time zone DEFAULT now(),
UNIQUE (given_id, platform)
);
CREATE TABLE IF NOT EXISTS user_rating (
user_id UUID PRIMARY KEY REFERENCES user_profile (user_id),
rating float NOT NULL
);
CREATE TABLE IF NOT EXISTS game_record (
record_id SERIAL PRIMARY KEY,
history smallint[] NOT NULL,
cause smallint NOT NULL,
win_color smallint,
channel_id UUID NOT NULL REFERENCES channel_profile (channel_id),
black_id UUID REFERENCES user_profile (user_id),
white_id UUID REFERENCES user_profile (user_id),
engine_level smallint,
rule smallint NOT NULL,
rating_delta float,
create_date timestamp without time zone DEFAULT now(),
CHECK (black_id IS NOT NULL OR white_id IS NOT NULL),
CHECK ((black_id IS NULL OR white_id IS NULL) = (engine_level IS NOT NULL))
);
CREATE INDEX recent_delta_black ON game_record (black_id, create_date DESC);
CREATE INDEX recent_delta_white ON game_record (white_id, create_date DESC);
CREATE INDEX game_record_channel ON game_record (channel_id) WHERE engine_level IS NOT NULL;
CREATE TABLE IF NOT EXISTS legacy_user_stats (
user_id UUID PRIMARY KEY REFERENCES user_profile (user_id),
black_wins int NOT NULL DEFAULT 0,
black_losses int NOT NULL DEFAULT 0,
black_draws int NOT NULL DEFAULT 0,
white_wins int NOT NULL DEFAULT 0,
white_losses int NOT NULL DEFAULT 0,
white_draws int NOT NULL DEFAULT 0,
last_update timestamp without time zone DEFAULT now()
);
CREATE OR REPLACE VIEW user_stats AS
WITH game_user_stats AS (
SELECT
user_id,
(COUNT(*) FILTER (WHERE color = 0 AND win_color = 0))::int AS black_wins,
(COUNT(*) FILTER (WHERE color = 0 AND win_color = 1))::int AS black_losses,
(COUNT(*) FILTER (WHERE color = 0 AND win_color IS DISTINCT FROM 0 AND win_color IS DISTINCT FROM 1))::int AS black_draws,
(COUNT(*) FILTER (WHERE color = 1 AND win_color = 1))::int AS white_wins,
(COUNT(*) FILTER (WHERE color = 1 AND win_color = 0))::int AS white_losses,
(COUNT(*) FILTER (WHERE color = 1 AND win_color IS DISTINCT FROM 0 AND win_color IS DISTINCT FROM 1))::int AS white_draws,
MAX(create_date) AS last_update
FROM (
SELECT
black_id AS user_id,
0::smallint AS color,
win_color,
create_date
FROM game_record
WHERE engine_level IS NOT NULL
AND black_id IS NOT NULL
UNION ALL
SELECT
white_id AS user_id,
1::smallint AS color,
win_color,
create_date
FROM game_record
WHERE engine_level IS NOT NULL
AND white_id IS NOT NULL
) AS ai_game_record
GROUP BY user_id
),
combined_user_stats AS (
SELECT * FROM legacy_user_stats
UNION ALL
SELECT
user_id,
black_wins,
black_losses,
black_draws,
white_wins,
white_losses,
white_draws,
last_update
FROM game_user_stats
)
SELECT
user_id,
SUM(black_wins)::int AS black_wins,
SUM(black_losses)::int AS black_losses,
SUM(black_draws)::int AS black_draws,
SUM(white_wins)::int AS white_wins,
SUM(white_losses)::int AS white_losses,
SUM(white_draws)::int AS white_draws,
MAX(last_update) AS last_update
FROM combined_user_stats
GROUP BY user_id;