-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.sql
175 lines (155 loc) · 6.33 KB
/
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
-- Create the database if it doesn't exist
CREATE DATABASE IF NOT EXISTS 0ce;
-- Use the database
USE 0ce;
CREATE TABLE IF NOT EXISTS player (
player_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
player_name VARCHAR(100) UNIQUE NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
hashed_password VARBINARY(255) NOT NULL,
salt VARBINARY(16) NOT NULL,
gold INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_login TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS world (
world_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
world_name VARCHAR(100) UNIQUE NOT NULL,
world_description TEXT,
seed INT,
action_speed TINYINT UNSIGNED DEFAULT 1,
unit_speed TINYINT UNSIGNED DEFAULT 1,
trade_speed TINYINT UNSIGNED DEFAULT 1,
night_bonus INT DEFAULT 0,
beginner_protection INT DEFAULT 0,
morale BOOL DEFAULT FALSE,
world_status TINYINT DEFAULT 2,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS player_world (
player_id INT UNSIGNED NOT NULL,
world_id INT UNSIGNED NOT NULL,
PRIMARY KEY (player_id, world_id),
FOREIGN KEY (player_id) REFERENCES player (player_id),
FOREIGN KEY (world_id) REFERENCES world (world_id)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS island (
island_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
x INT NOT NULL,
y INT NOT NULL,
slots INT DEFAULT 7 NOT NULL,
world_id INT UNSIGNED NOT NULL,
FOREIGN KEY (world_id) REFERENCES world (world_id)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS city (
city_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
city_name VARCHAR(100),
island_id INT UNSIGNED NOT NULL,
slot_number INT NOT NULL,
owner_id INT UNSIGNED NOT NULL,
world_id INT UNSIGNED NOT NULL,
FOREIGN KEY (island_id) REFERENCES island (island_id),
FOREIGN KEY (owner_id) REFERENCES player (player_id),
FOREIGN KEY (world_id) REFERENCES world (world_id),
UNIQUE (island_id, slot_number)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS building (
building_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
building_name VARCHAR(100),
building_level INT DEFAULT 0 NOT NULL,
max_level INT DEFAULT 10 NOT NULL,
city_id INT UNSIGNED NOT NULL,
FOREIGN KEY (city_id) REFERENCES city (city_id)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS city_building (
city_id INT UNSIGNED NOT NULL,
building_id INT UNSIGNED NOT NULL,
current_level INT DEFAULT 0 NOT NULL,
PRIMARY KEY (city_id, building_id),
FOREIGN KEY (city_id) REFERENCES city (city_id),
FOREIGN KEY (building_id) REFERENCES building (building_id)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS building_requirement (
building_id INT UNSIGNED NOT NULL,
building_level INT DEFAULT 0 NOT NULL,
required_wood INT DEFAULT 0 NOT NULL,
required_stone INT DEFAULT 0 NOT NULL,
required_silver INT DEFAULT 0 NOT NULL,
required_population INT DEFAULT 0 NOT NULL,
PRIMARY KEY (building_id, building_level),
FOREIGN KEY (building_id) REFERENCES building (building_id)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS building_prerequisite (
building_id INT UNSIGNED NOT NULL,
building_level INT NOT NULL,
prerequisite_building_id INT UNSIGNED NOT NULL,
PRIMARY KEY (building_id, building_level, prerequisite_building_id),
FOREIGN KEY (building_id) REFERENCES building (building_id),
FOREIGN KEY (prerequisite_building_id) REFERENCES building (building_id)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS unit (
unit_id TINYINT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
unit_name VARCHAR(100),
unit_description TEXT,
unit_type TINYINT NOT NULL,
wood_cost INT DEFAULT 0 NOT NULL,
stone_cost INT DEFAULT 0 NOT NULL,
silver_cost INT DEFAULT 0 NOT NULL,
population_cost INT DEFAULT 0 NOT NULL,
training_time INT DEFAULT 0 NOT NULL,
damage INT DEFAULT 0 NOT NULL,
defense_blunt INT DEFAULT 0 NOT NULL,
defense_distance INT DEFAULT 0 NOT NULL,
defense_sharp INT DEFAULT 0 NOT NULL,
speed INT DEFAULT 1 NOT NULL,
can_fly BOOL DEFAULT FALSE NOT NULL
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS city_unit (
city_id INT UNSIGNED NOT NULL,
unit_id TINYINT UNSIGNED NOT NULL,
quantity INT DEFAULT 0 NOT NULL,
PRIMARY KEY (city_id, unit_id),
FOREIGN KEY (city_id) REFERENCES city (city_id),
FOREIGN KEY (unit_id) REFERENCES unit (unit_id)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS battle (
battle_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
attacker_id INT UNSIGNED NOT NULL,
defender_id INT UNSIGNED NOT NULL,
battle_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
winner_id INT UNSIGNED NOT NULL,
loser_id INT UNSIGNED NOT NULL,
loot_wood INT DEFAULT 0 NOT NULL,
loot_stone INT DEFAULT 0 NOT NULL,
loot_silver INT DEFAULT 0 NOT NULL,
FOREIGN KEY (attacker_id) REFERENCES player (player_id),
FOREIGN KEY (defender_id) REFERENCES player (player_id),
FOREIGN KEY (winner_id) REFERENCES player (player_id),
FOREIGN KEY (loser_id) REFERENCES player (player_id)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS battle_unit (
battle_id INT UNSIGNED NOT NULL,
unit_id TINYINT UNSIGNED NOT NULL,
quantity INT DEFAULT 0 NOT NULL,
side TINYINT NOT NULL,
PRIMARY KEY (battle_id, unit_id),
FOREIGN KEY (battle_id) REFERENCES battle (battle_id),
FOREIGN KEY (unit_id) REFERENCES unit (unit_id)
) ENGINE = InnoDB;
-- Island & City Positioning
ALTER TABLE island ADD CONSTRAINT unique_island_position UNIQUE (x, y, world_id);
-- Player-Related Indexes
CREATE INDEX idx_player_email ON player (email);
CREATE INDEX idx_player_last_login ON player (last_login);
-- Player-World Index
CREATE INDEX idx_player_world_world_id ON player_world (world_id);
-- World & City Indexes
CREATE INDEX idx_world_status ON world (world_status);
CREATE INDEX idx_city_owner ON city (owner_id);
-- Composite index for attacker/defender queries
CREATE INDEX idx_battle_attacker_defender ON battle (attacker_id, defender_id);
-- Foreign Key Indexes
CREATE INDEX idx_city_island_id ON city (island_id);
CREATE INDEX idx_city_owner_id ON city (owner_id);
CREATE INDEX idx_battle_attacker_id ON battle (attacker_id);
CREATE INDEX idx_battle_defender_id ON battle (defender_id);