mourne_rcpp_fw/app/village/village_model.cpp

133 lines
3.8 KiB
C++
Raw Normal View History

2021-11-14 11:45:34 +01:00
#include "village_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 VILLAGE_TABLE_NAME "villages"
#define VILLAGE_BUILDINGS_TABLE_NAME "village_buildings"
#define VILLAGE_TECHNOLOGIES_TABLE_NAME "village_technologies"
#define VILLAGE_UNITS_TABLE_NAME "village_units"
2021-11-14 11:45:34 +01:00
void VillageModel::create_table() {
Ref<TableBuilder> tb = DatabaseManager::get_singleton()->ddb->get_table_builder();
tb->create_table(VILLAGE_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
2021-11-14 11:45:34 +01:00
tb->primary_key("id");
tb->foreign_key("userid")->references("users", "id");
2021-11-14 11:45:34 +01:00
tb->ccreate_table();
2021-11-14 11:45:34 +01:00
tb->run_query();
//tb->print();
tb->result = "";
tb->create_table(VILLAGE_BUILDINGS_TABLE_NAME);
tb->integer("id", 11)->auto_increment()->next_row();
tb->integer("villageid", 11)->not_null()->next_row();
tb->integer("slotid", 11)->not_null()->next_row();
tb->integer("buildingid", 11)->not_null()->next_row(); //foreign key
tb->primary_key("id");
tb->foreign_key("villageid")->references(VILLAGE_TABLE_NAME, "id");
tb->ccreate_table();
tb->run_query();
//tb->print();
tb->result = "";
tb->create_table(VILLAGE_TECHNOLOGIES_TABLE_NAME);
tb->integer("id", 11)->auto_increment()->next_row();
tb->integer("villageid", 11)->not_null()->next_row();
tb->integer("slotid", 11)->not_null()->next_row();
tb->integer("technologyid", 11)->not_null()->next_row(); //foreign key
tb->primary_key("id");
tb->foreign_key("villageid")->references(VILLAGE_TABLE_NAME, "id");
//todo
//KEY `villageid` (`villageid`,`slotid`,`technologyid`)
tb->ccreate_table();
tb->run_query();
//tb->print();
tb->result = "";
tb->create_table(VILLAGE_UNITS_TABLE_NAME);
tb->integer("id", 11)->auto_increment()->next_row();
tb->integer("userid", 11)->not_null()->next_row();
tb->integer("villageid", 11)->not_null()->next_row();
tb->integer("unitid", 11)->not_null()->next_row();
tb->integer("unitcount", 11)->not_null()->next_row();
tb->primary_key("id");
tb->foreign_key("villageid")->references(VILLAGE_TABLE_NAME, "id");
tb->foreign_key("userid")->references("users", "id");
tb->ccreate_table();
tb->run_query();
//tb->print();
tb->result = "";
2021-11-14 11:45:34 +01:00
}
void VillageModel::drop_table() {
Ref<TableBuilder> tb = DatabaseManager::get_singleton()->ddb->get_table_builder();
tb->drop_table_if_exists(VILLAGE_UNITS_TABLE_NAME)->cdrop_table();
tb->drop_table_if_exists(VILLAGE_TECHNOLOGIES_TABLE_NAME)->cdrop_table();
tb->drop_table_if_exists(VILLAGE_BUILDINGS_TABLE_NAME)->cdrop_table();
tb->drop_table_if_exists(VILLAGE_TABLE_NAME)->cdrop_table();
tb->run_query();
2021-11-14 11:45:34 +01:00
}
void VillageModel::migrate() {
drop_table();
create_table();
}
void VillageModel::add_default_data() {
}
2021-11-14 11:45:34 +01:00
VillageModel *VillageModel::get_singleton() {
return _self;
}
VillageModel::VillageModel() :
Object() {
if (_self) {
printf("VillageModel::VillageModel(): Error! self is not null!/n");
}
_self = this;
}
VillageModel::~VillageModel() {
if (_self == this) {
_self = nullptr;
}
}
VillageModel *VillageModel::_self = nullptr;