230 lines
9.8 KiB
C++
230 lines
9.8 KiB
C++
#pragma once
|
|
|
|
#include "battleground_requests.h"
|
|
#include "battleground_queue.hpp"
|
|
#include "gateway/forever_opcode_profile.hpp"
|
|
#include "gateway/inventory_result_profile.hpp"
|
|
#include "gateway/item_random_property_profile.hpp"
|
|
#include "gateway/legacy_stream.hpp"
|
|
#include "gateway/roster_translation.hpp"
|
|
#include <algorithm>
|
|
#include <map>
|
|
#include <optional>
|
|
|
|
namespace gateway::auction_battleground {
|
|
|
|
inline uint32_t word(Reader& r) { return static_cast<uint32_t>(r.le(4)); }
|
|
|
|
inline Guid128 player(uint64_t source, uint32_t realm) {
|
|
if (!source || source > UINT32_MAX) throw Error("invalid source player in service response");
|
|
return forever_player_guid(source, realm);
|
|
}
|
|
|
|
// Source AuctionHouseHandler::SendAuctionCommandResult appends a different
|
|
// tail for a successful bid, an inventory error and a competing higher bid.
|
|
inline std::optional<LegacyPacket> auction_command(Reader& r, uint32_t realm) {
|
|
uint32_t id = word(r), command = word(r), result = word(r);
|
|
if (command > 2) return std::nullopt;
|
|
uint32_t bag = 0, increment = 0, money = 0;
|
|
Guid128 bidder{};
|
|
switch (result) {
|
|
case 0:
|
|
if (command == 2) increment = word(r);
|
|
break;
|
|
case 1: {
|
|
uint32_t original = word(r);
|
|
if (original > UINT8_MAX) return std::nullopt;
|
|
auto matched = inventory_results::forever(static_cast<uint8_t>(original));
|
|
if (matched < 0) return std::nullopt;
|
|
bag = static_cast<uint32_t>(matched);
|
|
break;
|
|
}
|
|
case 5: {
|
|
uint64_t guid = r.le(8);
|
|
if (guid) bidder = player(guid, realm);
|
|
money = word(r);
|
|
increment = word(r);
|
|
break;
|
|
}
|
|
case 2: case 3: case 4: case 7: case 10: case 13: break;
|
|
default: return std::nullopt;
|
|
}
|
|
r.end();
|
|
Bytes out;
|
|
le(out, id, 4); le(out, command, 4); le(out, result, 4); le(out, bag, 4);
|
|
append(out, pack_guid(bidder)); le(out, increment, 8); le(out, money, 8);
|
|
le(out, 0, 4); // Source does not impose the native search throttle.
|
|
return LegacyPacket{forever_opcodes::SMSG_AUCTION_COMMAND_RESULT, std::move(out)};
|
|
}
|
|
|
|
// ItemPacketsCommon::ItemInstance. Only an exact source item/property pair
|
|
// can supply a native bonus list. Dropping a suffix would identify a different item.
|
|
inline bool write_item(Bytes& out, uint32_t item, uint32_t property) {
|
|
if (!item || item > INT32_MAX) return false;
|
|
uint32_t bonus = property ? item_random_property_profile::resolve(item, property) : 0;
|
|
if (property && !bonus) return false;
|
|
le(out, item, 4); le(out, 0, 1); le(out, bonus ? 0x80 : 0, 1);
|
|
if (bonus) {
|
|
le(out, 0, 1); le(out, 1, 4); le(out, bonus, 4);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
inline std::optional<LegacyPacket> bidder_notice(Reader& r, uint32_t realm) {
|
|
const auto house = word(r), id = word(r);
|
|
const auto bidder = player(r.le(8), realm);
|
|
const auto bid = word(r);
|
|
(void)word(r); // increment computed from the outgoing bidder's original bid
|
|
const auto item = word(r), property = word(r);
|
|
r.end();
|
|
// Source sends an outbid before installing the replacement bidder or price.
|
|
// The native outbid explicitly announces the replacement bidder and price.
|
|
if (!house || !id || bid) return std::nullopt;
|
|
Bytes out;
|
|
le(out, house, 4); le(out, id, 4); append(out, pack_guid(bidder));
|
|
if (!write_item(out, item, property)) return std::nullopt;
|
|
return LegacyPacket{forever_opcodes::SMSG_AUCTION_WON_NOTIFICATION, std::move(out)}; // won
|
|
}
|
|
|
|
inline std::optional<LegacyPacket> owner_notice(Reader& r, uint32_t realm) {
|
|
const auto id = word(r), amount = word(r), increment = word(r);
|
|
const auto bidder = r.le(8);
|
|
const auto item = word(r), property = word(r);
|
|
r.end();
|
|
if (!id) return std::nullopt;
|
|
Bytes out;
|
|
le(out, id, 4); le(out, amount, 8);
|
|
if (!write_item(out, item, property)) return std::nullopt;
|
|
if (bidder) {
|
|
le(out, increment, 8); append(out, pack_guid(player(bidder, realm)));
|
|
return LegacyPacket{forever_opcodes::SMSG_AUCTION_OWNER_BID_NOTIFICATION, std::move(out)};
|
|
}
|
|
// SendAuctionSuccessfulMail uses the original MailDraft's zero delivery
|
|
// delay. SendAuctionExpiredMail has no sale proceeds.
|
|
le(out, 0, 4); le(out, amount ? 0x80 : 0, 1);
|
|
return LegacyPacket{forever_opcodes::SMSG_AUCTION_CLOSED_NOTIFICATION, std::move(out)};
|
|
}
|
|
|
|
inline std::optional<LegacyPacket> auction_owned_or_bid_list(Reader& r, bool owned) {
|
|
// Both Source lists use AuctionEntry::BuildAuctionInfo (64 bytes per row)
|
|
// and end in the total count. Native owned/bid lists are always full and
|
|
// their rows include a bucket key, item GUID and/or account metadata.
|
|
const uint32_t count = word(r);
|
|
if (count > 50 || r.left() != count * 64u + 4u) return std::nullopt;
|
|
if (count || word(r) != 0) return std::nullopt;
|
|
return LegacyPacket{owned ? forever_opcodes::SMSG_AUCTION_LIST_OWNED_ITEMS_RESULT : forever_opcodes::SMSG_AUCTION_LIST_BIDDED_ITEMS_RESULT, Bytes(owned ? 13 : 9, 0)};
|
|
}
|
|
|
|
inline LegacyPacket game_error(uint32_t id) {
|
|
Bytes out; le(out, id, 4); le(out, 0, 1); // MiscPackets::DisplayGameError, both optional args absent
|
|
return {forever_opcodes::SMSG_DISPLAY_GAME_ERROR, std::move(out)};
|
|
}
|
|
|
|
inline std::optional<LegacyPacket> queue_feedback(uint32_t opcode, Reader& r) {
|
|
if (opcode == 0x297 || opcode == 0x298) {
|
|
r.end();
|
|
return game_error(opcode == 0x297 ? 570 : 571); // meeting stone complete / in progress
|
|
}
|
|
if (opcode == 0x2e8) {
|
|
const auto status = word(r); r.end();
|
|
if (status == 0xfffffffeu) return game_error(587); // group has deserters
|
|
if (status == 0xffffffffu) return game_error(590); // group join failed
|
|
return std::nullopt; // successful map ID has no native queue ticket
|
|
}
|
|
if (opcode == 0x2bb) {
|
|
const auto status = r.le(1); r.end();
|
|
switch (status) {
|
|
case 1: return game_error(578); // ERR_MEETING_STONE_MUST_BE_LEADER (join, not cancel)
|
|
case 2: return game_error(573); // group full
|
|
case 3: return game_error(579); // raid group
|
|
default: return std::nullopt;
|
|
}
|
|
}
|
|
(void)word(r); // area ID, used only by the named join/leave reports
|
|
// Group.cpp also writes an enum-width idle on disband. Other extended
|
|
// messages never qualify either.
|
|
if (r.left() != 1) return std::nullopt;
|
|
const auto status = r.le(1); r.end();
|
|
switch (status) {
|
|
case 2: return game_error(567); // other party member left
|
|
case 3: return game_error(568); // party removed from queue
|
|
case 4: return game_error(569); // member still in queue
|
|
default: return std::nullopt; // idle or a named-area update, no LFG ticket
|
|
}
|
|
}
|
|
|
|
inline LegacyPacket battlefield_request(uint32_t opcode, std::span<const uint8_t> body,
|
|
uint32_t current_map) {
|
|
Reader r(body);
|
|
if (opcode == GW_REQUEST_BATTLEFIELD_STATUS) {
|
|
r.end();
|
|
return {0x2d3, {}};
|
|
}
|
|
if (opcode == GW_BATTLEFIELD_LIST) {
|
|
const auto list = word(r);
|
|
r.end();
|
|
for (const auto& field : battleground::fields) {
|
|
if (field.list == list) {
|
|
Bytes request; le(request, field.map, 4);
|
|
return {0x23c, std::move(request)};
|
|
}
|
|
}
|
|
throw Error("unknown native battleground list");
|
|
}
|
|
if (opcode == GW_BATTLEFIELD_LEAVE) {
|
|
r.end();
|
|
for (const auto& field : battleground::fields) {
|
|
if (field.map == current_map) {
|
|
Bytes request; le(request, current_map, 4);
|
|
return {0x2e1, std::move(request)};
|
|
}
|
|
}
|
|
throw Error("leave battlefield outside a known source battleground");
|
|
}
|
|
throw Error("unknown battleground request");
|
|
}
|
|
|
|
inline std::optional<LegacyPacket> battlefield_list(Reader& r, uint64_t self,
|
|
const std::map<uint64_t, Guid128>& known) {
|
|
const auto actor = r.le(8);
|
|
const auto map = word(r);
|
|
const auto bracket = r.le(1);
|
|
const auto count = word(r);
|
|
if (bracket > 7 || count > 4096 || r.left() != count * 4u) return std::nullopt;
|
|
auto field = std::find_if(battleground::fields.begin(), battleground::fields.end(),
|
|
[map](const battleground::Field& value) { return value.map == map; });
|
|
if (field == battleground::fields.end()) return std::nullopt;
|
|
// Source uses the recipient's own GUID for its non-NPC list request.
|
|
// Native denotes that case with an empty battlemaster and PvpAnywhere.
|
|
Guid128 battlemaster{};
|
|
if (!actor || !self) return std::nullopt;
|
|
if (actor != self) {
|
|
auto it = known.find(actor);
|
|
if ((actor >> 48) != 0xf130 || it == known.end()) return std::nullopt;
|
|
battlemaster = it->second;
|
|
}
|
|
Bytes out = pack_guid(battlemaster);
|
|
le(out, field->list, 4); le(out, field->minimum, 1); le(out, 60, 1); le(out, count, 4);
|
|
append(out, r.take(count * 4u));
|
|
le(out, actor == self ? 0x80 : 0, 1); // original has no random-win reward
|
|
return LegacyPacket{forever_opcodes::SMSG_BATTLEFIELD_LIST, std::move(out)};
|
|
}
|
|
|
|
inline std::optional<LegacyPacket> response(uint32_t opcode, Reader& r, uint32_t realm) {
|
|
switch (opcode) {
|
|
case 0x25b: return auction_command(r, realm);
|
|
case 0x25d: case 0x265: return auction_owned_or_bid_list(r, opcode == 0x25d);
|
|
case 0x25e: return bidder_notice(r, realm);
|
|
case 0x25f: return owner_notice(r, realm);
|
|
case 0x2ec: case 0x2ed: {
|
|
auto member = player(r.le(8), realm); r.end();
|
|
return LegacyPacket{opcode == 0x2ec ? forever_opcodes::SMSG_BATTLEGROUND_PLAYER_JOINED : forever_opcodes::SMSG_BATTLEGROUND_PLAYER_LEFT, pack_guid(member)};
|
|
}
|
|
case 0x295: case 0x297: case 0x298: case 0x2bb: case 0x2e8:
|
|
return queue_feedback(opcode, r);
|
|
default: return std::nullopt;
|
|
}
|
|
}
|
|
|
|
} // namespace gateway::auction_battleground
|