Files
2026-11-23 03:28:32 +02:00

141 lines
5.9 KiB
C++

#pragma once
#include "gateway/legacy_stream.hpp"
#include "gateway/forever_opcode_profile.hpp"
#include <algorithm>
#include <array>
#include <optional>
namespace gateway::battleground {
struct Field {
uint32_t map, list;
uint8_t minimum;
};
// Tortoise's map IDs and the matching native BattlemasterList IDs.
inline constexpr std::array<Field, 3> fields = {{{30, 1, 51}, {489, 2, 0}, {529, 3, 0}}};
class Queue {
enum class Phase : uint32_t { Queued = 1, Invited = 2, Active = 3 };
struct Entry {
Guid128 player;
uint32_t map, list;
Bytes ticket;
Phase phase;
uint32_t instance, average, elapsed;
bool as_group;
};
// Source PLAYER_MAX_BATTLEGROUND_QUEUES is three. Retain the last ticket
// time after a clear so a new start in the same clock tick gets a fresh ID.
std::array<std::optional<Entry>, 3> slots_{};
std::array<std::optional<uint32_t>, 3> last_join_{};
static Bytes ticket(const Guid128& self, uint32_t slot, uint32_t joined) {
Bytes out = pack_guid(self);
le(out, slot, 4); le(out, 1, 4); // RideType::Battlegrounds
le(out, joined, 8); le(out, 0, 1); // IsCrossFaction defaults to false
return out;
}
static Bytes header(const Entry& entry) {
Bytes out = entry.ticket;
le(out, 1, 4); // one ordinary unrated queue
le(out, 0, 1); le(out, 60, 1); le(out, 0, 1); // min, cap, arena team size
le(out, entry.instance, 4);
// BattlegroundQueueTypeId::GetPacked, standard BG type, no arena size.
le(out, 0x1f10000000000000ULL | entry.list, 8);
le(out, 0, 1); // registered match and tournament rules
return out;
}
static LegacyPacket queued(const Entry& entry) {
auto out = header(entry);
le(out, entry.average, 4); le(out, entry.elapsed, 4); le(out, 0, 4);
// Tortoise separately confirms group enrollment with a positive 0x2e8.
le(out, entry.as_group ? 0xc0 : 0x40, 1); // eligible, queue not suspended
return {forever_opcodes::SMSG_BATTLEFIELD_STATUS_QUEUED, std::move(out)};
}
public:
void group_joined(std::span<const uint8_t> body, const Guid128& self,
std::vector<LegacyPacket>& out) {
Reader r(body); const uint32_t map = r.le(4); r.end();
for (auto& slot : slots_) {
if (slot && slot->player == self && slot->map == map &&
slot->phase == Phase::Queued && !slot->as_group) {
slot->as_group = true;
out.push_back(queued(*slot));
return;
}
}
}
void status(std::span<const uint8_t> body, const Guid128& self, uint32_t now_ms,
std::vector<LegacyPacket>& out) {
Reader r(body);
const uint32_t id = r.le(4), map = r.le(4);
if (id >= slots_.size()) return;
auto& slot = slots_[id];
if (!map) {
r.end();
if (slot && slot->player == self) out.push_back({forever_opcodes::SMSG_BATTLEFIELD_STATUS_NONE, slot->ticket});
slot.reset();
return;
}
const auto bracket = r.le(1);
// On initial enrollment Tortoise often writes the template bracket (-1).
if (bracket > 7 && bracket != 0xff) return;
const uint32_t instance = r.le(4);
const auto phase = static_cast<Phase>(r.le(4));
if (phase != Phase::Queued && phase != Phase::Invited && phase != Phase::Active) return;
const uint32_t first_time = r.le(4);
const uint32_t second_time = phase == Phase::Invited ? 0 : uint32_t(r.le(4));
r.end();
if (slot && (slot->player != self || slot->map != map)) {
if (slot->player == self) out.push_back({forever_opcodes::SMSG_BATTLEFIELD_STATUS_NONE, slot->ticket});
slot.reset();
}
if (!slot) {
if (phase != Phase::Queued) return;
auto field = std::find_if(fields.begin(), fields.end(), [map](const Field& f) { return f.map == map; });
if (field == fields.end()) return;
// Native tickets use the gateway's monotonic 32-bit millisecond
// clock, with modular subtraction just as in Trinity's queue clock.
uint32_t joined = now_ms - second_time;
if (last_join_[id]) {
const auto delta = joined - *last_join_[id];
if (delta == 0 || delta >= 0x80000000u) joined = *last_join_[id] + 1;
}
last_join_[id] = joined;
slot = Entry{self, map, field->list, ticket(self, id, joined), phase, instance, 0, 0, false};
}
slot->instance = instance;
slot->phase = phase;
if (phase == Phase::Queued) {
slot->average = first_time;
slot->elapsed = second_time;
out.push_back(queued(*slot));
} else if (phase == Phase::Invited) {
auto packet = header(*slot);
le(packet, map, 4); le(packet, first_time, 4); le(packet, 0, 1);
out.push_back({forever_opcodes::SMSG_BATTLEFIELD_STATUS_NEED_CONFIRMATION, std::move(packet)});
}
// An active update retires port eligibility. Its native presentation
// additionally requires the assigned BG team, absent in this report.
}
LegacyPacket port(std::span<const uint8_t> body, const Guid128& self) const {
for (const auto& slot : slots_) {
if (!slot || slot->player != self || slot->phase == Phase::Active ||
body.size() != slot->ticket.size() + 1 ||
!std::equal(slot->ticket.begin(), slot->ticket.end(), body.begin())) continue;
const auto action = body.back();
if (action != 0 && (action != 0x80 || slot->phase != Phase::Invited)) break;
Bytes request; le(request, slot->map, 4); le(request, action >> 7, 1);
return {0x2d5, std::move(request)};
}
throw Error("battleground port outside confirmed source queue ticket");
}
};
}