mourne_rcpp_fw/app/mourne_root.cpp

341 lines
6.8 KiB
C++
Raw Normal View History

#include "mourne_root.h"
2021-10-30 22:48:02 +02:00
2022-02-05 23:02:06 +01:00
#include "web/http/request.h"
2021-10-30 22:48:02 +02:00
#include <iostream>
2022-02-05 23:02:06 +01:00
#include "web/file_cache.h"
2021-10-30 22:48:02 +02:00
#include "core/os/arg_parser.h"
2022-02-05 23:02:06 +01:00
#include "core/os/platform.h"
2021-10-30 22:48:02 +02:00
2022-02-05 23:02:06 +01:00
#include "database/database_manager.h"
2021-10-30 22:48:02 +02:00
2022-02-05 23:02:06 +01:00
#include "web/html/html_builder.h"
#include "web/http/http_session.h"
#include "web/http/session_manager.h"
2021-10-30 22:48:02 +02:00
2022-02-05 23:02:06 +01:00
#include "web_modules/admin_panel/admin_panel.h"
#include "web_modules/users/user.h"
#include "web_modules/users/user_controller.h"
2021-10-30 22:48:02 +02:00
#include "mourne_user_controller.h"
2022-02-17 23:09:06 +01:00
#include "assignments/assignment_node.h"
#include "buildings/building_node.h"
2022-02-17 23:09:06 +01:00
#include "village/village_node.h"
2022-02-17 23:37:02 +01:00
#include "weather/weather_node.h"
void MourneRoot::handle_request_main(Request *request) {
if (process_middlewares(request)) {
return;
}
if (try_send_wwwroot_file(request)) {
return;
}
// this is a hack, until I have a simple index node, or port contentcontroller.
if (request->get_path_segment_count() == 0) {
index(request);
return;
}
WebNode *handler = get_request_handler_child(request);
if (!handler) {
request->send_error(404);
return;
}
add_menu(request, MENUENTRY_NEWS);
2022-02-10 17:20:23 +01:00
request->push_path();
handler->handle_request_main(request);
}
bool MourneRoot::is_logged_in(Request *request) {
if (!request->session.is_valid()) {
return false;
}
Ref<User> u = request->reference_data["user"];
2021-10-31 02:11:47 +01:00
return u.is_valid();
}
void MourneRoot::index(Request *request) {
ENSURE_LOGIN(request);
2021-10-30 22:48:02 +02:00
add_menu(request, MENUENTRY_NEWS);
2021-10-30 23:33:37 +02:00
/*
<?php if (isset($hero)): ?>
<link rel="stylesheet" type="text/css" href="<?=base_url('css/hero.css'); ?>">
<?php endif; ?>
<?php if ($userlevel > 2): ?>
<link rel="stylesheet" type="text/css" href="<?=base_url('css/admin.css'); ?>">
<?php endif; ?>
<?php if ($page == 'mail'): ?>
<link rel="stylesheet" type="text/css" href="<?=base_url('css/mail.css'); ?>">
<?php endif; ?>
<?php if ($resources): ?>
<script src="<?=base_url('js/resource.js'); ?>"></script>
<?php endif; ?>
*/
// dynamic_cast<ListPage *>(instance)->index(request);
2021-10-30 22:48:02 +02:00
request->body += "test";
request->compile_and_send_body();
}
void MourneRoot::add_menu(Request *request, const MenuEntries index) {
2021-10-30 22:48:02 +02:00
request->head += menu_head;
2021-10-30 23:33:37 +02:00
HTMLBuilder b;
HTMLTag *t;
int userlevel = 0;
if (request->session.is_valid()) {
Ref<User> user = request->reference_data["user"];
if (user.is_valid()) {
userlevel = user->rank;
}
}
if (userlevel > 4) {
request->head += admin_headers;
}
2021-10-31 02:11:47 +01:00
/*
2021-10-30 23:33:37 +02:00
<?php if ($weather): ?>
<div class="menu_base <?=$weather['css']; ?>">
<?php else: ?>
<div class="menu_base">
<?php endif; ?>
*/
b.div()->cls("menu_base");
{
b.div()->cls("left");
{
b.div()->cls("menu_news");
{
b.a()->href("/news/index");
b.w("News");
b.ca();
}
b.cdiv();
b.div()->cls("menu_mail");
{
b.a()->href("/mail/inbox");
b.w("Mails");
// if ($newmail) echo '!';
2021-10-30 23:33:37 +02:00
b.ca();
}
b.cdiv();
b.div()->cls("menu_hero");
{
b.a()->href("/hero/selected");
b.w("Hero");
b.ca();
}
b.cdiv();
b.div()->cls("menu_village");
{
b.a()->href("/village/selected");
b.w("Village"); // villagename
2021-10-30 23:33:37 +02:00
b.ca();
}
b.cdiv();
b.div()->cls("menu_sel_village");
{
b.a()->href("/village/select");
b.w("v");
b.ca();
}
b.cdiv();
/*
<?php if ($alliancename): ?>
<div class="menu_alliance">
<a href="<?=site_url($menu_alliance); ?>">[<?=$alliancename; ?>]</a>
</div>
<?php endif; ?>
*/
/*
<?php if ($weather): ?>
<div class="weather">
<abbr title="<?=$weather['description']; ?>"><?=$weather['name']; ?></abbr>
</div>
<?php endif; ?>
*/
}
b.cdiv();
b.div()->cls("right");
{
if (userlevel > 4) {
b.div()->cls("menu_gm");
{
b.a()->href("/gm")->f()->w("GM")->ca();
}
b.cdiv();
}
2021-10-30 23:33:37 +02:00
if (userlevel > 5) {
b.div()->cls("menu_admin");
{
b.a()->href("/admin")->f()->w("Admin")->ca();
}
b.cdiv();
}
2021-10-30 23:33:37 +02:00
b.div()->cls("menu_alliance_menu");
{
b.a()->href("/alliance/alliance_menu");
b.w("Alliances");
b.ca();
}
b.cdiv();
b.div()->cls("menu_forum");
{
b.a()->href("/forum/index");
b.w("Forum");
b.ca();
}
b.cdiv();
b.div()->cls("menu_settings");
{
b.a()->href("/user/settings");
b.w("Settings");
b.ca();
}
b.cdiv();
b.div()->cls("menu_logout");
{
b.a()->href("/user/logout");
b.w("Logout");
b.ca();
}
b.cdiv();
}
b.cdiv();
b.div()->cls("nofloat");
b.cdiv();
}
2021-10-31 02:11:47 +01:00
2021-10-30 23:33:37 +02:00
b.cdiv();
b.div()->cls("main");
b.write_tag();
request->body += b.result;
2021-10-30 22:48:02 +02:00
request->footer = footer;
}
void MourneRoot::setup_middleware() {
_middlewares.push_back(Ref<SessionSetupMiddleware>(new SessionSetupMiddleware()));
_middlewares.push_back(Ref<UserSessionSetupMiddleware>(new UserSessionSetupMiddleware()));
2021-10-30 22:48:02 +02:00
}
void MourneRoot::compile_menu() {
2021-10-30 22:48:02 +02:00
HTMLBuilder bh;
bh.meta()->charset_utf_8();
2021-10-30 23:33:37 +02:00
bh.meta()->name("description")->content("RPG browsergame");
bh.meta()->name("keywords")->content("RPG,browsergame,Mourne,game,play");
bh.title();
bh.w("Mourne");
bh.ctitle();
bh.link()->rel_stylesheet()->href("/css/base.css");
bh.link()->rel_stylesheet()->href("/css/menu.css");
2021-10-30 22:48:02 +02:00
bh.write_tag();
menu_head = bh.result;
bh.result = "";
bh.link()->rel("stylesheet")->type("text/css")->href("/css/admin.css")->f()->f();
admin_headers = bh.result;
2021-10-30 22:48:02 +02:00
HTMLBuilder bf;
bf.cdiv();
bf.footer();
bf.cfooter();
footer = bf.result;
}
MourneRoot::MourneRoot() :
WebRoot() {
2021-10-30 22:48:02 +02:00
_village = new VillageNode();
_village->set_uri_segment("village");
add_child(_village);
_building = new BuildingNode();
_building->set_uri_segment("building");
add_child(_building);
2022-02-17 23:09:06 +01:00
_assignments = new AssignmentNode();
_assignments->set_uri_segment("assignments");
add_child(_assignments);
2022-02-17 23:37:02 +01:00
_weathers = new WeatherNode();
//_weathers->set_uri_segment("");
_admin_panel = new AdminPanel();
_admin_panel->set_uri_segment("admin");
_admin_panel->register_admin_controller("buildings", _building);
2022-02-17 23:09:06 +01:00
_admin_panel->register_admin_controller("assignments", _assignments);
2022-02-17 23:37:02 +01:00
_admin_panel->register_admin_controller("weather", _weathers);
_admin_panel->add_child(_weathers);
_user_controller = new MourneUserController();
_user_controller->set_uri_segment("user");
// user_manager->set_path("./users/");
add_child(_user_controller);
add_child(_admin_panel);
HTMLBuilder b;
b.link()->rel("stylesheet")->type("text/css")->href("/css/base.css")->f()->f();
b.link()->rel("stylesheet")->type("text/css")->href("/css/admin.css")->f()->f();
_admin_panel->set_default_header(b.result);
b.result = "";
b.div()->cls("back")->f()->a()->href("/")->f()->w("<--- Back")->ca()->cdiv();
b.br();
2021-11-14 22:25:53 +01:00
_admin_panel->set_default_main_body_top(b.result);
2021-10-30 22:48:02 +02:00
compile_menu();
}
MourneRoot::~MourneRoot() {
2021-10-30 22:48:02 +02:00
}
String MourneRoot::menu_head = "";
String MourneRoot::admin_headers = "";
String MourneRoot::footer = "";