Skip to content

Commit

Permalink
Swapped out maps using const char pointers with std::string pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
luciensadi committed Jul 1, 2024
1 parent 946db97 commit d051a32
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3335,7 +3335,7 @@ void nanny(struct descriptor_data * d, char *arg)

DeleteChar(GET_IDNUM(d->character));

global_existing_player_cache[string_to_lowercase(GET_CHAR_NAME(d->character))] = FALSE;
global_existing_player_cache[std::string(string_to_lowercase(GET_CHAR_NAME(d->character)))] = FALSE;
snprintf(buf, sizeof(buf), "Character '%s' deleted!\r\nGoodbye.\r\n",
GET_CHAR_NAME(d->character));
SEND_TO_Q(buf, d);
Expand Down
17 changes: 9 additions & 8 deletions src/newdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2057,33 +2057,34 @@ DBIndex::vnum_t PCIndex::find_open_id()
return (tab[entry_cnt-1].id+1);
}

std::unordered_map<const char *, bool> global_existing_player_cache = {};
std::unordered_map<std::string, bool> global_existing_player_cache = {};
bool does_player_exist(const char *unknown_case_name)
{
const char *name = string_to_lowercase(unknown_case_name);
const char *composed_name = string_to_lowercase(unknown_case_name);
std::string string_name(composed_name);

// Look them up in our cache.
try {
return global_existing_player_cache.at(name);
return global_existing_player_cache.at(string_name);
} catch (std::out_of_range) {
log_vfprintf("Failed to find player '%s' in does_player_exist()'s global cache, falling back to database.", name);
log_vfprintf("Failed to find player '%s' in does_player_exist()'s global cache, falling back to database.", composed_name);
}

char buf[MAX_STRING_LENGTH];
char prepare_quotes_buf[250];
if (!name || !*name || !str_cmp(name, CHARACTER_DELETED_NAME_FOR_SQL))
if (!composed_name || !*composed_name || !str_cmp(composed_name, CHARACTER_DELETED_NAME_FOR_SQL))
return FALSE;
snprintf(buf, sizeof(buf), "SELECT idnum FROM pfiles WHERE Name='%s' LIMIT 1;", prepare_quotes(prepare_quotes_buf, name, 250));
snprintf(buf, sizeof(buf), "SELECT idnum FROM pfiles WHERE `Name`='%s' LIMIT 1;", prepare_quotes(prepare_quotes_buf, composed_name, 250));
if (mysql_wrapper(mysql, buf))
return FALSE;
MYSQL_RES *res = mysql_use_result(mysql);
MYSQL_ROW row = mysql_fetch_row(res);
if (!row && mysql_field_count(mysql)) {
mysql_free_result(res);
return (global_existing_player_cache[name] = FALSE);
return (global_existing_player_cache[string_name] = FALSE);
}
mysql_free_result(res);
return (global_existing_player_cache[name] = TRUE);
return (global_existing_player_cache[string_name] = TRUE);
}

bool does_player_exist(long id)
Expand Down
2 changes: 1 addition & 1 deletion src/newdb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void set_db_tag(idnum_t idnum, const char *tag_name);
void remove_db_tag(idnum_t idnum, const char *tag_name);

extern MYSQL *mysql;
extern std::unordered_map<const char *, bool> global_existing_player_cache;
extern std::unordered_map<std::string, bool> global_existing_player_cache;

extern int mysql_wrapper(MYSQL *mysql, const char *query);
#endif // ifndef __newdb_h__
16 changes: 8 additions & 8 deletions src/vision_overhaul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ void remove_vision_bit(struct char_data *ch, int type, int bit) {
/////////////////// Utilities and helpers for other files //////////////////////

// Local defines to make the get_vision_penalty code easier to handle.
#define TH_PENALTY(str, pen) { thermo_penalties.emplace(str, pen); tn_with_thermo += pen; }
#define LL_PENALTY(str, pen) { lowlight_penalties.emplace(str, pen); tn_with_ll += pen; }
#define NM_PENALTY(str, pen) { normal_penalties.emplace(str, pen); tn_with_none += pen; }
#define TH_PENALTY(name_str, pen) { thermo_penalties.emplace(std::string(name_str), pen); tn_with_thermo += pen; }
#define LL_PENALTY(name_str, pen) { lowlight_penalties.emplace(std::string(name_str), pen); tn_with_ll += pen; }
#define NM_PENALTY(name_str, pen) { normal_penalties.emplace(std::string(name_str), pen); tn_with_none += pen; }
int get_vision_penalty(struct char_data *ch, struct room_data *temp_room, char *rbuf, size_t rbuf_size) {
if (!ch || !temp_room || !rbuf) {
mudlog_vfprintf(ch, LOG_SYSLOG, "SYSERR: get_vision_penalty(%s, %s, %s, %ld) called with invalid parameters! Returning insane vision penalty.",
Expand All @@ -176,9 +176,9 @@ int get_vision_penalty(struct char_data *ch, struct room_data *temp_room, char *

// each vision mode has a vector that we tack things onto along with a running total
// at the end, we pick the one with the fewest penalties, and output the vector to rolls
std::map<const char *, int> thermo_penalties = {};
std::map<const char *, int> lowlight_penalties = {};
std::map<const char *, int> normal_penalties = {};
std::map<std::string, int> thermo_penalties = {};
std::map<std::string, int> lowlight_penalties = {};
std::map<std::string, int> normal_penalties = {};

switch (light_level(temp_room)) {
case LIGHT_FULLDARK:
Expand Down Expand Up @@ -320,7 +320,7 @@ int get_vision_penalty(struct char_data *ch, struct room_data *temp_room, char *
}

// We've reached the end of all the penalties. We'll hand back the lowest.
std::map<const char *, int> *penalty_vector;
std::map<std::string, int> *penalty_vector;
int penalty_chosen;

bool has_thermographic = is_rigging || has_vision(ch, VISION_THERMOGRAPHIC);
Expand Down Expand Up @@ -349,7 +349,7 @@ int get_vision_penalty(struct char_data *ch, struct room_data *temp_room, char *
}

for (auto it : *penalty_vector) {
buf_mod(rbuf, rbuf_size, it.first, it.second);
buf_mod(rbuf, rbuf_size, it.first.c_str(), it.second);
}

if (penalty_chosen > 8) {
Expand Down

0 comments on commit d051a32

Please sign in to comment.