From ced07f3bb7c0038f57c60209903474c4b551d1e0 Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 17 Feb 2022 22:59:27 +0100 Subject: [PATCH] Reworked building controller and building model to the new style. --- app/buildings/building_controller.cpp | 307 ----------------- app/buildings/building_initializer.cpp | 37 -- app/buildings/building_initializer.h | 23 -- app/buildings/building_model.h | 36 -- .../{building_model.cpp => building_node.cpp} | 318 ++++++++++++++++-- ...{building_controller.h => building_node.h} | 23 +- app/mourne_root.cpp | 14 +- app/mourne_root.h | 2 + 8 files changed, 318 insertions(+), 442 deletions(-) delete mode 100644 app/buildings/building_controller.cpp delete mode 100644 app/buildings/building_initializer.cpp delete mode 100644 app/buildings/building_initializer.h delete mode 100644 app/buildings/building_model.h rename app/buildings/{building_model.cpp => building_node.cpp} (55%) rename app/buildings/{building_controller.h => building_node.h} (56%) diff --git a/app/buildings/building_controller.cpp b/app/buildings/building_controller.cpp deleted file mode 100644 index 80ab97a..0000000 --- a/app/buildings/building_controller.cpp +++ /dev/null @@ -1,307 +0,0 @@ -#include "building_controller.h" - -#include "web/html/form_validator.h" -#include "web/html/html_builder.h" -#include "web/http/cookie.h" -#include "web/http/http_enums.h" -#include "web/http/http_session.h" -#include "web/http/request.h" -#include "web/http/session_manager.h" - -#include "building_model.h" - -#include "../html_macros.h" - -void BuildingController::handle_request_default(Request *request) { -} - -void BuildingController::admin_handle_request_main(Request *request) { - String seg = request->get_current_path_segment(); - - if (seg == "") { - admin_render_building_list(request); - return; - } else if (seg == "new") { - request->push_path(); - Ref b; - b.instance(); - - admin_render_building(request, b); - return; - } else if (seg == "edit") { - request->push_path(); - - String seg_building_id = request->get_current_path_segment(); - - if (!seg_building_id.is_int()) { - request->send_error(HTTP_STATUS_CODE_404_NOT_FOUND); - return; - } - - int bid = seg_building_id.to_int(); - - Ref b = BuildingModel::get_singleton()->get_building(bid); - - if (!b.is_valid()) { - request->send_error(HTTP_STATUS_CODE_404_NOT_FOUND); - return; - } - - admin_render_building(request, b); - return; - } - - request->send_error(404); -} -String BuildingController::admin_get_section_name() { - return "Buildings"; -} -void BuildingController::admin_add_section_links(Vector *links) { - links->push_back(AdminSectionLinkInfo("- Building Editor", "")); -} -bool BuildingController::admin_full_render() { - return false; -} - -void BuildingController::admin_render_building_list(Request *request) { - Vector > buildings = BuildingModel::get_singleton()->get_all(); - - HTMLBuilder b; - - b.div("back")->fa(request->get_url_root_parent(), "<--- Back")->cdiv(); - b.br(); - b.fdiv("Building Editor", "top_menu"); - b.br(); - b.div("top_menu")->fa(request->get_url_root("new"), "Create New")->cdiv(); - b.br(); - - b.div("list_container"); - - for (int i = 0; i < buildings.size(); ++i) { - Ref building = buildings[i]; - - if (!building.is_valid()) { - continue; - } - - if (i % 2 == 0) { - b.div("row"); - } else { - b.div("row second"); - } - { - b.fdiv(String::num(building->id), "attr_box"); - b.fdiv(String::num(building->rank), "attr_box"); - b.fdiv(String::num(building->next_rank), "attr_box"); - b.fdiv(building->name, "name"); - - b.div("actionbox")->fa(request->get_url_root("edit/" + String::num(building->id)), "Edit")->cdiv(); - } - b.cdiv(); - } - - b.cdiv(); - - request->body += b.result; -} - -void BuildingController::admin_render_building(Request *request, Ref building) { - if (!building.is_valid()) { - RLOG_ERR("admin_render_building: !building.is_valid()\n"); - request->send_error(HTTP_STATUS_CODE_500_INTERNAL_SERVER_ERROR); - return; - } - - Vector > buildings = BuildingModel::get_singleton()->get_all(); - - HTMLBuilder b; - - b.div("back")->fa(request->get_url_root_parent(), "<--- Back")->cdiv(); - b.br(); - b.fdiv("Building Editor", "top_menu"); - b.br(); - - b.form_post(request->get_url_root()); - - bool show_post = false; //request->get_method() == HTTP_METHOD_POST && validation errors; - - ADMIN_EDIT_INPUT_TEXT("Name:", "name", show_post, building->name, request->get_parameter("name")); - ADMIN_EDIT_INPUT_TEXTAREA("Description:", "description", show_post, building->description, request->get_parameter("description")); - - b.div("row_edit"); - b.fdiv("Icon:", "edit_name"); - //todo I'm not sure yet how this worked originally - //b.div("edit_input")->f()->input_image("icon", building->icon)->f()->cdiv(); - b.div("edit_input")->w("TODO")->cdiv(); - b.cdiv(); - - ADMIN_EDIT_INPUT_TEXT("Rank:", "rank", show_post, String::num(building->rank), request->get_parameter("rank")); - - Vector > nrbs = BuildingModel::get_singleton()->get_all(); - b.div("row_edit"); - b.fdiv("Next Rank:", "edit_name"); - b.div("edit_input"); - { - b.select("next_rank", "drop"); - { - int current_id = building->id; - int current_nr = building->next_rank; - - b.foption(String::num(0), "- None -", current_nr == 0); - - for (int i = 0; i < nrbs.size(); ++i) { - Ref build = nrbs[i]; - - int id = build->id; - - if (id == current_id) { - continue; - } - - b.foption(String::num(id), build->name + " R" + String::num(build->rank), current_nr == id); - } - } - b.cselect(); - } - b.cdiv(); - b.cdiv(); - - ADMIN_EDIT_INPUT_TEXT("Time to Build:", "time_to_build", show_post, String::num(building->time_to_build), request->get_parameter("time_to_build")); - - ADMIN_EDIT_LINE_SPACER(); - - ADMIN_EDIT_INPUT_TEXT("Score:", "score", show_post, String::num(building->score), request->get_parameter("score")); - ADMIN_EDIT_INPUT_TEXT("Defense:", "defense", show_post, String::num(building->defense), request->get_parameter("defense")); - - //TODO - /* - int ability; - -
-
- Ability: -
-
- -
-
- */ - - b.div("row_edit"); - b.fdiv("Ability:", "edit_name"); - b.div("edit_input")->w("TODO")->cdiv(); - b.cdiv(); - - ADMIN_EDIT_LINE_SPACER(); - - ADMIN_EDIT_INPUT_TEXT("Cost Food:", "cost_food", show_post, String::num(building->cost_food), request->get_parameter("cost_food")); - ADMIN_EDIT_INPUT_TEXT("Cost Wood:", "cost_wood", show_post, String::num(building->cost_wood), request->get_parameter("cost_wood")); - ADMIN_EDIT_INPUT_TEXT("Cost Stone:", "cost_stone", show_post, String::num(building->cost_stone), request->get_parameter("cost_stone")); - ADMIN_EDIT_INPUT_TEXT("Cost Iron:", "cost_iron", show_post, String::num(building->cost_iron), request->get_parameter("cost_iron")); - ADMIN_EDIT_INPUT_TEXT("Cost Mana:", "cost_mana", show_post, String::num(building->cost_food), request->get_parameter("cost_mana")); - - ADMIN_EDIT_LINE_SPACER(); - -/* - int creates; - int num_creates; - -
-
- Creates: -
-
- - X (max) -
-
-*/ - - b.div("row_edit"); - b.fdiv("Creates:", "edit_name"); - b.div("edit_input")->w("TODO")->cdiv(); - b.cdiv(); - - ADMIN_EDIT_LINE_SPACER(); - - ADMIN_EDIT_INPUT_TEXT("Mod Max Food:", "mod_max_food", show_post, String::num(building->mod_max_food), request->get_parameter("mod_max_food")); - ADMIN_EDIT_INPUT_TEXT("Mod Max Wood:", "mod_max_wood", show_post, String::num(building->mod_max_wood), request->get_parameter("mod_max_wood")); - ADMIN_EDIT_INPUT_TEXT("Mod Max Stone:", "mod_max_stone", show_post, String::num(building->mod_max_stone), request->get_parameter("mod_max_stone")); - ADMIN_EDIT_INPUT_TEXT("Mod Max Iron:", "mod_max_iron", show_post, String::num(building->mod_max_iron), request->get_parameter("mod_max_iron")); - ADMIN_EDIT_INPUT_TEXT("Mod Max Mana:", "mod_max_mana", show_post, String::num(building->mod_max_mana), request->get_parameter("mod_max_mana")); - - ADMIN_EDIT_LINE_SPACER(); - - ADMIN_EDIT_INPUT_TEXT("Mod Rate Food:", "mod_rate_food", show_post, String::num(building->mod_rate_food), request->get_parameter("mod_rate_food")); - ADMIN_EDIT_INPUT_TEXT("Mod Rate Wood:", "mod_rate_wood", show_post, String::num(building->mod_rate_wood), request->get_parameter("mod_rate_wood")); - ADMIN_EDIT_INPUT_TEXT("Mod Rate Stone:", "mod_rate_stone", show_post, String::num(building->mod_rate_stone), request->get_parameter("mod_rate_stone")); - ADMIN_EDIT_INPUT_TEXT("Mod Rate Iron:", "mod_rate_iron", show_post, String::num(building->mod_rate_iron), request->get_parameter("mod_rate_iron")); - ADMIN_EDIT_INPUT_TEXT("Mod Rate Mana:", "mod_rate_mana", show_post, String::num(building->mod_rate_mana), request->get_parameter("mod_rate_mana")); - - ADMIN_EDIT_LINE_SPACER(); - - ADMIN_EDIT_INPUT_TEXT("Mod Percent Food:", "mod_percent_food", show_post, String::num(building->mod_percent_food), request->get_parameter("mod_percent_food")); - ADMIN_EDIT_INPUT_TEXT("Mod Percent Wood:", "mod_percent_wood", show_post, String::num(building->mod_percent_wood), request->get_parameter("mod_percent_wood")); - ADMIN_EDIT_INPUT_TEXT("Mod Percent Stone:", "mod_percent_stone", show_post, String::num(building->mod_percent_stone), request->get_parameter("mod_percent_stone")); - ADMIN_EDIT_INPUT_TEXT("Mod Percent Iron:", "mod_percent_iron", show_post, String::num(building->mod_percent_iron), request->get_parameter("mod_percent_iron")); - ADMIN_EDIT_INPUT_TEXT("Mod Percent Mana:", "mod_percent_mana", show_post, String::num(building->mod_percent_mana), request->get_parameter("mod_percent_mana")); - - ADMIN_EDIT_LINE_SPACER(); - - //TODO - - ADMIN_EDIT_INPUT_TEXT("Assignment 1:", "assignment1", show_post, String::num(building->assignment1), request->get_parameter("assignment1")); - ADMIN_EDIT_INPUT_TEXT("Assignment 2:", "assignment2", show_post, String::num(building->assignment2), request->get_parameter("assignment2")); - ADMIN_EDIT_INPUT_TEXT("Assignment 3:", "assignment3", show_post, String::num(building->assignment3), request->get_parameter("assignment3")); - ADMIN_EDIT_INPUT_TEXT("Assignment 4:", "assignment4", show_post, String::num(building->assignment4), request->get_parameter("assignment4")); - ADMIN_EDIT_INPUT_TEXT("Assignment 5:", "assignment5", show_post, String::num(building->assignment5), request->get_parameter("assignment5")); - - ADMIN_EDIT_LINE_SPACER(); - - //TODO - ADMIN_EDIT_INPUT_TEXT("Required Technology:", "req_tech", show_post, String::num(building->req_tech), request->get_parameter("req_tech")); - ADMIN_EDIT_LINE_SPACER(); - //TODO - ADMIN_EDIT_INPUT_TEXT("Technology Group:", "tech_group", show_post, String::num(building->tech_group), request->get_parameter("tech_group")); - ADMIN_EDIT_LINE_SPACER(); - //TODO - ADMIN_EDIT_INPUT_TEXT("Secondary Technology Group:", "tech_secondary_group", show_post, String::num(building->tech_secondary_group), request->get_parameter("tech_secondary_group")); - - b.div("edit_submit")->input_submit("Save", "submit")->cdiv(); - - b.cform(); - - request->body += b.result; -} - -void BuildingController::create_table() { - BuildingModel::get_singleton()->create_table(); -} -void BuildingController::drop_table() { - BuildingModel::get_singleton()->drop_table(); -} -void BuildingController::create_default_entries() { - BuildingModel::get_singleton()->create_default_entries(); -} - -BuildingController *BuildingController::get_singleton() { - return _self; -} - -BuildingController::BuildingController() : - AdminNode() { - - if (_self) { - printf("BuildingController::BuildingController(): Error! self is not null!/n"); - } - - _self = this; -} - -BuildingController::~BuildingController() { - if (_self == this) { - _self = nullptr; - } -} - -BuildingController *BuildingController::_self = nullptr; diff --git a/app/buildings/building_initializer.cpp b/app/buildings/building_initializer.cpp deleted file mode 100644 index fbdd7a1..0000000 --- a/app/buildings/building_initializer.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "building_initializer.h" - -void BuildingInitializer::allocate_controller() { - ERR_FAIL_COND(_controller); - - _controller = new BuildingController(); -} -void BuildingInitializer::free_controller() { - if (_controller) { - delete _controller; - _controller = nullptr; - } -} - -void BuildingInitializer::allocate_model() { - ERR_FAIL_COND(_model); - - _model = new BuildingModel(); -} -void BuildingInitializer::free_model() { - if (_model) { - delete _model; - _model = nullptr; - } -} - -void BuildingInitializer::allocate_all() { - allocate_model(); - allocate_controller(); -} -void BuildingInitializer::free_all() { - free_controller(); - free_model(); -} - -BuildingController *BuildingInitializer::_controller = nullptr; -BuildingModel *BuildingInitializer::_model = nullptr; \ No newline at end of file diff --git a/app/buildings/building_initializer.h b/app/buildings/building_initializer.h deleted file mode 100644 index 305afe2..0000000 --- a/app/buildings/building_initializer.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef BUILDING_INITIALIZER_H -#define BUILDING_INITIALIZER_H - -#include "building_model.h" -#include "building_controller.h" - -class BuildingInitializer { -public: - static void allocate_controller(); - static void free_controller(); - - static void allocate_model(); - static void free_model(); - - static void allocate_all(); - static void free_all(); - -protected: - static BuildingController *_controller; - static BuildingModel *_model; -}; - -#endif \ No newline at end of file diff --git a/app/buildings/building_model.h b/app/buildings/building_model.h deleted file mode 100644 index d07e25e..0000000 --- a/app/buildings/building_model.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef BUILDING_MODEL_H -#define BUILDING_MODEL_H - -#include "core/string.h" -#include "core/containers/vector.h" - -#include "core/object.h" -#include "core/reference.h" - -class Building; -class QueryResult; - -class BuildingModel : public Object { - RCPP_OBJECT(BuildingModel, Object); - -public: - virtual Ref get_building(const int id); - virtual Vector > get_all(); - virtual void save_building(Ref &building); - - virtual void parse_row(Ref &result, Ref &building); - - void create_table(); - void drop_table(); - void create_default_entries(); - - static BuildingModel *get_singleton(); - - BuildingModel(); - ~BuildingModel(); - -protected: - static BuildingModel *_self; -}; - -#endif \ No newline at end of file diff --git a/app/buildings/building_model.cpp b/app/buildings/building_node.cpp similarity index 55% rename from app/buildings/building_model.cpp rename to app/buildings/building_node.cpp index 38225c3..7d494e9 100644 --- a/app/buildings/building_model.cpp +++ b/app/buildings/building_node.cpp @@ -1,4 +1,12 @@ -#include "building_model.h" +#include "building_node.h" + +#include "web/html/form_validator.h" +#include "web/html/html_builder.h" +#include "web/http/cookie.h" +#include "web/http/http_enums.h" +#include "web/http/http_session.h" +#include "web/http/request.h" +#include "web/http/session_manager.h" #include "database/database.h" #include "database/database_manager.h" @@ -8,19 +16,283 @@ #include "crypto/hash/sha256.h" -#include "building.h" +#include "../html_macros.h" + #define BUILDING_TABLE_NAME "buildings" #define BUILDING_TABLE_COLUMNS "id, name, description, icon, rank, next_rank, time_to_build, creates, num_creates, score, defense, ability, cost_food, cost_wood, cost_stone, cost_iron, cost_mana, mod_max_food, mod_max_wood, mod_max_stone, mod_max_iron, mod_max_mana, mod_rate_food, mod_rate_wood, mod_rate_stone, mod_rate_iron, mod_rate_mana, mod_percent_food, mod_percent_wood, mod_percent_stone, mod_percent_iron, mod_percent_mana, assignment1, assignment2, assignment3, assignment4, assignment5, req_tech, tech_group, tech_secondary_group" #define BUILDING_TABLE_COLUMNS_NOID "name, description, icon, rank, next_rank, time_to_build, creates, num_creates, score, defense, ability, cost_food, cost_wood, cost_stone, cost_iron, cost_mana, mod_max_food, mod_max_wood, mod_max_stone, mod_max_iron, mod_max_mana, mod_rate_food, mod_rate_wood, mod_rate_stone, mod_rate_iron, mod_rate_mana, mod_percent_food, mod_percent_wood, mod_percent_stone, mod_percent_iron, mod_percent_mana, assignment1, assignment2, assignment3, assignment4, assignment5, req_tech, tech_group, tech_secondary_group" -Ref BuildingModel::get_building(const int id) { + +void BuildingNode::handle_request_default(Request *request) { +} + +void BuildingNode::admin_handle_request_main(Request *request) { + String seg = request->get_current_path_segment(); + + if (seg == "") { + admin_render_building_list(request); + return; + } else if (seg == "new") { + request->push_path(); + Ref b; + b.instance(); + + admin_render_building(request, b); + return; + } else if (seg == "edit") { + request->push_path(); + + String seg_building_id = request->get_current_path_segment(); + + if (!seg_building_id.is_int()) { + request->send_error(HTTP_STATUS_CODE_404_NOT_FOUND); + return; + } + + int bid = seg_building_id.to_int(); + + Ref b = db_get_building(bid); + + if (!b.is_valid()) { + request->send_error(HTTP_STATUS_CODE_404_NOT_FOUND); + return; + } + + admin_render_building(request, b); + return; + } + + request->send_error(404); +} +String BuildingNode::admin_get_section_name() { + return "Buildings"; +} +void BuildingNode::admin_add_section_links(Vector *links) { + links->push_back(AdminSectionLinkInfo("- Building Editor", "")); +} +bool BuildingNode::admin_full_render() { + return false; +} + +void BuildingNode::admin_render_building_list(Request *request) { + Vector > buildings = db_get_all(); + + HTMLBuilder b; + + b.div("back")->fa(request->get_url_root_parent(), "<--- Back")->cdiv(); + b.br(); + b.fdiv("Building Editor", "top_menu"); + b.br(); + b.div("top_menu")->fa(request->get_url_root("new"), "Create New")->cdiv(); + b.br(); + + b.div("list_container"); + + for (int i = 0; i < buildings.size(); ++i) { + Ref building = buildings[i]; + + if (!building.is_valid()) { + continue; + } + + if (i % 2 == 0) { + b.div("row"); + } else { + b.div("row second"); + } + { + b.fdiv(String::num(building->id), "attr_box"); + b.fdiv(String::num(building->rank), "attr_box"); + b.fdiv(String::num(building->next_rank), "attr_box"); + b.fdiv(building->name, "name"); + + b.div("actionbox")->fa(request->get_url_root("edit/" + String::num(building->id)), "Edit")->cdiv(); + } + b.cdiv(); + } + + b.cdiv(); + + request->body += b.result; +} + +void BuildingNode::admin_render_building(Request *request, Ref building) { + if (!building.is_valid()) { + RLOG_ERR("admin_render_building: !building.is_valid()\n"); + request->send_error(HTTP_STATUS_CODE_500_INTERNAL_SERVER_ERROR); + return; + } + + Vector > buildings = db_get_all(); + + HTMLBuilder b; + + b.div("back")->fa(request->get_url_root_parent(), "<--- Back")->cdiv(); + b.br(); + b.fdiv("Building Editor", "top_menu"); + b.br(); + + b.form_post(request->get_url_root()); + + bool show_post = false; //request->get_method() == HTTP_METHOD_POST && validation errors; + + ADMIN_EDIT_INPUT_TEXT("Name:", "name", show_post, building->name, request->get_parameter("name")); + ADMIN_EDIT_INPUT_TEXTAREA("Description:", "description", show_post, building->description, request->get_parameter("description")); + + b.div("row_edit"); + b.fdiv("Icon:", "edit_name"); + //todo I'm not sure yet how this worked originally + //b.div("edit_input")->f()->input_image("icon", building->icon)->f()->cdiv(); + b.div("edit_input")->w("TODO")->cdiv(); + b.cdiv(); + + ADMIN_EDIT_INPUT_TEXT("Rank:", "rank", show_post, String::num(building->rank), request->get_parameter("rank")); + + Vector > nrbs = db_get_all(); + b.div("row_edit"); + b.fdiv("Next Rank:", "edit_name"); + b.div("edit_input"); + { + b.select("next_rank", "drop"); + { + int current_id = building->id; + int current_nr = building->next_rank; + + b.foption(String::num(0), "- None -", current_nr == 0); + + for (int i = 0; i < nrbs.size(); ++i) { + Ref build = nrbs[i]; + + int id = build->id; + + if (id == current_id) { + continue; + } + + b.foption(String::num(id), build->name + " R" + String::num(build->rank), current_nr == id); + } + } + b.cselect(); + } + b.cdiv(); + b.cdiv(); + + ADMIN_EDIT_INPUT_TEXT("Time to Build:", "time_to_build", show_post, String::num(building->time_to_build), request->get_parameter("time_to_build")); + + ADMIN_EDIT_LINE_SPACER(); + + ADMIN_EDIT_INPUT_TEXT("Score:", "score", show_post, String::num(building->score), request->get_parameter("score")); + ADMIN_EDIT_INPUT_TEXT("Defense:", "defense", show_post, String::num(building->defense), request->get_parameter("defense")); + + //TODO + /* + int ability; + +
+
+ Ability: +
+
+ +
+
+ */ + + b.div("row_edit"); + b.fdiv("Ability:", "edit_name"); + b.div("edit_input")->w("TODO")->cdiv(); + b.cdiv(); + + ADMIN_EDIT_LINE_SPACER(); + + ADMIN_EDIT_INPUT_TEXT("Cost Food:", "cost_food", show_post, String::num(building->cost_food), request->get_parameter("cost_food")); + ADMIN_EDIT_INPUT_TEXT("Cost Wood:", "cost_wood", show_post, String::num(building->cost_wood), request->get_parameter("cost_wood")); + ADMIN_EDIT_INPUT_TEXT("Cost Stone:", "cost_stone", show_post, String::num(building->cost_stone), request->get_parameter("cost_stone")); + ADMIN_EDIT_INPUT_TEXT("Cost Iron:", "cost_iron", show_post, String::num(building->cost_iron), request->get_parameter("cost_iron")); + ADMIN_EDIT_INPUT_TEXT("Cost Mana:", "cost_mana", show_post, String::num(building->cost_food), request->get_parameter("cost_mana")); + + ADMIN_EDIT_LINE_SPACER(); + +/* + int creates; + int num_creates; + +
+
+ Creates: +
+
+ + X (max) +
+
+*/ + + b.div("row_edit"); + b.fdiv("Creates:", "edit_name"); + b.div("edit_input")->w("TODO")->cdiv(); + b.cdiv(); + + ADMIN_EDIT_LINE_SPACER(); + + ADMIN_EDIT_INPUT_TEXT("Mod Max Food:", "mod_max_food", show_post, String::num(building->mod_max_food), request->get_parameter("mod_max_food")); + ADMIN_EDIT_INPUT_TEXT("Mod Max Wood:", "mod_max_wood", show_post, String::num(building->mod_max_wood), request->get_parameter("mod_max_wood")); + ADMIN_EDIT_INPUT_TEXT("Mod Max Stone:", "mod_max_stone", show_post, String::num(building->mod_max_stone), request->get_parameter("mod_max_stone")); + ADMIN_EDIT_INPUT_TEXT("Mod Max Iron:", "mod_max_iron", show_post, String::num(building->mod_max_iron), request->get_parameter("mod_max_iron")); + ADMIN_EDIT_INPUT_TEXT("Mod Max Mana:", "mod_max_mana", show_post, String::num(building->mod_max_mana), request->get_parameter("mod_max_mana")); + + ADMIN_EDIT_LINE_SPACER(); + + ADMIN_EDIT_INPUT_TEXT("Mod Rate Food:", "mod_rate_food", show_post, String::num(building->mod_rate_food), request->get_parameter("mod_rate_food")); + ADMIN_EDIT_INPUT_TEXT("Mod Rate Wood:", "mod_rate_wood", show_post, String::num(building->mod_rate_wood), request->get_parameter("mod_rate_wood")); + ADMIN_EDIT_INPUT_TEXT("Mod Rate Stone:", "mod_rate_stone", show_post, String::num(building->mod_rate_stone), request->get_parameter("mod_rate_stone")); + ADMIN_EDIT_INPUT_TEXT("Mod Rate Iron:", "mod_rate_iron", show_post, String::num(building->mod_rate_iron), request->get_parameter("mod_rate_iron")); + ADMIN_EDIT_INPUT_TEXT("Mod Rate Mana:", "mod_rate_mana", show_post, String::num(building->mod_rate_mana), request->get_parameter("mod_rate_mana")); + + ADMIN_EDIT_LINE_SPACER(); + + ADMIN_EDIT_INPUT_TEXT("Mod Percent Food:", "mod_percent_food", show_post, String::num(building->mod_percent_food), request->get_parameter("mod_percent_food")); + ADMIN_EDIT_INPUT_TEXT("Mod Percent Wood:", "mod_percent_wood", show_post, String::num(building->mod_percent_wood), request->get_parameter("mod_percent_wood")); + ADMIN_EDIT_INPUT_TEXT("Mod Percent Stone:", "mod_percent_stone", show_post, String::num(building->mod_percent_stone), request->get_parameter("mod_percent_stone")); + ADMIN_EDIT_INPUT_TEXT("Mod Percent Iron:", "mod_percent_iron", show_post, String::num(building->mod_percent_iron), request->get_parameter("mod_percent_iron")); + ADMIN_EDIT_INPUT_TEXT("Mod Percent Mana:", "mod_percent_mana", show_post, String::num(building->mod_percent_mana), request->get_parameter("mod_percent_mana")); + + ADMIN_EDIT_LINE_SPACER(); + + //TODO + + ADMIN_EDIT_INPUT_TEXT("Assignment 1:", "assignment1", show_post, String::num(building->assignment1), request->get_parameter("assignment1")); + ADMIN_EDIT_INPUT_TEXT("Assignment 2:", "assignment2", show_post, String::num(building->assignment2), request->get_parameter("assignment2")); + ADMIN_EDIT_INPUT_TEXT("Assignment 3:", "assignment3", show_post, String::num(building->assignment3), request->get_parameter("assignment3")); + ADMIN_EDIT_INPUT_TEXT("Assignment 4:", "assignment4", show_post, String::num(building->assignment4), request->get_parameter("assignment4")); + ADMIN_EDIT_INPUT_TEXT("Assignment 5:", "assignment5", show_post, String::num(building->assignment5), request->get_parameter("assignment5")); + + ADMIN_EDIT_LINE_SPACER(); + + //TODO + ADMIN_EDIT_INPUT_TEXT("Required Technology:", "req_tech", show_post, String::num(building->req_tech), request->get_parameter("req_tech")); + ADMIN_EDIT_LINE_SPACER(); + //TODO + ADMIN_EDIT_INPUT_TEXT("Technology Group:", "tech_group", show_post, String::num(building->tech_group), request->get_parameter("tech_group")); + ADMIN_EDIT_LINE_SPACER(); + //TODO + ADMIN_EDIT_INPUT_TEXT("Secondary Technology Group:", "tech_secondary_group", show_post, String::num(building->tech_secondary_group), request->get_parameter("tech_secondary_group")); + + b.div("edit_submit")->input_submit("Save", "submit")->cdiv(); + + b.cform(); + + request->body += b.result; +} + +Ref BuildingNode::db_get_building(const int id) { if (id == 0) { return Ref(); } - Ref b = DatabaseManager::get_singleton()->ddb->get_query_builder(); + Ref b = get_query_builder(); b->select(BUILDING_TABLE_COLUMNS); b->from(BUILDING_TABLE_NAME); @@ -38,13 +310,13 @@ Ref BuildingModel::get_building(const int id) { Ref building; building.instance(); - parse_row(r, building); + db_parse_row(r, building); return building; } -Vector > BuildingModel::get_all() { - Ref b = DatabaseManager::get_singleton()->ddb->get_query_builder(); +Vector > BuildingNode::db_get_all() { + Ref b = get_query_builder(); b->select(BUILDING_TABLE_COLUMNS); b->from(BUILDING_TABLE_NAME); @@ -59,7 +331,7 @@ Vector > BuildingModel::get_all() { Ref building; building.instance(); - parse_row(r, building); + db_parse_row(r, building); buildings.push_back(building); } @@ -67,8 +339,8 @@ Vector > BuildingModel::get_all() { return buildings; } -void BuildingModel::save_building(Ref &building) { - Ref b = DatabaseManager::get_singleton()->ddb->get_query_builder(); +void BuildingNode::db_save_building(Ref &building) { + Ref b = get_query_builder(); if (building->id == 0) { b->insert(BUILDING_TABLE_NAME, BUILDING_TABLE_COLUMNS_NOID); @@ -191,7 +463,7 @@ void BuildingModel::save_building(Ref &building) { } } -void BuildingModel::parse_row(Ref &result, Ref &building) { +void BuildingNode::db_parse_row(Ref &result, Ref &building) { building->id = result->get_cell_int(0); @@ -243,8 +515,8 @@ void BuildingModel::parse_row(Ref &result, Ref &building) building->tech_secondary_group = result->get_cell_int(39); } -void BuildingModel::create_table() { - Ref tb = DatabaseManager::get_singleton()->ddb->get_table_builder(); +void BuildingNode::create_table() { + Ref tb = get_table_builder(); tb->create_table(BUILDING_TABLE_NAME); tb->integer("id", 11)->auto_increment()->next_row(); @@ -300,18 +572,18 @@ void BuildingModel::create_table() { tb->run_query(); //tb->print(); } -void BuildingModel::drop_table() { - Ref tb = DatabaseManager::get_singleton()->ddb->get_table_builder(); +void BuildingNode::drop_table() { + Ref tb = get_table_builder(); tb->drop_table_if_exists(BUILDING_TABLE_NAME)->cdrop_table(); tb->run_query(); } -void BuildingModel::create_default_entries() { +void BuildingNode::create_default_entries() { String table_columns = "id, name, description, icon, rank, next_rank, time_to_build, creates, num_creates, score, defense, ability, cost_food, cost_wood, cost_stone, cost_iron, cost_mana, mod_max_food, mod_max_wood, mod_max_stone, mod_max_iron, mod_max_mana, mod_rate_food, mod_rate_wood, mod_rate_stone, mod_rate_iron, mod_rate_mana, mod_percent_food, mod_percent_wood, mod_percent_stone, mod_percent_iron, mod_percent_mana, assignment1, assignment2, assignment3, assignment4, assignment5, req_tech, tech_group, tech_secondary_group"; - Ref qb = DatabaseManager::get_singleton()->ddb->get_query_builder(); + Ref qb = get_query_builder(); qb->begin_transaction()->nl(); qb->insert(BUILDING_TABLE_NAME, table_columns)->nl()->w("VALUES(1, 'empty', '', 'empty/empty.png', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)")->end_command()->nl(); @@ -330,24 +602,24 @@ void BuildingModel::create_default_entries() { //qb->print(); } -BuildingModel *BuildingModel::get_singleton() { +BuildingNode *BuildingNode::get_singleton() { return _self; } -BuildingModel::BuildingModel() : - Object() { +BuildingNode::BuildingNode() : + AdminNode() { if (_self) { - printf("BuildingModel::BuildingModel(): Error! self is not null!/n"); + printf("BuildingNode::BuildingNode(): Error! self is not null!/n"); } _self = this; } -BuildingModel::~BuildingModel() { +BuildingNode::~BuildingNode() { if (_self == this) { _self = nullptr; } } -BuildingModel *BuildingModel::_self = nullptr; +BuildingNode *BuildingNode::_self = nullptr; diff --git a/app/buildings/building_controller.h b/app/buildings/building_node.h similarity index 56% rename from app/buildings/building_controller.h rename to app/buildings/building_node.h index 1090e94..9f82088 100644 --- a/app/buildings/building_controller.h +++ b/app/buildings/building_node.h @@ -1,5 +1,5 @@ -#ifndef BUILDING_CONTROLLER_H -#define BUILDING_CONTROLLER_H +#ifndef BUILDING_NODE_H +#define BUILDING_NODE_H #include "core/string.h" #include "core/containers/vector.h" @@ -10,9 +10,10 @@ class Request; class FormValidator; +class QueryResult; -class BuildingController : public AdminNode { - RCPP_OBJECT(BuildingController, AdminNode); +class BuildingNode : public AdminNode { + RCPP_OBJECT(BuildingNode, AdminNode); public: void handle_request_default(Request *request); @@ -25,17 +26,23 @@ public: void admin_render_building_list(Request *request); void admin_render_building(Request *request, Ref building); + virtual Ref db_get_building(const int id); + virtual Vector > db_get_all(); + virtual void db_save_building(Ref &building); + + virtual void db_parse_row(Ref &result, Ref &building); + void create_table(); void drop_table(); void create_default_entries(); - static BuildingController *get_singleton(); + static BuildingNode *get_singleton(); - BuildingController(); - ~BuildingController(); + BuildingNode(); + ~BuildingNode(); protected: - static BuildingController *_self; + static BuildingNode *_self; }; #endif \ No newline at end of file diff --git a/app/mourne_root.cpp b/app/mourne_root.cpp index 4c8082f..ff46db3 100644 --- a/app/mourne_root.cpp +++ b/app/mourne_root.cpp @@ -22,10 +22,10 @@ #include "mourne_user_controller.h" #include "assignments/assignment_initializer.h" -#include "buildings/building_initializer.h" #include "weather/weather_initializer.h" #include "village/village_node.h" +#include "buildings/building_node.h" void MourneRoot::handle_request_main(Request *request) { if (process_middlewares(request)) { @@ -256,24 +256,20 @@ void MourneRoot::setup_middleware() { void MourneRoot::create_table() { // TODO move these to the node system and remove from here - BuildingController::get_singleton()->create_table(); AssignmentController::get_singleton()->create_table(); WeatherController::get_singleton()->create_table(); } void MourneRoot::drop_table() { - BuildingController::get_singleton()->drop_table(); AssignmentController::get_singleton()->drop_table(); WeatherController::get_singleton()->drop_table(); } void MourneRoot::udpate_table() { // TODO move these to the node system and remove from here - BuildingController::get_singleton()->udpate_table(); AssignmentController::get_singleton()->udpate_table(); WeatherController::get_singleton()->udpate_table(); } void MourneRoot::create_default_entries() { // TODO move these to the node system and remove from here - BuildingController::get_singleton()->create_default_entries(); AssignmentController::get_singleton()->create_default_entries(); WeatherController::get_singleton()->create_default_entries(); } @@ -311,7 +307,6 @@ void MourneRoot::compile_menu() { MourneRoot::MourneRoot() : WebRoot() { - BuildingInitializer::allocate_all(); AssignmentInitializer::allocate_all(); WeatherInitializer::allocate_all(); @@ -319,9 +314,13 @@ MourneRoot::MourneRoot() : _village->set_uri_segment("village"); add_child(_village); + _building = new BuildingNode(); + _building->set_uri_segment("building"); + add_child(_building); + _admin_panel = new AdminPanel(); _admin_panel->set_uri_segment("admin"); - _admin_panel->register_admin_controller("buildings", BuildingController::get_singleton()); + _admin_panel->register_admin_controller("buildings", _building); _admin_panel->register_admin_controller("assignments", AssignmentController::get_singleton()); _admin_panel->register_admin_controller("weather", WeatherController::get_singleton()); @@ -349,7 +348,6 @@ MourneRoot::MourneRoot() : } MourneRoot::~MourneRoot() { - BuildingInitializer::free_all(); AssignmentInitializer::free_all(); WeatherInitializer::free_all(); } diff --git a/app/mourne_root.h b/app/mourne_root.h index 028d426..d03ea58 100644 --- a/app/mourne_root.h +++ b/app/mourne_root.h @@ -13,6 +13,7 @@ class UserController; class MenuNode; class MourneUserController; class VillageNode; +class BuildingNode; #define ENSURE_LOGIN(request) \ if (!is_logged_in(request)) { \ @@ -66,6 +67,7 @@ public: MenuNode *_menu; VillageNode *_village; + BuildingNode *_building; static String menu_head; static String admin_headers;