From 1aed7f509d4a298abb239c2dd5dd234ed96a731a Mon Sep 17 00:00:00 2001 From: Karrz1 <127596890+Karrz1@users.noreply.github.com> Date: Mon, 16 Nov 2026 20:09:48 +0000 Subject: [PATCH] Bots: one click on Trade accepts a trade with a bot (SIN's fix) SIN's Bot Trading fix for 0.1.36, as he wrote it. The add-on puts its own button over the stock Trade button; when a bot is the trader it asks the server for a view of the trade, and a click sends that view back, so the server accepts only the trade the player saw. Solo players send the request as a hidden whisper to themselves; grouped players use the add-on channel. Code by SIN (1amcoff1n). Applied unchanged; only the add-on version is set to 0.6.22. --- .../Addon/ForeverBots/ForeverBots.toc | 2 +- .../Addon/ForeverBots/ForeverBots_Trade.lua | 105 ++++++++++++++++++ .../PlayerbotV2/PlayerbotV2Scripts.cpp | 16 +++ .../PlayerbotV2/Session/AddonControl.cpp | 47 +++++++- 4 files changed, 166 insertions(+), 4 deletions(-) create mode 100644 src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots_Trade.lua diff --git a/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots.toc b/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots.toc index 0ebc0efade..2327b6624c 100644 --- a/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots.toc +++ b/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots.toc @@ -2,7 +2,7 @@ ## Title: WoW Forever Bots ## Notes: One-click PlayerbotV2 companions: add a bot of any class and role, call, dismiss and command your bots. ## Author: Karrz -## Version: 0.6.21 +## Version: 0.6.22 ## IconTexture: Interface\AddOns\ForeverBots\Textures\mono_fb64.tga ## SavedVariables: ForeverBotsDB diff --git a/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots_Trade.lua b/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots_Trade.lua new file mode 100644 index 0000000000..11e8c0b60e --- /dev/null +++ b/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots_Trade.lua @@ -0,0 +1,105 @@ +local _, ns = ... +local net = ns.Net +net.ANSWERS.BOT_TRADE_RESP = true + +local events = CreateFrame("Frame") +local version, open, button, token, accepted = 0, false, nil, nil, false +local function stockButton() + return TradeFrameTradeButton or (TradeFrame and TradeFrame.TradeButton) +end + +-- Solo clients on this branch can drop addon whispers. A normal whisper to +-- oneself carries only the request. The server answers privately via PBC. +local function request(kind, fields, cb) + if IsInGroup() then return net.Request(kind, fields, cb) end + net.seq = (net.seq % 0x7ffffffe) + 1 + local seq = net.seq + net.pending[seq] = { cb = cb, mtype = kind, at = GetTime() } + local send = SendChatMessage or (C_ChatInfo and C_ChatInfo.SendChatMessage) + if not send or not pcall(send, string.format("FBTrade %x %s", seq, fields[1] or "view"), + "WHISPER", nil, UnitName("player")) then + net.pending[seq] = nil + cb(nil) + end +end + +local function hideProtocol(_, _, message, sender) + return sender and Ambiguate(sender, "short") == UnitName("player") and + message:match("^FBTrade %x+ [%w:]+$") ~= nil +end +ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER", hideProtocol) +ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER_INFORM", hideProtocol) + +local refresh +local function click() + if not open or not token then return end + if accepted then + CancelTradeAccept() + refresh() + return + end + local clicked, v = token, version + token = nil + button:Disable() + request("BOT_TRADE_ACCEPT", {clicked}, function(fields, kind) + if not open or v ~= version then return end + if not fields or kind ~= "BOT_TRADE_RESP" or fields[1] ~= "submitted" then + UIErrorsFrame:AddMessage(ERR_TRADE_REQUEST_CHANGED or "Please review the trade again.", 1, 0.6, 0) + end + refresh() + end) +end + +local function ensureButton(stock) + if button then return end + button = CreateFrame("Button", nil, stock:GetParent(), "UIPanelButtonTemplate") + button:SetAllPoints(stock) + button:SetFrameLevel(stock:GetFrameLevel() + 5) + button:SetScript("OnClick", click) +end + +refresh = function() + version = version + 1 + local v = version + token = nil + if button and button:IsShown() then button:Disable() end + if not open then + if button then button:Hide() end + return + end + local stock = stockButton() + if stock and stock:IsVisible() then + ensureButton(stock) + button:SetText(stock:GetText()) + button:Disable() + button:Show() + end + C_Timer.After(0.12, function() + if v ~= version or not open then return end + request("BOT_TRADE_VIEW", {}, function(fields, kind) + if v ~= version or not open then return end + local stock = stockButton() + local partner = UnitName("NPC") or (TradeFrameRecipientNameText and TradeFrameRecipientNameText:GetText()) + if kind ~= "BOT_TRADE_RESP" or not fields or fields[1] ~= "ready" or + partner ~= fields[3] or not stock or not stock:IsVisible() then + if button then button:Hide() end + return + end + ensureButton(stock) + token, accepted = fields[2], fields[4] == "1" + button:SetText(accepted and (CANCEL or "Cancel") or stock:GetText()) + button:SetEnabled(accepted or stock:IsEnabled()) + button:Show() + end) + end) +end + +for _, event in ipairs({"TRADE_SHOW", "TRADE_CLOSED", "TRADE_PLAYER_ITEM_CHANGED", + "TRADE_TARGET_ITEM_CHANGED", "TRADE_MONEY_CHANGED", "TRADE_ACCEPT_UPDATE"}) do + events:RegisterEvent(event) +end +events:SetScript("OnEvent", function(_, event) + if event == "TRADE_SHOW" then open = true end + if event == "TRADE_CLOSED" then open = false end + refresh() +end) diff --git a/src/modules/PlayerbotV2/PlayerbotV2Scripts.cpp b/src/modules/PlayerbotV2/PlayerbotV2Scripts.cpp index dc9e7904d2..03eed1397e 100644 --- a/src/modules/PlayerbotV2/PlayerbotV2Scripts.cpp +++ b/src/modules/PlayerbotV2/PlayerbotV2Scripts.cpp @@ -1,3 +1,4 @@ +#include "Session/AddonControl.h" // Playerbot V2 - script registration // The module hangs off ScriptMgr: every core event it reacts to arrives through a WorldScript / PlayerScript / // UnitScript / GroupScript / GuildScript hook and is forwarded to the matching Module handler. AddSC_playerbot_v2 is @@ -242,6 +243,21 @@ public: void OnChatSent(Player* player, uint32 /*type*/, uint32 /*lang*/, std::string const& msg, Player* receiver) override { + if (player == receiver && msg.starts_with("FBTrade ") && msg.size() < 140) + { + auto split = msg.find(' ', 8); + if (split == std::string::npos) + return; + std::string seq = msg.substr(8, split - 8); + if (seq.empty() || seq.size() > 8 || seq.find_first_not_of("0123456789abcdef") != std::string::npos) + return; + std::string token = msg.substr(split + 1); + if (token != "view" && (token.empty() || token.find_first_not_of("0123456789:") != std::string::npos)) + return; + Playerbot::V2::AddonControl::OnAddonWhisper(player, receiver, "PBC", "1|" + seq + + (token == "view" ? "|1|1|BOT_TRADE_VIEW|" : "|1|1|BOT_TRADE_ACCEPT|" + token)); + return; + } Module::instance().OnWhisperReceived(player, receiver, msg); } diff --git a/src/modules/PlayerbotV2/Session/AddonControl.cpp b/src/modules/PlayerbotV2/Session/AddonControl.cpp index f50896cecf..a39a201571 100644 --- a/src/modules/PlayerbotV2/Session/AddonControl.cpp +++ b/src/modules/PlayerbotV2/Session/AddonControl.cpp @@ -1,4 +1,4 @@ -#include "AddonControl.h" +#include "AddonControl.h" #include "Bot/BotAI.h" #include "Bot/BotCommandParser.h" @@ -39,6 +39,8 @@ #include "RBAC.h" #include "World.h" #include "WorldSession.h" +#include "TradeData.h" +#include "TradePackets.h" #include #include @@ -202,7 +204,7 @@ void SendAddonRaw(WorldSession* sess, std::string const& body) if (!sess) return; Player* p = sess->GetPlayer(); if (!p) return; - if (SysReply(sess->GetAccountId())) + if (SysReply(sess->GetAccountId()) || body.find("|1|1|BOT_TRADE_RESP|") != std::string::npos) { WorldPackets::Chat::Chat sys; sys.Initialize(CHAT_MSG_SYSTEM, LANG_UNIVERSAL, nullptr, nullptr, std::string(kSysMarker) + body); @@ -1595,13 +1597,52 @@ void HandleLogoutReq(uint32 /*client_seq*/, WorldSession* sess, // ---------------------------------------------------------------------------- // Main dispatcher. Called once a complete logical frame is assembled. // ---------------------------------------------------------------------------- +// Snapshot both offers and the unique trade window. Delayed addon messages +// cannot authorize a different trade or an offer that changed after the click. +void HandleBotTrade(WorldSession* session, uint32 seq, bool commit, std::vector const& fields) +{ + Player* player = session->GetPlayer(); + TradeData* mine = player ? player->GetTradeData() : nullptr; + Player* partner = mine ? mine->GetTrader() : nullptr; + TradeData* theirs = partner ? partner->GetTradeData() : nullptr; + if (!theirs || theirs->GetTrader() != player || !Services::Initialized() || + !Services::SessionMgr().IsHeadless(partner->GetGUID())) + { + SendFields(session, seq, "BOT_TRADE_RESP", {"unavailable"}); + return; + } + + std::string token = std::to_string(mine->GetSerial()) + ":" + + std::to_string(mine->GetServerStateIndex()) + ":" + std::to_string(theirs->GetServerStateIndex()); + if (!commit) + { + SendFields(session, seq, "BOT_TRADE_RESP", {"ready", token, std::string(partner->GetName()), mine->IsAccepted() ? "1" : "0"}); + return; + } + if (fields.size() != 1 || fields.front() != token || mine->IsAccepted() || + mine->IsInAcceptProcess() || theirs->IsInAcceptProcess()) + { + SendFields(session, seq, "BOT_TRADE_RESP", {"changed"}); + return; + } + + WorldPacket packet(CMSG_ACCEPT_TRADE); + packet << theirs->GetServerStateIndex(); + WorldPackets::Trade::AcceptTrade accept(std::move(packet)); + accept.Read(); + session->HandleAcceptTradeOpcode(accept); + SendFields(session, seq, "BOT_TRADE_RESP", {"submitted"}); +} + void DispatchAssembled(WorldSession* sess, uint32 seq, std::string const& mtype, std::string const& body) { TC_LOG_DEBUG("playerbot.v2", "[addon_pbc] {} {} seq {:x} ({} bytes)", sess->GetPlayer() ? sess->GetPlayer()->GetName() : std::string("?"), mtype, seq, body.size()); auto fields = SplitPipes(body); - if (mtype == "ROSTER_REQ") + if (mtype == "BOT_TRADE_VIEW" || mtype == "BOT_TRADE_ACCEPT") + HandleBotTrade(sess, seq, mtype == "BOT_TRADE_ACCEPT", fields); + else if (mtype == "ROSTER_REQ") { const uint32 flags = fields.empty() ? 15u : uint32(std::strtoul(fields[0].c_str(), nullptr, 10));