瀏覽代碼

New UI, user and player actions separated.

Iñigo Valentin 5 年之前
父節點
當前提交
7864037b63

+ 0 - 1
application/API/v1/profile/PUT.php

@@ -1608,7 +1608,6 @@ try{
                 $statement->bindValue(":server", 2, SQLITE3_INTEGER);
                 $statement->bindValue(":country", $json->{"wizard_info"}->{"wizard_last_country"}, SQLITE3_TEXT);
                 $statement->bindValue(":lang", $json->{"wizard_info"}->{"wizard_last_lang"}, SQLITE3_TEXT);
-                error_log("CREATE NEW PLAYER: " . $statement->getSQL(true));
                 $statement->execute();
             }
             break;

+ 13 - 10
application/Controller.php

@@ -134,7 +134,7 @@ class Controller extends Context{
                 return; // End preparation here, the API controller will deal with everything.
                 break;
             case "ACTION": // Execute an action
-                $auth_required = true;
+                //$auth_required = true;
                 break;
             case "PLAYER": // A player page
                 if (sizeof($this->params) > 1){
@@ -209,6 +209,7 @@ class Controller extends Context{
         
         // If there was an error in preparation, it's time to throw it.
         if ($this->status >= 400 && $this->status < 500){
+            require_once(PATH::PAGE . "Error_Page.php");
             $page = new Error_Page($this->status, $this->message);
             require_once($page->get_view());
             http_response_code(404);
@@ -216,7 +217,6 @@ class Controller extends Context{
         }
         
         // Process the command
-        error_log("SWITCH COMMAND: " . $this->command);
         $page = null;
         switch ($this->command){
             case "": // Main page
@@ -250,7 +250,6 @@ class Controller extends Context{
                 }
                 break;
             case "API": // An API call
-                error_log("STARTING API CALL");
                 if (count($this->params) == 1){
                     // TODO: Redirect to current version
                 }
@@ -261,7 +260,6 @@ class Controller extends Context{
                     array_shift($api_params);
                     switch ($version){
                         case "v1":
-                            error_log("STARTING API V1 Controller");
                             require_once(__DIR__ . "/API/v1/API_Controller.php");
                             new API_Controller($this->params);
                             return;
@@ -341,19 +339,19 @@ class Controller extends Context{
                         return;
                 }
                 $action->execute();
-                if ($action->get_code() >= 400 && $action->get_code() < 500){
+                /*if ($action->get_code() >= 400 && $action->get_code() < 500){
                     require_once(PATH::PAGE . "Error_Page.php");
                     $page = new Error_Page($action->get_code(), $action->get_message());
                     require_once($page->get_view());
                     http_response_code($action->get_code());
-                }
+                }*/
                 if (strlen($action->get_output()) > 0){
                     echo($action->get_output());
                 }
+                http_response_code($action->get_code());
                 return $action->get_code();
                 break;
             case "PLAYER": // A player page
-                error_log("CASE PLAYER");
                 if ($this->context->get_player() == null || $this->context->get_player()->get_id() == null){
                     require_once(PATH::PAGE . "Error_Page.php");
                     $page = new Error_Page(404, "Player not found");
@@ -450,7 +448,6 @@ class Controller extends Context{
                 }
                 break;
             case "MONSTERS":
-                error_log("PARAM2: " . $this->params[1]);
                 if (sizeof($this->params) > 1){
                     require_once(PATH::PAGE . "Monster_Page.php");
                     $page = new Monster_Page($this->params[1]);
@@ -469,8 +466,14 @@ class Controller extends Context{
                 $page = new Register_Page();
                 break;
             case "SEARCH": // Search results
+                if (sizeof($this->params) > 1){
+                    $arg = $this->params[1];
+                }
+                else{
+                    $arg = "";
+                }
                 require_once(PATH::PAGE . "Search_Page.php");
-                $page = new Search_Page();
+                $page = new Search_Page($arg);
                 break;
             /*case "DUNGEONS": // Dungeon static pages
                 require_once(PATH::PAGE . "Login_Page.php");
@@ -505,7 +508,7 @@ class Controller extends Context{
         
         // Once the page is loaded, chack if it can be seen
         // TODO verify
-        if (! $page->is_public() && $this->get_user() == null){
+        if (! $page->is_public() && $this->context->get_user() == null){
             require_once(PATH::PAGE . "Error_Page.php");
             $page = new Error_Page(401, "Authentication required");
             http_response_code(401);

+ 1 - 1
application/action/Change_Game_Mode_Action.php

@@ -28,7 +28,7 @@ class Change_Game_Mode_Action extends Action{
     public function execute(){
         $player = filter_input(INPUT_GET, "player");
         $mode = filter_input(INPUT_GET, "mode");
-        if (mode != GAME_MODE_ID::NORMAL && mode != GAME_MODE_ID::RTA){
+        if ($mode != GAME_MODE_ID::NORMAL && $mode != GAME_MODE_ID::RTA){
             $this->code = 400;
             $this->message = "POST parameter 'mode' not specified or invalid.";
             return;

+ 2 - 0
application/action/Login_Action.php

@@ -98,6 +98,8 @@ class Login_Action extends Action{
         $this->code = 204;
         $this->message = "No content.";
         header("Location: " . URL::BASE);
+        // TODO: Not redirecting?
+        die();
         return;
     }
 }

+ 239 - 2
application/page/Search_Page.php

@@ -19,21 +19,258 @@ require_once(PATH::PAGE . "Page.php");
  */
 class Search_Page extends Page{
 
+    /**
+     * @var string Searched query.
+     * 
+     * Will be processed do standarize it.
+     */
+    private $query = "";
+
+    /**
+     * @var mixed[] Search results.
+     */
+    private $results = [
+        "monster" => [],
+        "dungeon" => [],
+        "building" => [],
+        "report" => [],
+        "player" => [],
+        "unit" => [],
+        "team" => [],
+    ];
+    
+    /**
+     * @var mixed[] Default filter values.
+     */
+    private $filters = [
+        "MONSTER" => true,
+        "DUNGEON" => true,
+        "BUILDING" => true,
+        "REPORT" => true,
+        "PLAYER" => true,
+        "UNIT" => true,
+        "TEAM" => true
+    ];
+
     /**
      * Constructor.
      *
      * Retrieves the data and initializes the variables.
      *
      */
-    public function __construct($query){
+    public function __construct($query = ""){
         parent::__construct();
-        $this->set_public(false);
+        $this->set_public(true);
         $this->set_view("search.php");
         $this->set_title("Search results");
         $this->set_description("Search results: " . htmlentities($query));
         $this->set_canonical("search/" . $query);
         $this->add_css("search.css");
+        
+        $this->parse_filters();
+        error_log("QUERY: " . $query);
+        if ($query != null && $query != ""){
+            $q = $query;
+            $q = preg_replace('/\s+/', ' ', $q); // Remove multiple whitespace
+            $q = strtolower($q); // Lowercase
+            $this->query = $q;
+            if ($this->filters["MONSTER"] == true){
+                $this->search_monster();
+            }
+            if ($this->filters["DUNGEON"] == true){
+                $this->search_dungeon();
+            }
+            if ($this->filters["BUILDING"] == true){
+                $this->search_building();
+            }
+            /*if ($this->filters["REPORT"] == true){
+                $this->search_report();
+            }
+            if ($this->filters["PLAYER"] == true){
+                $this->search_player();
+            }
+            if ($this->filters["UNIT"] == true){
+                $this->search_unit();
+            }
+            if ($this->filters["TEAM"] == true){
+                $this->search_team();
+            }*/
+        }
+        
         $this->set_code(200);
         $this->set_message("OK");
     }
+    
+    /**
+     * Parses the request looking for the selected filters, validates them
+     * and adds them to the $filters array.
+     */
+    private function parse_filters(){
+        // filters["MONSTER"]
+        if (isset($_GET["monster"]) && $_GET["monster"] != "on"){
+            $this->filters["MONSTER"] = false;
+        }
+        else{
+            $this->filters["MONSTER"] = true;
+        }
+        // filters["DUNGEON"]
+        if (isset($_GET["dungeon"]) && $_GET["dungeon"] != "on"){
+            $this->filters["DUNGEON"] = false;
+        }
+        else{
+            $this->filters["DUNGEON"] = true;
+        }
+        // filters["BUILDING"]
+        if (isset($_GET["building"]) && $_GET["building"] != "on"){
+            $this->filters["BUILDING"] = false;
+        }
+        else{
+            $this->filters["BUILDING"] = true;
+        }
+        // filters["REPORT"]
+        if (isset($_GET["report"]) && $_GET["report"] != "on"){
+            $this->filters["REPORT"] = false;
+        }
+        else{
+            $this->filters["DUNGEON"] = true;
+        }
+        // filters["PLAYER"]
+        if (isset($_GET["player"]) && $_GET["player"] != "on"){
+            $this->filters["PLAYER"] = false;
+        }
+        else{
+            $this->filters["PLAYER"] = true;
+        }
+        // filters["UNIT"]
+        if ((isset($_GET["unit"]) && $_GET["unit"] != "on") || get_context()->get_user() == null){
+            $this->filters["UNIT"] = false;
+        }
+        else{
+            $this->filters["UNIT"] = true;
+        }
+        // filters["TEAM"]
+        if ((isset($_GET["team"]) && $_GET["team"] != "on") || get_context()->get_user() == null){
+            $this->filters["TEAM"] = false;
+        }
+        else{
+            $this->filters["TEAM"] = true;
+        }
+        return;
+    }
+
+    /**
+     * Searchs monsters matching the query and adds them to the result array.
+     */
+    private function search_monster(){
+        $statement = get_context()->get_db()->prepare("
+          SELECT id
+          FROM monster
+          WHERE obtainable = 1 AND
+          (
+            lower(name) LIKE :query OR
+            id LIKE :query
+          );
+        ");
+        $statement->bindValue(":query", "%" . $this->query . "%", SQLITE3_TEXT);
+        error_log($statement->getSQL(true));
+        $result_set = $statement->execute();
+        while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
+            array_push($this->results["monster"], new Monster($result["id"]));
+        }
+        return;
+    }
+    
+    /**
+     * Searchs monsters matching the query and adds them to the result array.
+     * @todo: implement when dungeons area thing.
+     */
+    private function search_dungeon(){
+        return;
+    }
+    
+    /**
+     * Searchs monsters matching the query and adds them to the result array.
+     */
+    private function search_building(){
+        $statement = get_context()->get_db()->prepare("
+          SELECT id, type
+          FROM (
+            SELECT id, name, description, 'buildable' AS type FROM buildable
+            UNION
+            SELECT id, name, description, 'decorative' AS type FROM decorative
+          )
+          WHERE 
+            lower(name) LIKE :query OR
+            lower(description) LIKE :query OR
+            id LIKE :query;
+        ");
+        $statement->bindValue(":query", "%" . $this->query . "%", SQLITE3_TEXT);
+        error_log($statement->getSQL(true));
+        $result_set = $statement->execute();
+        while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
+            if ("buildable" == $result["type"]){
+                array_push($this->results["building"], new Buildable($result["id"]));
+            }
+            elseif ("decorative" == $result["type"]){
+                array_push($this->results["building"], new Decorative($result["id"]));
+            }
+            
+        }
+    }
+    
+    /**
+     * Retrieves the page filters.
+     *
+     * @return mixed[] Page filtes
+     */
+    public function get_filters(){
+        return $this->filters;
+    }
+    
+    /**
+     * Retrieves one filter.
+     *
+     * @param string $key
+     * @return mixed Filter value, null if if doesn't exist.
+     */
+    public function get_filter($key){
+        if (array_key_exists($key, $this->filters)){
+            return $this->filters[$key];
+        }
+        else{
+            return null;
+        }
+    }
+
+    /**
+     * Retrieves the results.
+     * 
+     * If no key is specified, an array with the following keys will be returned:
+     * "monster", "dungeon", "building", "player", "report". Each of these keys is an array itself
+     * with instances of the found elements. If a key is specified, only it's array will be
+     * returned.
+     * 
+     *
+     * @param string $key Result group name.
+     * @return mixed[] Result list.
+     */
+    public function get_results($key = null){
+        if ($key == null){
+            error_log("RET NULL");
+            return $this->results;
+        }
+        else{
+            error_log("RET KEY : $key");
+            if (isset($this->results[$key])){
+                error_log("RET KEY OK");
+                return $this->results[$key];
+            }
+            else{
+                error_log("RET NULL NULL");
+                return null;
+            }
+        }
+    }
+
+    
 }

+ 4 - 2
application/view/artifacts.php

@@ -18,10 +18,12 @@
         <?=$page->generate_head()?>
     </head>
     <body>
+        <header>
 <?php
-        include __DIR__ . "/inc/header_app.php";
-        include __DIR__ . "/inc/header_player.php";
+            include __DIR__ . "/inc/header_app.php";
+            include __DIR__ . "/inc/header_player.php";
 ?>
+        </header>
         <aside class='filters'>
             <h2>
                 Artifacts

+ 14 - 1
application/view/inc/header_app.php

@@ -16,6 +16,12 @@
         //$user_id = null;
     }
 ?>
+<script>
+    function setSearchAction(){
+        var query = document.getElementById('app_search_query').value;
+        document.getElementById('app_search_form').action = '/search/' + query;
+    }
+</script>
 <nav class='menu' id='menu_app'>
     <table>
         <tr>
@@ -38,7 +44,8 @@
                 <a href='/buildings/'>
                     Buildings
                 </a>
-            </td><td class='item'>
+            </td>
+            <td class='item'>
                 <a href='/fusion/'>
                     Fusion
                 </a>
@@ -73,6 +80,12 @@
 <?php
                 }
 ?>
+           <td class='item' id='search'>
+                <form id='app_search_form' onsubmit='setSearchAction();'>
+                    <input id='app_search_query' type='text' placeholder='Search...'/>
+                    <input type='submit' value='Search'/>
+                </form>
+            </td>
         </tr>
     </table>
 </nav>

+ 3 - 3
application/view/inc/header_player.php

@@ -24,7 +24,7 @@ if (get_context()->get_player() != null){
             var xhttp = new XMLHttpRequest();
             xhttp.onreadystatechange = function() {
                 if (this.readyState == 4){
-                    if (this.status == 201){
+                    if (this.status == 204){
                         window.location.reload(false);
                     }
                 }
@@ -98,13 +98,13 @@ if (get_context()->get_player() != null){
                 </td>
                 <!-- TODO: Check if player == user -->
                 <td class='item'>
-                    <a href='/<?=get_context()->get_player()->get_id()?>/report/'>
+                    <a href='/player/<?=get_context()->get_player()->get_id()?>/report/'>
                         Reports
                     </a>
                 </td>
                 <!-- TODO: Check if player == user -->
                 <td class='item'>
-                    <a href='/<?=get_context()->get_player()->get_id()?>/optimizer/'>
+                    <a href='/player/<?=get_context()->get_player()->get_id()?>/optimizer/'>
                         Optimizer
                     </a>
                 </td>

+ 3 - 1
application/view/login.php

@@ -18,9 +18,11 @@
         <?=$page->generate_head()?>
     </head>
     <body>
+        <header>
 <?php
-        include __DIR__ . "/inc/header_app.php";
+            include __DIR__ . "/inc/header_app.php";
 ?>
+        </header>
         <aside>
         </aside>
         <main>

+ 152 - 3
application/view/search.php

@@ -16,6 +16,15 @@
 <html lang='en'>
     <head>
         <?=$page->generate_head()?>
+        <script>
+            function setFormAction(){
+                console.log("ON SUBMIT");
+                var query = document.getElementById('query').value;
+                console.log(query);
+                document.getElementById('searchForm').action = '/search/' + query;
+                
+            }
+        </script>
     </head>
     <body>
         <header>
@@ -27,9 +36,105 @@
             <h2 id='filters_title'>
                 Search filters
             </h2>
-            <p>
-                
-            </p>
+            <form id='searchForm' action='/search/' onsubmit='setFormAction();'>
+                <input id='query' type='text' placeholder='Search...'/>
+                <span id='formError'></span>
+                <hr/>
+                <span id='include'>
+                    Include...
+                </span>
+                <table>
+                    <tr>
+                        <td>
+<?php
+                            $checked = "";
+                            if ($page->get_filter("MONSTER")){
+                                $checked = "checked";
+                            }
+?>
+                            <input type='checkbox' name='monster' id='filter_monster' <?=$checked?>/>
+                            <label for='filter_monster'>Monsters</label>
+                        </td>
+                        <td>
+<?php
+                            $checked = "";
+                            if ($page->get_filter("DUNGEON")){
+                                $checked = "checked";
+                            }
+?>
+                            <input type='checkbox' name='dungeon' id='filter_dungeon' <?=$checked?>/>
+                            <label for='filter_dungeon'>Dungeons</label>
+                        </td>
+                    </tr>
+                    <tr>
+                        <td>
+<?php
+                            $checked = "";
+                            if ($page->get_filter("BUILDING")){
+                                $checked = "checked";
+                            }
+?>
+                            <input type='checkbox' name='building' id='filter_building' <?=$checked?>/>
+                            <label for='filter_building'>Buildings</label>
+                        </td>
+                        <td>
+<?php
+                            $checked = "";
+                            if ($page->get_filter("REPORT")){
+                                $checked = "checked";
+                            }
+?>
+                            <input type='checkbox' name='report' id='filter_report' <?=$checked?>/>
+                            <label for='filter_report'>Reports</label>
+                        </td>
+                    </tr>
+                    <tr>
+                        <td>
+<?php
+                            $checked = "";
+                            if ($page->get_filter("PLAYER")){
+                                $checked = "checked";
+                            }
+?>
+                            <input type='checkbox' name='monster' id='filter_player' <?=$checked?>/>
+                            <label for='filter_monster'>Players</label>
+                        </td>
+                        <td>
+<?php
+                            if (get_context()->get_user() != null){
+                                $checked = "";
+                                if ($page->get_filter("UNIT")){
+                                    $checked = "checked";
+                                }
+?>
+                                <input type='checkbox' name='unit' id='filter_unit' <?=$checked?>/>
+                                <label for='filter_unit'>My units</label>
+<?php
+                            }
+?>
+                        </td>
+                    </tr>
+                    <tr>
+                        <td>
+<?php
+                            if (get_context()->get_user() != null){
+                                $checked = "";
+                                if ($page->get_filter("TEAM")){
+                                    $checked = "checked";
+                                }
+?>
+                                <input type='checkbox' name='team' id='filter_team' <?=$checked?>/>
+                                <label for='filter_team'>My teams</label>
+<?php
+                            }
+?>
+                        </td>
+                        <td>
+                        </td>
+                    </tr>
+                </table>
+                <input type='submit' value='Search'/>
+            </form>
         </aside>
         <main>
             <section class='content'>
@@ -37,6 +142,50 @@
                     Results
                 </h2>
                 <article>
+<?php
+                    if (sizeof($page->get_results("monster")) > 0){
+?>
+                        <h3>
+                            Monsters
+                        </h3>
+                        <div class='result_row'>
+<?php
+                            foreach($page->get_results("monster") as $monster){
+?>
+                                <div class='monster'>
+                                    <a href='/monsters/<?=$monster->get_id()?>'>
+                                        <div class='monster_panel'>
+                                            <?=HTML::unit_panel($monster)?>
+                                        </div>
+                                    </a>
+                                </div>
+<?php
+                            }
+?>
+                        </div>
+<?php
+                    }
+                    if (sizeof($page->get_results("monster")) > 0){
+                        ?>
+                        <h3>
+                            Buildings
+                        </h3>
+                        <div class='result_row'>
+<?php
+                            foreach($page->get_results("building") as $building){
+?>
+                                <div class='building'>
+                                    <a href='/building/<?=$building->get_id()?>'>
+                                        <?=HTML::building_panel($building)?>
+                                    </a>
+                                </div>
+<?php
+                            }
+?>
+                        </div>
+<?php
+                    }
+?>
                 </article>
             </section>
         </main>

+ 1 - 1
install_data/02_CREATE_DATA.sql

@@ -172,7 +172,7 @@ CREATE TABLE data.artifact_effect(
     artifact INT NOT NULL references artifact(id),
     slot INT NOT NULL CHECK(slot BETWEEN 1 AND 4),
     effect INT NOT NULL, --REFERENCES key.artifact_effect(id),
-    value INT NOT NULL CHECK(value >= 0),
+    value FLOAT NOT NULL CHECK(value >= 0),
     enchant INT NOT NULL CHECK(enchant IN (0, 1)),
     grind INT NOT NULL CHECK(grind >= 0),
     rolls INT NOT NULL CHECK(rolls BETWEEN 0 AND 4),

+ 29 - 0
public/css/search.css

@@ -0,0 +1,29 @@
+aside input#query{
+    display: block;
+    margin: auto;
+    width: 80%;
+}
+aside span#form_error{
+    color: #ff2222;
+    margin-bottom: 1em;
+}
+
+aside span#include{
+    margin: 2em auto 1.5em 1em;
+    display: block;
+}
+
+aside label{
+    margin-left: 1.8em;
+    margin-bottom: 1em;
+}
+
+aside input[type='submit']{
+    display: block;
+    margin: 1em auto;
+}
+
+main div.result_row div.monster, div.building{
+    font-size: 100%;
+    display: inline-block;
+}