* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3 * @package SWDB */ /** * Saves a run party as a team. * * Reads the POST parameters looking for the following KEYS: * player: Player ID. * run: Run ID. * name: New team name. * description: New team description. * score: New team score {@todo implement}. * Validates the data and creates the team in the database. * * @return int 201 on success, HTTP erro status codes on error. * @category Action */ function action(){ $player = filter_input(INPUT_POST, 'player'); $run = filter_input(INPUT_POST, 'run'); $name = filter_input(INPUT_POST, 'name'); $description = filter_input(INPUT_POST, 'description'); $score = intval(filter_input(INPUT_POST, 'score')); if ($name == null || strlen($name) == 0){ return 400; } $s = "SELECT CASE WHEN MAX(id) IS NULL THEN 1 ELSE MAX(id) + 1 END AS id FROM team;"; $q = get_context()->get_db()->query($s); $r = $q->fetchArray(SQLITE3_ASSOC); $team_id = $r["id"]; $statement = get_context()->get_db()->prepare(" SELECT run.area AS area, run.stage AS stage, run.difficulty AS difficulty, area.type AS area_type FROM run, area WHERE run.player = :player AND run.id = :run AND area.id = run.area AND run.helper = 0; "); $statement->bindValue(':player', $player, SQLITE3_TEXT); $statement->bindValue(':run', $run, SQLITE3_TEXT); $r = $statement->execute()->fetchArray(SQLITE3_ASSOC); if (!$r){ return 404; } $statement = get_context()->get_db()->prepare(" INSERT INTO team ( player, id, name, description, area_type, area, stage, difficulty, score ) VALUES ( :player, :id, :name, :description, :area_type, :area, :stage, :difficulty, :score); "); $statement->bindValue(':player', $player, SQLITE3_TEXT); $statement->bindValue(':id', $team_id, SQLITE3_TEXT); $statement->bindValue(':name', $name, SQLITE3_TEXT); $statement->bindValue(':description', $description, SQLITE3_TEXT); $statement->bindValue(':area_type', $r["area_type"], SQLITE3_TEXT); $statement->bindValue(':area', $r["area"], SQLITE3_TEXT); $statement->bindValue(':stage', $r["stage"], SQLITE3_TEXT); $statement->bindValue(':difficulty', $r["difficulty"], SQLITE3_TEXT); $statement->bindValue(':score', $score, SQLITE3_INTEGER); $res = $statement->execute(); if (!$res){ return 500; } $s = "SELECT unit FROM data.run_party WHERE run = $run AND unit IS NOT NULL;"; $q = get_context()->get_db()->query($s); while ($r = $q->fetchArray(SQLITE3_ASSOC)){ $statement = get_context()->get_db()->prepare(" INSERT INTO data.team_unit (team, unit) VALUES (:team, :unit) "); $statement->bindValue(':team', $team_id, SQLITE3_TEXT); $statement->bindValue(':unit', $r["unit"], SQLITE3_TEXT); $statement->execute(); } return 201; } ?>