Преглед на файлове

Code reformating and separation. Now its way faster. Rune sets bonuses are included in the computation, and the team list option has been implemented.

Iñigo Valentin преди 4 години
родител
ревизия
76e0927d47

+ 0 - 13
src/RuneOptimizer/RuneOptimizer.c

@@ -90,16 +90,3 @@ int main(int argc, char *argv[]){
         return ERROR_INPUT_INVALID_COMMAND;
     }
 }
-
-int open_database(){
-    // The database location and name is hardcoded in the same directory.
-    int db_status = sqlite3_open("../data.sqlite", &db);
-    if (db_status != SQLITE_OK) {
-        fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
-        sqlite3_close(db);
-        return ERROR_DB_CANT_OPEN;
-    }
-    else{
-        return SUCCESS;
-    }
-}

+ 8 - 14
src/RuneOptimizer/RuneOptimizer.h

@@ -15,21 +15,15 @@
  * RuneOptimizer. If not, see <https://www.gnu.org/licenses/>.
  */
 
+#define DB_PATH "../data.sqlite"
+
 /**
- * Connects to the database.
+ * Starts the program.
  *
- * Initializes the global db.
+ * Reads parameters and runs the appropiate functions.
  *
- * @return 0 on success, other on error.
- */
-int open_database();
-
-/**
- * @var Stores the database error statuses.
- */
-int db_status;
-
-/**
- * @var Database connection.
+ * @param argc Argument count.
+ * @param argv Argument list.
+ * @return SUCCESS on success, other on error.
  */
-sqlite3 *db;
+int main(int argc, char *argv[]);

+ 20 - 0
src/RuneOptimizer/help/help.c

