* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3 * @package SWDB */ /** * Logs the user out and redirects to the login page. * * Reads the POST parameters looking for the following KEYS: * player: The owner's player ID. * area_type: The area type ID ({@see AREA_TYPE_ID}). * area: The area ID. * stage: Optional, the stage ID. * difficulty: Optional, the difficulty ID ({@see DIFFICULTY_ID}) * ids: Unit IDs of the party. * name: Team name. * description: Optional, team description. * Validates the data and creates the team in the database. * * @return int 201 on success, HTTP error status codes on failure. * @category Action */ function action(){ $player = filter_input(INPUT_POST, 'player'); $area_type = filter_input(INPUT_POST, 'area_type'); $area = filter_input(INPUT_POST, 'area'); $stage = filter_input(INPUT_POST, 'stage'); $difficulty = filter_input(INPUT_POST, 'difficulty'); if ($difficulty == ''){ $difficulty = null; } $ids = filter_input(INPUT_POST, 'ids'); $name = filter_input(INPUT_POST, 'name'); $leader_id = filter_input(INPUT_POST, 'leader'); $total_front = filter_input(INPUT_POST, 'front'); $description = filter_input(INPUT_POST, 'description'); $score = 0; $id = preg_split("/[\s,]+/", $ids); $id_count = sizeof($id); if ($name == null || strlen($name) == 0){ return 400; } switch ($area_type){ case AREA_TYPE_ID::SCENARIO: if ($stage < 0 || $stage > 7){ return 400; } if (!($difficulty == 0 || APPLICATION::valid_id("DIFFICULTY_ID", $difficulty) || $difficulty == DIFFICULTY_ID::EASY)){ return 400; } if ($id_count < 1 || $id_count > 4){ return 400; } if (!APPLICATION::valid_id("SCENARIO_ID", $area)){ return 400; } break; case AREA_TYPE_ID::CAIROS_DUNGEON: if ($stage < 0){ return 400; } if ($stage > 12 && (DUNGEON_ID::GIANTS_KEEP == $area || DUNGEON_ID::DRAGONS_LAIR == $area || DUNGEON_ID::NECROPOLIS == $area)){ return 400; } if ($stage > 10 && ($area != DUNGEON_ID::GIANTS_KEEP && $area != DUNGEON_ID::DRAGONS_LAIR && $area != DUNGEON_ID::NECROPOLIS)){ return 400; } $difficulty = null; if ($id_count < 1 || $id_count > 5){ return 400; } if (!APPLICATION::valid_id("DUNGEON_ID", $area)){ return 400; } break; case AREA_TYPE_ID::RIFT_DUNGEON: $stage = null; $difficulty = null; if ($id_count < 1 || $id_count > 6){ return 400; } if (!APPLICATION::valid_id("ELEMENTAL_RIFT_DUNGEON_ID", $area)){ return 400; } break; case AREA_TYPE_ID::RIFT_RAID: if ($stage < 0 || $stage > 5){ return 400; } $difficulty = null; if ($id_count < 1 || $id_count > 6){ return 400; } $area = null; break; case AREA_TYPE_ID::ARENA: $area = null; $stage = null; $difficulty = null; break; case AREA_TYPE_ID::GUILD_WAR: $area = null; $stage = null; $difficulty = null; break; case AREA_TYPE_ID::GUILD_SIEGE: $area = null; $stage = null; $difficulty = null; break; case AREA_TYPE_ID::TARTARUS_LABYRINTH: if ($difficulty != null && !APPLICATION::valid_id("DIFFICULTY_ID", $difficulty)){ return 400; } if ($id_count < 1 || $id_count > 5){ return 400; } if (!APPLICATION::valid_id("LABYRINTH_STAGES_ID", $stage)){ return 400; } break; case AREA_TYPE_ID::TRIAL_OF_ASCENSION: if ($difficulty != null && $difficulty != DIFFICULTY_ID::NORMAL && $difficulty != DIFFICULTY_ID::HARD){ $difficulty = null; } $stage = null; if ($id_count < 1 || $id_count > 5){ return 400; } $area = null; break; case AREA_TYPE_ID::DIMENSIONAL_HOLE: if ($stage < 0 || $stage > 5){ return 400; } $difficulty = null; if ($id_count < 1 || $id_count > 4){ return 400; } if (!APPLICATION::valid_id("DUNGEON_ID", $area)){ return 400; } break; case AREA_TYPE_ID::DIMENSIONAL_RIFT: if ($difficulty != 0 && $difficulty != DIFFICULTY_ID::NORMAL && $difficulty != DIFFICULTY_ID::HARD){ return 400; } $stage = null; if ($id_count < 1 || $id_count > 5){ return 400; } $area = null; break; default: return 400; } $unique_id = array_unique($id); if ($id_count != sizeof($unique_id)){ 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(" 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', $area_type, SQLITE3_TEXT); $statement->bindValue(':area', $area, SQLITE3_TEXT); $statement->bindValue(':stage', $stage, SQLITE3_TEXT); $statement->bindValue(':difficulty', $difficulty, SQLITE3_TEXT); $statement->bindValue(':score', $score, SQLITE3_TEXT); $res = $statement->execute(); if (!$res){ return 500; } $j = 0; foreach (explode(',', $ids) as $i){ $leader = 0; if ((string) $i == (string) $leader_id){ $leader = 1; } $front = 0; if ($j < $total_front){ $front = 1; } $statement = get_context()->get_db()->prepare(" INSERT INTO team_unit (team, unit, leader, front) VALUES (:team, :unit, :leader, :front); "); $statement->bindValue(':team', $team_id, SQLITE3_TEXT); $statement->bindValue(':unit', $i, SQLITE3_TEXT); $statement->bindValue(':leader', $leader, SQLITE3_TEXT); $statement->bindValue(':front', $front, SQLITE3_TEXT); $statement->execute(); $j ++; } return 201; }