-
Notifications
You must be signed in to change notification settings - Fork 21
/
databasemanager.cpp
689 lines (593 loc) · 28.1 KB
/
databasemanager.cpp
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
////////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
////////////////////////////////////////////////////////////////////////
// This program 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 3 of the License, or
// (at your option) any later version.
//
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include "enums.h"
#include <iostream>
#include <stack>
#include "databasemanager.h"
#include "tools.h"
#include "ban.h"
#include "configmanager.h"
extern ConfigManager g_config;
bool DatabaseManager::optimizeTables()
{
Database* db = Database::getInstance();
DBQuery query;
switch(db->getDatabaseEngine())
{
case DATABASE_ENGINE_MYSQL:
{
query << "SELECT `TABLE_NAME` FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA` = " << db->escapeString(g_config.getString(ConfigManager::MYSQL_DB)) << " AND `DATA_FREE` > 0;";
DBResult* result = db->storeQuery(query.str());
if(!result)
return false;
do
{
std::string tableName = result->getDataString("TABLE_NAME");
std::cout << "> Optimizing table " << tableName << "..." << std::flush;
query.str("");
query << "OPTIMIZE TABLE `" << tableName << "`;";
if(db->executeQuery(query.str()))
std::cout << " [success]" << std::endl;
else
std::cout << " [failed]" << std::endl;
}
while(result->next());
db->freeResult(result);
break;
}
case DATABASE_ENGINE_SQLITE:
{
if(!db->executeQuery("VACUUM;"))
return false;
std::cout << "> Optimized database." << std::endl;
break;
}
default:
{
std::cout << "> Optimization is not supported for this database engine." << std::endl;
break;
}
}
return true;
}
bool DatabaseManager::triggerExists(const std::string& triggerName)
{
Database* db = Database::getInstance();
DBQuery query;
switch(db->getDatabaseEngine())
{
case DATABASE_ENGINE_MYSQL:
query << "SELECT `name` FROM `sqlite_master` WHERE `type` = 'trigger' AND `name` = " << db->escapeString(triggerName) << ";";
break;
case DATABASE_ENGINE_SQLITE:
query << "SELECT `TRIGGER_NAME` FROM `information_schema`.`TRIGGERS` WHERE `TRIGGER_SCHEMA` = " << db->escapeString(g_config.getString(ConfigManager::SQLITE_DB)) << " AND `TRIGGER_NAME` = " << db->escapeString(triggerName) << ";";
break;
default:
return false;
}
DBResult* result = db->storeQuery(query.str());
if(!result)
return false;
db->freeResult(result);
return true;
}
bool DatabaseManager::tableExists(const std::string& tableName)
{
Database* db = Database::getInstance();
DBQuery query;
switch(db->getDatabaseEngine())
{
case DATABASE_ENGINE_MYSQL:
query << "SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `TABLE_SCHEMA` = " << db->escapeString(g_config.getString(ConfigManager::MYSQL_DB)) << " AND `TABLE_NAME` = " << db->escapeString(tableName) << ";";
break;
case DATABASE_ENGINE_SQLITE:
query << "SELECT `name` FROM `sqlite_master` WHERE `type` = 'table' AND `name` = " << db->escapeString(tableName) << ";";
break;
default:
return false;
}
DBResult* result = db->storeQuery(query.str());
if(!result)
return false;
db->freeResult(result);
return true;
}
bool DatabaseManager::isDatabaseSetup()
{
Database* db = Database::getInstance();
DBQuery query;
switch(db->getDatabaseEngine())
{
case DATABASE_ENGINE_MYSQL:
{
query << "SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `TABLE_SCHEMA` = " << db->escapeString(g_config.getString(ConfigManager::MYSQL_DB)) << ";";
break;
}
case DATABASE_ENGINE_SQLITE:
{
query.str("SELECT `name` FROM `sqlite_master` WHERE `type` = 'table';");
break;
}
default:
return false;
}
DBResult* result = db->storeQuery(query.str());
if(!result)
return false;
db->freeResult(result);
return true;
}
int32_t DatabaseManager::getDatabaseVersion()
{
if(!tableExists("server_config"))
{
Database* db = Database::getInstance();
if(db->getDatabaseEngine() == DATABASE_ENGINE_MYSQL)
db->executeQuery("CREATE TABLE `server_config` (`config` VARCHAR(50) NOT NULL, `value` VARCHAR(256) NOT NULL DEFAULT '', UNIQUE(`config`)) ENGINE = InnoDB;");
else
db->executeQuery("CREATE TABLE `server_config` (`config` VARCHAR(50) NOT NULL, `value` VARCHAR(256) NOT NULL DEFAULT '', UNIQUE(`config`));");
db->executeQuery("INSERT INTO `server_config` VALUES ('db_version', 0);");
return 0;
}
int32_t version = 0;
if(getDatabaseConfig("db_version", version))
return version;
return -1;
}
uint32_t DatabaseManager::updateDatabase()
{
Database* db = Database::getInstance();
DBQuery query;
int32_t databaseVersion = getDatabaseVersion();
if(databaseVersion < 0)
return 0;
switch(databaseVersion)
{
case 0:
{
std::cout << "> Updating database to version 1 (account names)" << std::endl;
if (db->getDatabaseEngine() == DATABASE_ENGINE_MYSQL)
db->executeQuery("ALTER TABLE `accounts` ADD `name` VARCHAR(32) NOT NULL AFTER `id`;");
else
db->executeQuery("ALTER TABLE `accounts` ADD `name` VARCHAR(32) NOT NULL DEFAULT '';");
db->executeQuery("UPDATE `accounts` SET `name` = `id`;");
if (db->getDatabaseEngine() == DATABASE_ENGINE_MYSQL)
db->executeQuery("ALTER TABLE `accounts` ADD UNIQUE (`name`);");
else
db->executeQuery("CREATE UNIQUE INDEX IF NOT EXISTS account_name ON accounts(name);");
registerDatabaseConfig("db_version", 1);
return 1;
}
case 1:
{
std::cout << "> Updating database to version 2 (market offers)" << std::endl;
if(db->getDatabaseEngine() == DATABASE_ENGINE_MYSQL)
{
db->executeQuery("CREATE TABLE `market_offers` (`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `player_id` INT NOT NULL, `sale` TINYINT(1) NOT NULL DEFAULT 0, `itemtype` INT UNSIGNED NOT NULL, `amount` SMALLINT UNSIGNED NOT NULL, `created` BIGINT UNSIGNED NOT NULL, `anonymous` TINYINT(1) NOT NULL DEFAULT 0, `price` INT UNSIGNED NOT NULL DEFAULT 0, PRIMARY KEY (`id`), KEY(`sale`, `itemtype`), KEY(`created`), FOREIGN KEY (`player_id`) REFERENCES `players`(`id`) ON DELETE CASCADE) ENGINE = InnoDB;");
}
else
{
db->executeQuery("CREATE TABLE `market_offers` (`id` INTEGER PRIMARY KEY NOT NULL, `player_id` INTEGER NOT NULL, `sale` BOOLEAN NOT NULL DEFAULT 0, `itemtype` UNSIGNED INTEGER NOT NULL, `amount` UNSIGNED INTEGER NOT NULL, `created` UNSIGNED INTEGER NOT NULL, `anonymous` BOOLEAN NOT NULL DEFAULT 0, `price` UNSIGNED INTEGER NOT NULL DEFAULT 0, FOREIGN KEY (`player_id`) REFERENCES `players` (`id`) ON DELETE CASCADE);");
db->executeQuery("CREATE INDEX market_offers_idx ON market_offers(created);");
db->executeQuery("CREATE INDEX market_offers_idx2 ON market_offers(sale, itemtype);");
}
registerDatabaseConfig("db_version", 2);
return 2;
}
case 2:
{
std::cout << "> Updating database to version 3 (bank balance)" << std::endl;
if(db->getDatabaseEngine() == DATABASE_ENGINE_SQLITE)
db->executeQuery("DROP TRIGGER IF EXISTS `onupdate_players_after`;");
db->executeQuery("ALTER TABLE `players` ADD `balance` BIGINT UNSIGNED NOT NULL DEFAULT 0;");
registerDatabaseConfig("db_version", 3);
return 3;
}
case 3:
{
std::cout << "> Updating database to version 4 (market history)" << std::endl;
if(db->getDatabaseEngine() == DATABASE_ENGINE_MYSQL)
db->executeQuery("CREATE TABLE `market_history` (`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `player_id` INT NOT NULL, `sale` TINYINT(1) NOT NULL DEFAULT 0, `itemtype` INT UNSIGNED NOT NULL, `amount` SMALLINT UNSIGNED NOT NULL, `price` INT UNSIGNED NOT NULL DEFAULT 0, `expires_at` BIGINT UNSIGNED NOT NULL, `inserted` BIGINT UNSIGNED NOT NULL, `state` TINYINT(1) UNSIGNED NOT NULL, PRIMARY KEY(`id`), KEY(`player_id`, `sale`), FOREIGN KEY (`player_id`) REFERENCES `players`(`id`) ON DELETE CASCADE) ENGINE = InnoDB;");
else
{
db->executeQuery("CREATE TABLE `market_history` (`id` INTEGER PRIMARY KEY NOT NULL, `player_id` INTEGER NOT NULL, `sale` BOOLEAN NOT NULL DEFAULT 0, `itemtype` UNSIGNED INTEGER NOT NULL, `amount` UNSIGNED INTEGER NOT NULL, `price` UNSIGNED INTEGER NOT NULL DEFAULT 0, `expires_at` UNSIGNED INTEGER NOT NULL, `inserted` UNSIGNED INTEGER NOT NULL, `state` UNSIGNED INTEGER NOT NULL, FOREIGN KEY (`player_id`) REFERENCES `players` (`id`) ON DELETE CASCADE);");
db->executeQuery("CREATE INDEX market_history_idx ON market_history(player_id, sale);");
}
registerDatabaseConfig("db_version", 4);
return 4;
}
case 4:
{
std::cout << "> Updating database to version 5 (black skull & guild wars)" << std::endl;
if(db->getDatabaseEngine() == DATABASE_ENGINE_MYSQL)
{
db->executeQuery("ALTER TABLE `players` CHANGE `redskull` `skull` TINYINT(1) NOT NULL DEFAULT '0', CHANGE `redskulltime` `skulltime` INT(11) NOT NULL DEFAULT '0';");
db->executeQuery("CREATE TABLE IF NOT EXISTS `guild_wars` ( `id` int(11) NOT NULL AUTO_INCREMENT, `guild1` int(11) NOT NULL DEFAULT '0', `guild2` int(11) NOT NULL DEFAULT '0', `name1` varchar(255) NOT NULL, `name2` varchar(255) NOT NULL, `status` tinyint(2) NOT NULL DEFAULT '0', `started` bigint(15) NOT NULL DEFAULT '0', `ended` bigint(15) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `guild1` (`guild1`), KEY `guild2` (`guild2`)) ENGINE=InnoDB;");
db->executeQuery("CREATE TABLE IF NOT EXISTS `guildwar_kills` (`id` int(11) NOT NULL AUTO_INCREMENT, `killer` varchar(50) NOT NULL, `target` varchar(50) NOT NULL, `killerguild` int(11) NOT NULL DEFAULT '0', `targetguild` int(11) NOT NULL DEFAULT '0', `warid` int(11) NOT NULL DEFAULT '0', `time` bigint(15) NOT NULL, PRIMARY KEY (`id`), KEY `warid` (`warid`), FOREIGN KEY (`warid`) REFERENCES `guild_wars`(`id`) ON DELETE CASCADE) ENGINE=InnoDB;");
}
else
{
db->executeQuery("ALTER TABLE `players` ADD `skull` INTEGER NOT NULL DEFAULT 0;");
db->executeQuery("ALTER TABLE `players` ADD `skulltime` INTEGER NOT NULL DEFAULT 0;");
db->executeQuery("CREATE TABLE IF NOT EXISTS `guild_wars` ( `id` INTEGER PRIMARY KEY NOT NULL, `guild1` INTEGER NOT NULL DEFAULT '0', `guild2` INTEGER NOT NULL DEFAULT '0', `name1` VARCHAR(255) NOT NULL, `name2` VARCHAR(255) NOT NULL, `status` INTEGER NOT NULL DEFAULT '0', `started` INTEGER NOT NULL DEFAULT '0', `ended` INTEGER NOT NULL DEFAULT '0');");
db->executeQuery("CREATE TABLE IF NOT EXISTS `guildwar_kills` (`id` INTEGER PRIMARY KEY NOT NULL, `killer` varchar(50) NOT NULL, `target` varchar(50) NOT NULL, `killerguild` INTEGER NOT NULL DEFAULT '0', `targetguild` INTEGER NOT NULL DEFAULT '0', `warid` INTEGER NOT NULL DEFAULT '0', `time` INTEGER NOT NULL, FOREIGN KEY (`warid`) REFERENCES `guild_wars` (`id`));");
db->executeQuery("CREATE INDEX guild_wars_idx ON guild_wars(guild1);");
db->executeQuery("CREATE INDEX guild_wars_idx2 ON guild_wars(guild2);");
}
registerDatabaseConfig("db_version", 5);
return 5;
}
case 5:
{
std::cout << "> Updating database to version 6 (market bug fix)" << std::endl;
db->executeQuery("DELETE FROM `market_offers` WHERE `amount` = 0;");
registerDatabaseConfig("db_version", 6);
return 6;
}
case 6:
{
std::cout << "> Updating database to version 7 (offline training)" << std::endl;
db->executeQuery("ALTER TABLE `players` ADD `offlinetraining_time` SMALLINT UNSIGNED NOT NULL DEFAULT 43200;");
db->executeQuery("ALTER TABLE `players` ADD `offlinetraining_skill` INT NOT NULL DEFAULT -1;");
registerDatabaseConfig("db_version", 7);
return 7;
}
case 7:
{
if(db->getDatabaseEngine() == DATABASE_ENGINE_MYSQL)
{
std::cout << "> Updating database to version 8 (account viplist with description, icon and notify server side)" << std::endl;
db->executeQuery("RENAME TABLE `player_viplist` TO `account_viplist`;");
db->executeQuery("ALTER TABLE `account_viplist` DROP FOREIGN KEY `account_viplist_ibfk_1`;");
db->executeQuery("UPDATE `account_viplist` SET `player_id` = (SELECT `account_id` FROM `players` WHERE `id` = `player_id`);");
db->executeQuery("ALTER TABLE `account_viplist` CHANGE `player_id` `account_id` INT( 11 ) NOT NULL COMMENT 'id of account whose viplist entry it is';");
db->executeQuery("ALTER TABLE `account_viplist` DROP FOREIGN KEY `account_viplist_ibfk_2`;");
db->executeQuery("ALTER TABLE `account_viplist` CHANGE `vip_id` `player_id` INT( 11 ) NOT NULL COMMENT 'id of target player of viplist entry';");
db->executeQuery("ALTER TABLE `account_viplist` DROP INDEX `player_id`, ADD INDEX `account_id` (`account_id`);");
db->executeQuery("ALTER TABLE `account_viplist` DROP INDEX `vip_id`, ADD INDEX `player_id` (`player_id`);");
db->executeQuery("ALTER TABLE `account_viplist` ADD FOREIGN KEY (`account_id`) REFERENCES `accounts` (`id`) ON DELETE CASCADE;");
db->executeQuery("ALTER TABLE `account_viplist` ADD FOREIGN KEY (`player_id`) REFERENCES `players` (`id`) ON DELETE CASCADE;");
db->executeQuery("ALTER TABLE `account_viplist` ADD `description` VARCHAR(128) NOT NULL DEFAULT '', ADD `icon` TINYINT( 2 ) UNSIGNED NOT NULL DEFAULT '0', ADD `notify` TINYINT( 1 ) NOT NULL DEFAULT '0';");
// Remove duplicates
DBResult* result = db->storeQuery("SELECT `account_id`, `player_id`, COUNT(*) AS `count` FROM `account_viplist` GROUP BY `account_id`, `player_id` HAVING COUNT(*) > 1;");
if(result)
{
do
{
query.str("");
query << "DELETE FROM `account_viplist` WHERE `account_id` = " << result->getDataInt("account_id") << " AND `player_id` = " << result->getDataInt("player_id") << " LIMIT " << (result->getDataInt("count") - 1) << ";";
db->executeQuery(query.str());
}
while(result->next());
db->freeResult(result);
}
// Remove if an account has over 200 entries
result = db->storeQuery("SELECT `account_id`, COUNT(*) AS `count` FROM `account_viplist` GROUP BY `account_id` HAVING COUNT(*) > 200;");
if(result)
{
do
{
query.str("");
query << "DELETE FROM `account_viplist` WHERE `account_id` = " << result->getDataInt("account_id") << " LIMIT " << (result->getDataInt("count") - 200) << ";";
db->executeQuery(query.str());
}
while(result->next());
db->freeResult(result);
}
db->executeQuery("ALTER TABLE `account_viplist` ADD UNIQUE `account_player_index` (`account_id`, `player_id`);");
registerDatabaseConfig("db_version", 8);
return 8;
}
break;
}
case 8:
{
if(db->getDatabaseEngine() == DATABASE_ENGINE_MYSQL)
{
std::cout << "> Updating database to version 9 (global inbox)" << std::endl;
db->executeQuery("CREATE TABLE IF NOT EXISTS `player_inboxitems` (`player_id` int(11) NOT NULL, `sid` int(11) NOT NULL, `pid` int(11) NOT NULL DEFAULT '0', `itemtype` smallint(6) NOT NULL, `count` smallint(5) NOT NULL DEFAULT '0', `attributes` blob NOT NULL, UNIQUE KEY `player_id_2` (`player_id`,`sid`), KEY `player_id` (`player_id`), FOREIGN KEY (`player_id`) REFERENCES `players`(`id`) ON DELETE CASCADE) ENGINE=InnoDB DEFAULT CHARSET=latin1;");
// Delete "market" item
db->executeQuery("DELETE FROM `player_depotitems` WHERE `itemtype` = 14405;");
// Move up items in depot chests
DBResult* result = db->storeQuery("SELECT `player_id`, `pid`, (SELECT `dp2`.`sid` FROM `player_depotitems` AS `dp2` WHERE `dp2`.`player_id` = `dp1`.`player_id` AND `dp2`.`pid` = `dp1`.`sid` AND `itemtype` = 2594) AS `sid` FROM `player_depotitems` AS `dp1` WHERE `itemtype` = 2589;");
if(result)
{
do
{
query.str("");
query << "UPDATE `player_depotitems` SET `pid` = " << result->getDataInt("pid") << " WHERE `player_id` = " << result->getDataInt("player_id") << " AND `pid` = " << result->getDataInt("sid") << ";";
db->executeQuery(query.str());
}
while(result->next());
db->freeResult(result);
}
// Delete the depot lockers
db->executeQuery("DELETE FROM `player_depotitems` WHERE `itemtype` = 2589;");
// Delete the depot chests
db->executeQuery("DELETE FROM `player_depotitems` WHERE `itemtype` = 2594;");
std::ostringstream ss2;
result = db->storeQuery("SELECT DISTINCT `player_id` FROM `player_depotitems` WHERE `itemtype` = 14404;");
if(result)
{
do
{
int32_t runningId = 100;
DBInsert stmt(db);
stmt.setQuery("INSERT INTO `player_inboxitems` (`player_id`, `sid`, `pid`, `itemtype`, `count`, `attributes`) VALUES ");
std::ostringstream sss;
sss << "SELECT `sid` FROM `player_depotitems` WHERE `player_id` = " << result->getDataInt("player_id") << " AND `itemtype` = 14404;";
DBResult* result2 = db->storeQuery(sss.str());
if(result2)
{
do
{
std::stack<int32_t> sids;
sids.push(result2->getDataInt("sid"));
while(!sids.empty())
{
int32_t sid = sids.top();
sids.pop();
std::ostringstream ss;
ss << "SELECT * FROM `player_depotitems` WHERE `player_id` = " << result->getDataInt("player_id") << " AND `pid` = " << sid << ";";
DBResult* result3 = db->storeQuery(ss.str());
if(result3)
{
do
{
unsigned long attrSize = 0;
const char* attr = result3->getDataStream("attributes", attrSize);
ss2 << result->getDataInt("player_id") << "," << ++runningId << ",0," << result3->getDataInt("itemtype") << "," << result3->getDataInt("count") << "," << db->escapeBlob(attr, attrSize);
if(!stmt.addRow(ss2))
std::cout << "Failed to add row!" << std::endl;
sids.push(result3->getDataInt("sid"));
std::ostringstream tmpss;
tmpss << "DELETE FROM `player_depotitems` WHERE `player_id` = " << result->getDataInt("player_id") << " AND `sid` = " << result3->getDataInt("sid") << ";";
db->executeQuery(tmpss.str());
}
while(result3->next());
db->freeResult(result3);
}
}
}
while(result2->next());
db->freeResult(result2);
}
if (!stmt.execute())
std::cout << "Failed to execute statement!" << std::endl;
}
while(result->next());
db->freeResult(result);
}
// Delete the inboxes
db->executeQuery("DELETE FROM `player_depotitems` WHERE `itemtype` = 14404;");
registerDatabaseConfig("db_version", 9);
return 9;
}
break;
}
case 9:
{
std::cout << "> Updating database to version 10 (stamina)" << std::endl;
db->executeQuery("ALTER TABLE `players` ADD `stamina` SMALLINT UNSIGNED NOT NULL DEFAULT 2520;");
registerDatabaseConfig("db_version", 10);
return 10;
}
/*
case ?-1:
{
std::cout << "> Updating database to version ?" << std::endl;
if(db->getDatabaseEngine() == DATABASE_ENGINE_MYSQL)
{
db->executeQuery("CREATE TABLE IF NOT EXISTS `bans2` (`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `type` TINYINT(1) NOT NULL COMMENT 'this field defines if its ip, account, player, or any else ban', `value` INT UNSIGNED NOT NULL COMMENT 'ip, player guid, account number', `param` INT UNSIGNED NOT NULL DEFAULT 4294967295 COMMENT 'mask', `active` TINYINT(1) NOT NULL DEFAULT 1, `expires` INT UNSIGNED NOT NULL DEFAULT 0, `added` INT UNSIGNED NOT NULL, `admin_id` INT UNSIGNED NOT NULL DEFAULT 0, `comment` VARCHAR(1024) NOT NULL DEFAULT '', `reason` INT UNSIGNED NOT NULL DEFAULT 0, `action` INT UNSIGNED NOT NULL DEFAULT 0, `statement` VARCHAR(256) NOT NULL DEFAULT '', PRIMARY KEY (`id`), INDEX `type` (`type`, `value`), INDEX `active` (`active`)) ENGINE = InnoDB;");
}
else
{
db->executeQuery("CREATE TABLE IF NOT EXISTS `bans2` (`id` INTEGER NOT NULL, `type` UNSIGNED INTEGER NOT NULL, `value` UNSIGNED INTEGER NOT NULL, `param` UNSIGNED INTEGER NOT NULL DEFAULT 4294967295, `active` UNSIGNED INTEGER NOT NULL DEFAULT 1, `expires` UNSIGNED INTEGER NOT NULL DEFAULT 0, `added` UNSIGNED INTEGER NOT NULL, `admin_id` UNSIGNED INTEGER NOT NULL DEFAULT 0, `comment` VARCHAR(1024) NOT NULL DEFAULT '', `reason` UNSIGNED INTEGER NOT NULL DEFAULT 0, `action` UNSIGNED INTEGER NOT NULL DEFAULT 0, `statement` VARCHAR(256) NOT NULL DEFAULT '', PRIMARY KEY (`id`));");
db->executeQuery("CREATE INDEX bans_index_type ON bans2(type, value);");
db->executeQuery("CREATE INDEX bans_index_active ON bans2(active);");
}
DBResult* result = db->storeQuery("SELECT * FROM `bans`;");
if(result)
{
do
{
switch(result->getDataInt("type"))
{
case BAN_IPADDRESS:
query << "INSERT INTO `bans2` (`type`, `value`, `param`, `active`, `expires`, `added`, `admin_id`, `comment`, `reason`, `action`) VALUES (1, " << result->getDataInt("ip") << ", " << result->getDataInt("mask") << ", " << (result->getDataInt("time") <= time(NULL) ? 0 : 1) << ", " << result->getDataInt("time") << ", 0, " << result->getDataInt("banned_by") << ", " << db->escapeString(result->getDataString("comment")) << ", " << result->getDataInt("reason_id") << ", " << result->getDataInt("action_id") << ");";
break;
case NAMELOCK_PLAYER:
query << "INSERT INTO `bans2` (`type`, `value`, `active`, `expires`, `added`, `admin_id`, `comment`, `reason`, `action`) VALUES (2, " << result->getDataInt("player") << ", " << (result->getDataInt("time") <= time(NULL) ? 0 : 1) << ", 0, " << result->getDataInt("time") << ", " << result->getDataInt("banned_by") << ", " << db->escapeString(result->getDataString("comment")) << ", " << result->getDataInt("reason_id") << ", " << result->getDataInt("action_id") << ");";
break;
case BAN_ACCOUNT:
query << "INSERT INTO `bans2` (`type`, `value`, `active`, `expires`, `added`, `admin_id`, `comment`, `reason`, `action`) VALUES (3, " << result->getDataInt("player") << ", " << (result->getDataInt("time") <= time(NULL) ? 0 : 1) << ", " << result->getDataInt("time") << ", 0, " << result->getDataInt("banned_by") << ", " << db->escapeString(result->getDataString("comment")) << ", " << result->getDataInt("reason_id") << ", " << result->getDataInt("action_id") << ");";
break;
case NOTATION_ACCOUNT:
case DELETE_ACCOUNT:
query << "INSERT INTO `bans2` (`type`, `value`, `active`, `expires`, `added`, `admin_id`, `comment`, `reason`, `action`) VALUES (" << result->getDataInt("type") << ", " << result->getDataInt("player") << ", " << (result->getDataInt("time") <= time(NULL) ? 0 : 1) << ", 0, " << result->getDataInt("time") << ", " << result->getDataInt("banned_by") << ", " << db->escapeString(result->getDataString("comment")) << ", " << result->getDataInt("reason_id") << ", " << result->getDataInt("action_id") << ");";
break;
}
}
while(result->next());
db->freeResult(result);
}
db->executeQuery("DROP TABLE `bans`;");
if(db->getDatabaseEngine() == DATABASE_ENGINE_MYSQL)
db->executeQuery("RENAME TABLE `bans2` TO `bans`;");
else
db->executeQuery("ALTER TABLE `bans2` RENAME TO `bans`;");
registerDatabaseConfig("db_version", ?);
return ?;
}
*/
default: break;
}
return 0;
}
bool DatabaseManager::getDatabaseConfig(const std::string& config, int32_t &value)
{
Database* db = Database::getInstance();
DBQuery query;
query << "SELECT `value` FROM `server_config` WHERE `config` = " << db->escapeString(config) << ";";
DBResult* result = db->storeQuery(query.str());
if(!result)
return false;
value = atoi(result->getDataString("value").c_str());
db->freeResult(result);
return true;
}
bool DatabaseManager::getDatabaseConfig(const std::string& config, std::string& value)
{
Database* db = Database::getInstance();
DBQuery query;
query << "SELECT `value` FROM `server_config` WHERE `config` = " << db->escapeString(config) << ";";
DBResult* result = db->storeQuery(query.str());
if(!result)
return false;
value = result->getDataString("value");
db->freeResult(result);
return true;
}
void DatabaseManager::registerDatabaseConfig(const std::string& config, int32_t value)
{
Database* db = Database::getInstance();
DBQuery query;
int32_t tmp;
if(!getDatabaseConfig(config, tmp))
query << "INSERT INTO `server_config` VALUES (" << db->escapeString(config) << ", '" << value << "');";
else
query << "UPDATE `server_config` SET `value` = '" << value << "' WHERE `config` = " << db->escapeString(config) << ";";
db->executeQuery(query.str());
}
void DatabaseManager::registerDatabaseConfig(const std::string& config, const std::string& value)
{
Database* db = Database::getInstance();
DBQuery query;
std::string tmp;
if(!getDatabaseConfig(config, tmp))
query << "INSERT INTO `server_config` VALUES (" << db->escapeString(config) << ", " << db->escapeString(value) << ");";
else
query << "UPDATE `server_config` SET `value` = " << db->escapeString(value) << " WHERE `config` = " << db->escapeString(config) << ";";
db->executeQuery(query.str());
}
void DatabaseManager::checkEncryption()
{
int32_t currentValue = g_config.getNumber(ConfigManager::PASSWORD_TYPE);
int32_t oldValue = 0;
if(getDatabaseConfig("encryption", oldValue))
{
if(currentValue == oldValue)
return;
if(oldValue != PASSWORD_TYPE_PLAIN)
{
std::string oldName;
if(oldValue == PASSWORD_TYPE_MD5)
oldName = "md5";
else if(oldValue == PASSWORD_TYPE_SHA1)
oldName = "sha1";
else
oldName = "plain";
g_config.setNumber(ConfigManager::PASSWORD_TYPE, oldValue);
std::cout << "> WARNING: Unsupported password hashing switch! Change back passwordType in config.lua to \"" << oldName << "\"!" << std::endl;
return;
}
switch(currentValue)
{
case PASSWORD_TYPE_MD5:
{
Database* db = Database::getInstance();
DBQuery query;
if(db->getDatabaseEngine() != DATABASE_ENGINE_MYSQL)
{
DBResult* result = db->storeQuery("SELECT `id`, `password`, `key` FROM `accounts`;");
if(result)
{
do
{
query << "UPDATE `accounts` SET `password` = " << db->escapeString(transformToMD5(result->getDataString("password"))) << ", `key` = " << db->escapeString(transformToMD5(result->getDataString("key"))) << " WHERE `id` = " << result->getDataInt("id") << ";";
db->executeQuery(query.str());
}
while(result->next());
db->freeResult(result);
}
}
else
db->executeQuery("UPDATE `accounts` SET `password` = MD5(`password`), `key` = MD5(`key`);");
std::cout << "> Password type has been updated to MD5." << std::endl;
break;
}
case PASSWORD_TYPE_SHA1:
{
Database* db = Database::getInstance();
DBQuery query;
if(db->getDatabaseEngine() != DATABASE_ENGINE_MYSQL)
{
DBResult* result = db->storeQuery("SELECT `id`, `password`, `key` FROM `accounts`;");
if(result)
{
do
{
query << "UPDATE `accounts` SET `password` = " << db->escapeString(transformToSHA1(result->getDataString("password"))) << ", `key` = " << db->escapeString(transformToSHA1(result->getDataString("key"))) << " WHERE `id` = " << result->getDataInt("id") << ";";
db->executeQuery(query.str());
}
while(result->next());
db->freeResult(result);
}
}
else
db->executeQuery("UPDATE `accounts` SET `password` = SHA1(`password`), `key` = SHA1(`key`);");
std::cout << "> Password type has been updated to SHA1." << std::endl;
break;
}
default: break;
}
}
else if(g_config.getBoolean(ConfigManager::ACCOUNT_MANAGER))
{
switch(currentValue)
{
case PASSWORD_TYPE_MD5:
{
Database* db = Database::getInstance();
DBQuery query;
query << "UPDATE `accounts` SET `password` = " << db->escapeString(transformToMD5("1")) << " WHERE `id` = 1 AND `password` = '1';";
db->executeQuery(query.str());
break;
}
case PASSWORD_TYPE_SHA1:
{
Database* db = Database::getInstance();
DBQuery query;
query << "UPDATE `accounts` SET `password` = " << db->escapeString(transformToSHA1("1")) << " WHERE `id` = 1 AND `password` = '1';";
db->executeQuery(query.str());
break;
}
default: break;
}
}
registerDatabaseConfig("encryption", currentValue);
}
void DatabaseManager::checkTriggers()
{
/*
Database* db = Database::getInstance();
switch(db->getDatabaseEngine())
{
}
*/
}