-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreateDatabase.sql
165 lines (132 loc) · 4.7 KB
/
createDatabase.sql
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
-- MySQL Script generated by MySQL Workbench
-- Sat Jun 6 16:47:51 2015
-- Model: New Model Version: 1.0
-- MySQL Workbench Forward Engineering
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema PongHacks
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `PongHacks` ;
-- -----------------------------------------------------
-- Schema PongHacks
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `PongHacks` ;
USE `PongHacks` ;
-- -----------------------------------------------------
-- Table `PongHacks`.`User`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PongHacks`.`User` ;
CREATE TABLE IF NOT EXISTS `PongHacks`.`User` (
`userId` INT(11) NOT NULL,
`email` VARCHAR(100) NOT NULL,
`name` VARCHAR(150) NOT NULL,
`mentionName` VARCHAR(45) NOT NULL,
`avatarUrl` VARCHAR(150) NULL DEFAULT NULL,
`eloRanking` INT NOT NULL DEFAULT 2000,
PRIMARY KEY (`userId`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `PongHacks`.`Game`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `PongHacks`.`Game` ;
CREATE TABLE IF NOT EXISTS `PongHacks`.`Game` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`winnerUserId` INT(11) NOT NULL,
`winnerScore` INT(11) NOT NULL,
`loserUserId` INT(11) NOT NULL,
`loserScore` INT(11) NOT NULL,
`date` DATETIME NOT NULL,
PRIMARY KEY (`id`),
INDEX `winnerUserId` (`winnerUserId` ASC),
INDEX `loserUserId` (`loserUserId` ASC),
CONSTRAINT `loserUserId`
FOREIGN KEY (`loserUserId`)
REFERENCES `PongHacks`.`User` (`userId`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `winnerUserId`
FOREIGN KEY (`winnerUserId`)
REFERENCES `PongHacks`.`User` (`userId`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB
AUTO_INCREMENT = 51
DEFAULT CHARACTER SET = utf8;
-- CREATE PROCEDURE FOR PLAYER's LONGEST WINNING STREAK
DROP PROCEDURE IF EXISTS userLongestWinningStreak;
DROP PROCEDURE IF EXISTS userLongestLosingStreak;
DELIMITER //
CREATE PROCEDURE `userLongestWinningStreak` (IN userId INT, OUT streak INT)
BEGIN
DECLARE maxStreak INT DEFAULT 0;
DECLARE b, streakCounter, winnerId INT;
DECLARE curWinnerIds CURSOR FOR SELECT winnerUserID FROM Game WHERE winnerUserId = userId or loserUserId = userId ORDER BY date ASC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET b = 1;
OPEN curWinnerIds;
SET b = 0;
SET streakCounter = 0;
WHILE b = 0 DO
FETCH curWinnerIds INTO winnerId;
IF b = 0 THEN
IF winnerId = userId THEN
SET streakCounter = streakCounter + 1;
ELSE
IF streakCounter > maxStreak THEN
SET maxStreak = streakCounter;
END IF;
SET streakCounter = 0;
END IF;
END IF;
END WHILE;
CLOSE curWinnerIds;
IF streakCounter > maxStreak THEN
SET maxStreak = streakCounter;
SET streakCounter = 0;
END IF;
SET streak = maxStreak;
END //
-- CREATE PROCEDURE FOR PLAYER's LONGEST WINNING STREAK
CREATE PROCEDURE `userLongestLosingStreak` (IN userId INT, OUT streak INT)
BEGIN
DECLARE maxStreak INT DEFAULT 0;
DECLARE b, streakCounter, loserId INT;
DECLARE curLoserIds CURSOR FOR SELECT loserUserId FROM Game WHERE winnerUserId = userId or loserUserId = userId ORDER BY date ASC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET b = 1;
OPEN curLoserIds;
SET b = 0;
SET streakCounter = 0;
WHILE b = 0 DO
FETCH curLoserIds INTO loserId;
IF b = 0 THEN
IF loserId = userId THEN
SET streakCounter = streakCounter + 1;
ELSE
IF streakCounter > maxStreak THEN
SET maxStreak = streakCounter;
END IF;
SET streakCounter = 0;
END IF;
END IF;
END WHILE;
CLOSE curLoserIds;
IF streakCounter > maxStreak THEN
SET maxStreak = streakCounter;
SET streakCounter = 0;
END IF;
SET streak = maxStreak;
END //
DELIMITER ;
-- CREATE TABLE VIEW FOR USERS THAT HAVE PLAYED AT LEAST ONE GAME
CREATE OR REPLACE VIEW activeUsers AS
SELECT * FROM User
WHERE userId IN (
SELECT DISTINCT winnerUserID FROM Game
UNION
SELECT DISTINCT loserUserId FROM Game
);
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;