Repository navigation
Expand file tree
/
Copy pathinit.sql
More file actions
161 lines (141 loc) · 3.94 KB
/
Copy pathinit.sql
File metadata and controls
161 lines (141 loc) · 3.94 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
-- docker exec cb0e090df0ca createdb -U josip mtcg
drop table if exists "user" CASCADE;
drop table if exists "package" CASCADE;
drop table if exists "card" CASCADE;
drop table if exists "deck" CASCADE;
drop table if exists "stat" CASCADE;
drop table if exists "trade" CASCADE;
drop table if exists "battle" CASCADE;
create table if not exists "user"
(
id varchar not null PRIMARY KEY,
passwordHash varchar not null,
coins integer,
username varchar COLLATE "C" not null,
name varchar COLLATE "C",
bio varchar COLLATE "C",
image varchar COLLATE "C"
);
create unique index if not exists user_username_uindex
on "user" (username);
create table if not exists package
(
id varchar PRIMARY KEY,
price integer,
fk_userId varchar,
CONSTRAINT fk_userId
FOREIGN KEY (fk_userId)
REFERENCES "user" (id)
ON DELETE CASCADE
);
create table if not exists card
(
id varchar PRIMARY KEY,
name varchar COLLATE "C" not null,
damage double precision not null,
elementType varchar,
cardType varchar,
fk_ownerId varchar,
fk_packId varchar,
CONSTRAINT fk_ownerId
FOREIGN KEY (fk_ownerId)
REFERENCES "user" (id)
ON DELETE CASCADE,
CONSTRAINT fk_packId
FOREIGN KEY (fk_packId)
REFERENCES "package" (id)
ON DELETE CASCADE
);
CREATE OR REPLACE FUNCTION check_package_card_limit()
RETURNS TRIGGER AS
$$
BEGIN
IF (SELECT COUNT(*) FROM card WHERE fk_packId = NEW.fk_packId) > 5 THEN
RAISE EXCEPTION 'A package can only contain 5 cards';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Create the trigger
CREATE TRIGGER check_package_card_limit
BEFORE INSERT
ON card
FOR EACH ROW
EXECUTE PROCEDURE check_package_card_limit();
create table if not exists deck
(
fk_userId varchar,
fk_cardId varchar,
PRIMARY KEY (fk_cardId, fk_userId),
CONSTRAINT fk_userId
FOREIGN KEY (fk_userId)
REFERENCES "user" (id)
ON DELETE CASCADE,
CONSTRAINT fk_cardId
FOREIGN KEY (fk_cardId)
REFERENCES "card" (id)
ON DELETE CASCADE
);
CREATE OR REPLACE FUNCTION check_max_cards_per_user()
RETURNS TRIGGER AS
$$
BEGIN
IF (SELECT COUNT(*) FROM deck WHERE fk_userId = NEW.fk_userId) > 4 THEN
RAISE EXCEPTION 'Cannot have more than 4 cards per user';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER max_cards_per_user
BEFORE INSERT OR UPDATE
ON deck
FOR EACH ROW
EXECUTE PROCEDURE check_max_cards_per_user();
create table if not exists stat
(
fk_userId varchar PRIMARY KEY,
elo integer,
wins integer,
draws integer,
total integer,
CONSTRAINT fk_userId
FOREIGN KEY (fk_userId)
REFERENCES "user" (id)
ON DELETE CASCADE
);
create table if not exists battle
(
id varchar PRIMARY KEY,
fk_player1Id varchar,
fk_player2Id varchar,
battleLog varchar COLLATE "C",
battleOutcome varchar,
created_at TIMESTAMP DEFAULT NOW(),
CONSTRAINT fk_player1Id
FOREIGN KEY (fk_player1Id)
REFERENCES "user" (id)
ON DELETE CASCADE,
CONSTRAINT fk_player2Id
FOREIGN KEY (fk_player2Id)
REFERENCES "user" (id)
ON DELETE CASCADE
);
create table if not exists trade
(
id varchar PRIMARY KEY,
fk_cardToTradeId varchar,
cardType varchar,
minimumDamage double precision,
fk_user1Id varchar,
fk_user2Id varchar,
CONSTRAINT fk_player1Id
FOREIGN KEY (fk_user1Id)
REFERENCES "user" (id)
ON DELETE CASCADE,
CONSTRAINT fk_player2Id
FOREIGN KEY (fk_user2Id)
REFERENCES "user" (id)
ON DELETE CASCADE
);
SELECT *
FROM pg_stat_activity;