|
| 1 | +#include "../client.h" |
| 2 | + |
| 3 | +void command_zonevariable(Client *c, const Seperator *sep) |
| 4 | +{ |
| 5 | + const uint16 arguments = sep->argnum; |
| 6 | + |
| 7 | + if (!arguments) { |
| 8 | + c->Message(Chat::White, "Usage: #zonevariable clear - Clear all zone variables"); |
| 9 | + c->Message(Chat::White, "Usage: #zonevariable delete [Variable Name] - Delete a zone variable"); |
| 10 | + c->Message(Chat::White, "Usage: #zonevariable set [Variable Name] [Variable Value] - Set a zone variable"); |
| 11 | + c->Message(Chat::White, "Usage: #zonevariable view [Variable Name] - View a zone variable"); |
| 12 | + c->Message(Chat::White, "Note: You can have spaces in variable names and values by wrapping in double quotes like this"); |
| 13 | + c->Message(Chat::White, "Example: #zonevariable set \"Test Variable\" \"Test Value\""); |
| 14 | + c->Message(Chat::White, "Note: Variable Value is optional and can be set to empty by not providing a value"); |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + const char* action = arguments >= 1 ? sep->arg[1] : ""; |
| 19 | + const bool is_clear = !strcasecmp(action, "clear"); |
| 20 | + const bool is_delete = !strcasecmp(action, "delete"); |
| 21 | + const bool is_set = !strcasecmp(action, "set"); |
| 22 | + const bool is_view = !strcasecmp(action, "view"); |
| 23 | + |
| 24 | + if (!is_clear && !is_delete && !is_set && !is_view) { |
| 25 | + c->Message(Chat::White, "Usage: #zonevariable clear - Clear all zone variables"); |
| 26 | + c->Message(Chat::White, "Usage: #zonevariable delete [Variable Name] - Delete a zone variable"); |
| 27 | + c->Message(Chat::White, "Usage: #zonevariable set [Variable Name] [Variable Value] - Set a zone variable"); |
| 28 | + c->Message(Chat::White, "Usage: #zonevariable view [Variable Name] - View a zone variable"); |
| 29 | + c->Message(Chat::White, "Note: You can have spaces in variable names and values by wrapping in double quotes like this"); |
| 30 | + c->Message(Chat::White, "Example: #zonevariable set \"Test Variable\" \"Test Value\""); |
| 31 | + c->Message(Chat::White, "Note: Variable Value is optional and can be set to empty by not providing a value"); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + if (is_clear) { |
| 36 | + const bool cleared = zone->ClearVariables(); |
| 37 | + c->Message(Chat::White, cleared ? "Cleared all zone variables." : "There are no zone variables to clear."); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + if (is_delete) { |
| 42 | + const std::string variable_name = arguments >= 2 ? sep->argplus[2] : ""; |
| 43 | + if (variable_name.empty() || !zone->VariableExists(variable_name)) { |
| 44 | + c->Message(Chat::White, fmt::format("A zone variable named '{}' does not exist.", variable_name).c_str()); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + zone->DeleteVariable(variable_name); |
| 49 | + c->Message(Chat::White, fmt::format("Deleted a zone variable named '{}'.", variable_name).c_str()); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + if (is_set) { |
| 54 | + const std::string variable_name = arguments >= 2 ? sep->arg[2] : ""; |
| 55 | + const std::string variable_value = arguments >= 3 ? sep->arg[3] : ""; |
| 56 | + zone->SetVariable(variable_name, variable_value); |
| 57 | + c->Message(Chat::White, fmt::format("Set a zone variable named '{}' to a value of '{}'.", variable_name, variable_value).c_str()); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if (is_view) { |
| 62 | + const auto& l = zone->GetVariables(); |
| 63 | + const std::string search_criteria = arguments >= 2 ? sep->argplus[2] : ""; |
| 64 | + |
| 65 | + uint32 variable_count = 0; |
| 66 | + uint32 variable_number = 1; |
| 67 | + |
| 68 | + for (const auto& e : l) { |
| 69 | + if (search_criteria.empty() || Strings::Contains(Strings::ToLower(e), Strings::ToLower(search_criteria))) { |
| 70 | + c->Message(Chat::White, fmt::format( |
| 71 | + "Variable {} | Name: {} Value: {} | {}", |
| 72 | + variable_number, |
| 73 | + e, |
| 74 | + zone->GetVariable(e), |
| 75 | + Saylink::Silent(fmt::format("#zonevariable delete {}", e), "Delete") |
| 76 | + ).c_str()); |
| 77 | + |
| 78 | + variable_count++; |
| 79 | + variable_number++; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (!variable_count) { |
| 84 | + c->Message(Chat::White, fmt::format( |
| 85 | + "There are no zone variables{}.", |
| 86 | + (!search_criteria.empty() ? fmt::format(" matching '{}'", search_criteria) : "") |
| 87 | + ).c_str()); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + c->Message(Chat::White, fmt::format( |
| 92 | + "There {} {} zone variable{}{}, would you like to {} zone variables?", |
| 93 | + variable_count != 1 ? "are" : "is", |
| 94 | + variable_count, |
| 95 | + variable_count != 1 ? "s" : "", |
| 96 | + (!search_criteria.empty() ? fmt::format(" matching '{}'", search_criteria) : ""), |
| 97 | + Saylink::Silent("#zonevariable clear", "clear") |
| 98 | + ).c_str()); |
| 99 | + } |
| 100 | +} |
0 commit comments