summaryrefslogtreecommitdiffstats
path: root/src/sql/sqlite/createTables.sql
blob: d71858fa6688fb0d9f869056ffb2fb2e857695fb (plain)
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
--
-- The Mana World Server
-- Copyright 2009 The Mana World Development Team
--
-- This file is part of The Mana World.
--
-- The Mana World  is free software; you can redistribute  it and/or modify it
-- under the terms of the GNU General  Public License as published by the Free
-- Software Foundation; either version 2 of the License, or any later version.
--
-- The Mana  World is  distributed in  the hope  that it  will be  useful, but
-- WITHOUT ANY WARRANTY; without even  the implied warranty of MERCHANTABILITY
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
-- more details.
--
-- You should  have received a  copy of the  GNU General Public  License along
-- with The Mana  World; if not, write to the  Free Software Foundation, Inc.,
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
--

-----------------------------------------------------------------------------
-- Tables
-----------------------------------------------------------------------------

CREATE TABLE tmw_accounts
(
   id            INTEGER     PRIMARY KEY,
   username      TEXT        NOT NULL UNIQUE,
   password      TEXT        NOT NULL,
   email         TEXT        NOT NULL,
   level         INTEGER     NOT NULL,
   banned        INTEGER     NOT NULL,
   registration  INTEGER     NOT NULL,
   lastlogin     INTEGER     NOT NULL,
   authorization TEXT            NULL,
   expiration    INTEGER         NULL
);

CREATE UNIQUE INDEX tmw_accounts_username ON tmw_accounts ( username );
CREATE UNIQUE INDEX tmw_accounts_email    ON tmw_accounts ( email );

-----------------------------------------------------------------------------

CREATE TABLE tmw_characters
(
   id           INTEGER     PRIMARY KEY,
   user_id      INTEGER     NOT NULL,
   name         TEXT        NOT NULL UNIQUE,
   gender       INTEGER     NOT NULL,
   hair_style   INTEGER     NOT NULL,
   hair_color   INTEGER     NOT NULL,
   level        INTEGER     NOT NULL,
   char_pts     INTEGER     NOT NULL,
   correct_pts  INTEGER     NOT NULL,
   money        INTEGER     NOT NULL,
   x            INTEGER     NOT NULL,
   y            INTEGER     NOT NULL,
   map_id       INTEGER     NOT NULL,
   str          INTEGER     NOT NULL,
   agi          INTEGER     NOT NULL,
   dex          INTEGER     NOT NULL,
   vit          INTEGER     NOT NULL,
   int          INTEGER     NOT NULL,
   will         INTEGER     NOT NULL,
   --
   FOREIGN KEY (user_id) REFERENCES tmw_accounts(id)
);

CREATE INDEX tmw_characters_user ON tmw_characters ( user_id );
CREATE UNIQUE INDEX tmw_characters_name ON tmw_characters ( name );

-----------------------------------------------------------------------------

CREATE TABLE tmw_char_skills
(
    char_id     INTEGER     NOT NULL,
    skill_id    INTEGER     NOT NULL,
    skill_exp   INTEGER     NOT NULL,
    --
    FOREIGN KEY (char_id) REFERENCES tmw_characters(id)
);

CREATE INDEX tmw_char_skills_char ON tmw_char_skills ( char_id );

-----------------------------------------------------------------------------

CREATE TABLE tmw_char_status_effects
(
    char_id     INTEGER     NOT NULL,
    status_id   INTEGER     NOT NULL,
    status_time INTEGER     NOT NULL,
    --
    FOREIGN KEY (char_id) REFERENCES tmw_characters(id)
);

CREATE INDEX tmw_char_status_char on tmw_char_status_effects ( char_id );

-----------------------------------------------------------------------------

CREATE TABLE tmw_items
(
    id           INTEGER    PRIMARY KEY,
    name         TEXT       NOT NULL,
    description  TEXT       NOT NULL,
    image        TEXT       NOT NULL,
    weight       INTEGER    NOT NULL,
    itemtype     TEXT       NOT NULL,
    effect       TEXT,
    dyestring    TEXT
);

