Files

33 lines
2.0 KiB
C++

#pragma once
#include "gateway/group.hpp"
#include "gateway/protocol.hpp"
#include <algorithm>
#include <optional>
namespace gateway {
// Native70334 RandomRollClient optional PartyIndex uses the high bit first.
// Source GroupHandler owns randomness and accepts only 0 <= min <= max <=10000.
inline Bytes source_random_roll_request(std::span<const uint8_t> body){
Reader r(body);auto flags=r.le(1);if(flags&0x7f)throw Error("random roll optional flags");
auto minimum=r.le(4),maximum=r.le(4);
// #85: a raid/instance roll carries the party category (0 home, 1 instance). Turtle has one group and its
// GroupHandler chooses the recipients itself, so the valid categories are accepted and not forwarded.
if(flags&0x80){if(r.le(1)>1)throw Error("random roll unsupported party index");}
r.end();if(minimum>maximum || maximum>10000)throw Error("random roll source range");
Bytes out;le(out,minimum,4);le(out,maximum,4);return out;
}
struct SourceRandomRoll {uint32_t minimum,maximum,result;uint64_t player;};
inline SourceRandomRoll turtle_random_roll(std::span<const uint8_t> body){
Reader r(body);SourceRandomRoll roll{uint32_t(r.le(4)),uint32_t(r.le(4)),uint32_t(r.le(4)),r.le(8)};r.end();
if(roll.minimum>roll.maximum || roll.maximum>10000 || roll.result<roll.minimum || roll.result>roll.maximum || !roll.player || roll.player>UINT32_MAX)
throw Error("random roll source result bound");
return roll;
}
inline bool random_roll_player_bound(const SourceRandomRoll& roll,uint64_t self,const std::optional<TurtleGroupList>& group){
return roll.player==self || (group && !group->explicit_solo && std::any_of(group->members.begin(),group->members.end(),[&](const auto& member){return member.guid==roll.player;}));
}
inline Bytes random_roll_codec_input(const SourceRandomRoll& roll,uint16_t realm){
if(!realm)throw Error("random roll realm absent");
Bytes out;le(out,realm,2);le(out,roll.minimum,4);le(out,roll.maximum,4);le(out,roll.result,4);le(out,roll.player,8);return out;
}
}