itemdb.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // $Id: itemdb.c,v 1.1.1.1 2004/09/10 17:44:48 MagicalTux Exp $
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "itemdb.h"
  6. #include "db.h"
  7. #include "inter.h"
  8. #include "char.h"
  9. #include "utils.h"
  10. #ifdef MEMWATCH
  11. #include "memwatch.h"
  12. #endif
  13. #define MAX_RANDITEM 2000
  14. // ** ITEMDB_OVERRIDE_NAME_VERBOSE **
  15. // 定義すると、itemdb.txtとgrfで名前が異なる場合、表示します.
  16. //#define ITEMDB_OVERRIDE_NAME_VERBOSE 1
  17. char item_db_db[256]="item_db"; // added to specify item_db sql table [Valaris]
  18. static struct dbt* item_db;
  19. /*==========================================
  20. * DBの検索
  21. *------------------------------------------
  22. */
  23. struct item_data* itemdb_search(int nameid)
  24. {
  25. struct item_data *id;
  26. id = (struct item_data*)numdb_search(item_db,nameid);
  27. if(id) return id;
  28. CREATE(id, struct item_data, 1);
  29. numdb_insert(item_db,nameid,id);
  30. if(nameid>500 && nameid<600)
  31. id->type=0; //heal item
  32. else if(nameid>600 && nameid<700)
  33. id->type=2; //use item
  34. else if((nameid>700 && nameid<1100) ||
  35. (nameid>7000 && nameid<8000))
  36. id->type=3; //correction
  37. else if(nameid>=1750 && nameid<1771)
  38. id->type=10; //arrow
  39. else if(nameid>1100 && nameid<2000)
  40. id->type=4; //weapon
  41. else if((nameid>2100 && nameid<3000) ||
  42. (nameid>5000 && nameid<6000))
  43. id->type=5; //armor
  44. else if(nameid>4000 && nameid<5000)
  45. id->type=6; //card
  46. else if(nameid>9000 && nameid<10000)
  47. id->type=7; //egg
  48. else if(nameid>10000)
  49. id->type=8; //petequip
  50. return id;
  51. }
  52. /*==========================================
  53. *
  54. *------------------------------------------
  55. */
  56. int itemdb_isequip(int nameid)
  57. {
  58. int type=itemdb_type(nameid);
  59. if(type==0 || type==2 || type==3 || type==6 || type==10)
  60. return 0;
  61. return 1;
  62. }
  63. /*==========================================
  64. *
  65. *------------------------------------------
  66. */
  67. int itemdb_isequip2(struct item_data *data)
  68. {
  69. if(data) {
  70. int type=data->type;
  71. if(type==0 || type==2 || type==3 || type==6 || type==10)
  72. return 0;
  73. else
  74. return 1;
  75. }
  76. return 0;
  77. }
  78. /*==========================================
  79. * アイテムデータベースの読み込み
  80. *------------------------------------------
  81. */
  82. static int itemdb_readdb(void)
  83. {
  84. FILE *fp;
  85. char line[1024];
  86. int ln=0;
  87. int nameid,j;
  88. char *str[32],*p,*np;
  89. struct item_data *id;
  90. fp=fopen("db/item_db.txt","r");
  91. if(fp==NULL){
  92. printf("can't read db/item_db.txt\n");
  93. exit(1);
  94. }
  95. while(fgets(line,1020,fp)){
  96. if(line[0]=='/' && line[1]=='/')
  97. continue;
  98. memset(str,0,sizeof(str));
  99. for(j=0,np=p=line;j<17 && p;j++){
  100. str[j]=p;
  101. p=strchr(p,',');
  102. if(p){ *p++=0; np=p; }
  103. }
  104. if(str[0]==NULL)
  105. continue;
  106. nameid=atoi(str[0]);
  107. if(nameid<=0 || nameid>=20000)
  108. continue;
  109. ln++;
  110. //ID,Name,Jname,Type,Price,Sell,Weight,ATK,DEF,Range,Slot,Job,Gender,Loc,wLV,eLV,View
  111. id=itemdb_search(nameid);
  112. memcpy(id->name,str[1],24);
  113. memcpy(id->jname,str[2],24);
  114. id->type=atoi(str[3]);
  115. }
  116. fclose(fp);
  117. printf("read db/item_db.txt done (count=%d)\n",ln);
  118. return 0;
  119. }
  120. static int itemdb_read_sqldb(void) // sql item_db read, shortened version of map-server item_db read [Valaris]
  121. {
  122. unsigned int nameid; // Type should be "unsigned short int", but currently isn't for compatibility with numdb_insert()
  123. struct item_data *id;
  124. // ----------
  125. // Output query to retrieve all rows from the item database table
  126. sprintf(tmp_sql, "SELECT * FROM `%s`", item_db_db);
  127. // Execute the query; if the query execution fails, output an error
  128. if (mysql_query(&mysql_handle, tmp_sql)) {
  129. printf("Database server error (executing query for %s): %s\n", item_db_db, mysql_error(&mysql_handle));
  130. }
  131. // Store the query result
  132. sql_res = mysql_store_result(&mysql_handle);
  133. // If the storage of the query result succeeded
  134. if (sql_res) {
  135. // Parse each row in the query result into sql_row
  136. while ((sql_row = mysql_fetch_row(sql_res))) {
  137. nameid = atoi(sql_row[0]);
  138. // If the identifier is not within the valid range, process the next row
  139. if (nameid == 0 || nameid >= 20000) { // Should ">= 20000" be "> 20000"?
  140. continue;
  141. }
  142. // ----------
  143. // Insert a new row into the item database
  144. /*
  145. id = aCalloc(sizeof(struct item_data), 1);
  146. if (id == NULL) {
  147. printf("out of memory : itemdb_read_sqldb\n");
  148. exit(1);
  149. }
  150. memset(id, 0, sizeof(struct item_data));
  151. numdb_insert(item_db, nameid, id);
  152. // ----------
  153. */
  154. id=itemdb_search(nameid);
  155. memcpy(id->name, sql_row[1], 24);
  156. memcpy(id->jname, sql_row[2], 24);
  157. id->type = atoi(sql_row[3]);
  158. }
  159. // If the retrieval failed, output an error
  160. if (mysql_errno(&mysql_handle)) {
  161. printf("Database server error (retrieving rows from %s): %s\n", item_db_db, mysql_error(&mysql_handle));
  162. }
  163. printf("read %s done (count = %lu)\n", item_db_db, (unsigned long) mysql_num_rows(sql_res));
  164. // Free the query result
  165. mysql_free_result(sql_res);
  166. } else {
  167. printf("MySQL error (storing query result for %s): %s\n", item_db_db, mysql_error(&mysql_handle));
  168. }
  169. return 0;
  170. }
  171. static int itemdb_final(void *key,void *data,va_list ap)
  172. {
  173. struct item_data *id;
  174. id = (struct item_data*)data;
  175. if(id->use_script)
  176. aFree(id->use_script);
  177. if(id->equip_script)
  178. aFree(id->equip_script);
  179. aFree(id);
  180. return 0;
  181. }
  182. /*==========================================
  183. *
  184. *------------------------------------------
  185. */
  186. void do_final_itemdb(void)
  187. {
  188. if(item_db){
  189. numdb_final(item_db,itemdb_final);
  190. item_db=NULL;
  191. }
  192. }
  193. int do_init_itemdb(void)
  194. {
  195. item_db = numdb_init();
  196. if (db_use_sqldbs) // it db_use_sqldbs in inter config are yes, will read from item_db for char server display [Valaris]
  197. itemdb_read_sqldb();
  198. else
  199. itemdb_readdb();
  200. return 0;
  201. }