CREATE INDEX tmw_items_type ON tmw_items (itemtype);

-----------------------------------------------------------------------------

CREATE TABLE tmw_item_instances
(
    item_id       INTEGER    PRIMARY KEY,
    itemclass_id  INTEGER    NOT NULL,
    amount        INTEGER    NOT NULL,
    --
    FOREIGN KEY (itemclass_id) REFERENCES tmw_items(id)
);

CREATE INDEX tmw_item_instances_typ ON tmw_item_instances ( itemclass_id );

-----------------------------------------------------------------------------

CREATE TABLE tmw_item_attributes
(
    attribute_id    INTEGER    PRIMARY KEY,
    item_id         INTEGER    NOT NULL,
    attribute_class INTEGER    NOT NULL,
    attribute_value TEXT,
    --
    FOREIGN KEY (item_id) REFERENCES tmw_item_instances(item_id)
);

CREATE INDEX tmw_item_attributes_item ON tmw_item_attributes ( item_id );

-----------------------------------------------------------------------------

-- todo: remove class_id and amount and reference on tmw_item_instances
CREATE TABLE tmw_inventories
(
   id           INTEGER     PRIMARY KEY,
   owner_id     INTEGER     NOT NULL,
   slot         INTEGER     NOT NULL,
   class_id     INTEGER     NOT NULL,
   amount       INTEGER     NOT NULL,
   --
   FOREIGN KEY (owner_id) REFERENCES tmw_characters(id)
);

CREATE INDEX tmw_inventories_owner ON tmw_inventories ( owner_id );

-----------------------------------------------------------------------------

CREATE TABLE tmw_guilds
(
   id           INTEGER     PRIMARY KEY,
   name 		TEXT        NOT NULL UNIQUE
);

-----------------------------------------------------------------------------

CREATE TABLE tmw_guild_members
(
   guild_id     INTEGER     NOT NULL,
   member_id    INTEGER     NOT NULL,
   rights       INTEGER     NOT NULL,
   --
   FOREIGN KEY (guild_id)  REFERENCES tmw_guilds(id),
   FOREIGN KEY (member_id) REFERENCES tmw_characters(id)
);

CREATE INDEX tmw_guild_members_g ON tmw_guild_members ( guild_id );
CREATE INDEX tmw_guild_members_m ON tmw_guild_members ( member_id );

-----------------------------------------------------------------------------

CREATE TABLE tmw_quests
(
   owner_id     INTEGER     NOT NULL,
   name         TEXT        NOT NULL,
   value        TEXT        NOT NULL,
   --
   FOREIGN KEY (owner_id) REFERENCES tmw_characters(id)
);

-----------------------------------------------------------------------------

CREATE TABLE tmw_world_states
(
   state_name   TEXT        PRIMARY KEY,
   map_id       INTEGER     NULL,
   value        TEXT        NULL,
   moddate      INTEGER     NOT NULL
);

-----------------------------------------------------------------------------

CREATE TABLE tmw_auctions
(
   auction_id    INTEGER     PRIMARY KEY,
   auction_state INTEGER     NOT NULL,
   char_id       INTEGER     NOT NULL,
   itemclass_id  INTEGER     NOT NULL,
   amount        INTEGER     NOT NULL,
   start_time    INTEGER     NOT NULL,
   end_time      INTEGER     NOT NULL,
   start_price   INTEGER     NOT NULL,
   min_price     INTEGER,
   buyout_price  INTEGER,
   description   TEXT,
   --
   FOREIGN KEY (char_id) REFERENCES tmw_characters(id)
);

CREATE INDEX tmw_auctions_owner ON tmw_auctions ( char_id );
CREATE INDEX tmw_auctions_state ON tmw_auctions ( auction_state );
CREATE INDEX tmw_auctions_item  ON tmw_auctions ( itemclass_id );

-----------------------------------------------------------------------------

