Skip to content

Commit

Permalink
comments, removing unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Jul 8, 2024
1 parent a930ea5 commit 7869932
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 88 deletions.
4 changes: 2 additions & 2 deletions include/ssod/game_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ struct spell_info {
long mana_cost{0};
};

std::string describe_item(const std::string& modifier_flags, const std::string& name, const dpp::interaction_create_t& event, bool ansi = false, size_t max_desc_len = 250);
dpp::task<std::string> describe_item(const std::string& modifier_flags, const std::string& name, const dpp::interaction_create_t& event, bool ansi = false, size_t max_desc_len = 250);
dpp::component help_button(const dpp::interaction_create_t& event);
void premium_required(const dpp::interaction_create_t& event);
sale_info get_sale_info(const std::string& name);
dpp::task<sale_info> get_sale_info(const std::string& name);
std::string human_readable_spell_name(const std::string& spell, const dpp::interaction_create_t& event);
std::string human_readable_herb_name(const std::string& herb, const dpp::interaction_create_t& event);
spell_info get_spell_info(const std::string& name);
Expand Down
2 changes: 1 addition & 1 deletion include/ssod/js.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace js {
* @param _bot dpp cluster instance
* @param thread_pool_size size of thread pool, defaults to same as number of cores
*/
void init(class dpp::cluster& _bot, int thread_pool_size = std::thread::hardware_concurrency());
void init(class dpp::cluster& _bot, size_t thread_pool_size = std::thread::hardware_concurrency());

/**
* @brief Execute blocking js script
Expand Down
67 changes: 64 additions & 3 deletions include/ssod/ssod.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,49 +57,110 @@ inline std::string trim(std::string s)
return ltrim(rtrim(s));
}

std::string sha256(const std::string &buffer);

/**
* String to long
* @param str
* @return
*/
inline long atol(const std::string& str) {
if (str.empty()) return 0;
return atol(str.c_str());
}

/**
* String to long long
* @param str
* @return
*/
inline long long atoll(const std::string& str) {
if (str.empty()) return 0;
return atoll(str.c_str());
}

/**
* String to int
* @param str
* @return
*/
inline int atoi(const std::string& str) {
if (str.empty()) return 0;
return atoi(str.c_str());
}

/**
* Represents the description of an item.
*/
struct item_desc {
std::string name{};
std::string description{};
};

/**
* Represents a singular item on the ground, in the bank, or in the
* player's inventory.
*/
struct item {
std::string name{};
std::string flags{};
};


/**
* Represents a stack of identical items, on the ground, in the bank
* or in the player's inventory
*/
struct stacked_item {
std::string name{};
std::string flags{};
long qty{1};
};

/**
* Internationalisation functions
*/
namespace i18n {

/**
* Fetch translation with a given key name, using the language of an interaction's user
* @param k translation key
* @param interaction user interaction
* @return translated text
*/
std::string tr(const std::string& k, const dpp::interaction_create_t& interaction);

/**
* Translate a slash command into all possible languages
* @param cmd slash command
* @return translated slash command
*/
dpp::slashcommand tr(dpp::slashcommand cmd);

/**
* Translate name and description of an item into the language of an interaction's user
* @param i item
* @param description item description
* @param event user interaction
* @return translated item
*/
item_desc tr(const item& i, const std::string &description, const dpp::interaction_create_t& event);

/**
* Translate name and description of an item stack into the language of an interaction's user
* @param i item stack
* @param description item description
* @param event user interaction
* @return translated item
*/
item_desc tr(const stacked_item& i, const std::string &description, const dpp::interaction_create_t& event);

/**
* Fetch translation with a given key name, using the language of an interaction's user. Accepts optional
* fmt::format arguments.
* @tparam T variadic type names for argument list
* @param k translation key
* @param interaction user interaction
* @param args variadic argument list
* @return translated string
*/
template<typename... T>
std::string tr(const std::string& key, const dpp::interaction_create_t& interaction, T &&... args) {
try {
Expand Down
9 changes: 8 additions & 1 deletion include/ssod/thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@
#include <mutex>
#include <functional>

/**
* A task within a thread pool. A simple lambda that accepts no parameters and returns void.
*/
using thread_pool_task = std::function<void()>;

/**
* @brief A thread pool contains 1 or more worker threads which accept thread_pool_task lambadas
* into a queue, which is processed in-order by whichever thread is free.
*/
struct thread_pool {
std::vector<std::thread> threads;
std::queue<thread_pool_task> tasks;
std::mutex queue_mutex;
std::condition_variable cv;
bool stop = false;
bool stop{false};

explicit thread_pool(size_t num_threads = std::thread::hardware_concurrency());
~thread_pool();
Expand Down
10 changes: 5 additions & 5 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ dpp::task<void> game_select(const dpp::select_click_t &event) {
} else if (custom_id == "drop_item" && !event.values.empty() && p.in_inventory && p.stamina > 0) {
std::vector<std::string> parts = dpp::utility::tokenize(event.values[0], ";");
if (parts.size() >= 1 && p.has_possession(parts[0])) {
sale_info si = get_sale_info(parts[0]);
sale_info si = co_await get_sale_info(parts[0]);
if (dpp::lowercase(parts[0]) != "scroll" && !si.quest_item) {
/* Can't drop a scroll (is quest item) */
p.drop_possession(item{.name = parts[0], .flags = ""});
Expand Down Expand Up @@ -480,7 +480,7 @@ dpp::task<void> game_select(const dpp::select_click_t &event) {
claimed = true;
} else if (custom_id == "sell" && !p.in_inventory && !p.in_bank && !event.values.empty() && !p.in_combat) {
std::vector<std::string> parts = dpp::utility::tokenize(event.values[0], ";");
sale_info s = get_sale_info(parts[0]);
sale_info s = co_await get_sale_info(parts[0]);
if (p.has_possession(parts[0]) && s.sellable && !s.quest_item && dpp::lowercase(parts[0]) != "scroll") {
if (p.armour.name == parts[0]) {
p.armour.name = tr("NO_ARMOUR", event) + " 👙";
Expand Down Expand Up @@ -1153,7 +1153,7 @@ dpp::task<void> bank(const dpp::interaction_create_t& event, player p) {
size_t index{0};
std::set<std::string> ds;
for (const auto& inv : p.possessions) {
sale_info si = get_sale_info(inv.name);
sale_info si = co_await get_sale_info(inv.name);
auto i = tr(inv, "", event);
if (si.quest_item || dpp::lowercase(inv.name) == "scroll") {
/* Can't bank a scroll! */
Expand Down Expand Up @@ -1446,7 +1446,7 @@ dpp::task<void> continue_game(const dpp::interaction_create_t& event, player p)
size_t index2{0};
std::set<std::string> ds;
for (const auto& inv : p.possessions) {
sale_info s = get_sale_info(inv.name);
sale_info s = co_await get_sale_info(inv.name);
if (!s.sellable || s.quest_item || dpp::lowercase(inv.name) == "scroll") {
continue;
}
Expand All @@ -1455,7 +1455,7 @@ dpp::task<void> continue_game(const dpp::interaction_create_t& event, player p)
if (sell_menu.options.size() < 25) {
auto i = tr(inv, "", event);
sell_menu.add_select_option(
dpp::select_option(i.name, inv.name + ";" + inv.flags, tr("VALUE", event) + " " + std::to_string(s.value) + " - " + describe_item(inv.flags, inv.name, event).substr(0, 80))
dpp::select_option(i.name, inv.name + ";" + inv.flags, tr("VALUE", event) + " " + std::to_string(s.value) + " - " + (co_await describe_item(inv.flags, inv.name, event)).substr(0, 80))
.set_emoji(e.name, e.id)
);
ds.insert(inv.name);
Expand Down
2 changes: 1 addition & 1 deletion src/game_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ dpp::task<void> player::drop_everything() {
/* Drop everything to floor */
for (const auto& i : possessions) {
/* We don't drop quest items */
sale_info value = get_sale_info(i.name);
sale_info value = co_await get_sale_info(i.name);
if (!value.quest_item && dpp::lowercase(i.name) != "scroll") {
for (long qty = 0; qty < i.qty; ++qty) {
/* BUG: g++14.1 ICE: Can't pass members of `this` to an initialiser list within a coroutine */
Expand Down
47 changes: 24 additions & 23 deletions src/game_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ dpp::component help_button(const dpp::interaction_create_t& event) {
.set_style(dpp::cos_link);
}

sale_info get_sale_info(const std::string& name) {
auto res = db::query("SELECT * FROM game_item_descs WHERE name = ?", {name});
dpp::task<sale_info> get_sale_info(const std::string& name) {
auto res = co_await db::co_query("SELECT * FROM game_item_descs WHERE name = ?", {name});
long value{0};
bool sellable{false}, qi{false};
std::string flags;
if (res.empty()) {
auto food = db::query("SELECT * FROM food WHERE name = ?", {name});
auto food = co_await db::co_query("SELECT * FROM food WHERE name = ?", {name});
if (!food.empty()) {
value = atol(food[0].at("value"));
sellable = true;
} else {
auto ingredient = db::query("SELECT * FROM ingredients WHERE ingredient_name = ?", {name});
auto ingredient = co_await db::co_query("SELECT * FROM ingredients WHERE ingredient_name = ?", {name});
if (!ingredient.empty()) {
value = 1;
sellable = true;
Expand All @@ -61,7 +61,7 @@ sale_info get_sale_info(const std::string& name) {
flags = res[0].at("flags");
qi = res[0].at("quest_item") == "1";
}
return sale_info{
co_return sale_info{
.flags = flags,
.value = value,
.sellable = sellable,
Expand All @@ -76,44 +76,45 @@ std::string ellipsis(const std::string& in, size_t max_len) {
return in;
}

std::string describe_item(const std::string& modifier_flags, const std::string& name, const dpp::interaction_create_t& event, bool ansi, size_t max_desc_len) {
auto res = db::query("SELECT idesc FROM game_item_descs WHERE name = ?", {name});
dpp::task<std::string> describe_item(const std::string& modifier_flags, const std::string& name, const dpp::interaction_create_t& event, bool ansi, size_t max_desc_len) {
auto res = co_await db::co_query("SELECT idesc FROM game_item_descs WHERE name = ?", {name});
auto i = tr(item{ .name = name, .flags = modifier_flags }, res.size() ? res[0].at("idesc") : name, event);
std::string rv{ellipsis(i.description, max_desc_len)};


if (modifier_flags.substr(0, 3) == "ST+") {
return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("STAMINA", event) + "\033[0m \033[2;34m+{}\033[0m: {}" : tr("STAMINA", event) + " **+{}**: {}"), modifier_flags.substr(3), rv);
co_return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("STAMINA", event) + "\033[0m \033[2;34m+{}\033[0m: {}" : tr("STAMINA", event) + " **+{}**: {}"), modifier_flags.substr(3), rv);
} else if (modifier_flags.substr(0, 3) == "SK+") {
return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("SKILL", event) + "\033[0m \033[2;34m+{}\033[0m: {}" : tr("SKILL", event) + " **+{}**: {}"), modifier_flags.substr(3), rv);
co_return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("SKILL", event) + "\033[0m \033[2;34m+{}\033[0m: {}" : tr("SKILL", event) + " **+{}**: {}"), modifier_flags.substr(3), rv);
} else if (modifier_flags.substr(0, 3) == "LK+") {
return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("LUCK", event) + "\033[0m \033[2;34m+{}\033[0m: {}" : tr("LUCK", event) + " **+{}**: {}"), modifier_flags.substr(3), rv);
co_return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("LUCK", event) + "\033[0m \033[2;34m+{}\033[0m: {}" : tr("LUCK", event) + " **+{}**: {}"), modifier_flags.substr(3), rv);
} else if (modifier_flags.substr(0, 3) == "MA+") {
co_return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("MANA", event) + "\033[0m \033[2;34m+{}\033[0m: {}" : tr("MANA", event) + " **+{}**: {}"), modifier_flags.substr(3), rv);
} else if (modifier_flags.substr(0, 3) == "SN+") {
return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("SNEAK", event) + "\033[0m \033[2;34m+{}\033[0m: {}" : tr("SNEAK", event) + " **+{}**: {}"), modifier_flags.substr(3), rv);
co_return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("SNEAK", event) + "\033[0m \033[2;34m+{}\033[0m: {}" : tr("SNEAK", event) + " **+{}**: {}"), modifier_flags.substr(3), rv);
} else if (modifier_flags.substr(0, 2) == "W+") {
return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("WEAPON", event) + "\033[0m \033[2;34m+{}\033[0m: {}" : tr("WEAPON", event) + " **+{}**: {}"), modifier_flags.substr(2), rv);
co_return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("WEAPON", event) + "\033[0m \033[2;34m+{}\033[0m: {}" : tr("WEAPON", event) + " **+{}**: {}"), modifier_flags.substr(2), rv);
} else if (modifier_flags.substr(0, 2) == "A+") {
return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("ARMOUR", event) + "\033[0m \033[2;34m+{}\033[0m: {}" : tr("ARMOUR", event) + " **+{}**: {}"), modifier_flags.substr(2), rv);
co_return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("ARMOUR", event) + "\033[0m \033[2;34m+{}\033[0m: {}" : tr("ARMOUR", event) + " **+{}**: {}"), modifier_flags.substr(2), rv);
} else if (!modifier_flags.empty() && modifier_flags[0] == 'W') {
return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("WEAPON", event) + "\033[0m \033[2;34m{}\033[0m: {}" : tr("WEAPON", event) + " **{}**: {}"),modifier_flags.substr(1), rv);
co_return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("WEAPON", event) + "\033[0m \033[2;34m{}\033[0m: {}" : tr("WEAPON", event) + " **{}**: {}"),modifier_flags.substr(1), rv);
} else if (!modifier_flags.empty() && modifier_flags[0] == 'A') {
return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("ARMOUR", event) + "\033[0m \033[2;34m{}\033[0m: {}" : tr("ARMOUR", event) + " **{}**: {}"),modifier_flags.substr(1), rv);
co_return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("ARMOUR", event) + "\033[0m \033[2;34m{}\033[0m: {}" : tr("ARMOUR", event) + " **{}**: {}"),modifier_flags.substr(1), rv);
} else {
auto effect = db::query("SELECT * FROM passive_effect_types WHERE type = 'Consumable' AND requirements = ?", {name});
auto effect = co_await db::co_query("SELECT * FROM passive_effect_types WHERE type = 'Consumable' AND requirements = ?", {name});
if (!effect.empty()) {
return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("CONSUMABLE", event) + "\033[0m: {}" : tr("CONSUMABLE", event) + ": {}"), rv);
co_return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("CONSUMABLE", event) + "\033[0m: {}" : tr("CONSUMABLE", event) + ": {}"), rv);
}
auto food = db::query("SELECT * FROM food WHERE name = ?", {name});
auto food = co_await db::co_query("SELECT * FROM food WHERE name = ?", {name});
if (!food.empty()) {
i = tr(item{ .name = food[0].at("name"), .flags = "" }, food[0].at("description"), event);
return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("FOOD", event) + "\033[0m: {}" : tr("FOOD", event) + ": {}"), ellipsis(i.description, max_desc_len));
co_return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("FOOD", event) + "\033[0m: {}" : tr("FOOD", event) + ": {}"), ellipsis(i.description, max_desc_len));
}
auto ingredient = db::query("SELECT * FROM ingredients WHERE ingredient_name = ?", {name});
auto ingredient = co_await db::co_query("SELECT * FROM ingredients WHERE ingredient_name = ?", {name});
if (!ingredient.empty()) {
return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("INGREDIENT", event) + "\033[0m: {}" : tr("INGREDIENT", event) + ": {}"), tr("COOK_ME", event));
co_return fmt::format(fmt::runtime(ansi ? "\033[2;36m" + tr("INGREDIENT", event) + "\033[0m: {}" : tr("INGREDIENT", event) + ": {}"), tr("COOK_ME", event));
}
}
return rv;
co_return rv;
}

