summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorskotlex <skotlex@54d463be-8e91-2dee-dedb-b68131a5f0ec>2006-02-16 20:15:21 +0000
committerskotlex <skotlex@54d463be-8e91-2dee-dedb-b68131a5f0ec>2006-02-16 20:15:21 +0000
commit791b3307964a57345bdf45cb2d4d7c51881b66ff (patch)
treee3f3a0324abb14aab20e760e43a0076539653593 /src
parent78598d7156e3dee4663a4e5372b107257d0488f6 (diff)
downloadhercules-791b3307964a57345bdf45cb2d4d7c51881b66ff.tar.gz
hercules-791b3307964a57345bdf45cb2d4d7c51881b66ff.tar.bz2
hercules-791b3307964a57345bdf45cb2d4d7c51881b66ff.tar.xz
hercules-791b3307964a57345bdf45cb2d4d7c51881b66ff.zip
- Updated the login/char sql servers to let the sql tables autogenerate the new id, and then retrieve said id using mysql_insert_id rather than playing smart and loading the next insert ID on startup.
- Tables affected are: account creation, char creation, party creation, pet creation and guild creation. - Also corrected some functions to be of type (void) rather than () git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@5298 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src')
-rw-r--r--src/char_sql/char.c112
-rw-r--r--src/char_sql/int_guild.c104
-rw-r--r--src/char_sql/int_guild.h4
-rw-r--r--src/char_sql/int_party.c87
-rw-r--r--src/char_sql/int_party.h4
-rw-r--r--src/char_sql/int_pet.c83
-rw-r--r--src/char_sql/int_pet.h8
-rw-r--r--src/char_sql/int_storage.c4
-rw-r--r--src/char_sql/int_storage.h4
-rw-r--r--src/char_sql/inter.c6
-rw-r--r--src/char_sql/inter.h4
-rw-r--r--src/login_sql/login.c55
12 files changed, 132 insertions, 343 deletions
diff --git a/src/char_sql/char.c b/src/char_sql/char.c
index 0d5f6eca8..7d3ddf008 100644
--- a/src/char_sql/char.c
+++ b/src/char_sql/char.c
@@ -127,7 +127,6 @@ int auth_fifo_pos = 0;
int check_ip_flag = 1; // It's to check IP of a player between char-server and other servers (part of anti-hacking system)
-int char_id_count = START_CHAR_NUM;
struct mmo_charstatus *char_dat;
int char_num,char_max;
int max_connect_user = 0;
@@ -1213,9 +1212,6 @@ int mmo_char_fromsql_short(int char_id, struct mmo_charstatus *p){
}
//==========================================================================================================
int mmo_char_sql_init(void) {
- int charcount;
-
-
ShowInfo("Begin Initializing.......\n");
char_db_= db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA, sizeof(int));
// memory initialize
@@ -1225,44 +1221,11 @@ int mmo_char_sql_init(void) {
CREATE(char_dat, struct mmo_charstatus, 2);
memset(char_dat, 0, sizeof(struct mmo_charstatus)*2);
-
- //Check for max id (in case new chars would get their IDs set below 150K) [Skotlex]
- sprintf(tmp_sql , "SELECT max(`char_id`) FROM `%s`", char_db);
- if (mysql_query(&mysql_handle, tmp_sql)) {
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- } else {
- sql_res = mysql_store_result(&mysql_handle);
- if (sql_res)
- {
- if (mysql_num_rows(sql_res) > 0 &&
- (sql_row = mysql_fetch_row(sql_res)) != NULL &&
- sql_row[0] != NULL && atoi(sql_row[0]) >= char_id_count)
- char_id_count = 0; //No need for setting the char id.
- mysql_free_result(sql_res);
- }
- }
-
- sprintf(tmp_sql, "SELECT `char_id` FROM `%s`", char_db);
- if(mysql_query(&mysql_handle, tmp_sql)){
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- }else{
- sql_res = mysql_store_result(&mysql_handle);
- if(sql_res){
- charcount = (int)mysql_num_rows(sql_res);
- ShowStatus("total char data -> '%d'.......\n", charcount);
- mysql_free_result(sql_res);
- }else{
- ShowStatus("total char data -> '0'.......\n");
- }
- }
-
if(char_per_account == 0){
ShowStatus("Chars per Account: 'Unlimited'.......\n");
- }else{
- ShowStatus("Chars per Account: '%d'.......\n", char_per_account);
- }
+ }else{
+ ShowStatus("Chars per Account: '%d'.......\n", char_per_account);
+ }
//the 'set offline' part is now in check_login_conn ...
//if the server connects to loginserver
@@ -1440,15 +1403,7 @@ int make_new_char_sql(int fd, unsigned char *dat) {
//New Querys [Sirius]
//Insert the char to the 'chardb' ^^
- if (char_id_count) //Force initial char id. [Skotlex]
- sprintf(tmp_sql, "INSERT INTO `%s` (`char_id`,`account_id`, `char_num`, `name`, `zeny`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `max_hp`, `hp`,"
- "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`) VALUES ("
- "'%d', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d','%d', '%d','%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d')",
- char_db, char_id_count, sd->account_id , dat[30] , t_name, start_zeny, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29],
- (40 * (100 + dat[26])/100) , (40 * (100 + dat[26])/100 ), (11 * (100 + dat[27])/100), (11 * (100 + dat[27])/100), dat[33], dat[31],
- mapindex_id2name(start_point.map), start_point.x, start_point.y, mapindex_id2name(start_point.map), start_point.x, start_point.y);
- else
- sprintf(tmp_sql, "INSERT INTO `%s` (`account_id`, `char_num`, `name`, `zeny`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `max_hp`, `hp`,"
+ sprintf(tmp_sql, "INSERT INTO `%s` (`account_id`, `char_num`, `name`, `zeny`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `max_hp`, `hp`,"
"`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`) VALUES ("
"'%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d','%d', '%d','%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d')",
char_db, sd->account_id , dat[30] , t_name, start_zeny, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29],
@@ -1459,51 +1414,19 @@ int make_new_char_sql(int fd, unsigned char *dat) {
ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
return -2; //No, stop the procedure!
}
-
- if (char_id_count) //Clear this out for future inserts.
- char_id_count = 0;
-
//Now we need the charid from sql!
- sprintf(tmp_sql, "SELECT `char_id` FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' AND `name` = '%s'", char_db, sd->account_id , dat[30] , t_name);
- if(mysql_query(&mysql_handle, tmp_sql)){
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- //delete the char ..(no trash in DB!)
+ if(mysql_field_count(&mysql_handle) == 0 &&
+ mysql_insert_id(&mysql_handle) > 0)
+ char_id = mysql_insert_id(&mysql_handle);
+ else {
+ //delete the char ..(no trash in DB!) but how is this possible?
sprintf(tmp_sql, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' AND `name` = '%s'", char_db, sd->account_id, dat[30], t_name);
if(mysql_query(&mysql_handle, tmp_sql)){
ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
}
return -2; //XD end of the (World? :P) .. charcreate (denied)
- } else {
- //query ok -> get the data!
- sql_res = mysql_store_result(&mysql_handle);
- if(sql_res){
- sql_row = mysql_fetch_row(sql_res);
- char_id = sql_row?atoi(sql_row[0]):0; //char id :)
- mysql_free_result(sql_res);
- if(char_id <= 0){
- ShowError("failed (get char id..) CHARID (%d) wrong!\n", char_id);
- sprintf(tmp_sql, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' AND `name` = '%s'", char_db, sd->account_id, dat[30], t_name);
- if(mysql_query(&mysql_handle, tmp_sql)){
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- }
- return -2; //charcreate denied ..
- }
- }else{
- //prevent to crash (if its false, and we want to free -> segfault :)
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- sprintf(tmp_sql, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' AND `name` = '%s'", char_db, sd->account_id, dat[30], t_name);
- if(mysql_query(&mysql_handle, tmp_sql)){
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- }
- return -2; //end ...... -> charcreate failed :)
- }
}
-
//Give the char the default items
//`inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`)
if (start_weapon > 0) { //add Start Weapon (Knife?)
@@ -1511,12 +1434,6 @@ int make_new_char_sql(int fd, unsigned char *dat) {
if (mysql_query(&mysql_handle, tmp_sql)){
ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- sprintf(tmp_sql, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' AND `name` = '%s'", char_db, sd->account_id, dat[30], t_name);
- if(mysql_query(&mysql_handle, tmp_sql)){
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- }
- return -2;//end XD
}
}
if (start_armor > 0) { //Add default armor (cotton shirt?)
@@ -1524,17 +1441,6 @@ int make_new_char_sql(int fd, unsigned char *dat) {
if (mysql_query(&mysql_handle, tmp_sql)){
ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- sprintf(tmp_sql, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' AND `name` = '%s'", char_db, sd->account_id, dat[30], t_name);
- if(mysql_query(&mysql_handle, tmp_sql)){
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- }
- sprintf(tmp_sql, "DELETE FROM `%s` WHERE `char_id` = '%d'", inventory_db, char_id);
- if(mysql_query(&mysql_handle, tmp_sql)){
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- }
- return -2; //end....
}
}
diff --git a/src/char_sql/int_guild.c b/src/char_sql/int_guild.c
index 6e1f24ed6..a8259bb38 100644
--- a/src/char_sql/int_guild.c
+++ b/src/char_sql/int_guild.c
@@ -24,8 +24,6 @@ static struct dbt *guild_db_;
struct guild_castle castles[MAX_GUILDCASTLE];
-static int guild_newid=30000;
-
static int guild_exp[100];
#define GS_BASIC 0x01
@@ -113,7 +111,7 @@ int inter_guild_tosql(struct guild *g,int flag)
char emblem_data[4096];
int i=0;
- if (g->guild_id<=0) return -1;
+ if (g->guild_id<=0 && g->guild_id != -1) return 0;
#ifdef NOISY
ShowInfo("Save guild request ("CL_BOLD"%d"CL_RESET" - flag 0x%x).",g->guild_id, flag);
@@ -123,33 +121,25 @@ int inter_guild_tosql(struct guild *g,int flag)
t_info[0]='\0';
// Insert new guild to sqlserver
- if (flag&GS_BASIC){
+ if (flag&GS_BASIC || g->guild_id == -1){
int len=0;
- char updateflag=1;
strcat(t_info, " guild");
-
- // Check if the guild exists.
- sprintf(tmp_sql,"SELECT guild_id FROM `%s` WHERE guild_id='%d'",guild_db, g->guild_id);
- if(mysql_query(&mysql_handle, tmp_sql) ) {
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- updateflag = 0; //Assume insert?
- } else {
- sql_res = mysql_store_result(&mysql_handle) ;
- if (sql_res==NULL || mysql_num_rows(sql_res)<=0) { //Guild does not exists
- updateflag = 0;
- }
- mysql_free_result(sql_res); //Don't need it anymore...
- }
-
- //printf("- Insert guild %d to guild\n",g->guild_id);
+
for(i=0;i<g->emblem_len;i++){
len+=sprintf(emblem_data+len,"%02x",(unsigned char)(g->emblem_data[i]));
//printf("%02x",(unsigned char)(g->emblem_data[i]));
}
emblem_data[len] = '\0';
//printf("- emblem_len = %d \n",g->emblem_len);
- if (updateflag) {
+ if (g->guild_id == -1) //Insert
+ sprintf(tmp_sql,"INSERT INTO `%s` "
+ "(`name`,`master`,`guild_lv`,`connect_member`,`max_member`,`average_lv`,`exp`,`next_exp`,`skill_point`,`mes1`,`mes2`,`emblem_len`,`emblem_id`,`emblem_data`,`char_id`) "
+ "VALUES ('%s', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s', '%s', '%d', '%d', '%s', '%d')",
+ guild_db,t_name,jstrescapecpy(t_master,g->master),
+ g->guild_lv,g->connect_member,g->max_member,g->average_lv,g->exp,g->next_exp,g->skill_point,
+ jstrescapecpy(t_mes1,g->mes1),jstrescapecpy(t_mes2,g->mes2),g->emblem_len,g->emblem_id,emblem_data,
+ g->member[0].char_id);
+ else //Update
sprintf(tmp_sql,"UPDATE `%s` SET"
" `guild_id`=%d, `name`='%s', `master`='%s',`guild_lv`=%d, `connect_member`=%d,`max_member`=%d, "
"`average_lv`=%d,`exp`=%d,`next_exp`=%d,`skill_point`=%d,`mes1`='%s',`mes2`='%s',"
@@ -158,22 +148,18 @@ int inter_guild_tosql(struct guild *g,int flag)
g->guild_lv,g->connect_member,g->max_member,g->average_lv,g->exp,g->next_exp,g->skill_point,
jstrescapecpy(t_mes1,g->mes1),jstrescapecpy(t_mes2,g->mes2),g->emblem_len,g->emblem_id,emblem_data,
g->member[0].char_id, g->guild_id);
- //printf(" %s\n",tmp_sql);
-
- } else {
- sprintf(tmp_sql,"INSERT INTO `%s` "
- "(`guild_id`, `name`,`master`,`guild_lv`,`connect_member`,`max_member`,`average_lv`,`exp`,`next_exp`,`skill_point`,`mes1`,`mes2`,`emblem_len`,`emblem_id`,`emblem_data`,`char_id`) "
- "VALUES ('%d', '%s', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s', '%s', '%d', '%d', '%s', '%d')",
- guild_db, g->guild_id,t_name,jstrescapecpy(t_master,g->master),
- g->guild_lv,g->connect_member,g->max_member,g->average_lv,g->exp,g->next_exp,g->skill_point,
- jstrescapecpy(t_mes1,g->mes1),jstrescapecpy(t_mes2,g->mes2),g->emblem_len,g->emblem_id,emblem_data,
- g->member[0].char_id);
- //printf(" %s\n",tmp_sql);
-
- }
+
if(mysql_query(&mysql_handle, tmp_sql) ) {
ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
+ if (g->guild_id == -1)
+ return 0; //Failed to create guild!
+ } else if (g->guild_id == -1) { //New guild
+ if(mysql_field_count(&mysql_handle) == 0 &&
+ mysql_insert_id(&mysql_handle) != 0)
+ g->guild_id = mysql_insert_id(&mysql_handle);
+ else
+ return 0; //Failed to get ID??
}
}
@@ -306,7 +292,7 @@ int inter_guild_tosql(struct guild *g,int flag)
if (save_log)
ShowInfo("Saved guild (%d - %s):%s\n",g->guild_id,g->name,t_info);
- return 0;
+ return 1;
}
// Read guild from sql
@@ -632,7 +618,7 @@ int inter_guildcastle_fromsql(int castle_id,struct guild_castle *gc)
// Read exp_guild.txt
-int inter_guild_ReadEXP()
+int inter_guild_ReadEXP(void)
{
int i;
FILE *fp;
@@ -747,34 +733,13 @@ int inter_guild_CharOffline(int char_id, int guild_id) {
}
// Initialize guild sql
-int inter_guild_sql_init()
+int inter_guild_sql_init(void)
{
- int i;
-
//Initialize the guild cache
guild_db_= db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));
//Read exp file
inter_guild_ReadEXP();
-
- //Set the new guild ID
- sprintf (tmp_sql , "SELECT max(`guild_id`) FROM `%s`",guild_db);
- if(mysql_query(&mysql_handle, tmp_sql) ) {
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- exit(0);
- }
-
- sql_res = mysql_store_result(&mysql_handle) ;
- if(sql_res) {
- sql_row = mysql_fetch_row(sql_res);
- if(sql_row[0]) {
- if((i = atoi(sql_row[0])) != 0) {
- guild_newid = i+1;
- }
- }
- mysql_free_result(sql_res);
- }
add_timer_func_list(guild_save_timer, "guild_save_timer");
add_timer(gettick() + 10000, guild_save_timer, 0, 0);
@@ -791,7 +756,7 @@ static int guild_db_final(DBKey key, void *data, va_list ap)
return 0;
}
-void inter_guild_sql_final()
+void inter_guild_sql_final(void)
{
guild_db_->destroy(guild_db_, guild_db_final);
return;
@@ -1226,13 +1191,7 @@ int mapif_parse_CreateGuild(int fd,int account_id,char *name,struct guild_member
}
g = (struct guild *)aMalloc(sizeof(struct guild));
memset(g,0,sizeof(struct guild));
- g->guild_id=guild_newid++;
- if (inter_guild_fromsql(g->guild_id) != NULL) {
- ShowWarning("mapif_parse_CreateGuild: New Guild ID [%d] already exists!\n", g->guild_id);
- mapif_guild_created(fd,account_id,NULL);
- aFree(g);
- return 0;
- }
+
memcpy(g->name,name,NAME_LENGTH);
memcpy(g->master,master->name,NAME_LENGTH);
memcpy(&g->member[0],master,sizeof(struct guild_member));
@@ -1248,10 +1207,17 @@ int mapif_parse_CreateGuild(int fd,int account_id,char *name,struct guild_member
g->average_lv=master->lv;
for(i=0;i<MAX_GUILDSKILL;i++)
g->skill[i].id=i + GD_SKILLBASE;
- //Add to cache
+ g->guild_id= -1; //Request to create guild.
+ if (!inter_guild_tosql(g,GS_BASIC|GS_POSITION|GS_SKILL)) {
+ //Failed to Create guild....
+ ShowError("Failed to create Guild %s (Guild Master: %s)\n", g->name, g->master);
+ mapif_guild_created(fd,account_id,NULL);
+ aFree(g);
+ return 0;
+ }
ShowInfo("Created Guild %d - %s (Guild Master: %s)\n", g->guild_id, g->name, g->master);
+ //Add to cache
idb_put(guild_db_, g->guild_id, g);
- inter_guild_tosql(g,GS_BASIC|GS_POSITION|GS_SKILL); //Better save the whole guild right now.
// Report to client
mapif_guild_created(fd,account_id,g);
diff --git a/src/char_sql/int_guild.h b/src/char_sql/int_guild.h
index 452d55612..6faf4e2b1 100644
--- a/src/char_sql/int_guild.h
+++ b/src/char_sql/int_guild.h
@@ -5,8 +5,8 @@
#define _INT_GUILD_H_
int inter_guild_parse_frommap(int fd);
-int inter_guild_sql_init();
-void inter_guild_sql_final();
+int inter_guild_sql_init(void);
+void inter_guild_sql_final(void);
int inter_guild_mapif_init(int fd);
int inter_guild_leave(int guild_id,int account_id,int char_id);
int mapif_parse_BreakGuild(int fd,int guild_id);
diff --git a/src/char_sql/int_party.c b/src/char_sql/int_party.c
index 0479df848..e4161373f 100644
--- a/src/char_sql/int_party.c
+++ b/src/char_sql/int_party.c
@@ -14,7 +14,6 @@
#include "../common/showmsg.h"
static struct party *party_pt;
-static int party_newid = 100;
static struct dbt *party_db_;
int mapif_party_broken(int party_id,int flag);
@@ -46,15 +45,15 @@ int mapif_parse_PartyLeave(int fd, int party_id, int account_id, int char_id);
#define PS_BREAK 0x20
// Save party to mysql
-int inter_party_tosql(int party_id,struct party *p, int flag, int index)
+int inter_party_tosql(struct party *p, int flag, int index)
{
// 'party' ('party_id','name','exp','item','leader_id','leader_char')
char t_name[NAME_LENGTH*2]; //Required for jstrescapecpy [Skotlex]
- int party_exist = 0;
- if (p == NULL || party_id == 0 || p->party_id == 0 || party_id != p->party_id) {
- ShowError("Party pointer or party_id error (id: %d)\n", party_id);
+ int party_id;
+ if (p == NULL || p->party_id == 0)
return 0;
- }
+ party_id = p->party_id;
+
#ifdef NOISY
ShowInfo("Save party request ("CL_BOLD"%d"CL_RESET" - %s).\n", party_id, p->name);
#endif
@@ -77,30 +76,21 @@ int inter_party_tosql(int party_id,struct party *p, int flag, int index)
return 1;
}
- if(flag&PS_CREATE) {
- //Create party, first check if ID exists.
- sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `party_id`='%d'", party_db, party_id); // TBR
+ if(flag&PS_CREATE) { //Create party
+ sprintf(tmp_sql, "INSERT INTO `%s` "
+ "(`name`, `exp`, `item`, `leader_id`, `leader_char`) "
+ "VALUES ('%s', '%d', '%d', '%d', '%d')",
+ party_db, t_name, p->exp, p->item, p->member[index].account_id, p->member[index].char_id);
if (mysql_query(&mysql_handle, tmp_sql)) {
ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- return 0;
- }
- sql_res = mysql_store_result(&mysql_handle);
- if (sql_res != NULL && mysql_num_rows(sql_res) > 0) {
- sql_row = mysql_fetch_row(sql_res);
- party_exist = atoi (sql_row[0]);
- }
- mysql_free_result(sql_res);
- if (party_exist) { //TODO: Can't we just use an index, and then retrieve the new party's index from SQL? [Skotlex]
- ShowError("inter_party_tosql: Creating party with already existing ID %d!\n", party_id);
aFree(p); //Free party, couldn't create it.
return 0;
}
- sprintf(tmp_sql, "INSERT INTO `%s` (`party_id`, `name`, `exp`, `item`, `leader_id`, `leader_char`) VALUES ('%d', '%s', '%d', '%d', '%d', '%d')",
- party_db, party_id, t_name, p->exp, p->item, p->member[index].account_id, p->member[index].char_id);
- if (mysql_query(&mysql_handle, tmp_sql)) {
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
+ if(mysql_field_count(&mysql_handle) == 0 &&
+ mysql_insert_id(&mysql_handle) != 0)
+ party_id = p->party_id = mysql_insert_id(&mysql_handle);
+ else { //Failed to retrieve ID??
aFree(p); //Free party, couldn't create it.
return 0;
}
@@ -227,9 +217,7 @@ struct party *inter_party_fromsql(int party_id)
return p;
}
-int inter_party_sql_init(){
- int i;
-
+int inter_party_sql_init(void){
//memory alloc
party_db_ = db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));
party_pt = (struct party*)aCalloc(sizeof(struct party), 1);
@@ -237,33 +225,6 @@ int inter_party_sql_init(){
ShowFatalError("inter_party_sql_init: Out of Memory!\n");
exit(1);
}
- sprintf (tmp_sql , "SELECT count(*) FROM `%s`", party_db);
- if (mysql_query(&mysql_handle, tmp_sql)) {
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- }
- sql_res = mysql_store_result(&mysql_handle) ;
- sql_row = mysql_fetch_row(sql_res);
- ShowStatus("total party data -> '%s'.......\n",sql_row[0]);
- i = atoi (sql_row[0]);
- mysql_free_result(sql_res);
-
- if (i > 0) {
- //set party_newid
- sprintf (tmp_sql , "SELECT max(`party_id`) FROM `%s`", party_db);
- if(mysql_query(&mysql_handle, tmp_sql)) {
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- }
-
- sql_res = mysql_store_result(&mysql_handle) ;
-
- sql_row = mysql_fetch_row(sql_res);
- party_newid = atoi (sql_row[0])+1;
- mysql_free_result(sql_res);
- }
-
- ShowDebug("set party_newid: %d.......\n", party_newid);
/* Uncomment the following if you want to do a party_db cleanup (remove parties with no members) on startup.[Skotlex]
ShowStatus("cleaning party table...\n");
@@ -278,7 +239,7 @@ int inter_party_sql_init(){
return 0;
}
-void inter_party_sql_final()
+void inter_party_sql_final(void)
{
party_db_->destroy(party_db_, NULL);
aFree(party_pt);
@@ -355,7 +316,7 @@ int party_check_empty(struct party *p)
}
// If there is no member, then break the party
mapif_party_broken(p->party_id,0);
- inter_party_tosql(p->party_id,p, PS_BREAK, 0);
+ inter_party_tosql(p, PS_BREAK, 0);
return 1;
}
@@ -539,7 +500,6 @@ int mapif_parse_CreateParty(int fd, int account_id, int char_id, char *name, cha
}
p= aCalloc(1, sizeof(struct party));
- p->party_id=party_newid++;
memcpy(p->name,name,NAME_LENGTH);
p->exp=0;
p->item=(item?1:0)|(item2?2:0);
@@ -553,7 +513,8 @@ int mapif_parse_CreateParty(int fd, int account_id, int char_id, char *name, cha
p->member[0].online=1;
p->member[0].lv=lv;
- if (inter_party_tosql(p->party_id,p,PS_CREATE|PS_ADDMEMBER,0)) {
+ p->party_id=-1;//New party.
+ if (inter_party_tosql(p,PS_CREATE|PS_ADDMEMBER,0)) {
mapif_party_created(fd,account_id,char_id,p);
mapif_party_info(fd,p);
} else //Failed to create party.
@@ -606,7 +567,7 @@ int mapif_parse_PartyAddMember(int fd, int party_id, int account_id, int char_id
if(flag)
mapif_party_optionchanged(fd,p,0,0);
- inter_party_tosql(party_id, p, PS_ADDMEMBER, i);
+ inter_party_tosql(p, PS_ADDMEMBER, i);
return 0;
}
}
@@ -630,7 +591,7 @@ int mapif_parse_PartyChangeOption(int fd,int party_id,int account_id,int exp,int
p->exp=0;
}
mapif_party_optionchanged(fd,p,account_id,flag);
- inter_party_tosql(party_id, p, PS_BASIC, 0);
+ inter_party_tosql(p, PS_BASIC, 0);
return 0;
}
// パーティ脱退要求
@@ -669,7 +630,7 @@ int mapif_parse_PartyLeave(int fd, int party_id, int account_id, int char_id)
}
//Party gets deleted on the check_empty call below.
} else {
- inter_party_tosql(party_id,p,PS_DELMEMBER,i);
+ inter_party_tosql(p,PS_DELMEMBER,i);
memset(&p->member[i], 0, sizeof(struct party_member));
}
@@ -714,7 +675,7 @@ int mapif_parse_BreakParty(int fd,int party_id)
if(!p)
return 0;
- inter_party_tosql(party_id,p,PS_BREAK,0);
+ inter_party_tosql(p,PS_BREAK,0);
mapif_party_broken(fd,party_id);
return 0;
}
@@ -745,7 +706,7 @@ int mapif_parse_PartyLeaderChange(int fd,int party_id,int account_id,int char_id
p->member[i].leader = 0;
if(p->member[i].account_id == account_id && p->member[i].char_id == char_id) {
p->member[i].leader = 1;
- inter_party_tosql(party_id,p,PS_LEADER, i);
+ inter_party_tosql(p,PS_LEADER, i);
}
}
return 1;
diff --git a/src/char_sql/int_party.h b/src/char_sql/int_party.h
index 353787a2d..902739232 100644
--- a/src/char_sql/int_party.h
+++ b/src/char_sql/int_party.h
@@ -5,8 +5,8 @@
#define _INT_PARTY_H_
int inter_party_parse_frommap(int fd);
-int inter_party_sql_init();
-void inter_party_sql_final();
+int inter_party_sql_init(void);
+void inter_party_sql_final(void);
int inter_party_leave(int party_id,int account_id, int char_id);
int inter_party_logged(int party_id, int account_id, int char_id);
int inter_party_CharOnline(int char_id, int party_id);
diff --git a/src/char_sql/int_pet.c b/src/char_sql/int_pet.c
index e48347b82..4ae7e3fcc 100644
--- a/src/char_sql/int_pet.c
+++ b/src/char_sql/int_pet.c
@@ -13,9 +13,6 @@
#include "../common/showmsg.h"
struct s_pet *pet_pt;
-static int pet_newid = 100;
-
-
#ifndef SQL_DEBUG
@@ -32,8 +29,6 @@ int inter_pet_tosql(int pet_id, struct s_pet *p) {
//`pet` (`pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incuvate`)
char t_name[NAME_LENGTH*2];
-// ShowInfo("Saving pet (%d)...\n",pet_id);
-
jstrescapecpy(t_name, p->name);
if(p->hungry < 0)
@@ -44,30 +39,34 @@ int inter_pet_tosql(int pet_id, struct s_pet *p) {
p->intimate = 0;
else if(p->intimate > 1000)
p->intimate = 1000;
- sprintf(tmp_sql,"SELECT * FROM `%s` WHERE `pet_id`='%d'",pet_db, pet_id);
- if(mysql_query(&mysql_handle, tmp_sql) ) {
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- }
- sql_res = mysql_store_result(&mysql_handle) ;
- if (sql_res!=NULL && mysql_num_rows(sql_res)>0)
- //row reside -> updating
+ if (pet_id == -1) //New pet.
+ sprintf(tmp_sql,"INSERT INTO `%s` "
+ "(`class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incuvate`) "
+ "VALUES ('%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
+ pet_db, p->class_, t_name, p->account_id, p->char_id, p->level, p->egg_id,
+ p->equip, p->intimate, p->hungry, p->rename_flag, p->incuvate);
+
+ else //Update pet.
sprintf(tmp_sql, "UPDATE `%s` SET `class`='%d',`name`='%s',`account_id`='%d',`char_id`='%d',`level`='%d',`egg_id`='%d',`equip`='%d',`intimate`='%d',`hungry`='%d',`rename_flag`='%d',`incuvate`='%d' WHERE `pet_id`='%d'",
pet_db, p->class_, t_name, p->account_id, p->char_id, p->level, p->egg_id,
p->equip, p->intimate, p->hungry, p->rename_flag, p->incuvate, p->pet_id);
- else //no row -> insert
- sprintf(tmp_sql,"INSERT INTO `%s` (`pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incuvate`) VALUES ('%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
- pet_db, pet_id, p->class_, t_name, p->account_id, p->char_id, p->level, p->egg_id,
- p->equip, p->intimate, p->hungry, p->rename_flag, p->incuvate);
- mysql_free_result(sql_res) ; //resource free
if(mysql_query(&mysql_handle, tmp_sql) ) {
ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
+ return 0;
+ } else if (pet_id == -1) { //New pet inserted.
+ if(mysql_field_count(&mysql_handle) == 0 &&
+ mysql_insert_id(&mysql_handle) != 0) {
+ p->pet_id = pet_id = mysql_insert_id(&mysql_handle);
+ } else {
+ ShowError("inter_pet_tosql: Failed to retrieve new pet_id for '%s'. Pet creation aborted.\n", p->name);
+ return 0;
+ }
}
if (save_log)
ShowInfo("Pet saved %d - %s.\n", pet_id, p->name);
- return 0;
+ return 1;
}
int inter_pet_fromsql(int pet_id, struct s_pet *p){
@@ -119,45 +118,12 @@ int inter_pet_fromsql(int pet_id, struct s_pet *p){
}
//----------------------------------------------
-int inter_pet_sql_init(){
- int i;
-
+int inter_pet_sql_init(void){
//memory alloc
- ShowDebug("interserver pet memory initialize.... (%d byte)\n",sizeof(struct s_pet));
pet_pt = (struct s_pet*)aCalloc(sizeof(struct s_pet), 1);
-
- sprintf (tmp_sql , "SELECT count(*) FROM `%s`", pet_db);
- if(mysql_query(&mysql_handle, tmp_sql) ) {
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- exit(0);
- }
- sql_res = mysql_store_result(&mysql_handle) ;
- sql_row = mysql_fetch_row(sql_res);
- ShowStatus("total pet data: '%s'\n",sql_row[0]);
- i = atoi (sql_row[0]);
- mysql_free_result(sql_res);
-
- if (i > 0) {
- //set pet_newid
- sprintf (tmp_sql , "SELECT max(`pet_id`) FROM `%s`",pet_db );
- if(mysql_query(&mysql_handle, tmp_sql) ) {
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- }
-
- sql_res = mysql_store_result(&mysql_handle) ;
-
- sql_row = mysql_fetch_row(sql_res);
- pet_newid = atoi (sql_row[0])+1; //should SET MAX existing PET ID + 1 [Lupus]
- mysql_free_result(sql_res);
- }
-
- ShowDebug("set pet_newid: %d.\n",pet_newid);
-
return 0;
}
-void inter_pet_sql_final(){
+void inter_pet_sql_final(void){
if (pet_pt) aFree(pet_pt);
return;
}
@@ -233,7 +199,6 @@ int mapif_create_pet(int fd, int account_id, int char_id, short pet_class, short
short pet_equip, short intimate, short hungry, char rename_flag, char incuvate, char *pet_name){
memset(pet_pt, 0, sizeof(struct s_pet));
- pet_pt->pet_id = pet_newid++;
memcpy(pet_pt->name, pet_name, NAME_LENGTH-1);
if(incuvate == 1)
pet_pt->account_id = pet_pt->char_id = 0;
@@ -259,9 +224,11 @@ int mapif_create_pet(int fd, int account_id, int char_id, short pet_class, short
else if(pet_pt->intimate > 1000)
pet_pt->intimate = 1000;
- inter_pet_tosql(pet_pt->pet_id,pet_pt);
-
- mapif_pet_created(fd, account_id, pet_pt);
+ pet_pt->pet_id = -1; //Signal NEW pet.
+ if (inter_pet_tosql(pet_pt->pet_id,pet_pt))
+ mapif_pet_created(fd, account_id, pet_pt);
+ else //Failed...
+ mapif_pet_created(fd, account_id, NULL);
return 0;
}
diff --git a/src/char_sql/int_pet.h b/src/char_sql/int_pet.h
index d39574344..543dfe296 100644
--- a/src/char_sql/int_pet.h
+++ b/src/char_sql/int_pet.h
@@ -4,13 +4,13 @@
#ifndef _INT_PET_H_
#define _INT_PET_H_
-int inter_pet_init();
-void inter_pet_sql_final();
-int inter_pet_save();
+int inter_pet_init(void);
+void inter_pet_sql_final(void);
+int inter_pet_save(void);
int inter_pet_delete(int pet_id);
int inter_pet_parse_frommap(int fd);
-int inter_pet_sql_init();
+int inter_pet_sql_init(void);
//extern char pet_txt[256];
#endif
diff --git a/src/char_sql/int_storage.c b/src/char_sql/int_storage.c
index 1dac9dc51..780617055 100644
--- a/src/char_sql/int_storage.c
+++ b/src/char_sql/int_storage.c
@@ -176,7 +176,7 @@ int guild_storage_fromsql(int guild_id, struct guild_storage *p){
//---------------------------------------------------------
// storage data initialize
-int inter_storage_sql_init(){
+int inter_storage_sql_init(void){
//memory alloc
ShowDebug("interserver storage memory initialize....(%d byte)\n",sizeof(struct storage));
@@ -188,7 +188,7 @@ int inter_storage_sql_init(){
return 1;
}
// storage data finalize
-void inter_storage_sql_final()
+void inter_storage_sql_final(void)
{
if (storage_pt) aFree(storage_pt);
if (guild_storage_pt) aFree(guild_storage_pt);
diff --git a/src/char_sql/int_storage.h b/src/char_sql/int_storage.h
index f3b56a6d1..8cbf78fea 100644
--- a/src/char_sql/int_storage.h
+++ b/src/char_sql/int_storage.h
@@ -4,8 +4,8 @@
#ifndef _INT_STORAGE_H_
#define _INT_STORAGE_H_
-int inter_storage_sql_init();
-void inter_storage_sql_final();
+int inter_storage_sql_init(void);
+void inter_storage_sql_final(void);
int inter_storage_delete(int account_id);
int inter_guild_storage_delete(int guild_id);
diff --git a/src/char_sql/inter.c b/src/char_sql/inter.c
index 0c6145bf9..5a220ef7f 100644
--- a/src/char_sql/inter.c
+++ b/src/char_sql/inter.c
@@ -180,7 +180,7 @@ int inter_accreg_fromsql(int account_id,int char_id, struct accreg *reg, int typ
}
// Initialize
-int inter_accreg_sql_init()
+int inter_accreg_sql_init(void)
{
CREATE(accreg_pt, struct accreg, 1);
return 0;
@@ -397,7 +397,7 @@ int inter_sql_test (void)
}
// finalize
-void inter_final() {
+void inter_final(void) {
wis_db->destroy(wis_db, NULL);
inter_guild_sql_final();
@@ -552,7 +552,7 @@ int check_ttl_wisdata_sub(DBKey key, void *data, va_list ap) {
return 0;
}
-int check_ttl_wisdata() {
+int check_ttl_wisdata(void) {
unsigned long tick = gettick();
int i;
diff --git a/src/char_sql/inter.h b/src/char_sql/inter.h
index c1b40db01..12a06b314 100644
--- a/src/char_sql/inter.h
+++ b/src/char_sql/inter.h
@@ -5,10 +5,10 @@
#define _INTER_H_
int inter_init(const char *file);
-void inter_final();
+void inter_final(void);
int inter_parse_frommap(int fd);
int inter_mapif_init(int fd);
-int mapif_send_gmaccounts();
+int mapif_send_gmaccounts(void);
void mapif_send_maxid(int, int);
int mapif_disconnectplayer(int fd, int account_id, int char_id, int reason);
diff --git a/src/login_sql/login.c b/src/login_sql/login.c
index fe4248183..ca27c0036 100644
--- a/src/login_sql/login.c
+++ b/src/login_sql/login.c
@@ -64,7 +64,6 @@ void Gettimeofday(struct timeval *timenow)
//-----------------------------------------------------
// global variable
//-----------------------------------------------------
-int account_id_count = START_ACCOUNT_NUM;
int server_num;
int new_account_flag = 0; //Set from config too XD [Sirius]
int bind_ip_set_ = 0;
@@ -369,29 +368,6 @@ int mmo_auth_sqldb_init(void) {
ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
}
}
- if (new_account_flag)
- { //Check if the next new account will need to have it's ID set (to avoid bad DBs which would otherwise insert
- //new accounts with account_ids of less than 2M [Skotlex]
- sprintf(tmp_sql, "SELECT max(`%s`) from `%s`", login_db_account_id, login_db);
- if(mysql_query(&mysql_handle, tmp_sql)){
- ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
- ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
- } else {
- MYSQL_RES* sql_res;
- MYSQL_ROW sql_row;
-
- sql_res = mysql_store_result(&mysql_handle) ;
- if (sql_res)
- {
- if (mysql_num_rows(sql_res) > 0 &&
- (sql_row = mysql_fetch_row(sql_res)) != NULL &&
- sql_row[0] != NULL && atoi(sql_row[0]) >= account_id_count)
- //Ok, chars already exist, no need to use this.
- account_id_count = 0;
- mysql_free_result(sql_res);
- }
- }
- }
return 0;
}
@@ -490,10 +466,7 @@ int mmo_auth_new(struct mmo_account* account, char sex)
ShowInfo("New account: user: %s with passwd: %s sex: %c\n", account->userid, user_password, sex);
- if (account_id_count) //Force new Account ID
- sprintf(tmp_sql, "INSERT INTO `%s` (`%s`, `%s`, `%s`, `sex`, `email`) VALUES ('%d', '%s', '%s', '%c', '%s')", login_db, login_db_account_id, login_db_userid, login_db_user_pass, account_id_count, account->userid, user_password, sex, "a@a.com");
- else
- sprintf(tmp_sql, "INSERT INTO `%s` (`%s`, `%s`, `sex`, `email`) VALUES ('%s', '%s', '%c', '%s')", login_db, login_db_userid, login_db_user_pass, account->userid, user_password, sex, "a@a.com");
+ sprintf(tmp_sql, "INSERT INTO `%s` (`%s`, `%s`, `sex`, `email`) VALUES ('%s', '%s', '%c', '%s')", login_db, login_db_userid, login_db_user_pass, account->userid, user_password, sex, "a@a.com");
if(mysql_query(&mysql_handle, tmp_sql)){
//Failed to insert new acc :/
@@ -502,9 +475,25 @@ int mmo_auth_new(struct mmo_account* account, char sex)
return 1;
}
- if (account_id_count) //Clear it or all new accounts will try to use the same id :P
- account_id_count = 0;
-
+ if(mysql_field_count(&mysql_handle) == 0 &&
+ mysql_insert_id(&mysql_handle) < START_ACCOUNT_NUM) {
+ //Invalid Account ID! Must update it.
+ int id = mysql_insert_id(&mysql_handle);
+ sprintf(tmp_sql, "UPDATE `%s` SET `%s`='%d' WHERE `%s`='%d'", login_db, login_db_account_id, START_ACCOUNT_NUM, login_db_account_id, id);
+ if(mysql_query(&mysql_handle, tmp_sql)){
+ ShowError("New account %s has an invalid account ID [%d] which could not be updated (account_id must be %d or higher).", account->userid, id, START_ACCOUNT_NUM);
+ ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
+ ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
+ //Just delete it and fail.
+ sprintf(tmp_sql, "DELETE FROM `%s` WHERE `%s`='%d'", login_db, login_db_account_id, id);
+ if(mysql_query(&mysql_handle, tmp_sql)){
+ ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
+ ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
+ }
+ return 1;
+ }
+ ShowNotice("Updated New account %s's ID %d->%d (account_id must be %d or higher).", account->userid, id, START_ACCOUNT_NUM, START_ACCOUNT_NUM);
+ }
if(tick > new_reg_tick)
{ //Update the registration check.
num_regs=0;
@@ -817,8 +806,8 @@ int mmo_auth( struct mmo_account* account , int fd){
account->sex = sql_row[5][0] == 'S' ? 2 : sql_row[5][0]=='M';
account->level = atoi(sql_row[10]) > 99 ? 99 : atoi(sql_row[10]); // as was in isGM() [zzo]
- if (account->sex != 2 && account->account_id < 700000)
- ShowWarning("Account %s has account id %d! Account IDs must be over 700000 to work properly!\n", account->userid, account->account_id);
+ if (account->sex != 2 && account->account_id < START_ACCOUNT_NUM)
+ ShowWarning("Account %s has account id %d! Account IDs must be over %d to work properly!\n", account->userid, account->account_id, START_ACCOUNT_NUM);
sprintf(tmpsql, "UPDATE `%s` SET `lastlogin` = NOW(), `logincount`=`logincount` +1, `last_ip`='%s' WHERE %s `%s` = '%s'",
login_db, ip, case_sensitive ? "BINARY" : "", login_db_userid, sql_row[1]);
mysql_free_result(sql_res) ; //resource free