Skip to content

Commit 60040ef

Browse files
wrapper functions for accessing fields as boolean or number
1 parent 129a1ec commit 60040ef

File tree

16 files changed

+117
-124
lines changed

16 files changed

+117
-124
lines changed

include/ssod/database.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <map>
2323
#include <string>
2424
#include <variant>
25+
#include <ssod/ssod.h>
2526
#include <dpp/dpp.h>
2627

2728
/**
@@ -45,6 +46,14 @@ namespace db {
4546
}
4647
}
4748

49+
[[nodiscard]] inline long number(const std::string& index) const {
50+
return atol(at(index));
51+
}
52+
53+
[[nodiscard]] inline bool boolean(const std::string& index) const {
54+
return number(index);
55+
}
56+
4857
inline void emplace(const std::string& k, const std::string& v) {
4958
fields.emplace(k, v);
5059
}

include/ssod/paragraph.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -294,23 +294,6 @@ struct paragraph {
294294
dpp::task<void> parse(player& current_player, dpp::snowflake user_id);
295295
};
296296

297-
/**
298-
* Extract the next string value from a key/value list, e.g.
299-
* if the string contains 'NAME="Big Monster"' this will return
300-
* Big Monster.
301-
* @param p_text text to extract from
302-
* @return extracted string
303-
*/
304-
std::string extract_value(const std::string& p_text);
305-
306-
/**
307-
* Extract the next numeric value from a key/value list, e.g.
308-
* if the string contains 'A="1"' this will return 1.
309-
* @param p_text text to extract from
310-
* @return extracted number
311-
*/
312-
long extract_value_number(const std::string& p_text);
313-
314297
/**
315298
* Returns true if a global flag is set
316299
* @param flag flag name

include/ssod/parser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,5 @@ struct parse_end_exception : public std::exception {
8888
};
8989

9090
dpp::task<bool> route_tag(paragraph& p, std::string& p_text, std::stringstream& paragraph_content, std::stringstream& output, player& current_player, bool display);
91+
92+
std::unordered_map<std::string, std::string> parse_attributes(std::stringstream& ss);

src/campfire.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ dpp::task<void> campfire(const dpp::interaction_create_t& event, player p) {
8383
}
8484
}
8585
content << sprite::rawmeat.get_mention() << " ";
86-
if (stack.at("qty") != "1") {
87-
content << stack.at("qty") << "x ";
86+
if (stack.number("qty") != 1) {
87+
content << stack.number("qty") << "x ";
8888
}
8989
content << item << "\n";
9090
}

src/combat.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,8 @@ dpp::task<void> continue_combat(const dpp::interaction_create_t& event, player p
659659
long banked{0};
660660
bool critical{};
661661
if (!r.empty()) {
662-
long counter = atol(r[0].at("critical_counter"));
663-
banked = atol(r[0].at("banked_criticals"));
662+
long counter = r[0].number("critical_counter");
663+
banked = r[0].number("banked_criticals");
664664
if (banked > 0 && p.next_crit) {
665665
co_await db::co_query("UPDATE criticals SET banked_criticals = banked_criticals - 1 WHERE user_id = ?", {event.command.usr.id});
666666
critical = true;
@@ -764,10 +764,10 @@ dpp::task<void> continue_combat(const dpp::interaction_create_t& event, player p
764764
long increment = std::min(std::max(1L, p.luck + 1), 12L);
765765
co_await db::co_query("INSERT INTO criticals (user_id, critical_counter, banked_criticals) VALUES(?,1,0) ON DUPLICATE KEY UPDATE critical_counter = critical_counter + ?", {event.command.usr.id, increment});
766766
auto r = co_await db::co_query("SELECT * FROM criticals WHERE user_id = ?", {event.command.usr.id});
767-
long counter = atol(r[0].at("critical_counter"));
767+
long counter = r[0].number("critical_counter");
768768
if (counter > 1000 + (p.get_level() * 4)) {
769769
/* User gains a new banked critical */
770-
long new_banked = atol(r[0].at("banked_criticals")) + 1;
770+
long new_banked = r[0].number("banked_criticals") + 1;
771771
if (new_banked <= p.max_crits()) {
772772
co_await db::co_query("UPDATE criticals SET critical_counter = 0, banked_criticals = ? WHERE user_id = ?", {new_banked, event.command.usr.id});
773773
} else {

src/commands/profile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ dpp::task<void> profile_command::route(const dpp::slashcommand_t &event)
5353
event.reply(dpp::message(tr(self ? "NOPROFILE" : "NOSUCHUSER", event)).set_flags(dpp::m_ephemeral));
5454
co_return;
5555
}
56-
p.experience = atol(rs[0].at("experience"));
56+
p.experience = rs[0].number("experience");
5757
auto g = co_await db::co_query("SELECT * FROM guild_members JOIN guilds ON guild_id = guilds.id WHERE user_id = ?", {rs[0].at("user_id")});
5858
auto status = co_await db::co_query("SELECT passive_effect_status.*, on_end, on_after, type, requirements, duration, withdrawl FROM passive_effect_status join passive_effect_types on passive_effect_id = passive_effect_types.id where user_id = ?", {rs[0].at("user_id")});
5959
std::stringstream effects;
@@ -82,7 +82,7 @@ dpp::task<void> profile_command::route(const dpp::slashcommand_t &event)
8282
content += "\n\n**" + tr("GUILD", event) + ":** " + dpp::utility::markdown_escape(g[0].at("name"));
8383
}
8484

85-
player p2(atol(rs[0].at("user_id")), false);
85+
player p2(rs[0].number("user_id"), false);
8686

8787
std::string file = matrix_image((player_race)atoi(rs[0].at("race")), (player_profession)atoi(rs[0].at("profession")), rs[0].at("gender") == "male");
8888

