added redirect and ability to save checkboxes
This commit is contained in:
@@ -4,13 +4,13 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
HtmxShAttributeList::HtmxShAttributeList(const std::string& id, const vector<string>& itemList){
|
||||
HtmxShAttributeList::HtmxShAttributeList(const std::string& id, const vector<string>& itemList, std::map<std::string, std::string>& data){
|
||||
html += format("<h2>{}</h2>", id);
|
||||
html += "<div class='grid'>";
|
||||
for (auto& item : itemList){
|
||||
string item_id = utils::to_id_format(format("{}_{}", id, item));
|
||||
|
||||
html += format("<label>{}:<input type='text' name='{}'></label>", item, item_id);
|
||||
auto value = data.contains(item_id) ? data[item_id] : "";
|
||||
html += format("<label>{}:<input type='text' name='{}' value='{}'></label>", item, item_id, value);
|
||||
}
|
||||
html += "</div>";
|
||||
}
|
||||
|
||||
@@ -4,12 +4,13 @@
|
||||
#include "HtmxObject.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
class HtmxShAttributeList : public HtmxObject {
|
||||
|
||||
public:
|
||||
// create new item list
|
||||
HtmxShAttributeList(const std::string& id, const std::vector<std::string>& itemList);
|
||||
HtmxShAttributeList(const std::string& id, const std::vector<std::string>& itemList, std::map<std::string, std::string>& data);
|
||||
|
||||
// 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);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
HtmxShCondition::HtmxShCondition(std::string id, size_t nbrOfBoxes)
|
||||
HtmxShCondition::HtmxShCondition(std::string id, size_t nbrOfBoxes, std::map<std::string, std::string>& data)
|
||||
{
|
||||
html += "<div class='section'>";
|
||||
html += format("<h2>{}</h2>", id);
|
||||
@@ -14,8 +14,10 @@ HtmxShCondition::HtmxShCondition(std::string id, size_t nbrOfBoxes)
|
||||
|
||||
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);
|
||||
string item_id = utils::to_id_format(format("Checkbox_{}_{}",id, i));
|
||||
auto value = data.contains(item_id) && data[item_id] == "1" ? "checked" : "";
|
||||
|
||||
html += format("<label class='monitor-box'><input type='checkbox' name='{}' value='1' {}></label>", item_id, value);
|
||||
|
||||
if ( ((i + 1) % 3 == 0) && (i != 0) )
|
||||
{
|
||||
@@ -30,6 +32,6 @@ HtmxShCondition::HtmxShCondition(std::string id, size_t nbrOfBoxes)
|
||||
void HtmxShCondition::genIds(std::vector<std::string>& vec, std::string id, size_t nbrOfBoxes)
|
||||
{
|
||||
for (int i = 0; i < nbrOfBoxes; i++){
|
||||
vec.push_back(utils::to_id_format(format("{}_{}",id, i)));
|
||||
vec.push_back(utils::to_id_format(format("Checkbox_{}_{}",id, i)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,13 @@
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "HtmxObject.h"
|
||||
|
||||
class HtmxShCondition : public HtmxObject {
|
||||
|
||||
public:
|
||||
HtmxShCondition(std::string id, size_t nbrOfBoxes);
|
||||
HtmxShCondition(std::string id, size_t nbrOfBoxes, std::map<std::string, std::string>& data);
|
||||
|
||||
static void genIds(std::vector<std::string>& vec, std::string id, size_t nbrOfBoxes);
|
||||
};
|
||||
|
||||
@@ -4,14 +4,15 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
HtmxShItemList::HtmxShItemList(const std::string& id, const std::vector<std::string>& columns, size_t size){
|
||||
HtmxShItemList::HtmxShItemList(const std::string& id, const std::vector<std::string>& columns, size_t size, std::map<std::string, std::string>& data){
|
||||
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);
|
||||
auto value = data.contains(item_id) ? data[item_id] : "";
|
||||
html += format("<input type='text' name='{}' placeholder='{}' value='{}'>", item_id, col, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,12 +4,13 @@
|
||||
#include "HtmxObject.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
class HtmxShItemList : public HtmxObject {
|
||||
|
||||
public:
|
||||
// create new item list,
|
||||
HtmxShItemList(const std::string& id, const std::vector<std::string>& columns, size_t size);
|
||||
HtmxShItemList(const std::string& id, const std::vector<std::string>& columns, size_t size, std::map<std::string, std::string>& data);
|
||||
|
||||
// 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);
|
||||
|
||||
@@ -51,17 +51,25 @@ void initApi(crow::SimpleApp& app)
|
||||
return rsp("Failed to create id of character");
|
||||
}
|
||||
|
||||
vector<pair<const string&, const string&>> idValues;
|
||||
vector<pair<const string, const string>> idValues;
|
||||
idValues.reserve(ShadowrunCharacterForm::m_formIds.size());
|
||||
|
||||
auto checkboxes = std::set<string>(ShadowrunCharacterForm::m_checkboxIds.begin(), ShadowrunCharacterForm::m_checkboxIds.end());
|
||||
for (auto& id : ShadowrunCharacterForm::m_formIds) {
|
||||
auto data = params[id];
|
||||
if(!
|
||||
data.empty()){
|
||||
if(!data.empty()){
|
||||
idValues.push_back(make_pair(id, data));
|
||||
if (checkboxes.contains(id)){
|
||||
checkboxes.erase(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// append the checkboxes
|
||||
for (auto& checkbox : checkboxes){
|
||||
idValues.push_back(make_pair(checkbox, "0"));
|
||||
}
|
||||
|
||||
if (!storeCharacterData(key, idValues)){
|
||||
CROW_LOG_ERROR << "Failed to store character data of " << name_data;
|
||||
return rsp("Failed to store character data");
|
||||
@@ -76,7 +84,7 @@ void initApi(crow::SimpleApp& app)
|
||||
|
||||
auto data = getCharacterData(getKeyOfCharacter(name));
|
||||
|
||||
return crow::response{ShadowrunCharacterForm().htmx()};
|
||||
return crow::response{ShadowrunCharacterForm(data).htmx()};
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/api/shadowrun/character-list")
|
||||
@@ -85,7 +93,6 @@ void initApi(crow::SimpleApp& app)
|
||||
|
||||
// Simulated character database
|
||||
auto characters = getCharacters();
|
||||
|
||||
html << "<form hx-get='/api/shadowrun/character-form' hx-target='#form-container' hx-params='*'>"
|
||||
<< "<label>Character Name: "
|
||||
<< "<select name='name'>";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <format>
|
||||
#include "HtmxShItemList.hpp"
|
||||
#include "HtmxShAttributeList.hpp"
|
||||
#include "HtmxShCondition.hpp"
|
||||
@@ -76,6 +77,13 @@ static const vector<string> cArmorParamters = {
|
||||
"Notes",
|
||||
};
|
||||
|
||||
static const vector<string> genCheckboxIds(){
|
||||
vector<string> vec;
|
||||
HtmxShCondition::genIds(vec, "Physical Condition", 18);
|
||||
HtmxShCondition::genIds(vec, "Stun Condition", 12);
|
||||
return vec;
|
||||
}
|
||||
|
||||
static const vector<string> genFormIds(){
|
||||
vector<string> vec;
|
||||
vec.reserve(200);
|
||||
@@ -83,16 +91,20 @@ static const vector<string> genFormIds(){
|
||||
// OBS make sure to update both here and in ShadowrunCharacterForm()
|
||||
HtmxShAttributeList::genIds(vec, "Character Info", cCharacterInfo);
|
||||
HtmxShAttributeList::genIds(vec, "Attributes", cAttributes);
|
||||
HtmxShItemList::genIds(vec, "Active Skills", cSkillParameters, 6);
|
||||
HtmxShItemList::genIds(vec, "Knowledge Skills", cSkillParameters, 6);
|
||||
HtmxShItemList::genIds(vec, "Active Skills", cSkillParameters, 8);
|
||||
HtmxShItemList::genIds(vec, "Knowledge Skills", cSkillParameters, 8);
|
||||
vec.push_back("positive_qualities");
|
||||
vec.push_back("negative_qualities");
|
||||
vec.push_back("datapack_notes");
|
||||
|
||||
auto v = genCheckboxIds();
|
||||
vec.insert(vec.end(), v.begin(), v.end());
|
||||
|
||||
HtmxShCondition::genIds(vec, "Physical Condition", 18);
|
||||
HtmxShCondition::genIds(vec, "Stun Condition", 12);
|
||||
HtmxShItemList::genIds(vec, "Contacts", cContactsParameters, 6);
|
||||
HtmxShItemList::genIds(vec, "Ranged Weapons", cRangedWeaponsParameters, 7);
|
||||
HtmxShItemList::genIds(vec, "Cyberware and Bioware", cImplantParameters, 7);
|
||||
HtmxShItemList::genIds(vec, "Cyberware and Bioware", cImplantParameters, 9);
|
||||
HtmxShItemList::genIds(vec, "Melee Weapons", cMeleeWeaponParameters, 7);
|
||||
HtmxShItemList::genIds(vec, "Armor", cArmorParamters , 3);
|
||||
|
||||
@@ -100,44 +112,49 @@ static const vector<string> genFormIds(){
|
||||
}
|
||||
|
||||
const std::vector<std::string> ShadowrunCharacterForm::m_formIds = genFormIds();
|
||||
const std::vector<std::string> ShadowrunCharacterForm::m_checkboxIds = genCheckboxIds();
|
||||
|
||||
ShadowrunCharacterForm::ShadowrunCharacterForm() {
|
||||
ShadowrunCharacterForm::ShadowrunCharacterForm(std::map<std::string, std::string>& data) {
|
||||
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 += HtmxShAttributeList("Character Info", cCharacterInfo, data).htmx();
|
||||
html += HtmxShAttributeList("Attributes", cAttributes, data).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();
|
||||
html += HtmxShItemList("Active Skills", cSkillParameters, 8, data).htmx();
|
||||
html += HtmxShItemList("Knowledge Skills", cSkillParameters, 8, data).htmx();
|
||||
|
||||
auto valuePos = data.contains("positive_qualities") ? data["positive_qualities"] : "";
|
||||
auto valueNeg = data.contains("negative_qualities") ? data["negative_qualities"] : "";
|
||||
|
||||
// add Qualities
|
||||
html += "<div class='section'>"
|
||||
html += format("<div class='section'>"
|
||||
"<h2>Qualities</h2>"
|
||||
"<label>Positive Qualities:"
|
||||
"<textarea name='positive_qualities' rows='4'></textarea>"
|
||||
"<textarea name='positive_qualities' rows='4'>{}</textarea>"
|
||||
"</label>"
|
||||
"<label>Negative Qualities:"
|
||||
"<textarea name='negative_qualities' rows='4'></textarea>"
|
||||
"<textarea name='negative_qualities' rows='4'>{}</textarea>"
|
||||
"</label>"
|
||||
"</div>";
|
||||
"</div>", valuePos, valueNeg);
|
||||
|
||||
auto valueNotes = data.contains("datapack_notes") ? data["datapack_notes"] : "";
|
||||
// add datapack notes
|
||||
html += "<div class='section'>"
|
||||
html += format("<div class='section'>"
|
||||
"<h2>Datajack / Commlink / Cyberdeck / Notes</h2>"
|
||||
"<label>Notes:"
|
||||
"<textarea name='datapack_notes' rows='6'></textarea>"
|
||||
"<textarea name='datapack_notes' rows='6'>{}</textarea>"
|
||||
"</label>"
|
||||
"</div>";
|
||||
"</div>", valueNotes);
|
||||
|
||||
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 += HtmxShCondition("Physical Condition", 18, data).htmx();
|
||||
html += HtmxShCondition("Stun Condition", 12, data).htmx();
|
||||
html += HtmxShItemList("Contacts", cContactsParameters, 6, data).htmx();
|
||||
html += HtmxShItemList("Ranged Weapons", cRangedWeaponsParameters, 7, data).htmx();
|
||||
html += HtmxShItemList("Cyberware and Bioware", cImplantParameters, 9, data).htmx();
|
||||
html += HtmxShItemList("Melee Weapons", cMeleeWeaponParameters, 7, data).htmx();
|
||||
html += HtmxShItemList("Armor", cArmorParamters , 3, data).htmx();
|
||||
html += "</div>";
|
||||
|
||||
html += "<div style='text-align:center'>"
|
||||
|
||||
@@ -2,13 +2,16 @@
|
||||
#define SHADOWRUN_CHARACTER_FORM_HPP
|
||||
|
||||
#include "htmx/HtmxObject.h"
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
class ShadowrunCharacterForm : public HtmxObject {
|
||||
|
||||
public:
|
||||
ShadowrunCharacterForm();
|
||||
ShadowrunCharacterForm(std::map<std::string, std::string>& data);
|
||||
|
||||
static const std::vector<std::string> m_formIds;
|
||||
static const std::vector<std::string> m_checkboxIds;
|
||||
};
|
||||
|
||||
#endif // SHADOWRUN_CHARACTER_FORM_HPP
|
||||
@@ -31,7 +31,7 @@ bool initDb() {
|
||||
"value TEXT,"
|
||||
"created_at DATETIME DEFAULT CURRENT_TIMESTAMP,"
|
||||
"updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,"
|
||||
"FOREIGN KEY (character_id) REFERENCES characters(id) ON DELETE CASCADE);";
|
||||
"FOREIGN KEY (character_id) REFERENCES shadowrun_characters(id) ON DELETE CASCADE);";
|
||||
|
||||
if (!db.exec(create_sql_data)){
|
||||
CROW_LOG_ERROR << "Failed to create shadowrun_data table";
|
||||
@@ -41,7 +41,7 @@ bool initDb() {
|
||||
}
|
||||
|
||||
int64_t getKeyOfCharacter(const string& name){
|
||||
auto sql = format("SELECT 1 FROM shadowrun_characters WHERE name = '{}' LIMIT 1;", name);
|
||||
auto sql = format("SELECT id FROM shadowrun_characters WHERE name = '{}' LIMIT 1;", name);
|
||||
auto db = Database();
|
||||
|
||||
if (!db.open())
|
||||
@@ -63,7 +63,7 @@ int64_t getKeyOfCharacter(const string& name){
|
||||
}
|
||||
}
|
||||
|
||||
bool storeCharacterData(int64_t characterKey, vector<pair<const string&, const string&>>& idValues){
|
||||
bool storeCharacterData(int64_t characterKey, vector<pair<const string, const string>>& idValues){
|
||||
auto sql = format("SELECT name FROM shadowrun_data WHERE character_id = {};", characterKey);
|
||||
auto db = Database();
|
||||
if (!db.open())
|
||||
@@ -74,13 +74,13 @@ bool storeCharacterData(int64_t characterKey, vector<pair<const string&, const s
|
||||
for (auto& idValue : idValues) {
|
||||
// update if already exist
|
||||
if(set.contains(idValue.first)){
|
||||
auto sql = format("UPDATE shadowrun_data SET value = {}, updated_at = CURRENT_TIMESTAMP WHERE name = {} AND character_id = {}", idValue.second, idValue.first, characterKey);
|
||||
auto sql = format("UPDATE shadowrun_data SET value = '{}', updated_at = CURRENT_TIMESTAMP WHERE name = '{}' AND character_id = {}", idValue.second, idValue.first, characterKey);
|
||||
if (!db.exec(sql)){
|
||||
CROW_LOG_WARNING << "Failed to update " << idValue.first << " with " << idValue.second;
|
||||
}
|
||||
} else {
|
||||
auto sql = format("INSERT INTO shadowrun_data (character_id, name, value)"
|
||||
"VALUES ({}, {}, {})", characterKey, idValue.first, idValue.second);
|
||||
auto sql = format("INSERT INTO shadowrun_data (character_id, name, value) "
|
||||
"VALUES ({}, '{}', '{}')", characterKey, idValue.first, idValue.second);
|
||||
if (!db.exec(sql)){
|
||||
CROW_LOG_WARNING << "Failed to insert " << idValue.first << " with " << idValue.second;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace shadowrun{
|
||||
|
||||
bool initDb();
|
||||
int64_t getKeyOfCharacter(const std::string& name);
|
||||
bool storeCharacterData(int64_t characterKey, std::vector<std::pair<const std::string&, const std::string&>>& idValues);
|
||||
bool storeCharacterData(int64_t characterKey, std::vector<std::pair<const std::string, const std::string>>& idValues);
|
||||
std::set<std::string> getCharacters();
|
||||
|
||||
std::map<std::string, std::string> getCharacterData(int64_t characterKey);
|
||||
|
||||
Reference in New Issue
Block a user