Răsfoiți Sursa

Fixed a bug on rune selection for the optimizer.
Fixed a bug when there are more threads than combinations to test.
'gui' command renamerd to 'init'.

Iñigo Valentin 4 ani în urmă
părinte
comite
575e981238

+ 2 - 2
src/cli/runeoptimizer/help/help.c

@@ -50,9 +50,9 @@ void help(){
       "    Displays info about the player. It takes no options.\n"
       "\n"
       "\n"
-      "  Command: gui\n"
+      "  Command: init\n"
       "\n"
-      "    Sets up the database to be used by the GUI. No need to run it manually.\n"
+      "    Sets up the database. No need to run it manually.\n"
       "\n"
       "    Usage\n"
       "\n"

+ 4 - 3
src/cli/runeoptimizer/gui/gui.c → src/cli/runeoptimizer/init/init.c

@@ -21,13 +21,14 @@
  * Implementation of the functions used by the gui command.
  *
  * This file implements the functions used by the gui command declared in
- * {@link gui.h}.
+ * {@link init.h}.
  */
 
+#include "../init/init.h"
+
 #include <stddef.h>
-#include "gui.h"
 #include "../db/db.h"
 
-int gui(){
+int init(){
     return(db_open(NULL));
 }

+ 2 - 2
src/cli/runeoptimizer/gui/gui.h → src/cli/runeoptimizer/init/init.h

@@ -21,7 +21,7 @@
  * Declarations of the functions related to the gui command.
  *
  * This file declares all the functions used by the gui command implemented in
- * {@link gui.c}.
+ * {@link init.c}.
  */
 
 #pragma once
@@ -35,4 +35,4 @@
  * @return {@link SUCCESS} or {@link ERROR_DB_CANT_OPEN} if the database
  * can't be created or accessed.
  */
-int gui();
+int init();

+ 52 - 56
src/cli/runeoptimizer/optimize/optimize.c

@@ -190,12 +190,10 @@ static void set_default_mins(Optimizer_Data *data);
  * order and in descending direction, and it will be limited to
  * {@link LIMIT_RUNES_PER_SLOT} per slot.
  *
- * @param[in] options Options passed to the optimizer.
+ * @param[in] data Optimizer data package. Unit member must be populated.
  * @param[out] query String with the query.
  */
-static void query_for_even_slots(
-    Optimizer_Filters *options, char query[RUNE_QUERY_LEN]
-);
+static void query_for_even_slots(Optimizer_Data *data, char *query);
 
 /**
  * Generates the query to get the runes from the database for odd slots.
@@ -209,12 +207,10 @@ static void query_for_even_slots(
  * order and in descending direction, and it will be limited to
  * {@link LIMIT_RUNES_PER_SLOT} per slot.
  *
- * @param[in] options Options passed to the optimizer.
+ * @param[in] data Optimizer data package. Unit member must be populated.
  * @param[out] query String with the query.
  */
-static void query_for_odd_slots(
-  Optimizer_Filters *options, char query[RUNE_QUERY_LEN]
-);
+static void query_for_odd_slots(Optimizer_Data *data, char *query);
 
 /**
  * Gets the list of runes to test during the optimization from the database.
@@ -241,8 +237,7 @@ static void query_for_odd_slots(
  * Negative values are error codes defined in {@link error.h}.
  */
 static int select_runes(
-  char query_even[RUNE_QUERY_LEN], char query_odd[RUNE_QUERY_LEN],
-  Optimizer_Data * data
+  char *query_even, char *query_odd, Optimizer_Data *data
 );
 
 
@@ -491,8 +486,8 @@ extern int optimize(int argc, char *argv[]){
     // Set minimum values for filters on stats not set manually.
     set_default_mins(&opt_data);
     if ((status = calculate_rune_count(&opt_data)) < 0) return status;
-    query_for_odd_slots(opt_data.filters, query_odd);
-    query_for_even_slots(opt_data.filters, query_even);
+    query_for_odd_slots(&opt_data, query_odd);
+    query_for_even_slots(&opt_data, query_even);
     // Get selected runes.
     select_runes(query_even, query_odd, &opt_data);
     for (int i = 1; i < RUNE_SLOTS + 1; i ++){
@@ -1028,14 +1023,12 @@ static int read_unit(unsigned char id[UNIT_NAME_LEN], Unit *unit){
     return(SUCCESS);
 }
 
-static void query_for_even_slots(
-    Optimizer_Filters *options, char query[RUNE_QUERY_LEN]
-){
+static void query_for_even_slots(Optimizer_Data *data, char *query){
     char column[10];
     char cur_set[MAX_SETS];
     char stat_set[DIFFERENT_STATS];
-    if (options->level == LV12) strcpy(column, "lv12_");
-    else if (options->level == LV15) strcpy(column, "lv15_");
+    if (data->filters->level == LV12) strcpy(column, "lv12_");
+    else if (data->filters->level == LV15) strcpy(column, "lv15_");
     else strcpy(column, "current_");
     strcpy(query, "SELECT id, unit, slot, type, ");
     strcat(query, column);
@@ -1062,15 +1055,15 @@ static void query_for_even_slots(
     strcat(query, "acc\nFROM runes\nWHERE\n  slot = ? -- Slot\n");
     strcat(query, "  AND type IN (");
     for (int i = 0; i < MAX_SETS; i ++){
-        if (options->sets[i] != 0){
-            sprintf(cur_set, "%d", options->sets[i]);
+        if (data->filters->sets[i] != 0){
+            sprintf(cur_set, "%d", data->filters->sets[i]);
             strcat(query, cur_set);
             strcat(query, ", ");
         }
     }
-    if (options->full_set == FALSE){
+    if (data->filters->full_set == FALSE){
         for (int i = 0; i < DIFFERENT_SETS; i ++){
-            if (options->optional_sets[i] == TRUE){
+            if (data->filters->optional_sets[i] == TRUE){
                 sprintf(cur_set, "%d", i);
                 strcat(query, cur_set);
                 strcat(query, ", ");
@@ -1079,36 +1072,39 @@ static void query_for_even_slots(
     }
     query[strlen(query) - 2] = '\0'; // Remove last coma
     strcat(query, ") -- Sets\n");
-    if (options->storage == TRUE){
+    if (data->filters->storage == TRUE){
         // They are not null, are empty!
         strcat(query, "  AND (unit = '' OR unit = '");
-        strcat(query, (const char *) options->id);
+        strcat(query, (const char *) data->unit->id);
         strcat(query, "') -- Only storage\n");
     }
     // Excluded teams (overriden by storage option)
-    if (options->storage == FALSE && options->total_excluded_teams > 0){
+    if (
+      data->filters->storage == FALSE && data->filters->total_excluded_teams > 0
+    ){
         strcat(query, "  AND (unit = '' OR unit = '");
-        strcat(query, (const char *) options->id);
+        strcat(query, (const char *) data->unit->id);
         strcat(
         query, "' OR unit NOT IN (SELECT unit FROM units_teams WHERE team IN("
         );
-        for (int i = 0; i < options->total_excluded_teams; i ++){
+        for (int i = 0; i < data->filters->total_excluded_teams; i ++){
             strcat(query, "'");
-            strcat(query, (const char *) options->excluded_teams[i]);
+            strcat(query, (const char *) data->filters->excluded_teams[i]);
             strcat(query, "', ");
         }
         query[strlen(query) - 2] = '\0'; // Remove last coma
         strcat(query, "))) -- Excluded teams\n ");
     }
     // Excluded units (overriden by storage option)
-    if (options->storage == FALSE && options->total_excluded_units > 0){
-        printf("EVEN ERROR\n");
+    if (
+      data->filters->storage == FALSE && data->filters->total_excluded_units > 0
+    ){
         strcat(query, " AND (unit = '' OR unit = '");
-        strcat(query, (const char *) options->id);
+        strcat(query, (const char *) data->unit->id);
         strcat(query, "' OR unit NOT IN (");
-        for (int i = 0; i < options->total_excluded_units; i ++){
+        for (int i = 0; i < data->filters->total_excluded_units; i ++){
             strcat(query, "'");
-            strcat(query, (const char *) options->excluded_units[i]);
+            strcat(query, (const char *) data->filters->excluded_units[i]);
             strcat(query, "', ");
         }
         query[strlen(query) - 2] = '\0'; // Remove last coma
@@ -1116,7 +1112,7 @@ static void query_for_even_slots(
     }
     strcat(query, "  AND main_stat IN (");
     for (int i = 0; i < DIFFERENT_STATS; i ++){
-        if (options->stats[i] == TRUE){
+        if (data->filters->stats[i] == TRUE){
             sprintf(stat_set, "%d", i);
             strcat(query, stat_set);
             strcat(query, ", ");
@@ -1134,13 +1130,11 @@ static void query_for_even_slots(
     return;
 }
 
-static void query_for_odd_slots(
-  Optimizer_Filters *options, char query[RUNE_QUERY_LEN]
-){
+static void query_for_odd_slots(Optimizer_Data *data, char *query){
     char column[10];
     char cur_set[MAX_SETS];
-    if (options->level == LV12) strcpy(column, "lv12_");
-    else if (options->level == LV15) strcpy(column, "lv15_");
+    if (data->filters->level == LV12) strcpy(column, "lv12_");
+    else if (data->filters->level == LV15) strcpy(column, "lv15_");
     else strcpy(column, "current_");
     strcpy(query, "SELECT id, unit, slot, type, ");
     strcat(query, column);
@@ -1167,15 +1161,15 @@ static void query_for_odd_slots(
     strcat(query, "acc\nFROM runes\nWHERE\n  slot = ? -- Slot\n");
     strcat(query, "  AND type IN (");
     for (int i = 0; i < MAX_SETS; i ++){
-        if (options->sets[i] != 0){
-            sprintf(cur_set, "%d", options->sets[i]);
+        if (data->filters->sets[i] != 0){
+            sprintf(cur_set, "%d", data->filters->sets[i]);
             strcat(query, cur_set);
             strcat(query, ", ");
         }
     }
-    if (options->full_set == FALSE){
+    if (data->filters->full_set == FALSE){
         for (int i = 0; i < DIFFERENT_SETS; i ++){
-            if (options->optional_sets[i] == TRUE){
+            if (data->filters->optional_sets[i] == TRUE){
                 sprintf(cur_set, "%d", i);
                 strcat(query, cur_set);
                 strcat(query, ", ");
@@ -1184,35 +1178,39 @@ static void query_for_odd_slots(
     }
     query[strlen(query) - 2] = '\0'; // Remove last coma
     strcat(query, ") -- Sets\n");
-    if (options->storage == TRUE){
+    if (data->filters->storage == TRUE){
         // They are not null, are empty!
         strcat(query, "  AND (unit = '' OR unit = '");
-        strcat(query, (const char *) options->id);
+        strcat(query, (const char *) data->unit->id);
         strcat(query, "') -- Only storage\n");
     }
     // Excluded teams (overriden by storage option)
-    if (options->storage == FALSE && options->total_excluded_teams > 0){
+    if (
+      data->filters->storage == FALSE && data->filters->total_excluded_teams > 0
+    ){
         strcat(query, "  AND (unit = '' OR unit = '");
-        strcat(query, (const char *) options->id);
+        strcat(query, (const char *) data->unit->id);
         strcat(
           query, "' OR unit NOT IN (SELECT unit FROM units_teams WHERE team IN("
         );
-        for (int i = 0; i < options->total_excluded_teams; i ++){
+        for (int i = 0; i < data->filters->total_excluded_teams; i ++){
             strcat(query, "'");
-            strcat(query, (const char *) options->excluded_teams[i]);
+            strcat(query, (const char *) data->filters->excluded_teams[i]);
             strcat(query, "', ");
         }
         query[strlen(query) - 2] = '\0'; // Remove last coma
         strcat(query, "))) -- Excluded teams\n ");
     }
     // Excluded units (overriden by storage option)
-    if (options->storage == FALSE && options->total_excluded_units > 0){
+    if (
+      data->filters->storage == FALSE && data->filters->total_excluded_units > 0
+    ){
         strcat(query, " AND (unit = '' OR unit = '");
-        strcat(query, (const char *) options->id);
+        strcat(query, (const char *) data->unit->id);
         strcat(query, "' OR unit NOT IN (");
-        for (int i = 0; i < options->total_excluded_units; i ++){
+        for (int i = 0; i < data->filters->total_excluded_units; i ++){
             strcat(query, "'");
-            strcat(query, (const char *) options->excluded_units[i]);
+            strcat(query, (const char *) data->filters->excluded_units[i]);
             strcat(query, "', ");
         }
         query[strlen(query) - 2] = '\0'; // Remove last coma
@@ -1230,8 +1228,7 @@ static void query_for_odd_slots(
 }
 
 static int select_runes(
-  char query_even[RUNE_QUERY_LEN], char query_odd[RUNE_QUERY_LEN],
-  Optimizer_Data * data
+  char *query_even, char *query_odd, Optimizer_Data *data
 ){
     int total = 0;
     char **query;
@@ -1291,14 +1288,13 @@ static void *optimize_thread(void *tid){
     int thread_id = (int) (uintptr_t) tid;
     int start_at = thread_id * ceil((float) opt_data.count[1]
       / (float) opt_data.options->threads);
-    int end_at = start_at + (opt_data.count[1] / opt_data.options->threads);
+    int end_at = start_at + ceil((float) opt_data.count[1] / (float) opt_data.options->threads);
 
     // Initialize arrys and some counters
     unsigned int index[RUNE_SLOTS + 1] = {0, start_at, 0, 0, 0, 0};
     unsigned long valid_sets = 0;
     Rune_Set_Count set_count;
     unsigned char progress_printed = FALSE;
-
     while(
       index[1] < end_at
       && index[1] < opt_data.count[1]

+ 4 - 4
src/cli/runeoptimizer/optimize/output/output.c

@@ -585,6 +585,10 @@ static int print_human(
 }
 
 static int print_json(Optimizer_Data *data, FILE *target, unsigned int max){
+    if (data->total_results == 0){
+        fprintf(target, "{\"result_count\":0,\"results\":[]}\n");
+        return 0;
+    }
     char tmp[300];
     strcpy(tmp, "");
     char json[300];
@@ -592,10 +596,6 @@ static int print_json(Optimizer_Data *data, FILE *target, unsigned int max){
     fprintf(target, "\"rune_level\":%d,", data->filters->level);
     fprintf(target, "\"results\":[");
     int total_printed = 0;
-    if (data->total_results == 0){
-        fprintf(target, "{\"result_count\":0,\"results\":[]}\n");
-        return 0;
-    }
     for (int i = 0; i < data->total_results && i < max; i++){
         sprintf(tmp, "{\"id\":%d,\"rating\":%d,", i, data->results[i].rating);
         strcpy(json, tmp);

+ 4 - 4
src/cli/runeoptimizer/runeoptimizer.c

@@ -33,12 +33,12 @@
 #include "error/error.h"
 #include "db/db.h"
 #include "help/help.h"
+#include "init/init.h"
 #include "optimize/optimize.h"
 #include "team/team.h"
 #include "unit/unit.h"
 #include "update/update.h"
 #include "player/player.h"
-#include "gui/gui.h"
 
 /**
  * DEF stat multiplier.
@@ -122,7 +122,7 @@
  *
  * Keyword to launch the gui command.
  */
-#define CMD_GUI "gui"
+#define CMD_INIT "init"
 
 #ifdef _WIN32
 /**
@@ -302,8 +302,8 @@ int main(int argc, char *argv[]){
     }
 
     // GUI command.
-    else if (strcmp(argv[1], CMD_GUI) == 0){
-        return(gui());
+    else if (strcmp(argv[1], CMD_INIT) == 0){
+        return(init());
     }
 
     // Any other command is an error

+ 5 - 3
src/gui/runeoptimizer_gui/gui/RuneOptimizerFrame.py

@@ -80,6 +80,7 @@ class RuneOptimizerFrame(wx.Frame):
           "&Update from JSON file\tCtrl-J",
           "Updates the database from a profile JSON file."
         )
+        """
         update_swdb = update_menu.Append(
           -1,
           "&Update from SWDB\tCtrl-W",
@@ -95,6 +96,7 @@ class RuneOptimizerFrame(wx.Frame):
           "&Update from a sqlite database\tCtrl-Q",
           "Updates the database from a SWDB sqlite database."
         )
+        """
 
         file_menu = wx.Menu()
         file_about = file_menu.Append(wx.ID_ABOUT)
@@ -111,9 +113,9 @@ class RuneOptimizerFrame(wx.Frame):
         self.Bind(wx.EVT_MENU, self._close_app, file_exit)
         self.Bind(wx.EVT_MENU, self._show_about, file_about)
         self.Bind(wx.EVT_MENU, self._update_from_json, update_json)
-        self.Bind(wx.EVT_MENU, self._update_from_swdb, update_swdb)
-        self.Bind(wx.EVT_MENU, self._update_from_swarfarm, update_swarfarm)
-        self.Bind(wx.EVT_MENU, self._update_from_sqlite, update_sqlite)
+        #self.Bind(wx.EVT_MENU, self._update_from_swdb, update_swdb)
+        #self.Bind(wx.EVT_MENU, self._update_from_swarfarm, update_swarfarm)
+        #self.Bind(wx.EVT_MENU, self._update_from_sqlite, update_sqlite)
 
     def _close_app(self, event=None):
         """

+ 1 - 1
src/gui/runeoptimizer_gui/runeoptimizer_gui.py

@@ -68,7 +68,7 @@ if __name__ == '__main__':
     )
     
     # This makes sure that the executable has connection to the database.
-    runeoptimizer.run(["gui"])
+    runeoptimizer.run(["init"])
 
     data.reload_units()
     data.reload_teams()