update_efficiency.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_efficiency.c
  19. *
  20. * Implementation of {@link update_efficiency}.
  21. *
  22. * This file implements the funciton {@link update_efficiency} declared in
  23. * {@link update.h}.
  24. */
  25. #include <stdio.h>
  26. #include <sqlite3.h>
  27. #include "../error/error.h"
  28. #include "../db/db.h"
  29. #include "../runeoptimizer.h"
  30. extern int update_efficiency(char *id, float *efficiency, float *max_efficiency){
  31. *efficiency = 0.0f;
  32. *max_efficiency = 0.0f;
  33. float eff[5] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
  34. sqlite3_stmt *stmt_rune;
  35. sqlite3_stmt *stmt_stats;
  36. char *parameters[1] = {id};
  37. db_query(
  38. &stmt_rune, "SELECT stars, level FROM runes WHERE id = ?", parameters
  39. );
  40. if (SQLITE_ROW != sqlite3_step(stmt_rune)) {
  41. fprintf(stderr, "Rune not found: %s\n", sqlite3_errmsg(db));
  42. sqlite3_finalize(stmt_rune);
  43. return(ERROR_DB_SELECT_RUNE);
  44. }
  45. int stars = sqlite3_column_int(stmt_rune, 0);
  46. int level = sqlite3_column_int(stmt_rune, 1);
  47. db_query(
  48. &stmt_stats,
  49. "SELECT slot, stat, value FROM rune_stats WHERE rune = ? AND slot != -1",
  50. parameters
  51. );
  52. // Calculate efficiency for each slot.
  53. // Main slot is excluded from the calculation.
  54. while (SQLITE_ROW == sqlite3_step(stmt_stats)){
  55. int slot = sqlite3_column_int(stmt_stats, 0);
  56. int stat = sqlite3_column_int(stmt_stats, 1);
  57. int value = sqlite3_column_int(stmt_stats, 2);
  58. int max_roll_value = STAT_ROLL_MAX[stat][stars];
  59. eff[slot] += (((float) value) / ((float) max_roll_value));
  60. }
  61. sqlite3_finalize(stmt_stats);
  62. sqlite3_finalize(stmt_rune);
  63. // Sum each slot efficiency.
  64. // Each roll contributes only 1/9 to the total efficiency.
  65. for (int i = 0; i < 5; i ++){
  66. eff[i] = eff[i] / 9.0f;
  67. *efficiency += eff[i];
  68. }
  69. // Calculate max efficiency. For each power up remaining, add 1/9.
  70. *max_efficiency = *efficiency;
  71. if (level < 12) *max_efficiency += (1.0f / 9.0f);
  72. if (level < 9) *max_efficiency += (1.0f / 9.0f);
  73. if (level < 6) *max_efficiency += (1.0f / 9.0f);
  74. if (level < 3) *max_efficiency += (1.0f / 9.0f);
  75. // Multiply by 100 to get a percent value
  76. *efficiency *= 100.0f;
  77. *max_efficiency *= 100.0f;
  78. return(SUCCESS);
  79. }