CREATE TABLE tmw_auction_bids
(
   bid_id        INTEGER     PRIMARY KEY,
   auction_id    INTEGER     NOT NULL,
   char_id       INTEGER     NOT NULL,
   bid_time      INTEGER     NOT NULL,
   bid_price     INTEGER     NOT NULL,
   --
   FOREIGN KEY (auction_id) REFERENCES tmw_auctions(auction_id),
   FOREIGN KEY (char_id)    REFERENCES tmw_characters(id)
);

CREATE INDEX tmw_auction_bids_auction ON tmw_auction_bids ( auction_id );
CREATE INDEX tmw_auction_bids_owner   ON tmw_auction_bids ( char_id );

-----------------------------------------------------------------------------

CREATE TABLE tmw_post
(
   letter_id        INTEGER   PRIMARY KEY,
   sender_id        INTEGER   NOT NULL,
   receiver_id      INTEGER   NOT NULL,
   letter_type      INTEGER   NOT NULL,
   expiration_date  INTEGER   NOT NULL,
   sending_date     INTEGER   NOT NULL,
   letter_text      TEXT          NULL,
   --
   FOREIGN KEY (sender_id)   REFERENCES tmw_characters(id),
   FOREIGN KEY (receiver_id) REFERENCES tmw_characters(id)
);

CREATE INDEX tmw_post_sender   ON tmw_post ( sender_id );
CREATE INDEX tmw_post_receiver ON tmw_post ( receiver_id );

-----------------------------------------------------------------------------

CREATE TABLE tmw_post_attachments
(
   attachment_id    INTEGER   PRIMARY KEY,
   letter_id        INTEGER   NOT NULL,
   item_id          INTEGER   NOT NULL,
   --
   FOREIGN KEY (letter_id) REFERENCES tmw_post(letter_id),
   FOREIGN KEY (item_id)   REFERENCES tmw_item_instances(item_id)
);

CREATE INDEX tmw_post_attachments_ltr ON tmw_post_attachments ( letter_id );
CREATE INDEX tmw_post_attachments_itm ON tmw_post_attachments ( item_id );

-----------------------------------------------------------------------------

CREATE TABLE tmw_transaction_codes
(
   id            INTEGER     PRIMARY KEY,
   description   TEXT        NOT NULL,
   category      TEXT        NOT NULL
);

CREATE INDEX tmw_transaction_codes_cat    ON tmw_transaction_codes ( category );

-----------------------------------------------------------------------------

CREATE TABLE tmw_transactions
(
    id          INTEGER     PRIMARY KEY,
    char_id     INTEGER     NOT NULL,
    action      INTEGER     NOT NULL,
    message     TEXT,
    time        INTEGER     NOT NULL,
    --
    FOREIGN KEY (char_id) REFERENCES tmw_characters(id),
    FOREIGN KEY (action)  REFERENCES tmw_transaction_codes(id)
);

CREATE INDEX tmw_transactions_char    ON tmw_transactions ( char_id );
CREATE INDEX tmw_transactions_action  ON tmw_transactions ( action );
CREATE INDEX tmw_transactions_time    ON tmw_transactions ( time );

-----------------------------------------------------------------------------

CREATE TABLE tmw_online_list
(
    char_id     INTEGER     PRIMARY KEY,
    login_date  INTEGER     NOT NULL,
    --
    FOREIGN KEY (char_id) REFERENCES tmw_characters(id)
);

-----------------------------------------------------------------------------
-- Views
-----------------------------------------------------------------------------

-- List all online users

CREATE VIEW tmw_v_online_chars
AS
   SELECT l.char_id    as char_id,
          l.login_date as login_date,
          c.user_id    as user_id,
          c.name       as name,
          c.gender     as gender,
          c.level      as level,
          c.map_id     as map_id
     FROM tmw_online_list l
     JOIN tmw_characters c
       ON l.char_id = c.id;


-- Show all stored transactions

CREATE VIEW tmw_v_transactions
AS
   SELECT t.id           as transaction_id,
          t.time         as transacition_time,
          a.id           as user_id,
          a.username     as username,
          c.id           as char_id,
          c.name         as charname,
          tc.id          as action_id,
          tc.description as action,
          tc.category    as category,
          t.message      as message
     FROM tmw_transactions t
     JOIN tmw_characters c
       ON t.char_id = c.id
     JOIN tmw_accounts a
       ON c.user_id = a.id
     JOIN tmw_transaction_codes tc
       ON t.action = tc.id;

