team_add_unit.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 team_add_unit.c
  19. *
  20. * Implementation of {@link team_add_unit}.
  21. *
  22. * This file implements the function {@link team_add_unit} declared in
  23. * {@link team.h}. It also defines and implements an static function for
  24. * validation.
  25. */
  26. #include <stdio.h>
  27. #include "../runeoptimizer.h"
  28. #include "../error/error.h"
  29. #include "../db/db.h"
  30. #include "team.h"
  31. /**
  32. * Validates the options for adding a unit to a team.
  33. *
  34. * It checks if the unit and team IDs exist, and if the unit is already a member
  35. * of the team.
  36. *
  37. * @param[in] team ID of the team to add the unit to.
  38. * @param[in] unit ID of the unit to add.
  39. * @return {@link SUCCESS} if everything is OK and the unit can be added,
  40. * {@link ERROR_INPUT_TEAM_VERIFY_UNIT_TEAM} if the team doesn't exist,
  41. * {@link ERROR_INPUT_TEAM_VERIFY_UNIT} if the unit doesn't exist or
  42. * {@link ERROR_DB_VERIFY_TEAM_UNIT} for other errors.
  43. */
  44. static int team_add_unit_validate(char *team, char *unit);
  45. extern int team_add_unit(int argc, char *argv[]){
  46. if (argc != 2){
  47. fprintf(
  48. stderr, "add_unit needs exactly two arguments, %d supplied\n",
  49. argc
  50. );
  51. return(ERROR_INPUT_TEAM_NO_ACTION);
  52. }
  53. int validation = team_add_unit_validate(argv[0], argv[1]);
  54. if (SUCCESS != validation){
  55. char errmsg[86] = "Unable to verify unit and team.\n";
  56. switch (validation){
  57. case ERROR_INPUT_TEAM_VERIFY_TEAM:
  58. sprintf(errmsg, "Team with ID '%s' does not exists\n", argv[0]);
  59. break;
  60. case ERROR_INPUT_TEAM_VERIFY_UNIT:
  61. sprintf(errmsg, "Unit with ID '%s' does not exists\n", argv[1]);
  62. break;
  63. case ERROR_INPUT_TEAM_VERIFY_INCLUDED:
  64. sprintf(
  65. errmsg,
  66. "Unit with ID '%s' is already in team with ID %s\n",
  67. argv[1], argv[0]
  68. );
  69. break;
  70. }
  71. fprintf(stderr, errmsg);
  72. return(validation);
  73. }
  74. // Insert into units_teams
  75. char *parameters[2] = {argv[0], argv[1]};
  76. if (
  77. SUCCESS !=
  78. db_execute(
  79. "INSERT INTO units_teams (team, unit) VALUES (?, ?)", parameters
  80. )
  81. ){
  82. fprintf(stderr, "Unable to add unit to team: %s\n", sqlite3_errmsg(db));
  83. return(ERROR_DB_INSERT_UNITS_TEAMS);
  84. }
  85. // Inform about the newly created team
  86. printf(
  87. "Unit with ID '%s' succesfully added to team with id '%s'\n",
  88. argv[1], argv[0]
  89. );
  90. // Show the team composition and end.
  91. char *args_to_list[2] = {argv[0], "--units"};
  92. team_list(2, args_to_list);
  93. return(SUCCESS);
  94. }
  95. static int team_add_unit_validate(char *team, char *unit){
  96. int status = SUCCESS;
  97. sqlite3_stmt *stmt_verify;
  98. char *parameters[4] = {team, unit, team, unit};
  99. db_query(
  100. &stmt_verify,
  101. "SELECT "
  102. "(SELECT count(id) FROM teams WHERE id = ?) AS team, "
  103. "(SELECT count(id) FROM units WHERE id = ?) AS unit, "
  104. "(SELECT count(*) FROM units_teams WHERE team = ? AND unit = ?) AS inc",
  105. parameters
  106. );
  107. if (SQLITE_ROW != sqlite3_step(stmt_verify)){
  108. status = ERROR_DB_VERIFY_TEAM_UNIT;
  109. }
  110. else if (sqlite3_column_int(stmt_verify, 0) == 0){
  111. status = ERROR_INPUT_TEAM_VERIFY_TEAM;
  112. }
  113. else if (sqlite3_column_int(stmt_verify, 1) == 0){
  114. status = ERROR_INPUT_TEAM_VERIFY_UNIT;
  115. }
  116. else if (sqlite3_column_int(stmt_verify, 2) != 0){
  117. status = ERROR_INPUT_TEAM_VERIFY_INCLUDED;
  118. }
  119. sqlite3_finalize(stmt_verify);
  120. return(status);
  121. }