Same change to the weathers.

This commit is contained in:
Relintai 2022-02-17 23:37:02 +01:00
parent d174286f1a
commit a998bfbc23
9 changed files with 413 additions and 553 deletions

View File

@ -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<UserSessionSetupMiddleware>(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 = "";

View File

@ -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;

View File

@ -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<Weather> 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<Weather> 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<AdminSectionLinkInfo> *links) {
links->push_back(AdminSectionLinkInfo("- Weather Editor", ""));
}
bool WeatherController::admin_full_render() {
return false;
}
void WeatherController::admin_render_weather_list(Request *request) {
Vector<Ref<Weather> > 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> 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> 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<Ref<Weather> > 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;

View File

@ -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;

View File

@ -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

View File

@ -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<Weather> WeatherModel::get_weather(const int id) {
if (id == 0) {
return Ref<Weather>();
}
Ref<QueryBuilder> 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<QueryResult> r = b->run();
if (!r->next_row()) {
return Ref<Weather>();
}
Ref<Weather> weather;
weather.instance();
parse_row(r, weather);
return weather;
}
Vector<Ref<Weather> > WeatherModel::get_all() {
Ref<QueryBuilder> b = DatabaseManager::get_singleton()->ddb->get_query_builder();
b->select(WEATHER_TABLE_COLUMNS);
b->from(WEATHER_TABLE_NAME);
b->end_command();
//b->print();
Vector<Ref<Weather> > weathers;
Ref<QueryResult> r = b->run();
while (r->next_row()) {
Ref<Weather> weather;
weather.instance();
parse_row(r, weather);
weathers.push_back(weather);
}
return weathers;
}
void WeatherModel::save_weather(Ref<Weather> &weather) {
Ref<QueryBuilder> 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<QueryResult> 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<QueryResult> &result, Ref<Weather> &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<TableBuilder> 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<TableBuilder> 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<QueryBuilder> 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;

View File

@ -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<Weather> get_weather(const int id);
virtual Vector<Ref<Weather> > get_all();
virtual void save_weather(Ref<Weather> &weather);
virtual void parse_row(Ref<QueryResult> &result, Ref<Weather> &weather);
void create_table();
void drop_table();
void create_default_entries();
static WeatherModel *get_singleton();
WeatherModel();
~WeatherModel();
protected:
static WeatherModel *_self;
};
#endif

View File

@ -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<Weather> 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<Weather> 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<AdminSectionLinkInfo> *links) {
links->push_back(AdminSectionLinkInfo("- Weather Editor", ""));
}
bool WeatherNode::admin_full_render() {
return false;
}
void WeatherNode::admin_render_weather_list(Request *request) {
Vector<Ref<Weather> > 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> 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> 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<Ref<Weather> > 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<Weather> WeatherNode::db_get_weather(const int id) {
if (id == 0) {
return Ref<Weather>();
}
Ref<QueryBuilder> b = get_query_builder();
b->select(WEATHER_TABLE_COLUMNS);
b->from(WEATHER_TABLE_NAME);
b->where()->wp("id", id);
b->end_command();
Ref<QueryResult> r = b->run();
if (!r->next_row()) {
return Ref<Weather>();
}
Ref<Weather> weather;
weather.instance();
db_parse_row(r, weather);
return weather;
}
Vector<Ref<Weather> > WeatherNode::db_get_all() {
Ref<QueryBuilder> b = get_query_builder();
b->select(WEATHER_TABLE_COLUMNS);
b->from(WEATHER_TABLE_NAME);
b->end_command();
//b->print();
Vector<Ref<Weather> > weathers;
Ref<QueryResult> r = b->run();
while (r->next_row()) {
Ref<Weather> weather;
weather.instance();
db_parse_row(r, weather);
weathers.push_back(weather);
}
return weathers;
}
void WeatherNode::db_save_weather(Ref<Weather> &weather) {
Ref<QueryBuilder> 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<QueryResult> 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<QueryResult> &result, Ref<Weather> &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<TableBuilder> 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<TableBuilder> 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<QueryBuilder> 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;

View File

@ -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,9 +10,10 @@
class Request;
class FormValidator;
class QueryResult;
class WeatherController : public AdminNode {
RCPP_OBJECT(WeatherController, AdminNode);
class WeatherNode : public AdminNode {
RCPP_OBJECT(WeatherNode, 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> weather);
virtual Ref<Weather> db_get_weather(const int id);
virtual Vector<Ref<Weather> > db_get_all();
virtual void db_save_weather(Ref<Weather> &weather);
virtual void db_parse_row(Ref<QueryResult> &result, Ref<Weather> &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