| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <?php
- /**
- * File for the team creation action.
- *
- * Implements an action function to be called from the {@see Controller}.
- *
- * @author Iñigo Valentin <i@inigovalentin.com>
- * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
- * @package SWDB
- */
- require_once(PATH::ACTION . "Action.php");
- /**
- * Saves a team to the database.
- *
- * 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.
- * leader: Id of the leader unit.
- * front: Number of units in the frontline.
- * description: Optional, team description.
- * Validates the data and creates the team in the database.
- *
- * @category Action
- */
- class Add_Team_Action extends Action{
-
- /**
- * Executes the action.
- */
- function execute(){
- $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){
- $this->code = 400;
- $this->message = "POST parameter 'name' not specified or invalid.";
- return;
- }
- switch ($area_type){
- case AREA_TYPE_ID::SCENARIO:
- if ($stage < 0 || $stage > 7){
- $this->code = 400;
- $this->message = "Invalid stage for the selected area.";
- return;
- }
- if (!($difficulty == 0 || APPLICATION::valid_id("DIFFICULTY_ID", $difficulty) || $difficulty == DIFFICULTY_ID::EASY)){
- $this->code = 400;
- $this->message = "Invalid difficulty for the selected area.";
- return;
- }
- if ($id_count < 1 || $id_count > 4){
- $this->code = 400;
- $this->message = "Invalid number of units for the selected area.";
- return;
- }
- if (!APPLICATION::valid_id("SCENARIO_ID", $area)){
- $this->code = 400;
- $this->message = "Invalid area.";
- return;
- }
- break;
- case AREA_TYPE_ID::CAIROS_DUNGEON:
- if ($stage < 0){
- $this->code = 400;
- $this->message = "Invalid stage for the selected area.";
- return;
- }
- if ($stage > 12 && (DUNGEON_ID::GIANTS_KEEP == $area || DUNGEON_ID::DRAGONS_LAIR == $area || DUNGEON_ID::NECROPOLIS == $area)){
- $this->code = 400;
- $this->message = "Invalid stage for the selected area.";
- return;
- }
- if ($stage > 10 && ($area != DUNGEON_ID::GIANTS_KEEP && $area != DUNGEON_ID::DRAGONS_LAIR && $area != DUNGEON_ID::NECROPOLIS)){
- $this->code = 400;
- $this->message = "Invalid stage for the selected area.";
- return;
- }
- $difficulty = null;
- if ($id_count < 1 || $id_count > 5){
- $this->code = 400;
- $this->message = "Invalid number of units for the selected area.";
- return;
- }
- if (!APPLICATION::valid_id("DUNGEON_ID", $area)){
- $this->code = 400;
- $this->message = "Invalid area.";
- return;
- }
- break;
- case AREA_TYPE_ID::RIFT_DUNGEON:
- $stage = null;
- $difficulty = null;
- if ($id_count < 1 || $id_count > 6){
- $this->code = 400;
- $this->message = "Invalid number of units for the selected area.";
- return;
- }
- if (!APPLICATION::valid_id("ELEMENTAL_RIFT_DUNGEON_ID", $area)){
- $this->code = 400;
- $this->message = "Invalid area.";
- return;
- }
- break;
- case AREA_TYPE_ID::RIFT_RAID:
- if ($stage < 0 || $stage > 5){
- $this->code = 400;
- $this->message = "Invalid stage for the selected area.";
- return;
- }
- $difficulty = null;
- if ($id_count < 1 || $id_count > 6){
- $this->code = 400;
- $this->message = "Invalid number of units for the selected area.";
- return;
- }
- $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)){
- $this->code = 400;
- $this->message = "Invalid difficulty for the selected area.";
- return;
- }
- if ($id_count < 1 || $id_count > 5){
- $this->code = 400;
- $this->message = "Invalid number of units for the selected area.";
- return;
- }
- if (!APPLICATION::valid_id("LABYRINTH_STAGES_ID", $stage)){
- $this->code = 400;
- $this->message = "Invalid area.";
- return;
- }
- 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){
- $this->code = 400;
- $this->message = "Invalid number of units for the selected area.";
- return;
- }
- $area = null;
- break;
- case AREA_TYPE_ID::DIMENSIONAL_HOLE:
- if ($stage < 0 || $stage > 5){
- $this->code = 400;
- $this->message = "Invalid stage for the selected area.";
- return;
- }
- $difficulty = null;
- if ($id_count < 1 || $id_count > 4){
- $this->code = 400;
- $this->message = "Invalid number of units for the selected area.";
- return;
- }
- if (!APPLICATION::valid_id("DUNGEON_ID", $area)){
- $this->code = 400;
- $this->message = "Invalid area.";
- return;
- }
- break;
- case AREA_TYPE_ID::DIMENSIONAL_RIFT:
- if ($difficulty != 0 && $difficulty != DIFFICULTY_ID::NORMAL && $difficulty != DIFFICULTY_ID::HARD){
- $this->code = 400;
- $this->message = "Invalid difficulty for the selected area.";
- return;
- }
- $stage = null;
- if ($id_count < 1 || $id_count > 5){
- $this->code = 400;
- $this->message = "Invalid number of units for the selected area.";
- return;
- }
- $area = null;
- break;
- default:
- return 400;
- }
- $unique_id = array_unique($id);
- if ($id_count != sizeof($unique_id)){
- $this->code = 400;
- $this->message = "Duplicated units in the team.";
- return;
- }
- $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){
- $this->code = 500;
- $this->message = "Internal error.";
- return;
- }
-
- $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 ++;
- }
- $this->code = 204;
- $this->message = "No content.";
- return;
- }
- }
|