int_pet.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // original code from athena
  3. // SQL conversion by Jioh L. Jung
  4. //
  5. #include "char.h"
  6. #include "../common/strlib.h"
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. struct s_pet *pet_pt;
  11. static int pet_newid = 100;
  12. #define mysql_query(_x, _y) debug_mysql_query(__FILE__, __LINE__, _x, _y)
  13. //---------------------------------------------------------
  14. int inter_pet_tosql(int pet_id, struct s_pet *p) {
  15. //`pet` (`pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incuvate`)
  16. char t_name[100];
  17. printf("request save pet: %d.......\n",pet_id);
  18. jstrescapecpy(t_name, p->name);
  19. if(p->hungry < 0)
  20. p->hungry = 0;
  21. else if(p->hungry > 100)
  22. p->hungry = 100;
  23. if(p->intimate < 0)
  24. p->intimate = 0;
  25. else if(p->intimate > 1000)
  26. p->intimate = 1000;
  27. sprintf(tmp_sql,"SELECT * FROM `%s` WHERE `pet_id`='%d'",pet_db, pet_id);
  28. if(mysql_query(&mysql_handle, tmp_sql) ) {
  29. printf("DB server Error - %s\n", mysql_error(&mysql_handle) );
  30. }
  31. sql_res = mysql_store_result(&mysql_handle) ;
  32. if (sql_res!=NULL && mysql_num_rows(sql_res)>0)
  33. //row reside -> updating
  34. 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'",
  35. pet_db, p->class_, t_name, p->account_id, p->char_id, p->level, p->egg_id,
  36. p->equip, p->intimate, p->hungry, p->rename_flag, p->incuvate, p->pet_id);
  37. else //no row -> insert
  38. 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')",
  39. pet_db, pet_id, p->class_, t_name, p->account_id, p->char_id, p->level, p->egg_id,
  40. p->equip, p->intimate, p->hungry, p->rename_flag, p->incuvate);
  41. mysql_free_result(sql_res) ; //resource free
  42. if(mysql_query(&mysql_handle, tmp_sql) ) {
  43. printf("DB server Error (inset/update `pet`)- %s\n", mysql_error(&mysql_handle) );
  44. }
  45. printf("pet save success.......\n");
  46. return 0;
  47. }
  48. int inter_pet_fromsql(int pet_id, struct s_pet *p){
  49. printf("request load pet: %d.......\n",pet_id);
  50. memset(p, 0, sizeof(struct s_pet));
  51. //`pet` (`pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incuvate`)
  52. sprintf(tmp_sql,"SELECT `pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incuvate` FROM `%s` WHERE `pet_id`='%d'",pet_db, pet_id);
  53. if(mysql_query(&mysql_handle, tmp_sql) ) {
  54. printf("DB server Error (select `pet`)- %s\n", mysql_error(&mysql_handle) );
  55. return 0;
  56. }
  57. sql_res = mysql_store_result(&mysql_handle) ;
  58. if (sql_res!=NULL && mysql_num_rows(sql_res)>0) {
  59. sql_row = mysql_fetch_row(sql_res);
  60. p->pet_id = pet_id;
  61. p->class_ = atoi(sql_row[1]);
  62. memcpy(p->name, sql_row[2],24);
  63. p->account_id = atoi(sql_row[3]);
  64. p->char_id = atoi(sql_row[4]);
  65. p->level = atoi(sql_row[5]);
  66. p->egg_id = atoi(sql_row[6]);
  67. p->equip = atoi(sql_row[7]);
  68. p->intimate = atoi(sql_row[8]);
  69. p->hungry = atoi(sql_row[9]);
  70. p->rename_flag = atoi(sql_row[10]);
  71. p->incuvate = atoi(sql_row[11]);
  72. }
  73. if(p->hungry < 0)
  74. p->hungry = 0;
  75. else if(p->hungry > 100)
  76. p->hungry = 100;
  77. if(p->intimate < 0)
  78. p->intimate = 0;
  79. else if(p->intimate > 1000)
  80. p->intimate = 1000;
  81. mysql_free_result(sql_res);
  82. printf("pet load success.......\n");
  83. return 0;
  84. }
  85. //----------------------------------------------
  86. int inter_pet_sql_init(){
  87. int i;
  88. //memory alloc
  89. printf("interserver pet memory initialize.... (%d byte)\n",sizeof(struct s_pet));
  90. pet_pt = calloc(sizeof(struct s_pet), 1);
  91. sprintf (tmp_sql , "SELECT count(*) FROM `%s`", pet_db);
  92. if(mysql_query(&mysql_handle, tmp_sql) ) {
  93. printf("DB server Error - %s\n", mysql_error(&mysql_handle) );
  94. exit(0);
  95. }
  96. sql_res = mysql_store_result(&mysql_handle) ;
  97. sql_row = mysql_fetch_row(sql_res);
  98. printf("total pet data -> '%s'.......\n",sql_row[0]);
  99. i = atoi (sql_row[0]);
  100. mysql_free_result(sql_res);
  101. if (i > 0) {
  102. //set pet_newid
  103. sprintf (tmp_sql , "SELECT max(`pet_id`) FROM `%s`",pet_db );
  104. if(mysql_query(&mysql_handle, tmp_sql) ) {
  105. printf("DB server Error - %s\n", mysql_error(&mysql_handle) );
  106. }
  107. sql_res = mysql_store_result(&mysql_handle) ;
  108. sql_row = mysql_fetch_row(sql_res);
  109. pet_newid = atoi (sql_row[0])+1; //should SET MAX existing PET ID + 1 [Lupus]
  110. mysql_free_result(sql_res);
  111. }
  112. printf("set pet_newid: %d.......\n",pet_newid);
  113. return 0;
  114. }
  115. //----------------------------------
  116. int inter_pet_delete(int pet_id){
  117. printf("request delete pet: %d.......\n",pet_id);
  118. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `pet_id`='%d'",pet_db, pet_id);
  119. if(mysql_query(&mysql_handle, tmp_sql) ) {
  120. printf("DB server Error - %s\n", mysql_error(&mysql_handle) );
  121. }
  122. return 0;
  123. }
  124. //------------------------------------------------------
  125. int mapif_pet_created(int fd, int account_id, struct s_pet *p)
  126. {
  127. WFIFOW(fd, 0) =0x3880;
  128. WFIFOL(fd, 2) =account_id;
  129. if(p!=NULL){
  130. WFIFOB(fd, 6)=0;
  131. WFIFOL(fd, 7) =p->pet_id;
  132. printf("int_pet: created! %d %s\n", p->pet_id, p->name);
  133. }else{
  134. WFIFOB(fd, 6)=1;
  135. WFIFOL(fd, 7)=0;
  136. }
  137. WFIFOSET(fd, 11);
  138. return 0;
  139. }
  140. int mapif_pet_info(int fd, int account_id, struct s_pet *p){
  141. WFIFOW(fd, 0) =0x3881;
  142. WFIFOW(fd, 2) =sizeof(struct s_pet) + 9;
  143. WFIFOL(fd, 4) =account_id;
  144. WFIFOB(fd, 8)=0;
  145. memcpy(WFIFOP(fd, 9), p, sizeof(struct s_pet));
  146. WFIFOSET(fd, WFIFOW(fd, 2));
  147. return 0;
  148. }
  149. int mapif_pet_noinfo(int fd, int account_id){
  150. WFIFOW(fd, 0) =0x3881;
  151. WFIFOW(fd, 2) =sizeof(struct s_pet) + 9;
  152. WFIFOL(fd, 4) =account_id;
  153. WFIFOB(fd, 8)=1;
  154. memset(WFIFOP(fd, 9), 0, sizeof(struct s_pet));
  155. WFIFOSET(fd, WFIFOW(fd, 2));
  156. return 0;
  157. }
  158. int mapif_save_pet_ack(int fd, int account_id, int flag){
  159. WFIFOW(fd, 0) =0x3882;
  160. WFIFOL(fd, 2) =account_id;
  161. WFIFOB(fd, 6) =flag;
  162. WFIFOSET(fd, 7);
  163. return 0;
  164. }
  165. int mapif_delete_pet_ack(int fd, int flag){
  166. WFIFOW(fd, 0) =0x3883;
  167. WFIFOB(fd, 2) =flag;
  168. WFIFOSET(fd, 3);
  169. return 0;
  170. }
  171. int mapif_create_pet(int fd, int account_id, int char_id, short pet_class, short pet_lv, short pet_egg_id,
  172. short pet_equip, short intimate, short hungry, char rename_flag, char incuvate, char *pet_name){
  173. memset(pet_pt, 0, sizeof(struct s_pet));
  174. pet_pt->pet_id = pet_newid++;
  175. memcpy(pet_pt->name, pet_name, 24);
  176. if(incuvate == 1)
  177. pet_pt->account_id = pet_pt->char_id = 0;
  178. else {
  179. pet_pt->account_id = account_id;
  180. pet_pt->char_id = char_id;
  181. }
  182. pet_pt->class_ = pet_class;
  183. pet_pt->level = pet_lv;
  184. pet_pt->egg_id = pet_egg_id;
  185. pet_pt->equip = pet_equip;
  186. pet_pt->intimate = intimate;
  187. pet_pt->hungry = hungry;
  188. pet_pt->rename_flag = rename_flag;
  189. pet_pt->incuvate = incuvate;
  190. if(pet_pt->hungry < 0)
  191. pet_pt->hungry = 0;
  192. else if(pet_pt->hungry > 100)
  193. pet_pt->hungry = 100;
  194. if(pet_pt->intimate < 0)
  195. pet_pt->intimate = 0;
  196. else if(pet_pt->intimate > 1000)
  197. pet_pt->intimate = 1000;
  198. inter_pet_tosql(pet_pt->pet_id,pet_pt);
  199. mapif_pet_created(fd, account_id, pet_pt);
  200. return 0;
  201. }
  202. int mapif_load_pet(int fd, int account_id, int char_id, int pet_id){
  203. memset(pet_pt, 0, sizeof(struct s_pet));
  204. inter_pet_fromsql(pet_id, pet_pt);
  205. if(pet_pt!=NULL) {
  206. if(pet_pt->incuvate == 1) {
  207. pet_pt->account_id = pet_pt->char_id = 0;
  208. mapif_pet_info(fd, account_id, pet_pt);
  209. }
  210. else if(account_id == pet_pt->account_id && char_id == pet_pt->char_id)
  211. mapif_pet_info(fd, account_id, pet_pt);
  212. else
  213. mapif_pet_noinfo(fd, account_id);
  214. }
  215. else
  216. mapif_pet_noinfo(fd, account_id);
  217. return 0;
  218. }
  219. int mapif_save_pet(int fd, int account_id, struct s_pet *data) {
  220. //here process pet save request.
  221. int len=RFIFOW(fd, 2);
  222. if(sizeof(struct s_pet)!=len-8) {
  223. printf("inter pet: data size error %d %d\n", sizeof(struct s_pet), len-8);
  224. }
  225. else{
  226. if(data->hungry < 0)
  227. data->hungry = 0;
  228. else if(data->hungry > 100)
  229. data->hungry = 100;
  230. if(data->intimate < 0)
  231. data->intimate = 0;
  232. else if(data->intimate > 1000)
  233. data->intimate = 1000;
  234. inter_pet_tosql(data->pet_id,data);
  235. mapif_save_pet_ack(fd, account_id, 0);
  236. }
  237. return 0;
  238. }
  239. int mapif_delete_pet(int fd, int pet_id){
  240. mapif_delete_pet_ack(fd, inter_pet_delete(pet_id));
  241. return 0;
  242. }
  243. int mapif_parse_CreatePet(int fd){
  244. mapif_create_pet(fd, RFIFOL(fd, 2), RFIFOL(fd, 6), RFIFOW(fd, 10), RFIFOW(fd, 12), RFIFOW(fd, 14), RFIFOW(fd, 16), RFIFOL(fd, 18),
  245. RFIFOL(fd, 20), RFIFOB(fd, 22), RFIFOB(fd, 23), RFIFOP(fd, 24));
  246. return 0;
  247. }
  248. int mapif_parse_LoadPet(int fd){
  249. mapif_load_pet(fd, RFIFOL(fd, 2), RFIFOL(fd, 6), RFIFOL(fd, 10));
  250. return 0;
  251. }
  252. int mapif_parse_SavePet(int fd){
  253. mapif_save_pet(fd, RFIFOL(fd, 4), (struct s_pet *) RFIFOP(fd, 8));
  254. return 0;
  255. }
  256. int mapif_parse_DeletePet(int fd){
  257. mapif_delete_pet(fd, RFIFOL(fd, 2));
  258. return 0;
  259. }
  260. int inter_pet_parse_frommap(int fd){
  261. switch(RFIFOW(fd, 0)){
  262. case 0x3080: mapif_parse_CreatePet(fd); break;
  263. case 0x3081: mapif_parse_LoadPet(fd); break;
  264. case 0x3082: mapif_parse_SavePet(fd); break;
  265. case 0x3083: mapif_parse_DeletePet(fd); break;
  266. default:
  267. return 0;
  268. }
  269. return 1;
  270. }