update_db_tables.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * This file is part of RuneOptimizer.
  3. *
  4. * RuneOptimizer is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation, either version 3 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * RuneOptimizer is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * RuneOptimizer. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * @file update_db_tables.c
  19. *
  20. * Implementation of {@link update_db_tables}.
  21. *
  22. * This file implements the funciton {@link update_db_tables} declared in
  23. * {@link update.h}.
  24. */
  25. #include <stdio.h>
  26. #include <sqlite3.h>
  27. #include "../runeoptimizer.h"
  28. #include "../error/error.h"
  29. #include "../db/db.h"
  30. extern int update_db_tables(unsigned char clear_teams){
  31. // Drop all tables
  32. char table_names[6][11] = {
  33. "rune_stats", "runes", "units", "info", "teams", "units_teams"
  34. };
  35. char query_drop[64];
  36. int tables_to_drop = 4;
  37. // By default, only drop the first 6 tables. If clear teams flag, all 6.
  38. if (clear_teams == TRUE){
  39. tables_to_drop = 6;
  40. }
  41. for (int i = 0; i < tables_to_drop; i ++){
  42. sprintf(query_drop, "DROP TABLE IF EXISTS %s;", table_names[i]);
  43. if (SUCCESS != db_execute(query_drop, NULL)){
  44. fprintf(
  45. stderr, "Unable to drop table %s: %s\n",
  46. table_names[i], sqlite3_errmsg(db)
  47. );
  48. return ERROR_DB_DROP;
  49. }
  50. }
  51. // Table rune_stats
  52. if (
  53. SUCCESS !=
  54. db_execute(
  55. "CREATE TABLE IF NOT EXISTS rune_stats("
  56. " rune CHAR(12), slot INT, stat INT, value INT, grind INT, enchant INT"
  57. ");",
  58. NULL
  59. )
  60. ){
  61. fprintf(
  62. stderr,
  63. "Unable to create table rune_stats: %s\n", sqlite3_errmsg(db)
  64. );
  65. return ERROR_DB_CREATE_RUNE_STATS;
  66. }
  67. // Table runes
  68. if (
  69. SUCCESS !=
  70. db_execute(
  71. "CREATE TABLE IF NOT EXISTS runes("
  72. " id CHAR(12),"
  73. " unit CHAR(12),"
  74. " type INT,"
  75. " slot INT,"
  76. " stars INT,"
  77. " level INT,"
  78. " quality INT,"
  79. " efficiency FLOAT,"
  80. " max_efficiency FLOAT,"
  81. " main_stat INT,"
  82. " current_hp_percent INT,"
  83. " current_atk_percent INT,"
  84. " current_def_percent INT,"
  85. " current_hp_flat INT,"
  86. " current_atk_flat INT,"
  87. " current_def_flat INT,"
  88. " current_spd INT,"
  89. " current_crr INT,"
  90. " current_crd INT,"
  91. " current_acc INT,"
  92. " current_res INT,"
  93. " lv12_hp_percent INT,"
  94. " lv12_atk_percent INT,"
  95. " lv12_def_percent INT,"
  96. " lv12_hp_flat INT,"
  97. " lv12_atk_flat INT,"
  98. " lv12_def_flat INT,"
  99. " lv12_spd INT,"
  100. " lv12_crr INT,"
  101. " lv12_crd INT,"
  102. " lv12_acc INT,"
  103. " lv12_res INT,"
  104. " lv15_hp_percent INT,"
  105. " lv15_atk_percent INT,"
  106. " lv15_def_percent INT,"
  107. " lv15_hp_flat INT,"
  108. " lv15_atk_flat INT,"
  109. " lv15_def_flat INT,"
  110. " lv15_spd INT,"
  111. " lv15_crr INT,"
  112. " lv15_crd INT,"
  113. " lv15_acc INT,"
  114. " lv15_res INT"
  115. ");",
  116. NULL
  117. )
  118. ){
  119. fprintf(
  120. stderr, "Unable to create table runes: %s\n", sqlite3_errmsg(db)
  121. );
  122. return ERROR_DB_CREATE_RUNES;
  123. }
  124. // Table units
  125. if (
  126. SUCCESS !=
  127. db_execute(
  128. "CREATE TABLE IF NOT EXISTS units("
  129. " id CHAR(12),"
  130. " monster INT,"
  131. " name CHAR(128),"
  132. " stars INT,"
  133. " level INT,"
  134. " storage INT,"
  135. " base_hp INT,"
  136. " base_atk INT,"
  137. " base_def INT,"
  138. " base_spd INT,"
  139. " base_crr INT,"
  140. " base_crd INT,"
  141. " base_res INT,"
  142. " base_acc INT,"
  143. " current_hp INT,"
  144. " current_atk INT,"
  145. " current_def INT,"
  146. " current_spd INT,"
  147. " current_crr INT,"
  148. " current_crd INT,"
  149. " current_res INT,"
  150. " current_acc INT"
  151. ");",
  152. NULL
  153. )
  154. ){
  155. fprintf(
  156. stderr,
  157. "Unable to create table units: %s\n", sqlite3_errmsg(db)
  158. );
  159. return ERROR_DB_CREATE_UNITS;
  160. }
  161. // Table info
  162. if (
  163. SUCCESS !=
  164. db_execute(
  165. "CREATE TABLE IF NOT EXISTS info("
  166. " player_id CHAR(12),"
  167. " player_name CHAR(64),"
  168. " player_level INT,"
  169. " ts DATETIME,"
  170. " modified INT"
  171. ");",
  172. NULL
  173. )
  174. ){
  175. fprintf(
  176. stderr,
  177. "Unable to create table info: %s\n", sqlite3_errmsg(db)
  178. );
  179. return ERROR_DB_CREATE_INFO;
  180. }
  181. // Tables teams and units_teams (optional)
  182. if (clear_teams == TRUE){
  183. if (
  184. SUCCESS !=
  185. db_execute(
  186. "CREATE TABLE IF NOT EXISTS "
  187. "units_teams(unit CHAR(12), team CHAR(12));",
  188. NULL
  189. )
  190. ){
  191. fprintf(
  192. stderr,
  193. "Unable to create table units_teams: %s\n", sqlite3_errmsg(db)
  194. );
  195. return ERROR_DB_CREATE_UNITS_TEAMS;
  196. }
  197. if (
  198. SUCCESS !=
  199. db_execute(
  200. "CREATE TABLE IF NOT EXISTS "
  201. "teams(id CHAR(12), name CHAR(50), priority INT);",
  202. NULL
  203. )
  204. ){
  205. fprintf(
  206. stderr,
  207. "Unable to create table teams: %s\n", sqlite3_errmsg(db)
  208. );
  209. return ERROR_DB_CREATE_TEAMS;
  210. }
  211. }
  212. return SUCCESS;
  213. }