added shadowrun database

This commit is contained in:
2025-06-01 21:19:54 +02:00
parent 71f771b428
commit 69f7f625f8
20 changed files with 738 additions and 103 deletions

View File

@@ -0,0 +1,26 @@
#include <format>
#include "HtmxShAttributeList.hpp"
#include "utils.hpp"
using namespace std;
HtmxShAttributeList::HtmxShAttributeList(const std::string& id, const vector<string>& itemList){
html += format("<h2>{}</h2>", id);
html += "<div class='grid'>";
for (auto& item : itemList){
string item_id = utils::to_id_format(id + "_" + item);
html += format("<label>{}:<input type='text' name='{}'></label>", item, item_id);
}
html += "</div>";
}
HtmxShAttributeList::HtmxShAttributeList(const std::string& id, const std::vector<std::pair<std::string, std::string>>& itemValueList){
html += format("<h2>{}</h2>", id);
html += "<div class='grid'>";
for (auto& item : itemValueList){
string item_id = utils::to_id_format(id + "_" + item.first);
html += format("<label>{}:<input type='text' name='{}' value='{}'></label>", item, item_id, item.second);
}
html += "</div>";
}

View File

@@ -0,0 +1,18 @@
#ifndef HTMXSHATTRIBUTELIST_H
#define HTMXSHATTRIBUTELIST_H
#include "HtmxObject.h"
#include <vector>
#include <string>
class HtmxShAttributeList : public HtmxObject {
public:
// create new item list
HtmxShAttributeList(const std::string& id, const std::vector<std::string>& itemList);
// create new item list where each item as a value
HtmxShAttributeList(const std::string& id, const std::vector<std::pair<std::string, std::string>>& itemValueList);
};
#endif // HTMXSHATTRIBUTELIST_H

View File

@@ -0,0 +1,28 @@
#include "HtmxShCondition.hpp"
#include "../utils.hpp"
#include <format>
using namespace std;
HtmxShCondition::HtmxShCondition(std::string id, size_t nbrOfBoxes)
{
html += "<div class='section'>";
html += format("<h2>{}</h2>", id);
html += "<div class='monitor-track'>";
int con_value = -1;
for (size_t i = 0; i < nbrOfBoxes; i++)
{
string item_id = utils::to_id_format(format("{}_{}",id, i));
html += format("<label class='monitor-box'><input type='checkbox' name='{}'></label>", item_id);
if ( ((i + 1) % 3 == 0) && (i != 0) )
{
html += format("<div class='monitor-number'>{}</div>", con_value);
con_value--;
}
}
html += "</div></div>";
}

View File

@@ -0,0 +1,13 @@
#ifndef __HTMXSHCONDITION_H__
#define __HTMXSHCONDITION_H__
#include <string>
#include "HtmxObject.h"
class HtmxShCondition : public HtmxObject {
public:
HtmxShCondition(std::string id, size_t nbrOfBoxes);
};
#endif // __HTMXSHCONDITION_H__

View File

@@ -0,0 +1,43 @@
#include <format>
#include "HtmxShItemList.hpp"
#include "../utils.hpp"
using namespace std;
HtmxShItemList::HtmxShItemList(const std::string& id, const std::vector<std::string>& columns, size_t size){
html += "<div class='section'>";
html += format("<h2>{}</h2>", id);
html += format("<div class='grid grid-{}'>", columns.size());
for (size_t i = 0; i < size; i++){
for (auto& col : columns){
string item_id = utils::to_id_format(format("{}_{}_{}", id, i, col));
html += format("<input type='text' name='{}' placeholder='{}'>", item_id, col);
}
}
html += "</div></div>";
}
/*
HtmxShItemList::HtmxShItemList(const std::string& id, const std::vector<std::pair<std::string, std::string>>& itemValueList){
html += format("<h2>{}</h2>", id);
html += "<div class='grid'>";
for (auto& item : itemValueList){
string item_id = utils::to_id_format(id + "_" + item.first);
html += format("<label>{}:<input type='text' name='{}' value='{}'></label>", item, item_id, item.second);
}
html += "</div>";
html += "<div class='section'>";
html += format("<h2>{}</h2>", id);
html += format("<div class='grid grid-{}'>", columns.size());
for (size_t i = 0; i < size; i++){
for (auto& col : columns){
string item_id = utils::to_id_format(format("{}_{}_{}", id, i, col));
html += format("<input type='text' name='{}' placeholder='{}' value='{}'>", item_id, col);
}
}
html += "</div></div>";
}
*/

