-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql-setup.sql
More file actions
292 lines (250 loc) · 10.5 KB
/
Copy pathsql-setup.sql
File metadata and controls
292 lines (250 loc) · 10.5 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
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
CREATE TABLE IF NOT EXISTS users (
id SERIAL,
username varchar(255) NOT NULL UNIQUE,
hash char(64) NOT NULL,
PRIMARY KEY (id));
CREATE TABLE IF NOT EXISTS games (
id SERIAL,
name varchar(255) NOT NULL,
text varchar(65535) NOT NULL,
start int,
public bool DEFAULT false,
PRIMARY KEY (id));
CREATE TABLE IF NOT EXISTS user_to_game (
user_ int NOT NULL,
game int NOT NULL,
permission int NOT NULL DEFAULT 0,
FOREIGN KEY (user_) REFERENCES users (id) ON DELETE CASCADE,
FOREIGN KEY (game) REFERENCES games (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS locations (
id SERIAL,
game int NOT NULL,
name varchar(255) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (game) REFERENCES games (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS paths (
id SERIAL,
start int NOT NULL,
end_ int,
text varchar(65535) NOT NULL DEFAULT '',
win bool,
PRIMARY KEY (id),
FOREIGN KEY (start) REFERENCES locations (id) ON DELETE CASCADE,
FOREIGN KEY (end_) REFERENCES locations (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS objects (
id SERIAL,
name varchar(255),
location int,
game int NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (game) REFERENCES games (id) ON DELETE CASCADE,
FOREIGN KEY (location) REFERENCES locations (id) ON DELETE SET NULL);
CREATE TABLE IF NOT EXISTS actions (
id SERIAL,
obj1 int NOT NULL,
obj2 int,
text varchar(65535) NOT NULL DEFAULT '',
win bool,
PRIMARY KEY (id),
FOREIGN KEY (obj1) REFERENCES objects (id) ON DELETE CASCADE,
FOREIGN KEY (obj2) REFERENCES objects (id) ON DELETE CASCADE);
CREATE OR REPLACE FUNCTION grab_default(obj int) RETURNS varchar(255) LANGUAGE plpgsql AS $$
BEGIN
RETURN (SELECT 'You have a' || CASE WHEN name SIMILAR TO '(a|e|i|o|u)%'
THEN 'n ' ELSE ' ' END || name || '.' FROM objects WHERE id = obj);
END;
$$;
CREATE TABLE IF NOT EXISTS grab (
id SERIAL,
obj int NOT NULL,
success boolean NOT NULL DEFAULT true,
text varchar(65535) NOT NULL,
win bool,
PRIMARY KEY (id),
FOREIGN KEY (obj) REFERENCES objects (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS dialogs (
id SERIAL,
obj int NOT NULL,
text varchar(65535) NOT NULL DEFAULT '',
win bool,
PRIMARY KEY (id),
FOREIGN KEY (obj) REFERENCES objects (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS constraint_and_effect (
id SERIAL,
game int NOT NULL,
obj int,
loc int,
name varchar(255), -- null means default
PRIMARY KEY (id),
FOREIGN KEY (game) REFERENCES games (id) ON DELETE CASCADE,
FOREIGN KEY (obj) REFERENCES objects (id) ON DELETE CASCADE,
FOREIGN KEY (loc) REFERENCES locations (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS grab_to_constraint (
grab int NOT NULL,
constraint_ int NOT NULL,
FOREIGN KEY (grab) REFERENCES grab (id) ON DELETE CASCADE,
FOREIGN KEY (constraint_) REFERENCES constraint_and_effect (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS grab_to_effect (
grab int NOT NULL,
effect int NOT NULL,
FOREIGN KEY (grab) REFERENCES grab (id) ON DELETE CASCADE,
FOREIGN KEY (effect) REFERENCES constraint_and_effect (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS path_to_constraint (
path int NOT NULL,
constraint_ int NOT NULL,
FOREIGN KEY (path) REFERENCES paths (id) ON DELETE CASCADE,
FOREIGN KEY (constraint_) REFERENCES constraint_and_effect (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS path_to_effect (
path int NOT NULL,
effect int NOT NULL,
FOREIGN KEY (path) REFERENCES paths (id) ON DELETE CASCADE,
FOREIGN KEY (effect) REFERENCES constraint_and_effect (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS action_to_constraint (
action int NOT NULL,
constraint_ int NOT NULL,
FOREIGN KEY (action) REFERENCES actions (id) ON DELETE CASCADE,
FOREIGN KEY (constraint_) REFERENCES constraint_and_effect (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS action_to_effect (
action int NOT NULL,
effect int NOT NULL,
FOREIGN KEY (action) REFERENCES actions (id) ON DELETE CASCADE,
FOREIGN KEY (effect) REFERENCES constraint_and_effect (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS dialog_to_constraint (
dialog int NOT NULL,
constraint_ int NOT NULL,
FOREIGN KEY (dialog) REFERENCES dialogs (id) ON DELETE CASCADE,
FOREIGN KEY (constraint_) REFERENCES constraint_and_effect (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS dialog_to_effect (
dialog int NOT NULL,
effect int NOT NULL,
FOREIGN KEY (dialog) REFERENCES dialogs (id) ON DELETE CASCADE,
FOREIGN KEY (effect) REFERENCES constraint_and_effect (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS location_constraint_and_effect (
id SERIAL,
obj int NOT NULL,
location int,
PRIMARY KEY (id),
FOREIGN KEY (obj) REFERENCES objects (id) ON DELETE CASCADE,
FOREIGN KEY (location) REFERENCES locations (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS grab_to_location_constraint (
grab int NOT NULL,
constraint_ int NOT NULL,
FOREIGN KEY (grab) REFERENCES grab (id) ON DELETE CASCADE,
FOREIGN KEY (constraint_) REFERENCES location_constraint_and_effect (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS grab_to_location_effect (
grab int NOT NULL,
effect int NOT NULL,
FOREIGN KEY (grab) REFERENCES grab (id) ON DELETE CASCADE,
FOREIGN KEY (effect) REFERENCES location_constraint_and_effect (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS path_to_location_constraint (
path int NOT NULL,
constraint_ int NOT NULL,
FOREIGN KEY (path) REFERENCES paths (id) ON DELETE CASCADE,
FOREIGN KEY (constraint_) REFERENCES location_constraint_and_effect (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS path_to_location_effect (
path int NOT NULL,
effect int NOT NULL,
FOREIGN KEY (path) REFERENCES paths (id) ON DELETE CASCADE,
FOREIGN KEY (effect) REFERENCES location_constraint_and_effect (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS action_to_location_constraint (
action int NOT NULL,
constraint_ int NOT NULL,
FOREIGN KEY (action) REFERENCES actions (id) ON DELETE CASCADE,
FOREIGN KEY (constraint_) REFERENCES location_constraint_and_effect (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS action_to_location_effect (
action int NOT NULL,
effect int NOT NULL,
FOREIGN KEY (action) REFERENCES actions (id) ON DELETE CASCADE,
FOREIGN KEY (effect) REFERENCES location_constraint_and_effect (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS dialog_to_location_constraint (
dialog int NOT NULL,
constraint_ int NOT NULL,
FOREIGN KEY (dialog) REFERENCES dialogs (id) ON DELETE CASCADE,
FOREIGN KEY (constraint_) REFERENCES location_constraint_and_effect (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS dialog_to_location_effect (
dialog int NOT NULL,
effect int NOT NULL,
FOREIGN KEY (dialog) REFERENCES dialogs (id) ON DELETE CASCADE,
FOREIGN KEY (effect) REFERENCES location_constraint_and_effect (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS grab_to_inventory_constraint (
grab int NOT NULL,
obj int NOT NULL,
have_it bool NOT NULL,
FOREIGN KEY (grab) REFERENCES grab (id) ON DELETE CASCADE,
FOREIGN KEY (obj) REFERENCES objects (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS grab_to_inventory_effect (
grab int NOT NULL,
obj int NOT NULL,
FOREIGN KEY (grab) REFERENCES grab (id) ON DELETE CASCADE,
FOREIGN KEY (obj) REFERENCES objects (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS path_to_inventory_constraint (
path int NOT NULL,
obj int NOT NULL,
have_it bool NOT NULL,
FOREIGN KEY (path) REFERENCES paths(id) ON DELETE CASCADE,
FOREIGN KEY (obj) REFERENCES objects (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS path_to_inventory_effect (
path int NOT NULL,
obj int NOT NULL,
FOREIGN KEY (path) REFERENCES paths(id) ON DELETE CASCADE,
FOREIGN KEY (obj) REFERENCES objects (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS action_to_inventory_constraint (
action int NOT NULL,
obj int NOT NULL,
have_it bool NOT NULL,
FOREIGN KEY (action) REFERENCES actions (id) ON DELETE CASCADE,
FOREIGN KEY (obj) REFERENCES objects (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS action_to_inventory_effect (
action int NOT NULL,
obj int NOT NULL,
FOREIGN KEY (action) REFERENCES actions (id) ON DELETE CASCADE,
FOREIGN KEY (obj) REFERENCES objects (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS dialog_to_inventory_constraint (
dialog int NOT NULL,
obj int NOT NULL,
have_it bool NOT NULL,
FOREIGN KEY (dialog) REFERENCES dialogs (id) ON DELETE CASCADE,
FOREIGN KEY (obj) REFERENCES objects (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS dialog_to_inventory_effect (
dialog int NOT NULL,
obj int NOT NULL,
FOREIGN KEY (dialog) REFERENCES dialogs (id) ON DELETE CASCADE,
FOREIGN KEY (obj) REFERENCES objects (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS descriptions (
id SERIAL,
location int NOT NULL,
num int NOT NULL,
text varchar(65535) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (location) REFERENCES locations (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS description_to_location_constraint (
description int NOT NULL,
constraint_ int NOT NULL,
FOREIGN KEY (description) REFERENCES descriptions (id) ON DELETE CASCADE,
FOREIGN KEY (constraint_) REFERENCES location_constraint_and_effect (id));
CREATE TABLE IF NOT EXISTS description_to_inventory_constraint (
description int NOT NULL,
obj int NOT NULL,
have_it bool NOT NULL,
FOREIGN KEY (description) REFERENCES descriptions (id) ON DELETE CASCADE,
FOREIGN KEY (obj) REFERENCES objects (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS description_to_constraint (
description int NOT NULL,
constraint_ int NOT NULL,
FOREIGN KEY (description) REFERENCES descriptions (id) ON DELETE CASCADE,
FOREIGN KEY (constraint_) REFERENCES constraint_and_effect (id) ON DELETE CASCADE);
CREATE TABLE IF NOT EXISTS names (
name varchar(255) NOT NULL,
obj int NOT NULL,
FOREIGN KEY (obj) REFERENCES objects (id) ON DELETE CASCADE,
UNIQUE (name, obj));
CREATE TABLE IF NOT EXISTS path_names (
name varchar(255) NOT NULL,
path int NOT NULL,
FOREIGN KEY (path) REFERENCES paths (id) ON DELETE CASCADE,
UNIQUE (name, path));
ALTER TABLE games
ADD CONSTRAINT constraint_fk
FOREIGN KEY (start)
REFERENCES locations(id)
ON DELETE SET NULL;