Added base classes for buildings.

This commit is contained in:
Relintai 2021-11-14 14:19:13 +01:00
parent 165d0d5d70
commit 946b0b5acc
10 changed files with 266 additions and 0 deletions

View File

@ -33,6 +33,7 @@ import traceback
folders = [
'app',
'app/buildings',
'app/village',
]

View File

@ -0,0 +1,9 @@
#include "building.h"
Building::Building() :
Resource() {
}
Building::~Building() {
}

16
app/buildings/building.h Normal file
View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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