View File

@@ -0,0 +1,18 @@
#ifndef HTMXSHITEMLIST_H
#define HTMXSHITEMLIST_H
#include "HtmxObject.h"
#include <vector>
#include <string>
class HtmxShItemList : public HtmxObject {
public:
// create new item list,
HtmxShItemList(const std::string& id, const std::vector<std::string>& columns, size_t size);
// create new item list where each item as a value
HtmxShItemList(const std::string& id, const std::vector<std::pair<std::string, std::string>>& itemValueList);
};
#endif // HTMXSHITEMLIST_H

View File

@@ -0,0 +1,106 @@
#include "ShadowrunApi.hpp"
#include "ShadowrunCharacterForm.hpp"
#include "database.hpp"
namespace shadowrun
{
bool initDb() {
auto db = Database();
if (!db.open()){
return false;
}
// Create a tables
const char* create_sql_chars = "CREATE TABLE IF NOT EXISTS shadowrun_characters ("
"id INTEGER PRIMARY KEY,"
"name TEXT,"
"created_at DATETIME DEFAULT CURRENT_TIMESTAMP);";
if (!db.exec(create_sql_chars)){
CROW_LOG_ERROR << "Failed to create shadowrun_characters table";
return false;
}
const char* create_sql_data = "CREATE TABLE IF NOT EXISTS shadowrun_data ("
"id INTEGER PRIMARY KEY,"
"character_id INTEGER NOT NULL,"
"name TEXT NOT NULL,"
"value TEXT,"
"created_at DATETIME DEFAULT CURRENT_TIMESTAMP,"
"updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,"
"FOREIGN KEY (character_id) REFERENCES characters(id) ON DELETE CASCADE);";
if (!db.exec(create_sql_data)){
CROW_LOG_ERROR << "Failed to create shadowrun_data table";
return false;
}
return true;
}
void initApi(crow::SimpleApp& app)
{
CROW_ROUTE(app, "/api/shadowrun/submit-character").methods("POST"_method)(
[](const crow::request& req) {
auto params = crow::query_string(req.body);
std::string name = params.get("name") ? params.get("name") : "";
std::string metatype = params.get("metatype") ? params.get("metatype") : "";
std::string age = params.get("age") ? params.get("age") : "";
// ... extract more fields as needed
// Optionally save to a DB or do logic here
// Return response HTML
std::ostringstream out;
out << "<div class='alert alert-success'>"
<< "Character " << name << " submitted successfully!"
<< "</div>";
return crow::response{out.str()};
});
CROW_ROUTE(app, "/api/shadowrun/character-form")
([](const crow::request& req) {
auto query = crow::query_string(req.url_params);
std::string name = query.get("name") ? query.get("name") : "";
// TODO: Load data from file or DB using `name`
std::string metatype = "Troll";
int age = 28;
return crow::response{ShadowrunCharacterForm().htmx()};
});
CROW_ROUTE(app, "/api/shadowrun/character-list")
([] {
std::ostringstream html;
// Simulated character database
std::vector<std::string> characters = { "Trogdor", "Alice", "Zigzag" };
html << "<form hx-get='/api/shadowrun/character-form' hx-target='#form-container' hx-params='*'>"
<< "<label>Character Name: "
<< "<select name='name'>";
for (const auto& name : characters) {
html << "<option value='" << name << "'>" << name << "</option>";
}
html << "</select></label>"
<< "<button type='submit'>Load Character</button>"
<< "</form>";
return crow::response{html.str()};
});
if(initDb()){
CROW_LOG_ERROR << "Failed to Init shadowrun database";
}
}
}

View File

