| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- /*
- * 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/>.
- */
- /**
- * @file team_add_unit.c
- *
- * Implementation of {@link team_add_unit}.
- *
- * This file implements the function {@link team_add_unit} declared in
- * {@link team.h}. It also defines and implements an static function for
- * validation.
- */
- #include <stdio.h>
- #include "../runeoptimizer.h"
- #include "../error/error.h"
- #include "../db/db.h"
- #include "team.h"
- /**
- * Validates the options for adding a unit to a team.
- *
- * It checks if the unit and team IDs exist, and if the unit is already a member
- * of the team.
- *
- * @param[in] team ID of the team to add the unit to.
- * @param[in] unit ID of the unit to add.
- * @return {@link SUCCESS} if everything is OK and the unit can be added,
- * {@link ERROR_INPUT_TEAM_VERIFY_UNIT_TEAM} if the team doesn't exist,
- * {@link ERROR_INPUT_TEAM_VERIFY_UNIT} if the unit doesn't exist or
- * {@link ERROR_DB_VERIFY_TEAM_UNIT} for other errors.
- */
- static int team_add_unit_validate(char *team, char *unit);
- extern int team_add_unit(int argc, char *argv[]){
- if (argc != 2){
- fprintf(
- stderr, "add_unit needs exactly two arguments, %d supplied\n",
- argc
- );
- return(ERROR_INPUT_TEAM_NO_ACTION);
- }
- int validation = team_add_unit_validate(argv[0], argv[1]);
- if (SUCCESS != validation){
- char errmsg[86] = "Unable to verify unit and team.\n";
- switch (validation){
- case ERROR_INPUT_TEAM_VERIFY_TEAM:
- sprintf(errmsg, "Team with ID '%s' does not exists\n", argv[0]);
- break;
- case ERROR_INPUT_TEAM_VERIFY_UNIT:
- sprintf(errmsg, "Unit with ID '%s' does not exists\n", argv[1]);
- break;
- case ERROR_INPUT_TEAM_VERIFY_INCLUDED:
- sprintf(
- errmsg,
- "Unit with ID '%s' is already in team with ID %s\n",
- argv[1], argv[0]
- );
- break;
- }
- fprintf(stderr, errmsg);
- return(validation);
- }
- // Insert into units_teams
- char *parameters[2] = {argv[0], argv[1]};
- if (
- SUCCESS !=
- db_execute(
- "INSERT INTO units_teams (team, unit) VALUES (?, ?)", parameters
- )
- ){
- fprintf(stderr, "Unable to add unit to team: %s\n", sqlite3_errmsg(db));
- return(ERROR_DB_INSERT_UNITS_TEAMS);
- }
- // Inform about the newly created team
- printf(
- "Unit with ID '%s' succesfully added to team with id '%s'\n",
- argv[1], argv[0]
- );
- // Show the team composition and end.
- char *args_to_list[2] = {argv[0], "--units"};
- team_list(2, args_to_list);
- return(SUCCESS);
- }
- static int team_add_unit_validate(char *team, char *unit){
- int status = SUCCESS;
- sqlite3_stmt *stmt_verify;
- char *parameters[4] = {team, unit, team, unit};
- db_query(
- &stmt_verify,
- "SELECT "
- "(SELECT count(id) FROM teams WHERE id = ?) AS team, "
- "(SELECT count(id) FROM units WHERE id = ?) AS unit, "
- "(SELECT count(*) FROM units_teams WHERE team = ? AND unit = ?) AS inc",
- parameters
- );
- if (SQLITE_ROW != sqlite3_step(stmt_verify)){
- status = ERROR_DB_VERIFY_TEAM_UNIT;
- }
- else if (sqlite3_column_int(stmt_verify, 0) == 0){
- status = ERROR_INPUT_TEAM_VERIFY_TEAM;
- }
- else if (sqlite3_column_int(stmt_verify, 1) == 0){
- status = ERROR_INPUT_TEAM_VERIFY_UNIT;
- }
- else if (sqlite3_column_int(stmt_verify, 2) != 0){
- status = ERROR_INPUT_TEAM_VERIFY_INCLUDED;
- }
- sqlite3_finalize(stmt_verify);
- return(status);
- }
|