-
Notifications
You must be signed in to change notification settings - Fork 0
/
MS2_Database.sql
1770 lines (1515 loc) · 59.7 KB
/
MS2_Database.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
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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Seven Dwarfs Gem Hunt Project Physical Design Milestone One [refer to Logical Diagram v2.2]
-- UPDATES RELATED TO WORK COMPLETED FOR MILESTONE 2
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- Database Setup
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
DROP DATABASE IF EXISTS sdghGameDatabase;
BEGIN;
CREATE DATABASE sdghGameDatabase;
BEGIN;
USE sdghGameDatabase;
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- DDL | Making tables, indexes and checks
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
DROP PROCEDURE IF EXISTS CreateTables;
DELIMITER //
CREATE PROCEDURE CreateTables()
BEGIN
DROP TABLE IF EXISTS tblItemGame;
DROP TABLE IF EXISTS tblItem;
DROP TABLE IF EXISTS tblPlay;
DROP TABLE IF EXISTS tblGame;
DROP TABLE IF EXISTS tblBoardTile;
DROP TABLE IF EXISTS tblBoard;
DROP TABLE IF EXISTS tblTile;
DROP TABLE IF EXISTS tblGem;
DROP TABLE IF EXISTS tblCharacter;
DROP TABLE IF EXISTS tblPlayer;
CREATE TABLE tblPlayer (
PlayerID int AUTO_INCREMENT,
Email varchar(50) NOT NULL,
Username varchar(10) NOT NULL,
`Password` BLOB NOT NULL,
AccountAdmin bit DEFAULT FALSE NOT NULL,
AccountLocked bit DEFAULT FALSE NOT NULL,
ActiveStatus bit DEFAULT FALSE NOT NULL,
FailedLogins tinyint DEFAULT 0 NOT NULL,
HighScore int DEFAULT 0 NOT NULL,
PRIMARY KEY (PlayerID),
CONSTRAINT UC_Email UNIQUE (Email),
CONSTRAINT UC_Username UNIQUE (Username),
CONSTRAINT CHK_Email CHECK (Email Like '_%@_%._%')
);
ALTER TABLE tblPlayer AUTO_INCREMENT=000001;
ALTER TABLE tblPlayer ADD COLUMN Salt varchar(36);
ALTER TABLE tblPlayer ENCRYPTION='Y'; -- Encrypt Player table
CREATE TABLE tblCharacter (
CharacterName varchar(10) NOT NULL,
TileColour varchar(10) NOT NULL,
PRIMARY KEY (CharacterName)
);
CREATE TABLE tblGem (
GemType varchar(10) NOT NULL,
Points tinyint NOT NULL,
PRIMARY KEY (GemType)
);
CREATE TABLE tblTile (
TileID int AUTO_INCREMENT,
TileRow tinyint NOT NULL,
TileColumn tinyint NOT NULL,
HomeTile bit,
PRIMARY KEY (TileID)
);
ALTER TABLE tblTile AUTO_INCREMENT=001;
CREATE TABLE tblBoard (
BoardType varchar(20) NOT NULL,
XAxis tinyint NOT NULL,
YAxis tinyint NOT NULL,
PRIMARY KEY (BoardType)
);
CREATE TABLE tblBoardTile (
BoardType varchar(20) NOT NULL,
TileID int NOT NULL,
CONSTRAINT PK_BoardTile PRIMARY KEY (BoardType, TileID),
CONSTRAINT FK_BoardType_BT FOREIGN KEY (BoardType) REFERENCES tblBoard(BoardType),
CONSTRAINT FK_TileID_BT FOREIGN KEY (TileID) REFERENCES tblTile(TileID)
);
CREATE TABLE tblGame (
GameID int AUTO_INCREMENT,
BoardType varchar(20) NOT NULL,
CharacterTurn varchar(10),
PRIMARY KEY (GameID),
CONSTRAINT FK_BoardType_Game FOREIGN KEY (BoardType) REFERENCES tblBoard(BoardType)
);
ALTER TABLE tblGame AUTO_INCREMENT=100001;
CREATE TABLE tblPlay (
PlayID int AUTO_INCREMENT,
PlayerID int NOT NULL,
CharacterName varchar(10) NOT NULL,
GameID int NOT NULL,
TileID int DEFAULT 001 NOT NULL,
PlayScore int DEFAULT 0 NOT NULL,
PRIMARY KEY (PlayID),
CONSTRAINT FK_PlayerID_Play FOREIGN KEY (PlayerID) REFERENCES tblPlayer(PlayerID) ON DELETE CASCADE,
CONSTRAINT FK_CharacterName_Play FOREIGN KEY (CharacterName) REFERENCES tblCharacter(CharacterName),
CONSTRAINT FK_GameID_Play FOREIGN KEY (GameID) REFERENCES tblGame(GameID),
CONSTRAINT FK_TileID_Play FOREIGN KEY (TileID) REFERENCES tblTile(TileID)
);
ALTER TABLE tblPlay AUTO_INCREMENT=500001;
CREATE TABLE tblItem (
ItemID int AUTO_INCREMENT,
GemType varchar(10) NOT NULL,
PRIMARY KEY (ItemID),
CONSTRAINT FK_GemType_Item FOREIGN KEY (GemType) REFERENCES tblGem(GemType)
);
ALTER TABLE tblItem AUTO_INCREMENT=101;
CREATE TABLE tblItemGame (
ItemID int NOT NULL,
GameID int NOT NULL,
TileID int,
PlayID int,
CONSTRAINT PK_ItemGame PRIMARY KEY (ItemID, GameID),
CONSTRAINT FK_ItemID_IG FOREIGN KEY (ItemID) REFERENCES tblItem(ItemID),
CONSTRAINT FK_GameID_IG FOREIGN KEY (GameID) REFERENCES tblGame(GameID),
CONSTRAINT FK_TileID_IG FOREIGN KEY (TileID) REFERENCES tblTile(TileID),
CONSTRAINT FK_PlayID_IG FOREIGN KEY (PlayID) REFERENCES tblPlay(PlayID) ON DELETE CASCADE
);
END
//
DELIMITER ;
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- DML Inserting into tables
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
DROP PROCEDURE IF EXISTS InsertTables;
DELIMITER //
CREATE PROCEDURE InsertTables()
BEGIN
INSERT INTO tblPlayer
VALUES
(000001, 'mstirtle0@alibaba.com', 'Bob', 'P@ssword1', TRUE, FALSE, TRUE, 0, 0, 'dsaf5165fdg46fg4sg6-54sdfg5'),
(000002, 'cgrooby1@walmart.com', 'Jane', 'P@ssword1', TRUE, FALSE, FALSE, 0, 0, 'dsaf5165fdg46fg4sg6-54sdfg5'),
(000003, 'abartosinski2@irs.gov', 'John', 'P@ssword1', TRUE, FALSE, FALSE, 2, 0, 'dsaf5165fdg46fg4sg6-54sdfg5'),
(000004, 'mggghgh0@gmail.com', 'Troy', 'P@ssword1', TRUE, FALSE, TRUE, 0, 0, 'dsaf5165fdg46fg4sg6-54sdfg5'),
(000005, 'dringm@gmx.com', 'Chris', 'P@ssword1', TRUE, FALSE, FALSE, 3, 0, 'dsaf5165fdg46fg4sg6-54sdfg5'),
(000006, 'ythnfhhgj@frirs.gov', 'Sunny', 'P@ssword1', TRUE, FALSE, FALSE, 0, 0, 'dsaf5165fdg46fg4sg6-54sdfg5'),
(000007, 'looijnhg0@gmail.com', 'JCP', 'P@ssword1', TRUE, FALSE, FALSE, 0, 0, 'dsaf5165fdg46fg4sg6-54sdfg5'),
(000008, 'frigsngjj@gmx.com', 'Junior', 'P@ssword1', TRUE, FALSE, FALSE, 0, 0, 'dsaf5165fdg46fg4sg6-54sdfg5');
INSERT INTO tblCharacter
VALUES
('Doc', 'Red'),
('Grumpy', 'Yellow'),
('Happy', 'Green'),
('Sleepy', 'Navy'),
('Bashful', 'Orange'),
('Sneezy', 'Purple'),
('Dopey', 'Blue');
INSERT INTO tblGem
VALUES
('Garnet', 49),
('Amethyst', 21),
('Aquamarine', 14),
('Diamond', 38),
('Emerald', 80),
('Pearl', 5),
('Ruby', 19),
('Peridot', 42),
('Sapphire', 11),
('Opel', 3);
INSERT INTO tblTile
VALUES
(001, 5, 5, TRUE),
(002, 1, 1, FALSE),
(003, 2, 1, FALSE),
(004, 3, 1, FALSE),
(005, 4, 1, FALSE),
(006, 5, 1, FALSE),
(007, 6, 1, FALSE),
(008, 7, 1, FALSE),
(009, 8, 1, FALSE),
(010, 9, 1, FALSE),
(011, 1, 2, FALSE),
(012, 2, 2, FALSE),
(013, 3, 2, FALSE),
(014, 4, 2, FALSE),
(015, 5, 2, FALSE),
(016, 6, 2, FALSE),
(017, 7, 2, FALSE),
(018, 8, 2, FALSE),
(019, 9, 2, FALSE),
(020, 1, 3, FALSE),
(021, 2, 3, FALSE),
(022, 3, 3, FALSE),
(023, 4, 3, FALSE),
(024, 5, 3, FALSE),
(025, 6, 3, FALSE),
(026, 7, 3, FALSE),
(027, 8, 3, FALSE),
(028, 9, 3, FALSE),
(029, 1, 4, FALSE),
(030, 2, 4, FALSE),
(031, 3, 4, FALSE),
(032, 4, 4, FALSE),
(033, 5, 4, FALSE),
(034, 6, 4, FALSE),
(035, 7, 4, FALSE),
(036, 8, 4, FALSE),
(037, 9, 4, FALSE),
(038, 1, 5, FALSE),
(039, 2, 5, FALSE),
(040, 3, 5, FALSE),
(041, 4, 5, FALSE),
(042, 6, 5, FALSE),
(043, 7, 5, FALSE),
(044, 8, 5, FALSE),
(045, 9, 5, FALSE),
(046, 1, 6, FALSE),
(047, 2, 6, FALSE),
(048, 3, 6, FALSE),
(049, 4, 6, FALSE),
(050, 5, 6, FALSE),
(051, 6, 6, FALSE),
(052, 7, 6, FALSE),
(053, 8, 6, FALSE),
(054, 9, 6, FALSE),
(055, 1, 7, FALSE),
(056, 2, 7, FALSE),
(057, 3, 7, FALSE),
(058, 4, 7, FALSE),
(059, 5, 7, FALSE),
(060, 6, 7, FALSE),
(061, 7, 7, FALSE),
(062, 8, 7, FALSE),
(063, 9, 7, FALSE),
(064, 1, 8, FALSE),
(065, 2, 8, FALSE),
(066, 3, 8, FALSE),
(067, 4, 8, FALSE),
(068, 5, 8, FALSE),
(069, 6, 8, FALSE),
(070, 7, 8, FALSE),
(071, 8, 8, FALSE),
(072, 9, 8, FALSE),
(073, 1, 9, FALSE),
(074, 2, 9, FALSE),
(075, 3, 9, FALSE),
(076, 4, 9, FALSE),
(077, 5, 9, FALSE),
(078, 6, 9, FALSE),
(079, 7, 9, FALSE),
(080, 8, 9, FALSE),
(081, 9, 9, FALSE);
INSERT INTO tblBoard
VALUES
('9 X 9 Sq', 9, 9);
INSERT INTO tblBoardTile
VALUES
('9 X 9 Sq', 001),
('9 X 9 Sq', 002),
('9 X 9 Sq', 003),
('9 X 9 Sq', 004),
('9 X 9 Sq', 005),
('9 X 9 Sq', 006),
('9 X 9 Sq', 007),
('9 X 9 Sq', 008),
('9 X 9 Sq', 009),
('9 X 9 Sq', 010),
('9 X 9 Sq', 011),
('9 X 9 Sq', 012),
('9 X 9 Sq', 013),
('9 X 9 Sq', 014),
('9 X 9 Sq', 015),
('9 X 9 Sq', 016),
('9 X 9 Sq', 017),
('9 X 9 Sq', 018),
('9 X 9 Sq', 019),
('9 X 9 Sq', 020),
('9 X 9 Sq', 021),
('9 X 9 Sq', 022),
('9 X 9 Sq', 023),
('9 X 9 Sq', 024),
('9 X 9 Sq', 025),
('9 X 9 Sq', 026),
('9 X 9 Sq', 027),
('9 X 9 Sq', 028),
('9 X 9 Sq', 029),
('9 X 9 Sq', 030),
('9 X 9 Sq', 031),
('9 X 9 Sq', 032),
('9 X 9 Sq', 033),
('9 X 9 Sq', 034),
('9 X 9 Sq', 035),
('9 X 9 Sq', 036),
('9 X 9 Sq', 037),
('9 X 9 Sq', 038),
('9 X 9 Sq', 039),
('9 X 9 Sq', 040),
('9 X 9 Sq', 041),
('9 X 9 Sq', 042),
('9 X 9 Sq', 043),
('9 X 9 Sq', 044),
('9 X 9 Sq', 045),
('9 X 9 Sq', 046),
('9 X 9 Sq', 047),
('9 X 9 Sq', 048),
('9 X 9 Sq', 049),
('9 X 9 Sq', 050),
('9 X 9 Sq', 051),
('9 X 9 Sq', 052),
('9 X 9 Sq', 053),
('9 X 9 Sq', 054),
('9 X 9 Sq', 055),
('9 X 9 Sq', 056),
('9 X 9 Sq', 057),
('9 X 9 Sq', 058),
('9 X 9 Sq', 059),
('9 X 9 Sq', 060),
('9 X 9 Sq', 061),
('9 X 9 Sq', 062),
('9 X 9 Sq', 063),
('9 X 9 Sq', 064),
('9 X 9 Sq', 065),
('9 X 9 Sq', 066),
('9 X 9 Sq', 067),
('9 X 9 Sq', 068),
('9 X 9 Sq', 069),
('9 X 9 Sq', 070),
('9 X 9 Sq', 071),
('9 X 9 Sq', 072),
('9 X 9 Sq', 073),
('9 X 9 Sq', 074),
('9 X 9 Sq', 075),
('9 X 9 Sq', 076),
('9 X 9 Sq', 077),
('9 X 9 Sq', 078),
('9 X 9 Sq', 079),
('9 X 9 Sq', 080),
('9 X 9 Sq', 081);
INSERT INTO tblGame
VALUES
(100001, '9 X 9 Sq', 'Doc'),
(100002, '9 X 9 Sq', 'Doc');
INSERT INTO tblPlay
VALUES
(500001, 000001, 'Doc', 100001, 078, 3),
(500002, 000004, 'Grumpy', 100001, 079, 38),
(500003, 000005, 'Sleepy', 100001, 063, 11),
(500004, 000002, 'Doc', 100002, 033, 49),
(500005, 000003, 'Grumpy', 100002, 042, 21),
(500006, 000003, 'Happy', 100001, 042, 0);
INSERT INTO tblItem
VALUES
(101, 'Garnet'),
(102, 'Garnet'),
(103, 'Garnet'),
(104, 'Garnet'),
(105, 'Garnet'),
(106, 'Garnet'),
(107, 'Garnet'),
(108, 'Amethyst'),
(109, 'Amethyst'),
(110, 'Amethyst'),
(111, 'Amethyst'),
(112, 'Amethyst'),
(113, 'Amethyst'),
(114, 'Amethyst'),
(115, 'Aquamarine'),
(116, 'Aquamarine'),
(117, 'Aquamarine'),
(118, 'Aquamarine'),
(119, 'Aquamarine'),
(120, 'Aquamarine'),
(121, 'Aquamarine'),
(122, 'Diamond'),
(123, 'Diamond'),
(124, 'Diamond'),
(125, 'Diamond'),
(126, 'Diamond'),
(127, 'Diamond'),
(128, 'Diamond'),
(129, 'Emerald'),
(130, 'Emerald'),
(131, 'Emerald'),
(132, 'Emerald'),
(133, 'Emerald'),
(134, 'Emerald'),
(135, 'Emerald'),
(136, 'Pearl'),
(137, 'Pearl'),
(138, 'Pearl'),
(139, 'Pearl'),
(140, 'Pearl'),
(141, 'Pearl'),
(142, 'Pearl'),
(143, 'Ruby'),
(144, 'Ruby'),
(145, 'Ruby'),
(146, 'Ruby'),
(147, 'Ruby'),
(148, 'Ruby'),
(149, 'Ruby'),
(150, 'Peridot'),
(151, 'Peridot'),
(152, 'Peridot'),
(153, 'Peridot'),
(154, 'Peridot'),
(155, 'Peridot'),
(156, 'Peridot'),
(157, 'Sapphire'),
(158, 'Sapphire'),
(159, 'Sapphire'),
(160, 'Sapphire'),
(161, 'Sapphire'),
(162, 'Sapphire'),
(163, 'Sapphire'),
(164, 'Opel'),
(165, 'Opel'),
(166, 'Opel'),
(167, 'Opel'),
(168, 'Opel'),
(169, 'Opel'),
(170, 'Opel');
INSERT INTO tblItemGame
VALUES
(101, 100001, 051, NULL),
(102, 100001, 031, NULL),
(103, 100001, 021, NULL),
(104, 100001, 011, NULL),
(105, 100001, 051, NULL),
(106, 100001, 031, NULL),
(107, 100001, 021, NULL),
(108, 100001, 011, NULL),
(109, 100001, 061, NULL),
(110, 100001, 071, NULL),
(111, 100001, 012, NULL),
(112, 100001, 013, NULL),
(113, 100001, 014, NULL),
(114, 100001, 022, NULL),
(115, 100001, 023, NULL),
(116, 100001, 024, NULL),
(117, 100001, 015, NULL),
(118, 100001, 016, NULL),
(119, 100001, 017, NULL),
(120, 100001, 018, NULL),
(121, 100001, 018, NULL),
(122, 100001, 076, NULL),
(123, 100001, 077, NULL),
(124, 100001, 078, NULL),
(125, 100001, 079, NULL),
(126, 100001, 076, NULL),
(127, 100001, 071, NULL),
(128, 100001, 075, NULL),
(129, 100001, 075, NULL),
(130, 100001, 034, NULL),
(131, 100001, 035, NULL),
(132, 100001, 051, NULL),
(133, 100001, 005, NULL),
(134, 100001, 052, NULL),
(135, 100001, 080, NULL),
(136, 100001, 070, NULL),
(137, 100001, 002, NULL),
(138, 100001, 070, NULL),
(139, 100001, 022, NULL),
(140, 100001, 054, NULL),
(141, 100001, 046, NULL),
(142, 100001, 056, NULL),
(143, 100001, 057, NULL),
(144, 100001, 058, NULL),
(145, 100001, 059, NULL),
(146, 100001, 045, NULL),
(147, 100001, 044, NULL),
(148, 100001, 043, NULL),
(149, 100001, 042, NULL),
(150, 100001, 043, NULL),
(151, 100001, 067, NULL),
(152, 100001, 079, NULL),
(153, 100001, 079, NULL),
(154, 100001, 078, NULL),
(155, 100001, 023, NULL),
(156, 100001, 002, NULL),
(157, 100001, 003, NULL),
(158, 100001, 004, NULL),
(159, 100001, 005, NULL),
(160, 100001, 004, NULL),
(161, 100001, 005, NULL),
(162, 100001, 015, NULL),
(163, 100001, 014, NULL),
(164, 100001, 013, NULL),
(165, 100001, 058, NULL),
(166, 100001, 080, NULL),
(167, 100001, 040, NULL),
(168, 100001, 042, NULL),
(169, 100001, 034, NULL),
(170, 100001, 039, NULL),
(101, 100002, 051, NULL),
(102, 100002, 031, NULL),
(103, 100002, 021, NULL),
(104, 100002, 011, NULL),
(105, 100002, 051, NULL),
(106, 100002, 031, NULL),
(107, 100002, 021, NULL),
(108, 100002, 011, NULL),
(109, 100002, 061, NULL),
(110, 100002, 071, NULL),
(111, 100002, 081, NULL),
(112, 100002, 013, NULL),
(113, 100002, 014, NULL),
(114, 100002, 022, NULL),
(115, 100002, 023, NULL),
(116, 100002, 024, NULL),
(117, 100002, 015, NULL),
(118, 100002, 016, NULL),
(119, 100002, 017, NULL),
(120, 100002, 018, NULL),
(121, 100002, 018, NULL),
(122, 100002, 076, NULL),
(123, 100002, 077, NULL),
(124, 100002, 078, NULL),
(125, 100002, 079, NULL),
(126, 100002, 076, NULL),
(127, 100002, 071, NULL),
(128, 100002, 075, NULL),
(129, 100002, 075, NULL),
(130, 100002, 034, NULL),
(131, 100002, 035, NULL),
(132, 100002, 051, NULL),
(133, 100002, 005, NULL),
(134, 100002, 052, NULL),
(135, 100002, 080, NULL),
(136, 100002, 070, NULL),
(137, 100002, 009, NULL),
(138, 100002, 070, NULL),
(139, 100002, 022, NULL),
(140, 100002, 054, NULL),
(141, 100002, 046, NULL),
(142, 100002, 056, NULL),
(143, 100002, 057, NULL),
(144, 100002, 058, NULL),
(145, 100002, 059, NULL),
(146, 100002, 045, NULL),
(147, 100002, 044, NULL),
(148, 100002, 043, NULL),
(149, 100002, 042, NULL),
(150, 100002, 043, NULL),
(151, 100002, 067, NULL),
(152, 100002, 079, NULL),
(153, 100002, 079, NULL),
(154, 100002, 078, NULL),
(155, 100002, 023, NULL),
(156, 100002, 002, NULL),
(157, 100002, 003, NULL),
(158, 100002, 004, NULL),
(159, 100002, 005, NULL),
(160, 100002, 004, NULL),
(161, 100002, 005, NULL),
(162, 100002, 015, NULL),
(163, 100002, 014, NULL),
(164, 100002, 013, NULL),
(165, 100002, 058, NULL),
(166, 100002, 080, NULL),
(167, 100002, 040, NULL),
(168, 100002, 042, NULL),
(169, 100002, 034, NULL),
(170, 100002, 039, NULL);
END
//
DELIMITER ;
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- Call Create, Insert Procedures
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
USE sdghGameDatabase;
CALL CreateTables;
CALL InsertTables;
-- Seven Dwarfs Gem Hunt Project Transactional SQL Milestone 2
-- CREATE PROCEDURES
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- Database Admins
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- SELECT `user`, `host` FROM mysql.user;
DROP USER IF EXISTS 'databaseAdmin'@'localhost';
CREATE USER IF NOT EXISTS 'databaseAdmin'@'localhost' IDENTIFIED BY 'P@ssword1';
GRANT ALL ON sdghGameDatabase TO 'databaseAdmin'@'localhost';
-- SHOW GRANTS FOR 'databaseAdmin'@'localhost';
-- SHOW GRANTS FOR 'root'@'localhost';
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- Global Database Controls
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- Check table is encrypted
SELECT TABLE_SCHEMA, TABLE_NAME, CREATE_OPTIONS
FROM INFORMATION_SCHEMA.TABLES
WHERE CREATE_OPTIONS LIKE '%ENCRYPTION%';
SET GLOBAL TRANSACTION ISOLATION LEVEL read committed;
-- SET GLOBAL TRANSACTION ISOLATION LEVEL read uncommitted;
-- SET GLOBAL TRANSACTION ISOLATION LEVEL repeatable read;
-- SET GLOBAL TRANSACTION ISOLATION LEVEL serialization;
-- SHOW GLOBAL VARIABLES LIKE '%isolation%';
SELECT @@autocommit;
SET AUTOCOMMIT=0;
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- New User Registration Procedure
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- The database contains a constraint that only allows unique values to be allocated to Email and Username, should
-- a new user attempt to register with either value found to exist the procedure will not run. Otherwise, the procedure
-- requires a user to enter an email, username and password, the remaining fields are created as the defaults, the
-- procedure gives new players the ability to register an account.
DROP PROCEDURE IF EXISTS NewUserRegistration;
DELIMITER //
CREATE DEFINER = 'root'@'localhost' PROCEDURE NewUserRegistration(
IN pEmail varchar(50),
IN pUsername varchar(10),
IN pPassword BLOB
)
SQL SECURITY DEFINER
BEGIN
DECLARE EXIT HANDLER FOR 1062
BEGIN
SELECT 'Either the email or username entered already exists' AS MESSAGE;
END;
BEGIN
DECLARE newSalt varchar(36);
SELECT UUID() INTO newSalt;
START TRANSACTION;
INSERT INTO tblPlayer(Email, Username, `Password`, Salt)
VALUES (pEmail, pUsername, AES_ENCRYPT(CONCAT(newSalt, pPassword), 'Game_Key_To_Encrypt'), newSalt);
SELECT 'Your account is created, let the games begin!!!' AS MESSAGE;
COMMIT;
END;
END //
DELIMITER ;
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- Login Check Credentials Procedure
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- This procedure allows a user to log in to the game, it retrieves the users salt record to ensure the password is
-- passed correctly and the users active status to ensure they are not already logged in. If the user is logged in
-- their details are displayed and a message is passed stating their active status. If an incorrect username or password
-- is entered and an error message is returned.
DROP PROCEDURE IF EXISTS LoginCheckCredentials;
DELIMITER //
CREATE DEFINER = 'root'@'localhost' PROCEDURE LoginCheckCredentials(
IN pUsername varchar(50),
IN pPassword BLOB
)
SQL SECURITY DEFINER
BEGIN
DECLARE retrieveSalt varchar(36) DEFAULT NULL;
DECLARE proposedUID int DEFAULT NULL;
DECLARE currentAS bit DEFAULT NULL;
SELECT Salt
FROM tblPlayer
WHERE
Username = pUsername
INTO retrieveSalt; -- Retrieves the users SALT record
SELECT PlayerID
FROM tblPlayer
WHERE
AES_ENCRYPT(CONCAT(retrieveSalt, pPassword), 'Game_Key_To_Encrypt') = `Password`
AND pUsername = Username
INTO proposedUID; -- Retrieves the users Username and Password
SELECT ActiveStatus
FROM tblPlayer
WHERE
Username = pUsername
INTO currentAS;
START TRANSACTION;
IF proposedUID IS NULL AND currentAS = 0 THEN
UPDATE tblPlayer
SET FailedLogins = FailedLogins +1, AccountLocked = (FailedLogins +1) > 5, ActiveStatus = (FailedLogins +1) < 1
WHERE
Username = pUsername;
SELECT 'You have entered an incorrect Username or Password, after 5 failed attempts your account will be locked' AS MESSAGE;
-- Increments the failed logins, if it equals 5 then account is locked
ELSEIF proposedUID IS NOT NULL AND currentAS = 0 THEN
UPDATE tblPlayer
SET ActiveStatus = 1, FailedLogins = 0, AccountLocked = 0
WHERE
Username = pUsername;
SELECT 'Success' AS MESSAGE;
SELECT GameID AS 'GameID', COUNT(pl.GameID) AS 'PlayerCount'
FROM tblPlayer py
JOIN tblPlay pl ON py.PlayerID = pl.PlayerID
GROUP BY pl.GameID;
SELECT Username AS 'Player', HighScore AS 'HighScore'
FROM tblPlayer;
-- If credentials are correct user is logged into account by setting active status to true
ELSE
SELECT 'You are logged in' AS MESSAGE;
SELECT GameID AS 'GameID', COUNT(pl.GameID) AS 'PlayerCount'
FROM tblPlayer py
JOIN tblPlay pl ON py.PlayerID = pl.PlayerID
GROUP BY pl.GameID;
SELECT Username AS 'Player', HighScore AS 'HighScore'
FROM tblPlayer;
-- Conditions are met so user is already logged in
END IF;
COMMIT;
END //
DELIMITER ;
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- Home Screen Display Procedure
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- Two select statements make up the procedure and have been designed with thought given to the end GUI, should a
-- login attempt be successful, which is further check by selecting the active status of the user, then the relevant
-- information as described in the storyboarding is displayed.
DROP PROCEDURE IF EXISTS HomeScreen;
DELIMITER //
CREATE DEFINER = 'root'@'localhost' PROCEDURE HomeScreen(
IN pUsername varchar(10)
)
SQL SECURITY DEFINER
BEGIN
DECLARE accessScreen bit DEFAULT NULL;
SELECT ActiveStatus
FROM tblPlayer
WHERE
Username = pUsername
INTO accessScreen;
IF accessScreen IS TRUE THEN
SELECT GameID AS 'Game ID', COUNT(pl.GameID) AS 'Player Count'
FROM tblPlayer py
JOIN tblPlay pl ON py.PlayerID = pl.PlayerID
GROUP BY pl.GameID;
SELECT Username AS 'Player', HighScore AS 'High Score'
FROM tblPlayer;
END IF;
END //
DELIMITER ;
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- New Game Procedure
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- The new game procedure must create a new game in the game, which includes an autoincrement ID, a new play instance
-- for the player that creates the new game and a new item list associated with the game in the item/game table. The
-- play table and item/game table must pull in the newly created game ID, this is achieved by declaring the new game ID
-- with the LAST_INSERT_ID() function. Finally, the items are allocated to tiles within the game, this is done randomly
-- so each game is different to the last.
DROP PROCEDURE IF EXISTS NewGame;
DELIMITER //
CREATE DEFINER = 'root'@'localhost' PROCEDURE NewGame(
IN pUsername varchar(10)
)
SQL SECURITY DEFINER
BEGIN
DECLARE firstItem int DEFAULT NULL;
DECLARE lastItem int DEFAULT NULL;
DECLARE chosenBoardType varchar(20) DEFAULT NULL; -- More boards may be added in the future so player would want to select board type
DECLARE firstCharacter varchar(10) DEFAULT NULL;
DECLARE excludeHomeTile int DEFAULT NULL;
DECLARE lastTile int DEFAULT NULL;
DECLARE newGameId int DEFAULT NULL;
SELECT ItemID
FROM tblItem
ORDER BY ItemID LIMIT 1
INTO firstItem;
SELECT MAX(ItemID)
FROM tblItem
INTO lastItem;
SELECT BoardType
FROM tblBoard LIMIT 1
INTO chosenBoardType; -- This statement would be updated is player could choose from multiple board types
SELECT CharacterName
FROM tblCharacter
WHERE
CharacterName = 'Doc'
INTO firstCharacter;
SELECT TileID
FROM tblTile LIMIT 1, 1
INTO excludeHomeTile;
SELECT MAX(TileID)
FROM tblBoardTile
WHERE
BoardType = chosenBoardType
INTO lastTile;
INSERT INTO tblGame(BoardType, CharacterTurn)
VALUES (chosenBoardType, firstCharacter);
SET newGameId = LAST_INSERT_ID();
START TRANSACTION;
IF newGameId > 0 THEN
INSERT INTO tblPlay(PlayerID, CharacterName, GameID)
VALUES ((SELECT PlayerID
FROM tblPlayer
WHERE
Username = pUsername), firstCharacter, newGameId);
END IF;
WHILE firstItem <= lastItem DO
INSERT INTO tblItemGame(ItemID, GameID, TileID)
VALUES (firstItem, newGameId, (SELECT FLOOR(RAND()*(lastTile-excludeHomeTile+1)+excludeHomeTile)));
SET firstItem = firstItem + 1;
END WHILE;
COMMIT;
SELECT 'Your new game is created, find those gems!!!' AS MESSAGE;
END //
DELIMITER ;
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- Join Game Procedure
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- When a player joins a new game, the next available character is selected, a play instance is created that is
-- assigned to the game with the character and the player ID. If all seven dwarf characters are playing in the game,
-- then an error message is displayed.
DROP PROCEDURE IF EXISTS JoinGame;
DELIMITER //
CREATE DEFINER = 'root'@'localhost' PROCEDURE JoinGame(
IN pGameID int,
IN pPlayerID int
)
SQL SECURITY DEFINER
BEGIN
DECLARE selectedCharacter varchar(10) DEFAULT NULL;
DECLARE selectedUser int DEFAULT NULL;
SELECT CharacterName
FROM tblCharacter
WHERE
CharacterName NOT IN (SELECT CharacterName
FROM tblPlay
WHERE
GameID = pGameID) LIMIT 1
INTO selectedCharacter;
SELECT PlayerID
FROM tblPlayer
WHERE
PlayerID NOT IN (SELECT PlayerID
FROM tblPlay
WHERE
GameID = pGameID)
AND PlayerID = pPlayerID
INTO selectedUser;
START TRANSACTION;
IF selectedCharacter IS NOT NULL AND selectedUser IS NOT NULL THEN -- Prevents more then Character count of 7 joining a game and prevents update from happening if player re-joining the game
INSERT INTO tblPlay(PlayerID, CharacterName, GameID)
VALUES (selectedUser, selectedCharacter, pGameID);
SELECT 'Youve joined the game!!!' AS MESSAGE;
ELSEIF selectedUser IS NULL THEN
SELECT 'You are back in the game!!!' AS MESSAGE;
ELSEIF selectedCharacter IS NULL AND selectedUser IS NOT NULL THEN
SELECT 'All seven dwarfs are playing this game!!!' AS MESSAGE;
END IF;
COMMIT;
END //
DELIMITER ;
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- Player Moves Procedure
-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-- The procedure moves a player to a new tile if the tile is plus or minus one from the player's current tile position
-- in a game instance, the play table records the current player's position. If a player is already on the tile, except
-- for the home tile, then the player cannot move to it and an error message is displayed. Likewise, if a player selects
-- a tile that is not plus or minus one adjacent from the current tile an error message is displayed.
DROP PROCEDURE IF EXISTS MovePlayer;
DELIMITER //
CREATE DEFINER = 'root'@'localhost' PROCEDURE MovePlayer(
IN pTileID int,
IN pPlayerID int,
IN pGameID int
)
SQL SECURITY DEFINER
BEGIN
DECLARE currentTurn varchar(10) DEFAULT NULL;
DECLARE availableTile int DEFAULT NULL;
DECLARE ifPlayerOnTileAreTheyActive bit DEFAULT NULL;
DECLARE currentTileRow tinyint DEFAULT NULL;
DECLARE currentTileColumn tinyint DEFAULT NULL;
DECLARE newTileRow tinyint DEFAULT NULL;
DECLARE newTileColumn tinyint DEFAULT NULL;
SELECT CharacterTurn -- Checks the character turn for the game
FROM tblGame
WHERE
GameID = pGameID
INTO currentTurn;
SELECT TileID -- Checks if a tile is empty and available
FROM tblTile
WHERE
TileID NOT IN (SELECT TileID
FROM tblPlay
WHERE
GameID = pGameID)
AND TileID = pTileID
AND HomeTile = FALSE
INTO availableTile;
SELECT ActiveStatus -- Checks the active status if a player is on the tile selected
FROM tblPlayer pl
JOIN tblPlay py ON pl.PlayerID = py.PlayerID
WHERE
py.TileID = pTileID
AND GameID = pGameID
INTO ifPlayerOnTileAreTheyActive; -- This allows player to move to a tile with another player located but the active status is 0
SELECT TileRow -- The current player tile row
FROM tblTile ti
JOIN tblPlay pl ON ti.TileID = pl.TileID
WHERE
PlayerID = pPlayerID
AND GameID = pGameID
INTO currentTileRow;
SELECT TileColumn -- The current player tile column
FROM tblTile ti
JOIN tblPlay pl ON ti.TileID = pl.TileID
WHERE
PlayerID = pPlayerID
AND GameID = pGameID
INTO currentTileColumn;
SELECT TileRow -- The selected tile row
FROM tblTile
WHERE
TileID = pTileID
INTO newTileRow;
SELECT TileColumn -- The selected tile column