@@ -0,0 +1,11 @@
#ifndef __SHADOWRUNAPI_H__
#define __SHADOWRUNAPI_H__
#include <crow.h>
namespace shadowrun {
void initApi(crow::SimpleApp& app);
}
#endif // __SHADOWRUNAPI_H__

View File

@@ -0,0 +1,122 @@
#include <string>
#include <vector>
#include "HtmxShItemList.hpp"
#include "HtmxShAttributeList.hpp"
#include "HtmxShCondition.hpp"
#include "ShadowrunCharacterForm.hpp"
using namespace std;
static const vector<string> cCharacterInfo = {
"Name",
"Metatype",
"Age",
"Sex",
"Nuyen",
"Lifestyle",
"Total Karma",
"Current Karma",
"Street Cred",
"Notoriety",
"Public Awareness"
};
static const vector<string> cAttributes = {
"Body",
"Agility",
"Reaction",
"Strength",
"Charisma",
"Intuition",
"Logic",
"Willpower",
"Edge",
"Essence",
"Initiative"
};
static const vector<string> cSkillParameters = {
"Name",
"RTG.",
"ATT.",
};
static const vector<string> cContactsParameters = {
"Name",
"Loyalty",
};
static const vector<string> cRangedWeaponsParameters = {
"Weapon",
"Damage",
"AP",
"Mode",
"RC",
"Ammo"
};
static const vector<string> cImplantParameters = {
"Implant",
"Rating",
"Essence",
"Notes",
};
static const vector<string> cMeleeWeaponParameters = {
"Weapon",
"Reach",
"Damage",
"AP",
};
static const vector<string> cArmorParamters = {
"Armor",
"Ballistic",
"Impact",
"Notes",
};
ShadowrunCharacterForm::ShadowrunCharacterForm() {
html.reserve(30000);
html += "<form hx-post='/api/shadowrun/submit-character' hx-target='#form-response' hx-swap='innerHTML'>";
html += HtmxShAttributeList("Character Info", cCharacterInfo).htmx();
html += HtmxShAttributeList("Attributes", cAttributes).htmx();
html += "<div style='display: grid; grid-template-columns: 1fr 1fr; gap: 2em;'>";
html += HtmxShItemList("Active Skills", cSkillParameters, 6).htmx();
html += HtmxShItemList("Knowledge Skills", cSkillParameters, 6).htmx();
// add Qualities
html += "<div class='section'>"
"<h2>Qualities</h2>"
"<label>Positive Qualities:"
"<textarea name='positive_qualities' rows='4'></textarea>"
"</label>"
"<label>Negative Qualities:"
"<textarea name='negative_qualities' rows='4'></textarea>"
"</label>"
"</div>";
// add datapack notes
html += "<div class='section'>"
"<h2>Datajack / Commlink / Cyberdeck / Notes</h2>"
"<label>Notes:"
"<textarea name='datapack_notes' rows='6'></textarea>"
"</label>"
"</div>";
html += HtmxShCondition("Physical Condition", 18).htmx();
html += HtmxShCondition("Stun Condition", 12).htmx();
html += HtmxShItemList("Contacts", cContactsParameters, 6).htmx();
html += HtmxShItemList("Ranged Weapons", cRangedWeaponsParameters, 7).htmx();
html += HtmxShItemList("Cyberware and Bioware", cImplantParameters, 7).htmx();
html += HtmxShItemList("Melee Weapons", cMeleeWeaponParameters, 7).htmx();
html += HtmxShItemList("Armor", cArmorParamters , 3).htmx();
html += "</div>";
html += "<div style='text-align:center'>"
"<button type='submit'>Submit</button>"
"</div>"
"</form>";
}

View File

@@ -0,0 +1,14 @@
#ifndef SHADOWRUN_CHARACTER_FORM_HPP
#define SHADOWRUN_CHARACTER_FORM_HPP
#include "htmx/HtmxObject.h"
class ShadowrunCharacterForm : public HtmxObject {
public:
ShadowrunCharacterForm();
private:
};
#endif // SHADOWRUN_CHARACTER_FORM_HPP