-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmonopoly.sql
More file actions
110 lines (97 loc) · 2.86 KB
/
monopoly.sql
File metadata and controls
110 lines (97 loc) · 2.86 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
SET GLOBAL event_scheduler = ON;
USE mysql;
CREATE USER 'root'@'%' IDENTIFIED BY 'root123';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
DROP DATABASE IF EXISTS monopoly;
CREATE DATABASE monopoly;
USE monopoly;
CREATE TABLE IF NOT EXISTS user (
id int NOT NULL AUTO_INCREMENT,
email varchar(255),
sessionId varchar(32) DEFAULT NULL,
lastActivity int DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY email (email),
UNIQUE KEY sessionId (sessionId)
);
CREATE EVENT remove_old_sessions
ON SCHEDULE EVERY 3 MINUTE
DO UPDATE user SET sessionId=NULL, lastActivity=NULL WHERE UNIX_TIMESTAMP(CURRENT_TIMESTAMP) - lastActivity > 1800;
/* status: 0=not started, 1=started, 2=finished */
CREATE TABLE IF NOT EXISTS tournament (
id int NOT NULL AUTO_INCREMENT,
tourUID varchar(32) NOT NULL,
tourType tinyint DEFAULT 0,
noPlayers smallint DEFAULT 2,
noGames smallint DEFAULT 100,
noPpg tinyint DEFAULT NULL,
createUserId int NOT NULL,
createTime int NOT NULL,
timePerMove smallint DEFAULT 5,
allowHumans tinyint DEFAULT 0,
status tinyint DEFAULT 0,
finishedGames smallint DEFAULT 0,
winUserId int DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY tourUID (tourUID),
FOREIGN KEY (createUserId)
REFERENCES user(id),
FOREIGN KEY (winUserId)
REFERENCES user(id)
);
/* when tourType=0, its a sologame and hence we map it to this table */
CREATE TABLE IF NOT EXISTS user_sologame (
id int NOT NULL AUTO_INCREMENT,
userId int NOT NULL,
tourId int NOT NULL,
winCount smallint DEFAULT 0,
PRIMARY KEY (id),
UNIQUE KEY unique_user (userId,tourId),
FOREIGN KEY (tourId)
REFERENCES tournament(id)
ON DELETE CASCADE,
FOREIGN KEY (userId)
REFERENCES user(id)
ON DELETE CASCADE
);
/* when tourType!=0, its a tournament and hence we map it to this table */
CREATE TABLE IF NOT EXISTS user_tour (
id int NOT NULL AUTO_INCREMENT,
userId int NOT NULL,
tourId int NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY unique_user (userId,tourId),
FOREIGN KEY (tourId)
REFERENCES tournament(id)
ON DELETE CASCADE,
FOREIGN KEY (userId)
REFERENCES user(id)
ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS tourGame (
id int NOT NULL AUTO_INCREMENT,
tourId int NOT NULL,
noPlayers smallint,
createTime int DEFAULT NULL,
winUserId int DEFAULT NULL,
status tinyint DEFAULT 0,
finishedGames smallint DEFAULT 0,
PRIMARY KEY (id),
FOREIGN KEY (tourId)
REFERENCES tournament(id)
ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS user_tourGame (
id int NOT NULL AUTO_INCREMENT,
userId int NOT NULL,
gameId int NOT NULL,
winCount smallint DEFAULT 0,
PRIMARY KEY (id),
UNIQUE KEY unique_user (userId,gameId),
FOREIGN KEY (gameId)
REFERENCES tourGame(id)
ON DELETE CASCADE,
FOREIGN KEY (userId)
REFERENCES user(id)
ON DELETE CASCADE
);