|
|
@@ -24,21 +24,51 @@
|
|
|
*/
|
|
|
int update(int argc, char *argv[]){
|
|
|
|
|
|
+ // Options
|
|
|
+ char six_stars = FALSE;
|
|
|
+ char with_runes = FALSE;
|
|
|
+ char clear_teams = FALSE;
|
|
|
+ char gui = FALSE;
|
|
|
+
|
|
|
// One argument mandatory
|
|
|
if (argc == 0){
|
|
|
fprintf(
|
|
|
stderr,
|
|
|
- "Team creation takes 1 or 2 arguments, %d supplied\n", argc - 3
|
|
|
+ "Team creation takes 1 or more arguments, %d supplied\n", argc - 3
|
|
|
);
|
|
|
return ERROR_INPUT_UPDATE_NO_ARGUMENTS;
|
|
|
}
|
|
|
|
|
|
+ // Parse options, skip first arg
|
|
|
+ for (int i = 1; i < argc; i ++){
|
|
|
+ if (strcmp("--six-stars", argv[i]) == 0 || strcmp("-s", argv[i]) == 0){
|
|
|
+ six_stars = TRUE;
|
|
|
+ }
|
|
|
+ else if (
|
|
|
+ strcmp("--with-runes", argv[i]) == 0 || strcmp("-r", argv[i]) == 0
|
|
|
+ ){
|
|
|
+ with_runes = TRUE;
|
|
|
+ }
|
|
|
+ else if (
|
|
|
+ strcmp("--clear-teams", argv[i]) == 0 || strcmp("-t", argv[i]) == 0
|
|
|
+ ){
|
|
|
+ clear_teams = TRUE;
|
|
|
+ }
|
|
|
+ else if (
|
|
|
+ strcmp("--gui", argv[i]) == 0 || strcmp("-g", argv[i]) == 0
|
|
|
+ ){
|
|
|
+ gui = TRUE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// First, test json
|
|
|
if (strlen(argv[0]) > 5){
|
|
|
char extension[6];
|
|
|
strncpy(extension, argv[0] + strlen(argv[0]) - 5, 5);
|
|
|
if (strcmp(extension, ".json") == 0){
|
|
|
- return update_json(argv[0]);
|
|
|
+ return update_json(
|
|
|
+ argv[0], six_stars, with_runes, clear_teams, gui
|
|
|
+ );
|
|
|
}
|
|
|
}
|
|
|
// TODO: More ifs as more options are implemented...
|
|
|
@@ -50,10 +80,18 @@ int update(int argc, char *argv[]){
|
|
|
* Updates the database from a json file.
|
|
|
*
|
|
|
* @param[in] file Path to the file.
|
|
|
- * @return SUCCESS or an error code..
|
|
|
+ * @param[in] six_stars Indicator to import only 6* units. Units with runes will
|
|
|
+ * be saved anyway.
|
|
|
+ * @param[in] with_runes Indicator to import only units with runes.
|
|
|
+ * @param[in] clear_teams Indicator to recreate tables teams and units_teams.
|
|
|
+ * @param[in] gui Indicator to format output for thr GUI.
|
|
|
+ * @return SUCCESS or an error code.
|
|
|
*/
|
|
|
-int update_json(char file[]){
|
|
|
- printf("Updating from json file: %s\n", file);
|
|
|
+int update_json(
|
|
|
+ char file[], unsigned char six_stars,
|
|
|
+ unsigned char with_runes, unsigned char clear_teams, unsigned char gui
|
|
|
+){
|
|
|
+ if (gui == FALSE) printf("Updating from json file: %s\n", file);
|
|
|
|
|
|
// Read the JSON content
|
|
|
json_object *root = json_object_from_file(file);
|
|
|
@@ -63,23 +101,34 @@ int update_json(char file[]){
|
|
|
}
|
|
|
|
|
|
json_object *wizard_id = json_object_object_get(root, "wizard_id");
|
|
|
- printf("\tPlayer ID: %s\n", json_object_get_string(wizard_id));
|
|
|
|
|
|
json_object *wizard_info = json_object_object_get(root, "wizard_info");
|
|
|
json_object *wizard_name = json_object_object_get(wizard_info, "wizard_name");
|
|
|
- printf("\tPlayer name: %s\n", json_object_get_string(wizard_name));
|
|
|
- json_object *wizard_level = json_object_object_get(wizard_info, "wizard_level");
|
|
|
- printf("\tPlayer level: %s\n", json_object_get_string(wizard_level));
|
|
|
+ json_object *wizard_level =
|
|
|
+ json_object_object_get(wizard_info, "wizard_level");
|
|
|
+ if (gui == TRUE){
|
|
|
+ printf("Player ID: %s\n", json_object_get_string(wizard_id));
|
|
|
+ printf("Player name: %s\n", json_object_get_string(wizard_name));
|
|
|
+ printf("Player level: %s\n", json_object_get_string(wizard_level));
|
|
|
+ fflush(stdout);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ printf("\tPlayer ID: %s\n", json_object_get_string(wizard_id));
|
|
|
+ printf("\tPlayer name: %s\n", json_object_get_string(wizard_name));
|
|
|
+ printf("\tPlayer level: %s\n", json_object_get_string(wizard_level));
|
|
|
+ }
|
|
|
|
|
|
// Recreate the database tables
|
|
|
- int status = recreate_tables();
|
|
|
+ int status = recreate_tables(clear_teams);
|
|
|
if (SUCCESS != status){
|
|
|
return status;
|
|
|
}
|
|
|
|
|
|
// Open the database connection
|
|
|
sqlite3 *db;
|
|
|
- if (SUCCESS != sqlite3_open_v2(db_location, &db, SQLITE_OPEN_READWRITE, NULL)){
|
|
|
+ if (
|
|
|
+ SUCCESS != sqlite3_open_v2(db_location, &db, SQLITE_OPEN_READWRITE, NULL)
|
|
|
+ ){
|
|
|
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
|
|
|
sqlite3_close(db);
|
|
|
return ERROR_DB_CANT_OPEN;
|
|
|
@@ -146,7 +195,6 @@ int update_json(char file[]){
|
|
|
rune.stats[i].enchant = json_object_get_int(enchant);
|
|
|
json_object *grind = json_object_array_get_idx(stat_idx, 3);
|
|
|
rune.stats[i].grind = json_object_get_int(grind);
|
|
|
- //printf("\t\t\tStat %d: %d: (+%d, %d)\n", json_object_get_int(stat), json_object_get_int(value), json_object_get_int(grind), json_object_get_int(enchant));
|
|
|
}
|
|
|
|
|
|
// All the rune info has been loaded. Now, calculate the rest of stats
|
|
|
@@ -381,15 +429,17 @@ int update_json(char file[]){
|
|
|
|
|
|
// Parse units
|
|
|
unsigned int total_units = 0;
|
|
|
+ unsigned char unit_has_runes = FALSE;
|
|
|
json_object *unit_list = json_object_object_get(root, "unit_list");
|
|
|
int unit_count = json_object_array_length(unit_list);
|
|
|
for (int i = 0; i < unit_count; i++){
|
|
|
- total_units ++;
|
|
|
+ unit_has_runes = FALSE;
|
|
|
struct DB_Unit unit;
|
|
|
idx = json_object_array_get_idx(unit_list, i);
|
|
|
json_object *unit_id = json_object_object_get(idx, "unit_id");
|
|
|
strcpy(unit.id, json_object_get_string(unit_id));
|
|
|
- json_object *unit_master_id = json_object_object_get(idx, "unit_master_id");
|
|
|
+ json_object *unit_master_id =
|
|
|
+ json_object_object_get(idx, "unit_master_id");
|
|
|
unit.monster= json_object_get_int(unit_master_id);
|
|
|
get_monster_name(unit.monster, unit.name);
|
|
|
json_object *unit_level = json_object_object_get(idx, "unit_level");
|
|
|
@@ -410,95 +460,24 @@ int update_json(char file[]){
|
|
|
unit.base_res = json_object_get_int(resist);
|
|
|
json_object *accuracy = json_object_object_get(idx, "accuracy");
|
|
|
unit.base_acc = json_object_get_int(accuracy);
|
|
|
- json_object *critical_rate = json_object_object_get(idx, "critical_rate");
|
|
|
+ json_object *critical_rate =
|
|
|
+ json_object_object_get(idx, "critical_rate");
|
|
|
unit.base_crr = json_object_get_int(critical_rate);
|
|
|
- json_object *critical_damage = json_object_object_get(idx, "critical_damage");
|
|
|
+ json_object *critical_damage =
|
|
|
+ json_object_object_get(idx, "critical_damage");
|
|
|
unit.base_crd = json_object_get_int(critical_damage);
|
|
|
- json_object *homunculus_name = json_object_object_get(idx, "homunculus_name");
|
|
|
+ json_object *homunculus_name =
|
|
|
+ json_object_object_get(idx, "homunculus_name");
|
|
|
if (strlen(json_object_get_string(homunculus_name)) > 0){
|
|
|
strcpy(unit.name, json_object_get_string(homunculus_name));
|
|
|
}
|
|
|
|
|
|
- // Insert unit
|
|
|
- sqlite3_stmt *res;
|
|
|
- char sql[2000];
|
|
|
- strcpy(
|
|
|
- sql,
|
|
|
- "INSERT INTO units ("
|
|
|
- " id,"
|
|
|
- " monster,"
|
|
|
- " name,"
|
|
|
- " stars,"
|
|
|
- " level,"
|
|
|
- " base_hp,"
|
|
|
- " base_atk,"
|
|
|
- " base_def,"
|
|
|
- " base_spd,"
|
|
|
- " base_crr,"
|
|
|
- " base_crd,"
|
|
|
- " base_res,"
|
|
|
- " base_acc,"
|
|
|
- " current_hp,"
|
|
|
- " current_atk,"
|
|
|
- " current_def,"
|
|
|
- " current_spd,"
|
|
|
- " current_crr,"
|
|
|
- " current_crd,"
|
|
|
- " current_res,"
|
|
|
- " current_acc"
|
|
|
- ") VALUES ("
|
|
|
- " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?"
|
|
|
- ")"
|
|
|
- );
|
|
|
- if (SQLITE_OK != sqlite3_prepare_v2(db, sql, -1, &res, 0)) {
|
|
|
- fprintf(
|
|
|
- stderr,
|
|
|
- "Failed to prepare statement to insert into runes: %s\n",
|
|
|
- sqlite3_errmsg(db)
|
|
|
- );
|
|
|
- sqlite3_finalize(res);
|
|
|
- sqlite3_close(db);
|
|
|
- return ERROR_DB_INSERT_RUNES;
|
|
|
- }
|
|
|
- sqlite3_bind_text(res, 1, unit.id, strlen(unit.id), NULL);
|
|
|
- sqlite3_bind_int(res, 2, unit.monster);
|
|
|
- sqlite3_bind_text(res, 3, unit.name, strlen(unit.name), NULL);
|
|
|
- sqlite3_bind_int(res, 4, unit.stars);
|
|
|
- sqlite3_bind_int(res, 5, unit.level);
|
|
|
- sqlite3_bind_int(res, 6, unit.base_hp);
|
|
|
- sqlite3_bind_int(res, 7, unit.base_atk);
|
|
|
- sqlite3_bind_int(res, 8, unit.base_def);
|
|
|
- sqlite3_bind_int(res, 9, unit.base_spd);
|
|
|
- sqlite3_bind_int(res, 10, unit.base_crr);
|
|
|
- sqlite3_bind_int(res, 11, unit.base_crd);
|
|
|
- sqlite3_bind_int(res, 12, unit.base_res);
|
|
|
- sqlite3_bind_int(res, 13, unit.base_acc);
|
|
|
- // Current stats, instert as base, will be calculated later.
|
|
|
- sqlite3_bind_int(res, 14, unit.base_atk);
|
|
|
- sqlite3_bind_int(res, 15, unit.base_atk);
|
|
|
- sqlite3_bind_int(res, 16, unit.base_def);
|
|
|
- sqlite3_bind_int(res, 17, unit.base_spd);
|
|
|
- sqlite3_bind_int(res, 18, unit.base_crr);
|
|
|
- sqlite3_bind_int(res, 19, unit.base_crd);
|
|
|
- sqlite3_bind_int(res, 20, unit.base_res);
|
|
|
- sqlite3_bind_int(res, 21, unit.base_acc);
|
|
|
- if (SQLITE_DONE != sqlite3_step(res)){
|
|
|
- fprintf(
|
|
|
- stderr,
|
|
|
- "Failed to execute statement to insert into units: %s\n",
|
|
|
- sqlite3_errmsg(db)
|
|
|
- );
|
|
|
- sqlite3_finalize(res);
|
|
|
- sqlite3_close(db);
|
|
|
- return ERROR_DB_INSERT_RUNES;
|
|
|
- }
|
|
|
- sqlite3_finalize(res);
|
|
|
-
|
|
|
// Parse unit runes
|
|
|
json_object *unit_runes = json_object_object_get(idx, "runes");
|
|
|
rune_count = json_object_array_length(unit_runes);
|
|
|
|
|
|
for (int i = 0; i < rune_count; i++){
|
|
|
+ unit_has_runes = TRUE;
|
|
|
total_runes ++;
|
|
|
struct DB_Rune rune;
|
|
|
strcpy(rune.unit, unit.id);
|
|
|
@@ -516,7 +495,8 @@ int update_json(char file[]){
|
|
|
json_object *set_id = json_object_object_get(idx, "set_id");
|
|
|
rune.set = json_object_get_int(set_id);
|
|
|
// upgrade_curre -> Power up level
|
|
|
- json_object *upgrade_curr = json_object_object_get(idx, "upgrade_curr");
|
|
|
+ json_object *upgrade_curr =
|
|
|
+ json_object_object_get(idx, "upgrade_curr");
|
|
|
rune.level = json_object_get_int(upgrade_curr);
|
|
|
json_object *base_value = json_object_object_get(idx, "base_value");
|
|
|
rune.buy_value = json_object_get_int(base_value);
|
|
|
@@ -535,7 +515,8 @@ int update_json(char file[]){
|
|
|
// Innate stat
|
|
|
json_object *prefix_stat = json_object_array_get_idx(prefix_eff, 0);
|
|
|
rune.innate.stat = json_object_get_int(prefix_stat);
|
|
|
- json_object *prefix_value = json_object_array_get_idx(prefix_eff, 1);
|
|
|
+ json_object *prefix_value =
|
|
|
+ json_object_array_get_idx(prefix_eff, 1);
|
|
|
rune.innate.value = json_object_get_int(prefix_value);
|
|
|
|
|
|
json_object *sec_eff = json_object_object_get(idx, "sec_eff");
|
|
|
@@ -555,7 +536,8 @@ int update_json(char file[]){
|
|
|
rune.stats[i].grind = json_object_get_int(grind);
|
|
|
}
|
|
|
|
|
|
- // All the rune info has been loaded. Now, calculate the rest of stats
|
|
|
+ // All the rune info has been loaded. Now, calculate the rest of
|
|
|
+ // stats
|
|
|
calculate_rune_totals(&rune);
|
|
|
|
|
|
// Insert the rune into the database
|
|
|
@@ -608,8 +590,8 @@ int update_json(char file[]){
|
|
|
" lv15_acc,"
|
|
|
" lv15_res"
|
|
|
") VALUES ("
|
|
|
- " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,"
|
|
|
- " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?"
|
|
|
+ " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,"
|
|
|
+ " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?"
|
|
|
")"
|
|
|
);
|
|
|
if (SQLITE_OK != sqlite3_prepare_v2(db, sql, -1, &res, 0)) {
|
|
|
@@ -722,7 +704,8 @@ int update_json(char file[]){
|
|
|
if (SQLITE_OK != sqlite3_prepare_v2(db, sql, -1, &res, 0)) {
|
|
|
fprintf(
|
|
|
stderr,
|
|
|
- "Failed to prepare statement to insert into rune_stats: %s\n",
|
|
|
+ "Failed to prepare statement to "
|
|
|
+ "insert into rune_stats: %s\n",
|
|
|
sqlite3_errmsg(db)
|
|
|
);
|
|
|
sqlite3_finalize(res);
|
|
|
@@ -738,7 +721,8 @@ int update_json(char file[]){
|
|
|
if (SQLITE_DONE != sqlite3_step(res)){
|
|
|
fprintf(
|
|
|
stderr,
|
|
|
- "Failed to execute statement to insert into rune_stats: %s\n",
|
|
|
+ "Failed to execute statement to "
|
|
|
+ "insert into rune_stats: %s\n",
|
|
|
sqlite3_errmsg(db)
|
|
|
);
|
|
|
sqlite3_finalize(res);
|
|
|
@@ -758,7 +742,8 @@ int update_json(char file[]){
|
|
|
if (SQLITE_OK != sqlite3_prepare_v2(db, sql, -1, &res, 0)) {
|
|
|
fprintf(
|
|
|
stderr,
|
|
|
- "Failed to prepare statement to insert into rune_stats: %s\n",
|
|
|
+ "Failed to prepare statement to "
|
|
|
+ "insert into rune_stats: %s\n",
|
|
|
sqlite3_errmsg(db)
|
|
|
);
|
|
|
sqlite3_finalize(res);
|
|
|
@@ -774,7 +759,8 @@ int update_json(char file[]){
|
|
|
if (SQLITE_DONE != sqlite3_step(res)){
|
|
|
fprintf(
|
|
|
stderr,
|
|
|
- "Failed to execute statement to insert into rune_stats: %s\n",
|
|
|
+ "Failed to execute statement to "
|
|
|
+ "insert into rune_stats: %s\n",
|
|
|
sqlite3_errmsg(db)
|
|
|
);
|
|
|
sqlite3_finalize(res);
|
|
|
@@ -785,6 +771,90 @@ int update_json(char file[]){
|
|
|
sqlite3_finalize(res);
|
|
|
}
|
|
|
|
|
|
+ // Insert unit, depending on flags and status
|
|
|
+ if (
|
|
|
+ unit_has_runes == TRUE ||
|
|
|
+ (six_stars == FALSE && with_runes == FALSE) ||
|
|
|
+ (six_stars == TRUE && unit.stars == TRUE)
|
|
|
+ ){
|
|
|
+ total_units ++;
|
|
|
+ sqlite3_stmt *res;
|
|
|
+ char sql[2000];
|
|
|
+ strcpy(
|
|
|
+ sql,
|
|
|
+ "INSERT INTO units ("
|
|
|
+ " id,"
|
|
|
+ " monster,"
|
|
|
+ " name,"
|
|
|
+ " stars,"
|
|
|
+ " level,"
|
|
|
+ " base_hp,"
|
|
|
+ " base_atk,"
|
|
|
+ " base_def,"
|
|
|
+ " base_spd,"
|
|
|
+ " base_crr,"
|
|
|
+ " base_crd,"
|
|
|
+ " base_res,"
|
|
|
+ " base_acc,"
|
|
|
+ " current_hp,"
|
|
|
+ " current_atk,"
|
|
|
+ " current_def,"
|
|
|
+ " current_spd,"
|
|
|
+ " current_crr,"
|
|
|
+ " current_crd,"
|
|
|
+ " current_res,"
|
|
|
+ " current_acc,"
|
|
|
+ " storage"
|
|
|
+ ") VALUES ("
|
|
|
+ " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,"
|
|
|
+ " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?"
|
|
|
+ ")"
|
|
|
+ );
|
|
|
+ if (SQLITE_OK != sqlite3_prepare_v2(db, sql, -1, &res, 0)) {
|
|
|
+ fprintf(
|
|
|
+ stderr,
|
|
|
+ "Failed to prepare statement to insert into units: %s\n",
|
|
|
+ sqlite3_errmsg(db)
|
|
|
+ );
|
|
|
+ sqlite3_finalize(res);
|
|
|
+ sqlite3_close(db);
|
|
|
+ return ERROR_DB_INSERT_RUNES;
|
|
|
+ }
|
|
|
+ sqlite3_bind_text(res, 1, unit.id, strlen(unit.id), NULL);
|
|
|
+ sqlite3_bind_int(res, 2, unit.monster);
|
|
|
+ sqlite3_bind_text(res, 3, unit.name, strlen(unit.name), NULL);
|
|
|
+ sqlite3_bind_int(res, 4, unit.stars);
|
|
|
+ sqlite3_bind_int(res, 5, unit.level);
|
|
|
+ sqlite3_bind_int(res, 6, unit.base_hp);
|
|
|
+ sqlite3_bind_int(res, 7, unit.base_atk);
|
|
|
+ sqlite3_bind_int(res, 8, unit.base_def);
|
|
|
+ sqlite3_bind_int(res, 9, unit.base_spd);
|
|
|
+ sqlite3_bind_int(res, 10, unit.base_crr);
|
|
|
+ sqlite3_bind_int(res, 11, unit.base_crd);
|
|
|
+ sqlite3_bind_int(res, 12, unit.base_res);
|
|
|
+ sqlite3_bind_int(res, 13, unit.base_acc);
|
|
|
+ // Current stats, instert as base, will be calculated later.
|
|
|
+ sqlite3_bind_int(res, 14, unit.base_atk);
|
|
|
+ sqlite3_bind_int(res, 15, unit.base_atk);
|
|
|
+ sqlite3_bind_int(res, 16, unit.base_def);
|
|
|
+ sqlite3_bind_int(res, 17, unit.base_spd);
|
|
|
+ sqlite3_bind_int(res, 18, unit.base_crr);
|
|
|
+ sqlite3_bind_int(res, 19, unit.base_crd);
|
|
|
+ sqlite3_bind_int(res, 20, unit.base_res);
|
|
|
+ sqlite3_bind_int(res, 21, unit.base_acc);
|
|
|
+ sqlite3_bind_int(res, 21, 0); // TODO: Storage
|
|
|
+ if (SQLITE_DONE != sqlite3_step(res)){
|
|
|
+ fprintf(
|
|
|
+ stderr,
|
|
|
+ "Failed to execute statement to insert into units: %s\n",
|
|
|
+ sqlite3_errmsg(db)
|
|
|
+ );
|
|
|
+ sqlite3_finalize(res);
|
|
|
+ sqlite3_close(db);
|
|
|
+ return ERROR_DB_INSERT_RUNES;
|
|
|
+ }
|
|
|
+ sqlite3_finalize(res);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
int calculate_result = calculate_current_stats();
|
|
|
@@ -796,8 +866,16 @@ int update_json(char file[]){
|
|
|
time_t t = time(NULL);
|
|
|
struct tm tm = *localtime(&t);
|
|
|
char ts[130];
|
|
|
- sprintf(ts, "%d-%02d-%02d %02d:%02d:%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
|
|
|
- //printf("now: %d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
|
|
|
+ sprintf(
|
|
|
+ ts,
|
|
|
+ "%d-%02d-%02d %02d:%02d:%02d",
|
|
|
+ tm.tm_year + 1900,
|
|
|
+ tm.tm_mon + 1,
|
|
|
+ tm.tm_mday,
|
|
|
+ tm.tm_hour,
|
|
|
+ tm.tm_min,
|
|
|
+ tm.tm_sec
|
|
|
+ );
|
|
|
sqlite3_stmt *stmt_info;
|
|
|
char sql[1000];
|
|
|
strcpy(
|
|
|
@@ -815,9 +893,15 @@ int update_json(char file[]){
|
|
|
sqlite3_close(db);
|
|
|
return ERROR_DB_INSERT_INFO;
|
|
|
}
|
|
|
- sqlite3_bind_text(stmt_info, 1, json_object_get_string(wizard_id), strlen(json_object_get_string(wizard_id)), NULL);
|
|
|
- sqlite3_bind_text(stmt_info, 2, json_object_get_string(wizard_name), strlen(json_object_get_string(wizard_name)), NULL);
|
|
|
- sqlite3_bind_int(stmt_info, 3, json_object_get_string(wizard_level));
|
|
|
+ sqlite3_bind_text(
|
|
|
+ stmt_info, 1, json_object_get_string(wizard_id),
|
|
|
+ strlen(json_object_get_string(wizard_id)), NULL
|
|
|
+ );
|
|
|
+ sqlite3_bind_text(
|
|
|
+ stmt_info, 2, json_object_get_string(wizard_name),
|
|
|
+ strlen(json_object_get_string(wizard_name)), NULL
|
|
|
+ );
|
|
|
+ sqlite3_bind_int(stmt_info, 3, atoi(json_object_get_string(wizard_level)));
|
|
|
sqlite3_bind_text(stmt_info, 4, ts, strlen(ts), NULL);
|
|
|
sqlite3_bind_int(stmt_info, 5, 0);
|
|
|
if (SQLITE_DONE != sqlite3_step(stmt_info)){
|
|
|
@@ -835,12 +919,11 @@ int update_json(char file[]){
|
|
|
printf("%d units saved.\n", total_units);
|
|
|
printf("%d runes saved.\n", total_runes);
|
|
|
printf("%d rune stats saved.\n", total_stats);
|
|
|
- printf("Player info updated");
|
|
|
+ printf("Player info updated\n");
|
|
|
|
|
|
+ sqlite3_finalize(stmt_info);
|
|
|
sqlite3_close(db);
|
|
|
-
|
|
|
-
|
|
|
- return
|
|
|
+ return SUCCESS;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -1138,10 +1221,15 @@ int calculate_current_stats(){
|
|
|
|
|
|
/**
|
|
|
* Recreates the tables in the database.
|
|
|
+ *
|
|
|
+ * @param[in] clear_teams Indicator to recreate tables teams and units_teams.
|
|
|
+ * @return SUCCESS or an error code.
|
|
|
*/
|
|
|
-int recreate_tables(){
|
|
|
+int recreate_tables(unsigned char clear_teams){
|
|
|
sqlite3 *db;
|
|
|
- if (SUCCESS != sqlite3_open_v2(db_location, &db, SQLITE_OPEN_READWRITE, NULL)){
|
|
|
+ if (
|
|
|
+ SUCCESS != sqlite3_open_v2(db_location, &db, SQLITE_OPEN_READWRITE, NULL)
|
|
|
+ ){
|
|
|
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
|
|
|
sqlite3_close(db);
|
|
|
return ERROR_DB_CANT_OPEN;
|
|
|
@@ -1324,6 +1412,7 @@ int recreate_tables(){
|
|
|
" name CHAR(128),"
|
|
|
" stars INT,"
|
|
|
" level INT,"
|
|
|
+ " storage INT,"
|
|
|
" base_hp INT,"
|
|
|
" base_atk INT,"
|
|
|
" base_def INT,"
|
|
|
@@ -1415,7 +1504,96 @@ int recreate_tables(){
|
|
|
sqlite3_close(db);
|
|
|
return ERROR_DB_CREATE_INFO;
|
|
|
}
|
|
|
- // TODO: Other tables
|
|
|
+ // Tables teams and units_teams (optional)
|
|
|
+ if (clear_teams == TRUE){
|
|
|
+ strcpy(sql, "DROP TABLE IF EXISTS units_teams;");
|
|
|
+ if (SQLITE_OK != sqlite3_prepare_v2(db, sql, -1, &res, 0)) {
|
|
|
+ fprintf(
|
|
|
+ stderr,
|
|
|
+ "Failed to create statement to drop table units_teams: %s\n",
|
|
|
+ sqlite3_errmsg(db)
|
|
|
+ );
|
|
|
+ sqlite3_finalize(res);
|
|
|
+ sqlite3_close(db);
|
|
|
+ return ERROR_DB_DROP_UNITS_TEAMS;
|
|
|
+ }
|
|
|
+ if (SQLITE_DONE != sqlite3_step(res)){
|
|
|
+ fprintf(
|
|
|
+ stderr,
|
|
|
+ "Failed to execute statement to drop table units_teams: %s\n",
|
|
|
+ sqlite3_errmsg(db)
|
|
|
+ );
|
|
|
+ sqlite3_finalize(res);
|
|
|
+ sqlite3_close(db);
|
|
|
+ return ERROR_DB_DROP_UNITS_TEAMS;
|
|
|
+ }
|
|
|
+ strcpy(sql, "CREATE TABLE units_teams(unit CHAR(12), team CHAR(12));");
|
|
|
+ if (SQLITE_OK != sqlite3_prepare_v2(db, sql, -1, &res, 0)) {
|
|
|
+ fprintf(
|
|
|
+ stderr,
|
|
|
+ "Failed to create statement to create table units_teams: %s\n",
|
|
|
+ sqlite3_errmsg(db)
|
|
|
+ );
|
|
|
+ sqlite3_finalize(res);
|
|
|
+ sqlite3_close(db);
|
|
|
+ return ERROR_DB_CREATE_UNITS_TEAMS;
|
|
|
+ }
|
|
|
+ if (SQLITE_DONE != sqlite3_step(res)){
|
|
|
+ fprintf(
|
|
|
+ stderr,
|
|
|
+ "Failed to execute statement to create table units_teams: %s\n",
|
|
|
+ sqlite3_errmsg(db)
|
|
|
+ );
|
|
|
+ sqlite3_finalize(res);
|
|
|
+ sqlite3_close(db);
|
|
|
+ return ERROR_DB_CREATE_UNITS_TEAMS;
|
|
|
+ }
|
|
|
+ strcpy(sql, "DROP TABLE IF EXISTS teams;");
|
|
|
+ if (SQLITE_OK != sqlite3_prepare_v2(db, sql, -1, &res, 0)) {
|
|
|
+ fprintf(
|
|
|
+ stderr,
|
|
|
+ "Failed to create statement to drop table teams: %s\n",
|
|
|
+ sqlite3_errmsg(db)
|
|
|
+ );
|
|
|
+ sqlite3_finalize(res);
|
|
|
+ sqlite3_close(db);
|
|
|
+ return ERROR_DB_DROP_TEAMS;
|
|
|
+ }
|
|
|
+ if (SQLITE_DONE != sqlite3_step(res)){
|
|
|
+ fprintf(
|
|
|
+ stderr,
|
|
|
+ "Failed to execute statement to drop table teams: %s\n",
|
|
|
+ sqlite3_errmsg(db)
|
|
|
+ );
|
|
|
+ sqlite3_finalize(res);
|
|
|
+ sqlite3_close(db);
|
|
|
+ return ERROR_DB_DROP_TEAMS;
|
|
|
+ }
|
|
|
+ strcpy(
|
|
|
+ sql,
|
|
|
+ "CREATE TABLE teams(unit CHAR(12), name CHAR(50), priority INT);"
|
|
|
+ );
|
|
|
+ if (SQLITE_OK != sqlite3_prepare_v2(db, sql, -1, &res, 0)) {
|
|
|
+ fprintf(
|
|
|
+ stderr,
|
|
|
+ "Failed to create statement to create table teams: %s\n",
|
|
|
+ sqlite3_errmsg(db)
|
|
|
+ );
|
|
|
+ sqlite3_finalize(res);
|
|
|
+ sqlite3_close(db);
|
|
|
+ return ERROR_DB_CREATE_TEAMS;
|
|
|
+ }
|
|
|
+ if (SQLITE_DONE != sqlite3_step(res)){
|
|
|
+ fprintf(
|
|
|
+ stderr,
|
|
|
+ "Failed to execute statement to create table teams: %s\n",
|
|
|
+ sqlite3_errmsg(db)
|
|
|
+ );
|
|
|
+ sqlite3_finalize(res);
|
|
|
+ sqlite3_close(db);
|
|
|
+ return ERROR_DB_CREATE_TEAMS;
|
|
|
+ }
|
|
|
+ }
|
|
|
sqlite3_finalize(res);
|
|
|
sqlite3_close(db);
|
|
|
return SUCCESS;
|