Kelsidavis-WoWee/tools/editor/cli_trade_rules_catalog.cpp
Kelsi 05bb96d23b feat(editor): add WTRD (Trade Window Rules) — 115th open format
Novel replacement for the implicit player-to-player
trade policy rules vanilla WoW hardcoded across the
trade-window message handlers (CMSG_INITIATE_TRADE,
CMSG_SET_TRADE_ITEM, CMSG_SET_TRADE_GOLD), the
soulbound-item check, the cross-faction-trade
rejection, and the GM-trade audit hooks. Each entry is
one trade-policy rule the trade-window state machine
consults at every state transition.

Seven ruleKind values (Allowed / Forbidden /
SoulboundException / CrossFactionAllowed / LevelGated /
GoldEscrowMax / AuditLogged) and five targetingFilter
values (AnyPlayer / SameRealmOnly / SameFactionOnly /
SameAccountOnly / GMOnly) cover the full trade-policy
surface. Priority field resolves rule conflicts —
higher priority wins (Allowed at 100 overrides
Forbidden at 10).

Three preset emitters cover real-world deployment
patterns: makeStandard (4 baseline rules — Soulbound
Forbidden globally, Quest items Forbidden, 2hr Soul-
boundException for raid trade-back, SameFactionOnly),
makeServerAdmin (3 server-custom overrides — GM-only
escrow at priority 100, AccountBound own-character
transfer, CrossFactionAllowed at level 80 for RP
servers), makeRMTPrevent (4 anti-RMT rules — 10g cap
for low-level trades, 500g cap for accounts < 30 days,
audit log for trades > 1000g, 24hr first-trade delay).