@@ -1,3 +1,23 @@
+/*
+ * This file is part of RuneOptimizer.
+ *
+ * RuneOptimizer is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * RuneOptimizer is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * RuneOptimizer. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+/**
+ * Displays the help text.
+ */
 void show_help(){
     printf("\nRune Optimizer v0.1\n");
     printf("\n  Usage:\n");

Файловите разлики са ограничени, защото са твърде много
+ 752 - 338
src/RuneOptimizer/optimize/optimize.c


+ 128 - 9
src/RuneOptimizer/optimize/optimize.h

@@ -147,11 +147,130 @@ char set_names[][24] = {
   "FIGHT",   "DETERMINATION", "ENHANCE", "ACCURACY", "TOLERANCE"
 };
 
+/**
+ * Parses the arguments for the optimize command.
+ *
+ * @param[in] argc Argument count.
+ * @param[in] argv Argument list.
+ * @param[out] id Unit id or name.
+ * @param[out] level Level to treat the runes as.
+ * @param[out] sets List of requested sets.
+ * @param[out] stats List of requested stats.
+ * @param[out] min_stats Minimum stats for the optimization.
+ * @param[out] gui 1 to format the output for the GUI
+ * @return SUCCESS or an error code.
+ */
+int parse_optimization_arguments(
+  int argc,  char *argv[], char id[64],             char *level,
+  int *sets, int *stats,   struct Stats *min_stats, char *gui
+
+);
+
+/**
+ * Populates a Unit structure with data from the database.
+ *
+ * @param[in] id Argument count.
+ * @param[out] unit Structure to populate.
+ * @return SUCCESS or an error code.
+ */
+int retrieveUnit(char id[64], struct Unit *unit);
+
+/**
+ * Populates a Rune_Set_Count structure with the number of each rune requested
+ * for the optimization.
+ *
+ * @param[in] sets Sets requested as arguments.
+ * @param[out] requested_set_count Structure to populate.
+ * @return Number of requested runes.
+ */
+char calculateRequiredSetCount(
+  int sets[3],
+  struct Rune_Set_Count *requested_set_count
+);
+
+/**
+ * Generates the query to get the runes from the databas for even slots.
+ *
+ * @param[in] level Level to consider runes at.
+ * @param[in] sets Requested sets.
+ * @param[in] stats Requested sets for even slots.
+ * @param[out] query String with the query.
+ */
+void createQueryForEvenSlots(char level, int* sets, int* stats, char query[1500]);
+
+/**
+ * Generates the query to get the runes from the databas for odd slots.
+ *
+ * @param[in] level Level to consider runes at.
+ * @param[in] sets Requested sets.
+ * @param[out] query String with the query.
+ */
+void createQueryForOddSlots(char level, int* sets, char query[1500]);
+
+/**
+ * Gets the list of runes to test during the optimization from the database.
+ *
+ * Im assumming 600 runes per slot is a safe estimate. I hope it doesn't
+ * come back to bite me.
+ * Im using a 7 position array, and the index 0 is ignored. This is
+ * is because I REALLY NEED to use 1-indexes to match rune slots.
+ *
+ * @param[in] query_even Query to execute for even slots.
+ * @param[in] query_odd Query to execute for odd slots.
+ * @param[out] runes List of runes to populate.
+ * @param[out] rune_count Number of runes retrieved for each slot.
+ * @return SUCCESS or an error code.
+ */
+int get_runes(
+  char query_even[1500],     char query_odd[1500],
+  struct Rune runes[7][600], int rune_count[7]
+);
+
+/**
+ * Print a nice summary with all the data fethced from the command line and db.
+ *
+ * It is 17 lines high and un to 80 characters wide.
+ *
+ * @param[in] unit Unit data.
+ * @param[in] sets Requested rune sets.
+ * @param[in] stats Requested stats for even slots.
+ * @param[in] min_stats Requested minimum stats sets.
+ * @param[in] level Level to treat runes as.
+ * @param[in] rune_count Number of runes for each slot.
+ * @param[in] max_combinations Total combinations to test during optimization.
+ */
+void print_summary(
+  struct Unit *unit, int* sets, int* stats, struct Stats *min_stats,
+  char level, int rune_count[7], unsigned long max_combinations
+);
+
+/**
+ * Prints a nice summary with all de details of a result.
+ *
+ * It is 43 lines high and 97 characters wide.
+ *
+ * @param[in] unit Unit data.
+ * @param[in] result Result to display.
+ * @return SUCCESS or an error code.
+ */
+int present_option(struct Unit *unit, Result *result);
+
+/**
+ * Prepares and prints the output for the GUI.
+ *
+ * Basically, it's JSON.
+ *
+ * @param[in] results All results.
+ * @param[in] result_count Number of results.
+ * @return SUCCESS or an error code.
+ */
+int gui_output(Result results[5000], int result_count);
+
 /**
  * Starts the optimization process.
  *
- * @param argc Argument count.
- * @param argv Argument list.
+ * @param[in] argc Argument count.
+ * @param[in] argv Argument list.
  * @return 0 on success, other on error.
  */
 int optimize(int argc, char *argv[]);
@@ -161,16 +280,16 @@ int optimize(int argc, char *argv[]);
  *
  * Sorts them by rating.
  *
- * @param results List of results to sort.
- * @param total Number of results.
+ * @param[in,out] results List of results to sort.
+ * @param[in] total Number of results.
  */
 void sort_results(Result results[1000], int total);
 
 /**
  * Calculates efficient HP.
  *
- * @param hp HP stat.
- * @param def DEF stat.
+ * @param[in] hp HP stat.
+ * @param[in] def DEF stat.
  * @return Calculated EHP.
  */
 unsigned int calculate_ehp(unsigned int hp, unsigned short def);
@@ -178,9 +297,9 @@ unsigned int calculate_ehp(unsigned int hp, unsigned short def);
 /**
  * Calculates damage.
  *
- * @param atk ATK stat.
- * @param crr CRR stat.
- * @param crd CRD stat.
+ * @param[in] atk ATK stat.
+ * @param[in] crr CRR stat.
+ * @param[in] crd CRD stat.
  * @return Calculated DMG.
  */
 unsigned short calculate_dmg(unsigned short atk, unsigned short crr, unsigned short crd);

+ 22 - 7
src/RuneOptimizer/team/team.c

@@ -15,8 +15,14 @@
  * RuneOptimizer. If not, see <https://www.gnu.org/licenses/>.
  */
 
+/**
+ * Displays teams.
+ *
+ * @param argc[in] Argument count.
+ * @param argv[in] Argument list.
+ * @return 0 on success, other on error.
+ */
 int list_teams(int argc, char *argv[]){
-    printf("A");
     char id[8] = "\0";
     char query[300] = "SELECT id, name, priority FROM teams ";
     char tmp[64];
@@ -26,25 +32,34 @@ int list_teams(int argc, char *argv[]){
         sprintf(tmp, "WHERE id = '%s' OR name = '%s' ", id, id);
         strcat(query, tmp);
     }
-    printf("LISTING TEAMS %s\n%s\n", id, query);
-    if (SUCCESS != open_database()){
+    strcat(query, " ORDER BY priority DESC");
+    sqlite3 *db;
+    if (SUCCESS != sqlite3_open_v2(DB_PATH, &db, SQLITE_OPEN_READWRITE, NULL)){
+        fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
+        sqlite3_close(db);
         return ERROR_DB_CANT_OPEN;
     }
     sqlite3_stmt *stmt_teams;
-    db_status = sqlite3_prepare_v2(db, query, -1, &stmt_teams, 0);
-    if (db_status != SQLITE_OK) {
-        fprintf(stderr, "Error getting runes for slot 1: %s\n", sqlite3_errmsg(db));
+    if (SQLITE_OK != sqlite3_prepare_v2(db, query, -1, &stmt_teams, 0)) {
+        fprintf(stderr, "Error getting teams: %s\n", sqlite3_errmsg(db));
         return ERROR_DB_RUNES_SLOT;
     }
 
+    printf(" --------------------------------------------------\n");
+    printf(" | ID   | Name                             | Prio |\n");
+    printf(" --------------------------------------------------\n");
     while (1 == 1){
         int status = sqlite3_step(stmt_teams);
         if (status == SQLITE_ROW){
-            printf("OK %s\n", sqlite3_column_text(stmt_teams, 2));
+            printf(" | %*s | %*s | %*d |\n", 4, sqlite3_column_text(stmt_teams, 0), -32, sqlite3_column_text(stmt_teams, 1), 4, sqlite3_column_int(stmt_teams, 2));
 
         }
         else{
             break;
         }
     }
+    printf(" --------------------------------------------------\n");
+    sqlite3_finalize(stmt_teams);
+    sqlite3_close(db);
+    return SUCCESS;
 }

+ 2 - 2
src/RuneOptimizer/team/team.h

@@ -18,8 +18,8 @@
 /**
  * Displays teams.
  *
- * @param argc Argument count.
- * @param argv Argument list.
+ * @param argc[in] Argument count.
+ * @param argv[in] Argument list.
  * @return 0 on success, other on error.
  */
 int list_teams(int argc, char *argv[]);

Някои файлове не бяха показани, защото твърде много файлове са промени