diff --git a/app/mourne_root.cpp b/app/mourne_root.cpp index ec61ed6..5181246 100644 --- a/app/mourne_root.cpp +++ b/app/mourne_root.cpp @@ -21,11 +21,10 @@ #include "mourne_user_controller.h" -#include "weather/weather_initializer.h" - #include "assignments/assignment_node.h" #include "buildings/building_node.h" #include "village/village_node.h" +#include "weather/weather_node.h" void MourneRoot::handle_request_main(Request *request) { if (process_middlewares(request)) { @@ -254,22 +253,6 @@ void MourneRoot::setup_middleware() { _middlewares.push_back(Ref(new UserSessionSetupMiddleware())); } -void MourneRoot::create_table() { - // TODO move these to the node system and remove from here - WeatherController::get_singleton()->create_table(); -} -void MourneRoot::drop_table() { - WeatherController::get_singleton()->drop_table(); -} -void MourneRoot::udpate_table() { - // TODO move these to the node system and remove from here - WeatherController::get_singleton()->udpate_table(); -} -void MourneRoot::create_default_entries() { - // TODO move these to the node system and remove from here - WeatherController::get_singleton()->create_default_entries(); -} - void MourneRoot::compile_menu() { HTMLBuilder bh; @@ -303,8 +286,6 @@ void MourneRoot::compile_menu() { MourneRoot::MourneRoot() : WebRoot() { - WeatherInitializer::allocate_all(); - _village = new VillageNode(); _village->set_uri_segment("village"); add_child(_village); @@ -317,11 +298,16 @@ MourneRoot::MourneRoot() : _assignments->set_uri_segment("assignments"); add_child(_assignments); + _weathers = new WeatherNode(); + //_weathers->set_uri_segment(""); + _admin_panel = new AdminPanel(); _admin_panel->set_uri_segment("admin"); _admin_panel->register_admin_controller("buildings", _building); _admin_panel->register_admin_controller("assignments", _assignments); - _admin_panel->register_admin_controller("weather", WeatherController::get_singleton()); + _admin_panel->register_admin_controller("weather", _weathers); + + _admin_panel->add_child(_weathers); _user_controller = new MourneUserController(); _user_controller->set_uri_segment("user"); @@ -347,7 +333,6 @@ MourneRoot::MourneRoot() : } MourneRoot::~MourneRoot() { - WeatherInitializer::free_all(); } String MourneRoot::menu_head = ""; diff --git a/app/mourne_root.h b/app/mourne_root.h index 08f17a9..546a015 100644 --- a/app/mourne_root.h +++ b/app/mourne_root.h @@ -15,6 +15,7 @@ class MourneUserController; class VillageNode; class BuildingNode; class AssignmentNode; +class WeatherNode; #define ENSURE_LOGIN(request) \ if (!is_logged_in(request)) { \ @@ -53,11 +54,6 @@ public: virtual void setup_middleware(); - void create_table(); - void drop_table(); - void udpate_table(); - void create_default_entries(); - void compile_menu(); MourneRoot(); @@ -70,6 +66,7 @@ public: VillageNode *_village; BuildingNode *_building; AssignmentNode *_assignments; + WeatherNode *_weathers; static String menu_head; static String admin_headers; diff --git a/app/weather/weather_controller.cpp b/app/weather/weather_controller.cpp deleted file mode 100644 index d0f4d47..0000000 --- a/app/weather/weather_controller.cpp +++ /dev/null @@ -1,189 +0,0 @@ -#include "weather_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 "weather_model.h" - -#include "../html_macros.h" - -void WeatherController::handle_request_default(Request *request) { -} - -void WeatherController::admin_handle_request_main(Request *request) { - String seg = request->get_current_path_segment(); - - if (seg == "") { - admin_render_weather_list(request); - return; - } else if (seg == "new") { - request->push_path(); - Ref b; - b.instance(); - - admin_render_weather(request, b); - return; - } else if (seg == "edit") { - request->push_path(); - - String seg_weather_id = request->get_current_path_segment(); - - if (!seg_weather_id.is_int()) { - request->send_error(HTTP_STATUS_CODE_404_NOT_FOUND); - return; - } - - int bid = seg_weather_id.to_int(); - - Ref b = WeatherModel::get_singleton()->get_weather(bid); - - if (!b.is_valid()) { - request->send_error(HTTP_STATUS_CODE_404_NOT_FOUND); - return; - } - - admin_render_weather(request, b); - return; - } - - request->send_error(404); -} -String WeatherController::admin_get_section_name() { - return "Weathers"; -} -void WeatherController::admin_add_section_links(Vector *links) { - links->push_back(AdminSectionLinkInfo("- Weather Editor", "")); -} -bool WeatherController::admin_full_render() { - return false; -} - -void WeatherController::admin_render_weather_list(Request *request) { - Vector > weathers = WeatherModel::get_singleton()->get_all(); - - HTMLBuilder b; - - b.div("back")->fa(request->get_url_root_parent(), "<--- Back")->cdiv(); - b.br(); - b.fdiv("Weather 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 < weathers.size(); ++i) { - Ref weather = weathers[i]; - - if (!weather.is_valid()) { - continue; - } - - if (i % 2 == 0) { - b.div("row"); - } else { - b.div("row second"); - } - { - b.fdiv(String::num(weather->id), "attr_box"); - b.fdiv(weather->name, "name"); - - b.div("actionbox")->fa(request->get_url_root("edit/" + String::num(weather->id)), "Edit")->cdiv(); - } - b.cdiv(); - } - - b.cdiv(); - - request->body += b.result; -} - -void WeatherController::admin_render_weather(Request *request, Ref weather) { - if (!weather.is_valid()) { - RLOG_ERR("admin_render_weather: !weather.is_valid()\n"); - request->send_error(HTTP_STATUS_CODE_500_INTERNAL_SERVER_ERROR); - return; - } - - Vector > weathers = WeatherModel::get_singleton()->get_all(); - - HTMLBuilder b; - - b.div("back")->fa(request->get_url_root_parent(), "<--- Back")->cdiv(); - b.br(); - b.fdiv("Weather 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, weather->name, request->get_parameter("name")); - ADMIN_EDIT_INPUT_TEXTAREA("Description:", "description", show_post, weather->description, request->get_parameter("description")); - //I think this was supposed to be an icon - ADMIN_EDIT_INPUT_TEXT("Art:", "art", show_post, weather->art, request->get_parameter("art")); - ADMIN_EDIT_INPUT_TEXT("CSS:", "css", show_post, weather->css, request->get_parameter("css")); - - ADMIN_EDIT_LINE_SPACER(); - - ADMIN_EDIT_INPUT_TEXT("Effect:", "effect", show_post, String::num(weather->effect), request->get_parameter("effect")); - - ADMIN_EDIT_LINE_SPACER(); - - ADMIN_EDIT_INPUT_TEXT("Mod Max Food:", "mod_max_food", show_post, String::num(weather->mod_max_food), request->get_parameter("mod_max_food")); - ADMIN_EDIT_INPUT_TEXT("Mod Max Wood:", "mod_max_wood", show_post, String::num(weather->mod_max_wood), request->get_parameter("mod_max_wood")); - ADMIN_EDIT_INPUT_TEXT("Mod Max Stone:", "mod_max_stone", show_post, String::num(weather->mod_max_stone), request->get_parameter("mod_max_stone")); - ADMIN_EDIT_INPUT_TEXT("Mod Max Iron:", "mod_max_iron", show_post, String::num(weather->mod_max_iron), request->get_parameter("mod_max_iron")); - ADMIN_EDIT_INPUT_TEXT("Mod Max Mana:", "mod_max_mana", show_post, String::num(weather->mod_max_mana), request->get_parameter("mod_max_mana")); - - ADMIN_EDIT_LINE_SPACER(); - - ADMIN_EDIT_INPUT_TEXT("Mod Percent Food:", "mod_percent_food", show_post, String::num(weather->mod_percent_food), request->get_parameter("mod_percent_food")); - ADMIN_EDIT_INPUT_TEXT("Mod Percent Wood:", "mod_percent_wood", show_post, String::num(weather->mod_percent_wood), request->get_parameter("mod_percent_wood")); - ADMIN_EDIT_INPUT_TEXT("Mod Percent Stone:", "mod_percent_stone", show_post, String::num(weather->mod_percent_stone), request->get_parameter("mod_percent_stone")); - ADMIN_EDIT_INPUT_TEXT("Mod Percent Iron:", "mod_percent_iron", show_post, String::num(weather->mod_percent_iron), request->get_parameter("mod_percent_iron")); - ADMIN_EDIT_INPUT_TEXT("Mod Percent Mana:", "mod_percent_mana", show_post, String::num(weather->mod_percent_mana), request->get_parameter("mod_percent_mana")); - - b.div("edit_submit")->input_submit("Save", "submit")->cdiv(); - - b.cform(); - - request->body += b.result; -} - -void WeatherController::create_table() { - WeatherModel::get_singleton()->create_table(); -} -void WeatherController::drop_table() { - WeatherModel::get_singleton()->drop_table(); -} -void WeatherController::create_default_entries() { - WeatherModel::get_singleton()->create_default_entries(); -} - -WeatherController *WeatherController::get_singleton() { - return _self; -} - -WeatherController::WeatherController() : - AdminNode() { - - if (_self) { - printf("WeatherController::WeatherController(): Error! self is not null!/n"); - } - - _self = this; -} - -WeatherController::~WeatherController() { - if (_self == this) { - _self = nullptr; - } -} - -WeatherController *WeatherController::_self = nullptr; diff --git a/app/weather/weather_initializer.cpp b/app/weather/weather_initializer.cpp deleted file mode 100644 index a10bf3d..0000000 --- a/app/weather/weather_initializer.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "weather_initializer.h" - -void WeatherInitializer::allocate_controller() { - ERR_FAIL_COND(_controller); - - _controller = new WeatherController(); -} -void WeatherInitializer::free_controller() { - if (_controller) { - delete _controller; - _controller = nullptr; - } -} - -void WeatherInitializer::allocate_model() { - ERR_FAIL_COND(_model); - - _model = new WeatherModel(); -} -void WeatherInitializer::free_model() { - if (_model) { - delete _model; - _model = nullptr; - } -} - -void WeatherInitializer::allocate_all() { - allocate_model(); - allocate_controller(); -} -void WeatherInitializer::free_all() { - free_controller(); - free_model(); -} - -WeatherController *WeatherInitializer::_controller = nullptr; -WeatherModel *WeatherInitializer::_model = nullptr; \ No newline at end of file diff --git a/app/weather/weather_initializer.h b/app/weather/weather_initializer.h deleted file mode 100644 index 58a6578..0000000 --- a/app/weather/weather_initializer.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef WEATHER_INITIALIZER_H -#define WEATHER_INITIALIZER_H - -#include "weather_model.h" -#include "weather_controller.h" - -class WeatherInitializer { -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 WeatherController *_controller; - static WeatherModel *_model; -}; - -#endif \ No newline at end of file diff --git a/app/weather/weather_model.cpp b/app/weather/weather_model.cpp deleted file mode 100644 index 2a5bd6d..0000000 --- a/app/weather/weather_model.cpp +++ /dev/null @@ -1,233 +0,0 @@ -#include "weather_model.h" - -#include "database/database.h" -#include "database/database_manager.h" -#include "database/query_builder.h" -#include "database/query_result.h" -#include "database/table_builder.h" - -#include "crypto/hash/sha256.h" - -#include "weather.h" - -#define WEATHER_TABLE_NAME "weathers" - -#define WEATHER_TABLE_COLUMNS "id, name, description, art, css, effect, mod_max_food, mod_max_wood, mod_max_stone, mod_max_iron, mod_max_mana, mod_percent_food, mod_percent_wood, mod_percent_stone, mod_percent_iron, mod_percent_mana" -#define WEATHER_TABLE_COLUMNS_NOID "name, description, art, css, effect, mod_max_food, mod_max_wood, mod_max_stone, mod_max_iron, mod_max_mana, mod_percent_food, mod_percent_wood, mod_percent_stone, mod_percent_iron, mod_percent_mana" - -Ref WeatherModel::get_weather(const int id) { - if (id == 0) { - return Ref(); - } - - Ref b = DatabaseManager::get_singleton()->ddb->get_query_builder(); - - b->select(WEATHER_TABLE_COLUMNS); - b->from(WEATHER_TABLE_NAME); - - b->where()->wp("id", id); - - b->end_command(); - - Ref r = b->run(); - - if (!r->next_row()) { - return Ref(); - } - - Ref weather; - weather.instance(); - - parse_row(r, weather); - - return weather; -} - -Vector > WeatherModel::get_all() { - Ref b = DatabaseManager::get_singleton()->ddb->get_query_builder(); - - b->select(WEATHER_TABLE_COLUMNS); - b->from(WEATHER_TABLE_NAME); - b->end_command(); - //b->print(); - - Vector > weathers; - - Ref r = b->run(); - - while (r->next_row()) { - Ref weather; - weather.instance(); - - parse_row(r, weather); - - weathers.push_back(weather); - } - - return weathers; -} - -void WeatherModel::save_weather(Ref &weather) { - Ref b = DatabaseManager::get_singleton()->ddb->get_query_builder(); - - if (weather->id == 0) { - b->insert(WEATHER_TABLE_NAME, WEATHER_TABLE_COLUMNS_NOID); - - b->values(); - - b->val(weather->name); - b->val(weather->description); - b->val(weather->art); - b->val(weather->css); - - b->val(weather->effect); - - b->val(weather->mod_max_food); - b->val(weather->mod_max_wood); - b->val(weather->mod_max_stone); - b->val(weather->mod_max_iron); - b->val(weather->mod_max_mana); - - b->val(weather->mod_percent_food); - b->val(weather->mod_percent_wood); - b->val(weather->mod_percent_stone); - b->val(weather->mod_percent_iron); - b->val(weather->mod_percent_mana); - - b->cvalues(); - - b->end_command(); - b->select_last_insert_id(); - //b->print(); - - Ref r = b->run(); - - weather->id = r->get_last_insert_rowid(); - } else { - b->update(WEATHER_TABLE_NAME); - b->set(); - - b->setp("name", weather->name); - b->setp("description", weather->description); - b->setp("art", weather->art); - b->setp("css", weather->css); - - b->setp("effect", weather->effect); - - b->setp("mod_max_food", weather->mod_max_food); - b->setp("mod_max_wood", weather->mod_max_wood); - b->setp("mod_max_stone", weather->mod_max_stone); - b->setp("mod_max_iron", weather->mod_max_iron); - b->setp("mod_max_mana", weather->mod_max_mana); - - b->setp("mod_percent_food", weather->mod_percent_food); - b->setp("mod_percent_wood", weather->mod_percent_wood); - b->setp("mod_percent_stone", weather->mod_percent_stone); - b->setp("mod_percent_iron", weather->mod_percent_iron); - b->setp("mod_percent_mana", weather->mod_percent_mana); - - b->cset(); - b->where()->wp("id", weather->id); - - //b->print(); - - b->run_query(); - } -} - -void WeatherModel::parse_row(Ref &result, Ref &weather) { - - weather->id = result->get_cell_int(0); - - weather->name = result->get_cell(1); - weather->description = result->get_cell(2); - weather->art = result->get_cell(3); - weather->css = result->get_cell(4); - - weather->effect = result->get_cell_int(5); - - weather->mod_max_food = result->get_cell_int(6); - weather->mod_max_wood = result->get_cell_int(7); - weather->mod_max_stone = result->get_cell_int(8); - weather->mod_max_iron = result->get_cell_int(9); - weather->mod_max_mana = result->get_cell_int(10); - - weather->mod_percent_food = result->get_cell_int(11); - weather->mod_percent_wood = result->get_cell_int(12); - weather->mod_percent_stone = result->get_cell_int(13); - weather->mod_percent_iron = result->get_cell_int(14); - weather->mod_percent_mana = result->get_cell_int(15); -} - -void WeatherModel::create_table() { - Ref tb = DatabaseManager::get_singleton()->ddb->get_table_builder(); - - tb->create_table(WEATHER_TABLE_NAME); - tb->integer("id", 11)->auto_increment()->next_row(); - tb->varchar("name", 200)->not_null()->next_row(); - tb->text("description")->not_null()->next_row(); - tb->varchar("art", 500)->not_null()->next_row(); - tb->text("css")->not_null()->next_row(); - tb->integer("effect", 11)->not_null()->next_row(); - - tb->integer("mod_max_food", 11)->not_null()->defval("0")->next_row(); - tb->integer("mod_max_wood", 11)->not_null()->defval("0")->next_row(); - tb->integer("mod_max_stone", 11)->not_null()->defval("0")->next_row(); - tb->integer("mod_max_iron", 11)->not_null()->defval("0")->next_row(); - tb->integer("mod_max_mana", 11)->not_null()->defval("0")->next_row(); - - tb->integer("mod_percent_food", 11)->not_null()->defval("0")->next_row(); - tb->integer("mod_percent_wood", 11)->not_null()->defval("0")->next_row(); - tb->integer("mod_percent_stone", 11)->not_null()->defval("0")->next_row(); - tb->integer("mod_percent_iron", 11)->not_null()->defval("0")->next_row(); - tb->integer("mod_percent_mana", 11)->not_null()->defval("0")->next_row(); - - tb->primary_key("id"); - tb->ccreate_table(); - - tb->run_query(); - //tb->print(); -} -void WeatherModel::drop_table() { - Ref tb = DatabaseManager::get_singleton()->ddb->get_table_builder(); - - tb->drop_table_if_exists(WEATHER_TABLE_NAME)->cdrop_table(); - - tb->run_query(); -} - -void WeatherModel::create_default_entries() { - String table_columns = "id, name, description, art, css, effect, mod_max_food, mod_max_wood, mod_max_stone, mod_max_iron, mod_max_mana, mod_percent_food, mod_percent_wood, mod_percent_stone, mod_percent_iron, mod_percent_mana"; - - Ref qb = DatabaseManager::get_singleton()->ddb->get_query_builder(); - - qb->begin_transaction()->nl(); - qb->insert(WEATHER_TABLE_NAME, table_columns)->nl()->w("VALUES(1, 'Sunny', 'Your maximum food increases by 1000, also increases food production by 10%.', 'E_NOTIMPL', 'sunny', 0, 1000, 0, 0, 0, 0, 10, 0, 0, 0, 0)")->end_command()->nl(); - qb->insert(WEATHER_TABLE_NAME, table_columns)->nl()->w("VALUES(2, 'Cold', 'Your max iron increases by 1000, also iron production increases by 10%.', 'E_NOTIMPL', 'cold', 0, 0, 0, 0, 1000, 0, 0, 0, 0, 10, 0)")->end_command()->nl(); - qb->commit(); - - qb->run_query(); - //qb->print(); -} - -WeatherModel *WeatherModel::get_singleton() { - return _self; -} - -WeatherModel::WeatherModel() : - Object() { - - if (_self) { - printf("WeatherModel::WeatherModel(): Error! self is not null!/n"); - } - - _self = this; -} - -WeatherModel::~WeatherModel() { - if (_self == this) { - _self = nullptr; - } -} - -WeatherModel *WeatherModel::_self = nullptr; diff --git a/app/weather/weather_model.h b/app/weather/weather_model.h deleted file mode 100644 index 0524c94..0000000 --- a/app/weather/weather_model.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef WEATHER_MODEL_H -#define WEATHER_MODEL_H - -#include "core/string.h" -#include "core/containers/vector.h" - -#include "core/object.h" -#include "core/reference.h" - -class Weather; -class QueryResult; - -class WeatherModel : public Object { - RCPP_OBJECT(WeatherModel, Object); - -public: - virtual Ref get_weather(const int id); - virtual Vector > get_all(); - virtual void save_weather(Ref &weather); - - virtual void parse_row(Ref &result, Ref &weather); - - void create_table(); - void drop_table(); - void create_default_entries(); - - static WeatherModel *get_singleton(); - - WeatherModel(); - ~WeatherModel(); - -protected: - static WeatherModel *_self; -}; - -#endif \ No newline at end of file diff --git a/app/weather/weather_node.cpp b/app/weather/weather_node.cpp new file mode 100644 index 0000000..b68c1ff --- /dev/null +++ b/app/weather/weather_node.cpp @@ -0,0 +1,389 @@ +#include "weather_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 "../html_macros.h" + +#include "database/database.h" +#include "database/database_manager.h" +#include "database/query_builder.h" +#include "database/query_result.h" +#include "database/table_builder.h" + +#include "crypto/hash/sha256.h" + +#include "weather.h" + +#define WEATHER_TABLE_NAME "weathers" + +#define WEATHER_TABLE_COLUMNS "id, name, description, art, css, effect, mod_max_food, mod_max_wood, mod_max_stone, mod_max_iron, mod_max_mana, mod_percent_food, mod_percent_wood, mod_percent_stone, mod_percent_iron, mod_percent_mana" +#define WEATHER_TABLE_COLUMNS_NOID "name, description, art, css, effect, mod_max_food, mod_max_wood, mod_max_stone, mod_max_iron, mod_max_mana, mod_percent_food, mod_percent_wood, mod_percent_stone, mod_percent_iron, mod_percent_mana" + + +void WeatherNode::handle_request_default(Request *request) { +} + +void WeatherNode::admin_handle_request_main(Request *request) { + String seg = request->get_current_path_segment(); + + if (seg == "") { + admin_render_weather_list(request); + return; + } else if (seg == "new") { + request->push_path(); + Ref b; + b.instance(); + + admin_render_weather(request, b); + return; + } else if (seg == "edit") { + request->push_path(); + + String seg_weather_id = request->get_current_path_segment(); + + if (!seg_weather_id.is_int()) { + request->send_error(HTTP_STATUS_CODE_404_NOT_FOUND); + return; + } + + int bid = seg_weather_id.to_int(); + + Ref b = db_get_weather(bid); + + if (!b.is_valid()) { + request->send_error(HTTP_STATUS_CODE_404_NOT_FOUND); + return; + } + + admin_render_weather(request, b); + return; + } + + request->send_error(404); +} +String WeatherNode::admin_get_section_name() { + return "Weathers"; +} +void WeatherNode::admin_add_section_links(Vector *links) { + links->push_back(AdminSectionLinkInfo("- Weather Editor", "")); +} +bool WeatherNode::admin_full_render() { + return false; +} + +void WeatherNode::admin_render_weather_list(Request *request) { + Vector > weathers = db_get_all(); + + HTMLBuilder b; + + b.div("back")->fa(request->get_url_root_parent(), "<--- Back")->cdiv(); + b.br(); + b.fdiv("Weather 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 < weathers.size(); ++i) { + Ref weather = weathers[i]; + + if (!weather.is_valid()) { + continue; + } + + if (i % 2 == 0) { + b.div("row"); + } else { + b.div("row second"); + } + { + b.fdiv(String::num(weather->id), "attr_box"); + b.fdiv(weather->name, "name"); + + b.div("actionbox")->fa(request->get_url_root("edit/" + String::num(weather->id)), "Edit")->cdiv(); + } + b.cdiv(); + } + + b.cdiv(); + + request->body += b.result; +} + +void WeatherNode::admin_render_weather(Request *request, Ref weather) { + if (!weather.is_valid()) { + RLOG_ERR("admin_render_weather: !weather.is_valid()\n"); + request->send_error(HTTP_STATUS_CODE_500_INTERNAL_SERVER_ERROR); + return; + } + + Vector > weathers = db_get_all(); + + HTMLBuilder b; + + b.div("back")->fa(request->get_url_root_parent(), "<--- Back")->cdiv(); + b.br(); + b.fdiv("Weather 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, weather->name, request->get_parameter("name")); + ADMIN_EDIT_INPUT_TEXTAREA("Description:", "description", show_post, weather->description, request->get_parameter("description")); + //I think this was supposed to be an icon + ADMIN_EDIT_INPUT_TEXT("Art:", "art", show_post, weather->art, request->get_parameter("art")); + ADMIN_EDIT_INPUT_TEXT("CSS:", "css", show_post, weather->css, request->get_parameter("css")); + + ADMIN_EDIT_LINE_SPACER(); + + ADMIN_EDIT_INPUT_TEXT("Effect:", "effect", show_post, String::num(weather->effect), request->get_parameter("effect")); + + ADMIN_EDIT_LINE_SPACER(); + + ADMIN_EDIT_INPUT_TEXT("Mod Max Food:", "mod_max_food", show_post, String::num(weather->mod_max_food), request->get_parameter("mod_max_food")); + ADMIN_EDIT_INPUT_TEXT("Mod Max Wood:", "mod_max_wood", show_post, String::num(weather->mod_max_wood), request->get_parameter("mod_max_wood")); + ADMIN_EDIT_INPUT_TEXT("Mod Max Stone:", "mod_max_stone", show_post, String::num(weather->mod_max_stone), request->get_parameter("mod_max_stone")); + ADMIN_EDIT_INPUT_TEXT("Mod Max Iron:", "mod_max_iron", show_post, String::num(weather->mod_max_iron), request->get_parameter("mod_max_iron")); + ADMIN_EDIT_INPUT_TEXT("Mod Max Mana:", "mod_max_mana", show_post, String::num(weather->mod_max_mana), request->get_parameter("mod_max_mana")); + + ADMIN_EDIT_LINE_SPACER(); + + ADMIN_EDIT_INPUT_TEXT("Mod Percent Food:", "mod_percent_food", show_post, String::num(weather->mod_percent_food), request->get_parameter("mod_percent_food")); + ADMIN_EDIT_INPUT_TEXT("Mod Percent Wood:", "mod_percent_wood", show_post, String::num(weather->mod_percent_wood), request->get_parameter("mod_percent_wood")); + ADMIN_EDIT_INPUT_TEXT("Mod Percent Stone:", "mod_percent_stone", show_post, String::num(weather->mod_percent_stone), request->get_parameter("mod_percent_stone")); + ADMIN_EDIT_INPUT_TEXT("Mod Percent Iron:", "mod_percent_iron", show_post, String::num(weather->mod_percent_iron), request->get_parameter("mod_percent_iron")); + ADMIN_EDIT_INPUT_TEXT("Mod Percent Mana:", "mod_percent_mana", show_post, String::num(weather->mod_percent_mana), request->get_parameter("mod_percent_mana")); + + b.div("edit_submit")->input_submit("Save", "submit")->cdiv(); + + b.cform(); + + request->body += b.result; +} + + +Ref WeatherNode::db_get_weather(const int id) { + if (id == 0) { + return Ref(); + } + + Ref b = get_query_builder(); + + b->select(WEATHER_TABLE_COLUMNS); + b->from(WEATHER_TABLE_NAME); + + b->where()->wp("id", id); + + b->end_command(); + + Ref r = b->run(); + + if (!r->next_row()) { + return Ref(); + } + + Ref weather; + weather.instance(); + + db_parse_row(r, weather); + + return weather; +} + +Vector > WeatherNode::db_get_all() { + Ref b = get_query_builder(); + + b->select(WEATHER_TABLE_COLUMNS); + b->from(WEATHER_TABLE_NAME); + b->end_command(); + //b->print(); + + Vector > weathers; + + Ref r = b->run(); + + while (r->next_row()) { + Ref weather; + weather.instance(); + + db_parse_row(r, weather); + + weathers.push_back(weather); + } + + return weathers; +} + +void WeatherNode::db_save_weather(Ref &weather) { + Ref b = get_query_builder(); + + if (weather->id == 0) { + b->insert(WEATHER_TABLE_NAME, WEATHER_TABLE_COLUMNS_NOID); + + b->values(); + + b->val(weather->name); + b->val(weather->description); + b->val(weather->art); + b->val(weather->css); + + b->val(weather->effect); + + b->val(weather->mod_max_food); + b->val(weather->mod_max_wood); + b->val(weather->mod_max_stone); + b->val(weather->mod_max_iron); + b->val(weather->mod_max_mana); + + b->val(weather->mod_percent_food); + b->val(weather->mod_percent_wood); + b->val(weather->mod_percent_stone); + b->val(weather->mod_percent_iron); + b->val(weather->mod_percent_mana); + + b->cvalues(); + + b->end_command(); + b->select_last_insert_id(); + //b->print(); + + Ref r = b->run(); + + weather->id = r->get_last_insert_rowid(); + } else { + b->update(WEATHER_TABLE_NAME); + b->set(); + + b->setp("name", weather->name); + b->setp("description", weather->description); + b->setp("art", weather->art); + b->setp("css", weather->css); + + b->setp("effect", weather->effect); + + b->setp("mod_max_food", weather->mod_max_food); + b->setp("mod_max_wood", weather->mod_max_wood); + b->setp("mod_max_stone", weather->mod_max_stone); + b->setp("mod_max_iron", weather->mod_max_iron); + b->setp("mod_max_mana", weather->mod_max_mana); + + b->setp("mod_percent_food", weather->mod_percent_food); + b->setp("mod_percent_wood", weather->mod_percent_wood); + b->setp("mod_percent_stone", weather->mod_percent_stone); + b->setp("mod_percent_iron", weather->mod_percent_iron); + b->setp("mod_percent_mana", weather->mod_percent_mana); + + b->cset(); + b->where()->wp("id", weather->id); + + //b->print(); + + b->run_query(); + } +} + +void WeatherNode::db_parse_row(Ref &result, Ref &weather) { + + weather->id = result->get_cell_int(0); + + weather->name = result->get_cell(1); + weather->description = result->get_cell(2); + weather->art = result->get_cell(3); + weather->css = result->get_cell(4); + + weather->effect = result->get_cell_int(5); + + weather->mod_max_food = result->get_cell_int(6); + weather->mod_max_wood = result->get_cell_int(7); + weather->mod_max_stone = result->get_cell_int(8); + weather->mod_max_iron = result->get_cell_int(9); + weather->mod_max_mana = result->get_cell_int(10); + + weather->mod_percent_food = result->get_cell_int(11); + weather->mod_percent_wood = result->get_cell_int(12); + weather->mod_percent_stone = result->get_cell_int(13); + weather->mod_percent_iron = result->get_cell_int(14); + weather->mod_percent_mana = result->get_cell_int(15); +} + +void WeatherNode::create_table() { + Ref tb = get_table_builder(); + + tb->create_table(WEATHER_TABLE_NAME); + tb->integer("id", 11)->auto_increment()->next_row(); + tb->varchar("name", 200)->not_null()->next_row(); + tb->text("description")->not_null()->next_row(); + tb->varchar("art", 500)->not_null()->next_row(); + tb->text("css")->not_null()->next_row(); + tb->integer("effect", 11)->not_null()->next_row(); + + tb->integer("mod_max_food", 11)->not_null()->defval("0")->next_row(); + tb->integer("mod_max_wood", 11)->not_null()->defval("0")->next_row(); + tb->integer("mod_max_stone", 11)->not_null()->defval("0")->next_row(); + tb->integer("mod_max_iron", 11)->not_null()->defval("0")->next_row(); + tb->integer("mod_max_mana", 11)->not_null()->defval("0")->next_row(); + + tb->integer("mod_percent_food", 11)->not_null()->defval("0")->next_row(); + tb->integer("mod_percent_wood", 11)->not_null()->defval("0")->next_row(); + tb->integer("mod_percent_stone", 11)->not_null()->defval("0")->next_row(); + tb->integer("mod_percent_iron", 11)->not_null()->defval("0")->next_row(); + tb->integer("mod_percent_mana", 11)->not_null()->defval("0")->next_row(); + + tb->primary_key("id"); + tb->ccreate_table(); + + tb->run_query(); + //tb->print(); +} +void WeatherNode::drop_table() { + Ref tb = get_table_builder(); + + tb->drop_table_if_exists(WEATHER_TABLE_NAME)->cdrop_table(); + + tb->run_query(); +} + +void WeatherNode::create_default_entries() { + String table_columns = "id, name, description, art, css, effect, mod_max_food, mod_max_wood, mod_max_stone, mod_max_iron, mod_max_mana, mod_percent_food, mod_percent_wood, mod_percent_stone, mod_percent_iron, mod_percent_mana"; + + Ref qb = get_query_builder(); + + qb->begin_transaction()->nl(); + qb->insert(WEATHER_TABLE_NAME, table_columns)->nl()->w("VALUES(1, 'Sunny', 'Your maximum food increases by 1000, also increases food production by 10%.', 'E_NOTIMPL', 'sunny', 0, 1000, 0, 0, 0, 0, 10, 0, 0, 0, 0)")->end_command()->nl(); + qb->insert(WEATHER_TABLE_NAME, table_columns)->nl()->w("VALUES(2, 'Cold', 'Your max iron increases by 1000, also iron production increases by 10%.', 'E_NOTIMPL', 'cold', 0, 0, 0, 0, 1000, 0, 0, 0, 0, 10, 0)")->end_command()->nl(); + qb->commit(); + + qb->run_query(); + //qb->print(); +} + +WeatherNode *WeatherNode::get_singleton() { + return _self; +} + +WeatherNode::WeatherNode() : + AdminNode() { + + if (_self) { + printf("WeatherNode::WeatherNode(): Error! self is not null!/n"); + } + + _self = this; +} + +WeatherNode::~WeatherNode() { + if (_self == this) { + _self = nullptr; + } +} + +WeatherNode *WeatherNode::_self = nullptr; diff --git a/app/weather/weather_controller.h b/app/weather/weather_node.h similarity index 61% rename from app/weather/weather_controller.h rename to app/weather/weather_node.h index 8b0e3e7..5a2bcd4 100644 --- a/app/weather/weather_controller.h +++ b/app/weather/weather_node.h @@ -1,8 +1,8 @@ #ifndef WEATHER_CONTROLLER_H #define WEATHER_CONTROLLER_H -#include "core/string.h" #include "core/containers/vector.h" +#include "core/string.h" #include "web_modules/admin_panel/admin_node.h" @@ -10,10 +10,11 @@ class Request; class FormValidator; +class QueryResult; + +class WeatherNode : public AdminNode { + RCPP_OBJECT(WeatherNode, AdminNode); -class WeatherController : public AdminNode { - RCPP_OBJECT(WeatherController, AdminNode); - public: void handle_request_default(Request *request); @@ -25,17 +26,23 @@ public: void admin_render_weather_list(Request *request); void admin_render_weather(Request *request, Ref weather); + virtual Ref db_get_weather(const int id); + virtual Vector > db_get_all(); + virtual void db_save_weather(Ref &weather); + + virtual void db_parse_row(Ref &result, Ref &weather); + void create_table(); void drop_table(); void create_default_entries(); - static WeatherController *get_singleton(); + static WeatherNode *get_singleton(); - WeatherController(); - ~WeatherController(); + WeatherNode(); + ~WeatherNode(); protected: - static WeatherController *_self; + static WeatherNode *_self; }; #endif \ No newline at end of file