Validator's most novel check is the GoldEscrowMax /
goldEscrowMaxCopper consistency rule: a GoldEscrowMax-
kind rule MUST specify a non-zero gold cap (zero would
mean unlimited which contradicts the rule's purpose).
Also warns on GMOnly targeting with priority < 50 (GM-
mediated rules typically need high priority to override
player-initiated rules) and levelRequirement > 80
(exceeds current cap, rule never applies).

Format count 114 -> 115. CLI flag count 1227 -> 1232.
2026-05-10 02:30:32 -07:00

282 lines
10 KiB
C++

#include "cli_trade_rules_catalog.hpp"
#include "cli_arg_parse.hpp"
#include "cli_box_emitter.hpp"
#include "pipeline/wowee_trade_rules.hpp"
#include <nlohmann/json.hpp>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <set>
#include <string>
#include <vector>
namespace wowee {
namespace editor {
namespace cli {
namespace {
std::string stripWtrdExt(std::string base) {
stripExt(base, ".wtrd");
return base;
}
const char* ruleKindName(uint8_t k) {
using T = wowee::pipeline::WoweeTradeRules;
switch (k) {
case T::Allowed: return "allowed";
case T::Forbidden: return "forbidden";
case T::SoulboundException: return "soulboundexception";
case T::CrossFactionAllowed: return "crossfactionallowed";
case T::LevelGated: return "levelgated";
case T::GoldEscrowMax: return "goldescrowmax";
case T::AuditLogged: return "auditlogged";
default: return "unknown";
}
}
const char* targetingFilterName(uint8_t t) {
using T = wowee::pipeline::WoweeTradeRules;
switch (t) {
case T::AnyPlayer: return "anyplayer";
case T::SameRealmOnly: return "samerealmonly";
case T::SameFactionOnly: return "samefactiononly";
case T::SameAccountOnly: return "sameaccountonly";
case T::GMOnly: return "gmonly";
default: return "unknown";
}
}
bool saveOrError(const wowee::pipeline::WoweeTradeRules& c,
const std::string& base, const char* cmd) {
if (!wowee::pipeline::WoweeTradeRulesLoader::save(c, base)) {
std::fprintf(stderr, "%s: failed to save %s.wtrd\n",
cmd, base.c_str());
return false;
}
return true;
}
void printGenSummary(const wowee::pipeline::WoweeTradeRules& c,
const std::string& base) {
std::printf("Wrote %s.wtrd\n", base.c_str());
std::printf(" catalog : %s\n", c.name.c_str());
std::printf(" rules : %zu\n", c.entries.size());
}
int handleGenStandard(int& i, int argc, char** argv) {
std::string base = argv[++i];
std::string name = "StandardTradeRules";
if (parseOptArg(i, argc, argv)) name = argv[++i];
base = stripWtrdExt(base);
auto c = wowee::pipeline::WoweeTradeRulesLoader::makeStandard(name);
if (!saveOrError(c, base, "gen-trd")) return 1;
printGenSummary(c, base);
return 0;
}
int handleGenServerAdmin(int& i, int argc, char** argv) {
std::string base = argv[++i];
std::string name = "ServerAdminTradeRules";
if (parseOptArg(i, argc, argv)) name = argv[++i];
base = stripWtrdExt(base);
auto c = wowee::pipeline::WoweeTradeRulesLoader::makeServerAdmin(name);
if (!saveOrError(c, base, "gen-trd-admin")) return 1;
printGenSummary(c, base);
return 0;
}
int handleGenRMT(int& i, int argc, char** argv) {
std::string base = argv[++i];
std::string name = "AntiRMTTradeRules";
if (parseOptArg(i, argc, argv)) name = argv[++i];
base = stripWtrdExt(base);
auto c = wowee::pipeline::WoweeTradeRulesLoader::makeRMTPrevent(name);
if (!saveOrError(c, base, "gen-trd-rmt")) return 1;
printGenSummary(c, base);
return 0;
}
int handleInfo(int& i, int argc, char** argv) {
std::string base = argv[++i];
bool jsonOut = consumeJsonFlag(i, argc, argv);
base = stripWtrdExt(base);
if (!wowee::pipeline::WoweeTradeRulesLoader::exists(base)) {
std::fprintf(stderr, "WTRD not found: %s.wtrd\n", base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweeTradeRulesLoader::load(base);
if (jsonOut) {
nlohmann::json j;
j["wtrd"] = base + ".wtrd";
j["name"] = c.name;
j["count"] = c.entries.size();
nlohmann::json arr = nlohmann::json::array();
for (const auto& e : c.entries) {
arr.push_back({
{"ruleId", e.ruleId},
{"name", e.name},
{"description", e.description},
{"ruleKind", e.ruleKind},
{"ruleKindName", ruleKindName(e.ruleKind)},
{"targetingFilter", e.targetingFilter},
{"targetingFilterName",
targetingFilterName(e.targetingFilter)},
{"levelRequirement", e.levelRequirement},
{"priority", e.priority},
{"itemCategoryFilter", e.itemCategoryFilter},
{"goldEscrowMaxCopper", e.goldEscrowMaxCopper},
{"iconColorRGBA", e.iconColorRGBA},
});
}
j["entries"] = arr;
std::printf("%s\n", j.dump(2).c_str());
return 0;
}
std::printf("WTRD: %s.wtrd\n", base.c_str());
std::printf(" catalog : %s\n", c.name.c_str());
std::printf(" rules : %zu\n", c.entries.size());
if (c.entries.empty()) return 0;
std::printf(" id kind targeting lvlReq prio catFilter goldMax(c) name\n");
for (const auto& e : c.entries) {
std::printf(" %4u %-19s %-15s %3u %3u 0x%08X %12llu %s\n",
e.ruleId,
ruleKindName(e.ruleKind),
targetingFilterName(e.targetingFilter),
e.levelRequirement, e.priority,
e.itemCategoryFilter,
(unsigned long long)e.goldEscrowMaxCopper,
e.name.c_str());
}
return 0;
}
int handleValidate(int& i, int argc, char** argv) {
std::string base = argv[++i];
bool jsonOut = consumeJsonFlag(i, argc, argv);
base = stripWtrdExt(base);
if (!wowee::pipeline::WoweeTradeRulesLoader::exists(base)) {
std::fprintf(stderr,
"validate-wtrd: WTRD not found: %s.wtrd\n",
base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweeTradeRulesLoader::load(base);
std::vector<std::string> errors;
std::vector<std::string> warnings;
if (c.entries.empty()) {
warnings.push_back("catalog has zero entries");
}
std::set<uint32_t> idsSeen;
for (size_t k = 0; k < c.entries.size(); ++k) {
const auto& e = c.entries[k];
std::string ctx = "entry " + std::to_string(k) +
" (id=" + std::to_string(e.ruleId);
if (!e.name.empty()) ctx += " " + e.name;
ctx += ")";
if (e.ruleId == 0)
errors.push_back(ctx + ": ruleId is 0");
if (e.name.empty())
errors.push_back(ctx + ": name is empty");
if (e.ruleKind > 6) {
errors.push_back(ctx + ": ruleKind " +
std::to_string(e.ruleKind) +
" out of range (must be 0..6)");
}
if (e.targetingFilter > 4) {
errors.push_back(ctx + ": targetingFilter " +
std::to_string(e.targetingFilter) +
" out of range (must be 0..4)");
}
if (e.levelRequirement > 80) {
warnings.push_back(ctx + ": levelRequirement " +
std::to_string(e.levelRequirement) +
" > 80 — exceeds current cap, the rule "
"would never apply on a WotLK realm");
}
// Per-kind validity: GoldEscrowMax must specify
// a non-zero gold cap to be meaningful (zero
// would mean unlimited which contradicts the
// rule's purpose).
using T = wowee::pipeline::WoweeTradeRules;
if (e.ruleKind == T::GoldEscrowMax &&
e.goldEscrowMaxCopper == 0) {
errors.push_back(ctx +
": GoldEscrowMax kind with goldEscrow"
"MaxCopper=0 — rule contradicts itself "
"(0 means unlimited but the rule's "
"purpose is to cap)");
}
// GMOnly rules with low priority would be useless
// (can't be triggered without GM intervention).
if (e.targetingFilter == T::GMOnly &&
e.priority < 50) {
warnings.push_back(ctx +
": GMOnly targeting with priority " +
std::to_string(e.priority) +
" < 50 — GM-mediated trades typically "
"need high priority to override player-"
"initiated rules");
}
if (!idsSeen.insert(e.ruleId).second) {
errors.push_back(ctx + ": duplicate ruleId");
}
}
bool ok = errors.empty();
if (jsonOut) {
nlohmann::json j;
j["wtrd"] = base + ".wtrd";
j["ok"] = ok;
j["errors"] = errors;
j["warnings"] = warnings;
std::printf("%s\n", j.dump(2).c_str());
return ok ? 0 : 1;
}
std::printf("validate-wtrd: %s.wtrd\n", base.c_str());
if (ok && warnings.empty()) {
std::printf(" OK — %zu rules, all ruleIds unique, "
"per-kind constraints satisfied\n",
c.entries.size());
return 0;
}
if (!warnings.empty()) {
std::printf(" warnings (%zu):\n", warnings.size());
for (const auto& w : warnings)
std::printf(" - %s\n", w.c_str());
}
if (!errors.empty()) {
std::printf(" ERRORS (%zu):\n", errors.size());
for (const auto& e : errors)
std::printf(" - %s\n", e.c_str());
}
return ok ? 0 : 1;
}
} // namespace
bool handleTradeRulesCatalog(int& i, int argc, char** argv,
int& outRc) {
if (std::strcmp(argv[i], "--gen-trd") == 0 && i + 1 < argc) {
outRc = handleGenStandard(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--gen-trd-admin") == 0 && i + 1 < argc) {
outRc = handleGenServerAdmin(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--gen-trd-rmt") == 0 && i + 1 < argc) {
outRc = handleGenRMT(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--info-wtrd") == 0 && i + 1 < argc) {
outRc = handleInfo(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--validate-wtrd") == 0 && i + 1 < argc) {
outRc = handleValidate(i, argc, argv); return true;
}
return false;
}
} // namespace cli
} // namespace editor
} // namespace wowee