diff --git a/SConstruct b/SConstruct index 6a1d5dc..2f068a5 100644 --- a/SConstruct +++ b/SConstruct @@ -33,6 +33,7 @@ import traceback folders = [ 'app', + 'app/buildings', 'app/village', ] diff --git a/app/buildings/building.cpp b/app/buildings/building.cpp new file mode 100644 index 0000000..1e76054 --- /dev/null +++ b/app/buildings/building.cpp @@ -0,0 +1,9 @@ +#include "building.h" + +Building::Building() : + Resource() { + +} + +Building::~Building() { +} diff --git a/app/buildings/building.h b/app/buildings/building.h new file mode 100644 index 0000000..1f45162 --- /dev/null +++ b/app/buildings/building.h @@ -0,0 +1,16 @@ +#ifndef BUILDING_H +#define BUILDING_H + +#include "core/string.h" + +#include "core/resource.h" + +class Building : public Resource { + RCPP_OBJECT(Building, Resource); + +public: + Building(); + ~Building(); +}; + +#endif \ No newline at end of file diff --git a/app/buildings/building_controller.cpp b/app/buildings/building_controller.cpp new file mode 100644 index 0000000..2b17025 --- /dev/null +++ b/app/buildings/building_controller.cpp @@ -0,0 +1,42 @@ +#include "building_controller.h" + +#include "core/html/form_validator.h" +#include "core/html/html_builder.h" +#include "core/http/cookie.h" +#include "core/http/http_session.h" +#include "core/http/request.h" +#include "core/http/session_manager.h" + +#include "building_model.h" + +void BuildingController::handle_request_default(Request *request) { +} + +void BuildingController::migrate() { + BuildingModel::get_singleton()->migrate(); +} +void BuildingController::add_default_data() { + BuildingModel::get_singleton()->add_default_data(); +} + +BuildingController *BuildingController::get_singleton() { + return _self; +} + +BuildingController::BuildingController() : + Object() { + + 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_controller.h b/app/buildings/building_controller.h new file mode 100644 index 0000000..30e2481 --- /dev/null +++ b/app/buildings/building_controller.h @@ -0,0 +1,32 @@ +#ifndef BUILDING_CONTROLLER_H +#define BUILDING_CONTROLLER_H + +#include "core/string.h" +#include "core/containers/vector.h" + +#include "core/object.h" + +#include "building.h" + +class Request; +class FormValidator; + +class BuildingController : public Object { + RCPP_OBJECT(BuildingController, Object); + +public: + void migrate(); + virtual void add_default_data(); + + virtual void handle_request_default(Request *request); + + static BuildingController *get_singleton(); + + BuildingController(); + ~BuildingController(); + +protected: + static BuildingController *_self; +}; + +#endif \ No newline at end of file diff --git a/app/buildings/building_initializer.cpp b/app/buildings/building_initializer.cpp new file mode 100644 index 0000000..fbdd7a1 --- /dev/null +++ b/app/buildings/building_initializer.cpp @@ -0,0 +1,37 @@ +#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 new file mode 100644 index 0000000..305afe2 --- /dev/null +++ b/app/buildings/building_initializer.h @@ -0,0 +1,23 @@ +#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.cpp b/app/buildings/building_model.cpp new file mode 100644 index 0000000..4277743 --- /dev/null +++ b/app/buildings/building_model.cpp @@ -0,0 +1,74 @@ +#include "building_model.h" + +#include "core/database/database.h" +#include "core/database/database_manager.h" +#include "core/database/query_builder.h" +#include "core/database/query_result.h" +#include "core/database/table_builder.h" + +#include "core/hash/sha256.h" + +#define BUILDING_TABLE_NAME "buildings" + +void BuildingModel::create_table() { + Ref tb = DatabaseManager::get_singleton()->ddb->get_table_builder(); + + tb->create_table(BUILDING_TABLE_NAME); + tb->integer("id", 11)->auto_increment()->next_row(); + tb->integer("userid", 11)->not_null()->next_row(); + tb->varchar("name", 60)->not_null()->next_row(); + tb->tiny_integer("score", 4)->not_null()->defval("100")->next_row(); + tb->tiny_integer("selected", 4)->not_null()->next_row(); + tb->tiny_integer("new_log", 4)->not_null()->defval("0")->next_row(); + tb->tiny_integer("ai_on", 4)->not_null()->defval("0")->next_row(); + tb->tiny_integer("ai_flagged", 4)->not_null()->defval("0")->next_row(); + tb->integer("weather", 11)->not_null()->defval("0")->next_row(); //foreigh key + tb->integer("last_weather_change", 11)->not_null()->defval("0")->next_row(); + tb->integer("weather_change_to", 11)->not_null()->defval("0")->next_row(); //foreigh key + + tb->primary_key("id"); + tb->foreign_key("userid")->references("users", "id"); + tb->ccreate_table(); + + tb->run_query(); + //tb->print(); + +} +void BuildingModel::drop_table() { + Ref tb = DatabaseManager::get_singleton()->ddb->get_table_builder(); + + tb->drop_table_if_exists(BUILDING_TABLE_NAME)->cdrop_table(); + + tb->run_query(); +} +void BuildingModel::migrate() { + drop_table(); + create_table(); +} + +void BuildingModel::add_default_data() { + +} + + +BuildingModel *BuildingModel::get_singleton() { + return _self; +} + +BuildingModel::BuildingModel() : + Object() { + + if (_self) { + printf("BuildingModel::BuildingModel(): Error! self is not null!/n"); + } + + _self = this; +} + +BuildingModel::~BuildingModel() { + if (_self == this) { + _self = nullptr; + } +} + +BuildingModel *BuildingModel::_self = nullptr; diff --git a/app/buildings/building_model.h b/app/buildings/building_model.h new file mode 100644 index 0000000..5de9b07 --- /dev/null +++ b/app/buildings/building_model.h @@ -0,0 +1,27 @@ +#ifndef BUILDING_MODEL_H +#define BUILDING_MODEL_H + +#include "core/string.h" +#include "core/containers/vector.h" + +#include "core/object.h" + +class BuildingModel : public Object { + RCPP_OBJECT(BuildingModel, Object); + +public: + virtual void create_table(); + virtual void drop_table(); + virtual void migrate(); + virtual void add_default_data(); + + static BuildingModel *get_singleton(); + + BuildingModel(); + ~BuildingModel(); + +protected: + static BuildingModel *_self; +}; + +#endif \ No newline at end of file diff --git a/app/mourne_application.cpp b/app/mourne_application.cpp index a74cee7..fbab8a8 100644 --- a/app/mourne_application.cpp +++ b/app/mourne_application.cpp @@ -17,6 +17,7 @@ #include "modules/users/user.h" #include "modules/users/user_controller.h" +#include "buildings/building_initializer.h" #include "village/village_initializer.h" bool MourneApplication::is_logged_in(Request *request) { @@ -235,10 +236,12 @@ void MourneApplication::setup_middleware() { } void MourneApplication::migrate() { + BuildingController::get_singleton()->migrate(); VillageController::get_singleton()->migrate(); } void MourneApplication::add_default_data() { + BuildingController::get_singleton()->add_default_data(); VillageController::get_singleton()->add_default_data(); } @@ -270,6 +273,7 @@ void MourneApplication::compile_menu() { MourneApplication::MourneApplication() : DWebApplication() { + BuildingInitializer::allocate_all(); VillageInitializer::allocate_all(); compile_menu(); @@ -278,6 +282,7 @@ MourneApplication::MourneApplication() : MourneApplication::~MourneApplication() { VillageInitializer::free_all(); + BuildingInitializer::free_all(); } std::string MourneApplication::menu_head = "";