-----------------------------------------------------------------------------
-- Initial data
-----------------------------------------------------------------------------

-- initial world states and database version

INSERT INTO tmw_world_states VALUES('accountserver_startup',NULL,NULL, strftime('%s','now'));
INSERT INTO tmw_world_states VALUES('accountserver_version',NULL,NULL, strftime('%s','now'));
INSERT INTO tmw_world_states VALUES('database_version',     NULL,'6',  strftime('%s','now'));

-- all known transaction codes

INSERT INTO tmw_transaction_codes VALUES (  1, 'Character created',        'Character' );
INSERT INTO tmw_transaction_codes VALUES (  2, 'Character selected',       'Character' );
INSERT INTO tmw_transaction_codes VALUES (  3, 'Character deleted',        'Character' );
INSERT INTO tmw_transaction_codes VALUES (  4, 'Public message sent',      'Chat' );
INSERT INTO tmw_transaction_codes VALUES (  5, 'Public message annouced',  'Chat' );
INSERT INTO tmw_transaction_codes VALUES (  6, 'Private message sent',     'Chat' );
INSERT INTO tmw_transaction_codes VALUES (  7, 'Channel joined',           'Chat' );
INSERT INTO tmw_transaction_codes VALUES (  8, 'Channel kicked',           'Chat' );
INSERT INTO tmw_transaction_codes VALUES (  9, 'Channel MODE',             'Chat' );
INSERT INTO tmw_transaction_codes VALUES ( 10, 'Channel QUIT',             'Chat' );
INSERT INTO tmw_transaction_codes VALUES ( 11, 'Channel LIST',             'Chat' );
INSERT INTO tmw_transaction_codes VALUES ( 12, 'Channel USERLIST',         'Chat' );
INSERT INTO tmw_transaction_codes VALUES ( 13, 'Channel TOPIC',            'Chat' );
INSERT INTO tmw_transaction_codes VALUES ( 14, 'Command BAN',              'Commands' );
INSERT INTO tmw_transaction_codes VALUES ( 15, 'Command DROP',             'Commands' );
INSERT INTO tmw_transaction_codes VALUES ( 16, 'Command ITEM',             'Commands' );
INSERT INTO tmw_transaction_codes VALUES ( 17, 'Command MONEY',            'Commands' );
INSERT INTO tmw_transaction_codes VALUES ( 18, 'Command SETGROUP',         'Commands' );
INSERT INTO tmw_transaction_codes VALUES ( 19, 'Command SPAWN',            'Commands' );
INSERT INTO tmw_transaction_codes VALUES ( 20, 'Command WARP',             'Commands' );
INSERT INTO tmw_transaction_codes VALUES ( 21, 'Item picked up',           'Actions' );
INSERT INTO tmw_transaction_codes VALUES ( 22, 'Item used',                'Actions' );
INSERT INTO tmw_transaction_codes VALUES ( 23, 'Item dropped',             'Actions' );
INSERT INTO tmw_transaction_codes VALUES ( 24, 'Item moved',               'Actions' );
INSERT INTO tmw_transaction_codes VALUES ( 25, 'Target attacked',          'Actions' );
INSERT INTO tmw_transaction_codes VALUES ( 26, 'ACTION Changed',           'Actions' );
INSERT INTO tmw_transaction_codes VALUES ( 27, 'Trade requested',          'Actions' );
INSERT INTO tmw_transaction_codes VALUES ( 28, 'Trade ended',              'Actions' );
INSERT INTO tmw_transaction_codes VALUES ( 29, 'Trade money',              'Actions' );
INSERT INTO tmw_transaction_codes VALUES ( 30, 'Trade items',              'Actions' );
INSERT INTO tmw_transaction_codes VALUES ( 31, 'Attribute increased',      'Character' );
INSERT INTO tmw_transaction_codes VALUES ( 32, 'Attribute decreased',      'Character' );