void premium_required(const dpp::interaction_create_t& event) {
Expand Down
6 changes: 3 additions & 3 deletions src/inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ dpp::task<void> inventory(const dpp::interaction_create_t& event, player p) {

std::set<std::string> dup;
for (const auto& inv : possessions) {
sale_info value = get_sale_info(inv.name);
sale_info value = co_await get_sale_info(inv.name);
dpp::emoji e = co_await get_emoji(inv.name, inv.flags);
auto effect = co_await db::co_query("SELECT * FROM passive_effect_types WHERE type = 'Consumable' AND requirements = ?", {inv.name});
auto food = co_await db::co_query("SELECT * FROM food WHERE name = ?", {inv.name});
Expand Down Expand Up @@ -185,15 +185,15 @@ dpp::task<void> inventory(const dpp::interaction_create_t& event, player p) {
size_t c{content.str().length()};
for (const auto &inv: possessions) {
std::string emoji = (co_await get_emoji(inv.name, inv.flags)).format();
std::string description{"```ansi\n" + describe_item(inv.flags, inv.name, event, true, 80) + "\n"};
std::string description{"```ansi\n" + (co_await describe_item(inv.flags, inv.name, event, true, 80)) + "\n"};
if (p.armour.name == inv.name && !equip_a) {
description += "\033[2;31m🫱🏼 " + tr("EQUIPPED", event) + "\033[0m ";
equip_a = true;
} else if (p.weapon.name == inv.name && !equip_w) {
description += "\033[2;31m🫱🏼 " + tr("EQUIPPED", event) + "\033[0m ";
equip_w = true;
}
sale_info value = get_sale_info(inv.name);
sale_info value = co_await get_sale_info(inv.name);
if (value.quest_item) {
description += "\033[2;32m❗ " + tr("QUESTITEM", event) + "\033[0m ";
}
Expand Down
3 changes: 1 addition & 2 deletions src/js/js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "duktape.h"
#include <ssod/paragraph.h>
#include <ssod/database.h>
#include <ssod/achievement.h>
#include <ssod/thread_pool.h>

namespace js {
Expand Down Expand Up @@ -137,7 +136,7 @@ static duk_ret_t js_exit(duk_context *cx) {
throw exit_exception();
}

void init(dpp::cluster& _bot, int thread_pool_size) {
void init(dpp::cluster& _bot, size_t thread_pool_size) {
bot = &_bot;
pool = std::make_unique<thread_pool>(thread_pool_size);
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser/i.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct i_tag : public tag {
output << " ✅ [Owned]";
}
}
output << "** (*" << Cost << " gold*) - " << describe_item(Value, ItemName, current_player.event) << "\n";
output << "** (*" << Cost << " gold*) - " << (co_await describe_item(Value, ItemName, current_player.event)) << "\n";
p.words++;

p.links++;
Expand Down
Loading

0 comments on commit 7869932

Please sign in to comment.