src/game.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@ dpp::task<void> continue_game(const dpp::interaction_create_t& event, player p,
14911491
std::string list_others, list_dropped, text;
14921492
for (const auto & other : others) {
14931493
list_others += dpp::utility::markdown_escape(other.at("name"), true);
1494-
if (other.at("is_npc") == "1") {
1494+
if (other.boolean("is_npc")) {
14951495
list_others += " *" + tr("NPC", event) + "*";
14961496
has_npcs_here = true;
14971497
} else {

src/game_util.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ dpp::task<sale_info> get_sale_info(const std::string& name) {
6161
}
6262
}
6363
} else {
64-
value = atol(res[0].at("value"));
65-
sellable = res[0].at("sellable") == "1";
64+
value = res[0].number("value");
65+
sellable = res[0].boolean("sellable");
6666
flags = res[0].at("flags");
67-
qi = res[0].at("quest_item") == "1";
67+
qi = res[0].boolean("quest_item");
6868
}
6969
co_return sale_info{
7070
.flags = flags,
@@ -222,5 +222,5 @@ spell_info get_spell_info(const std::string& name) {
222222
if (rs.empty()) {
223223
return {};
224224
}
225-
return { .name = rs[0].at("name"), .component_herb = rs[0].at("herb"), .combat_rating = atol(rs[0].at("combat_rating")), .mana_cost = atol(rs[0].at("mana_cost")) };
225+
return { .name = rs[0].at("name"), .component_herb = rs[0].at("herb"), .combat_rating = rs[0].number("combat_rating"), .mana_cost = rs[0].number("mana_cost") };
226226
}

src/paragraph.cpp

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -112,30 +112,6 @@ dpp::task<bool> paragraph::valid_next(long current, long next) {
112112
co_return paralist.find(next) != paralist.end();
113113
}
114114

115-
// extracts a value from any NAME="Value" pair
116-
std::string extract_value(const std::string& p_text) {
117-
if (p_text.find("\"") == std::string::npos) {
118-
return extract_without_quotes(p_text);
119-
}
120-
std::string item_name;
121-
bool copying{false};
122-
for (const char c : p_text) {
123-
if (c == '"') {
124-
copying = !copying;
125-
continue;
126-
}
127-
if (copying) {
128-
item_name += c;
129-
}
130-
}
131-
return item_name;
132-
}
133-
134-
long extract_value_number(const std::string& p_text)
135-
{
136-
return atol(extract_value(p_text));
137-
}
138-
139115
dpp::task<bool> global_set(const std::string& flag) {
140116
co_return !(co_await db::co_query("SELECT flag FROM game_global_flags WHERE flag = ?", {flag})).empty();
141117
}
@@ -177,10 +153,10 @@ dpp::task<void> paragraph::parse(player& current_player, dpp::snowflake user_id)
177153
text = location[0].at("data");
178154
}
179155
secure_id = location[0].at("secure_id");
180-
combat_disabled = location[0].at("combat_disabled") == "1";
181-
magic_disabled = location[0].at("magic_disabled") == "1";
182-
theft_disabled = location[0].at("theft_disabled") == "1";
183-
chat_disabled = location[0].at("chat_disabled") == "1";
156+
combat_disabled = location[0].boolean("combat_disabled");
157+
magic_disabled = location[0].boolean("magic_disabled");
158+
theft_disabled = location[0].boolean("theft_disabled");
159+
chat_disabled = location[0].boolean("chat_disabled");
184160
auto dropped = co_await db::co_query("SELECT item_desc, item_flags, count(item_desc) as stack_count FROM game_dropped_items WHERE location_id = ? GROUP BY item_desc, item_flags ORDER BY item_desc, item_flags LIMIT 50", {paragraph_id});
185161
for (const auto &dropped_item: dropped) {
186162
dropped_items.push_back(stacked_item{.name = dropped_item.at("item_desc"), .flags = dropped_item.at("item_flags"), .qty = atol(dropped_item.at("stack_count"))});

src/parser.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,52 @@ dpp::task<bool> route_tag(paragraph& p, std::string& p_text, std::stringstream&
4949
}
5050
co_return false;
5151
}
52+
53+
std::unordered_map<std::string, std::string> parse_attributes(std::stringstream& ss) {
54+
std::unordered_map<std::string, std::string> attributes;
55+
std::string token;
56+
57+
while (ss >> token) {
58+
if (token == ">") {
59+
break;
60+
}
61+
62+
bool ends_with_gt = false;
63+
if (!token.empty() && token.back() == '>') {
64+
ends_with_gt = true;
65+
token.pop_back(); // remove '>'
66+
}
67+
68+
auto eq_pos = token.find('=');
69+
if (eq_pos == std::string::npos) {
70+
attributes[dpp::lowercase(token)] = "";
71+
} else {
72+
std::string key = token.substr(0, eq_pos);
73+
std::string value = token.substr(eq_pos + 1);
74+
75+
if (!value.empty() && value[0] == '"') {
76+
std::string quoted = value;
77+
if (quoted.back() != '"') {
78+
std::string fragment;
79+
while (ss >> fragment) {
80+
quoted += ' ' + fragment;
81+
if (!fragment.empty() && fragment.back() == '"') {
82+
break;
83+
}
84+
}
85+
}
86+
if (quoted.size() >= 2 && quoted.front() == '"' && quoted.back() == '"') {
87+
value = quoted.substr(1, quoted.size() - 2);
88+
}
89+
}
90+
91+
attributes[dpp::lowercase(key)] = value;
92+
}
93+
94+
if (ends_with_gt) {
95+
break;
96+
}
97+
}
98+
99+
return attributes;
100+
}

0 commit comments

Comments
 (0)