fixup! fixup! Add a service to announce multiplayer rooms to web service; Add the abiltiy to receive a list of all announced rooms from web service

This commit is contained in:
B3n30 2017-11-05 20:23:22 +01:00
parent 1485093fd9
commit 93742f17b3
6 changed files with 35 additions and 32 deletions

View File

@ -13,7 +13,7 @@
namespace Common {
struct WebResult {
enum Code : u32 {
enum class Code : u32 {
Success,
InvalidURL,
CredentialsMissing,
@ -39,7 +39,7 @@ struct Room {
u64 game_id;
};
std::string name;
std::string GUID;
std::string UID;
std::string owner;
std::string ip;
u16 port;
@ -60,8 +60,8 @@ using RoomList = std::vector<Room>;
class Backend : NonCopyable {
public:
virtual ~Backend() = default;
virtual void SetRoomInformation(const std::string& guid, const std::string& name,
const u16 port, const u32 max_player, const u32 net_version,
virtual void SetRoomInformation(const std::string& uid, const std::string& name, const u16 port,
const u32 max_player, const u32 net_version,
const bool has_password, const std::string& preferred_game,
const u64 preferred_game_id) = 0;
virtual void AddPlayer(const std::string& nickname, const MacAddress& mac_address,
@ -79,7 +79,7 @@ public:
class NullBackend : public Backend {
public:
~NullBackend() = default;
void SetRoomInformation(const std::string& /*guid*/, const std::string& /*name*/,
void SetRoomInformation(const std::string& /*uid*/, const std::string& /*name*/,
const u16 /*port*/, const u32 /*max_player*/, const u32 /*net_version*/,
const bool /*has_password*/, const std::string& /*preferred_game*/,
const u64 /*preferred_game_id*/) override {}

View File

@ -77,29 +77,30 @@ void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() {
finished = false;
std::future<Common::WebResult> future;
while (announce) {
if (std::shared_ptr<Network::Room> room = Network::GetRoom().lock()) {
if (room->GetState() == Network::Room::State::Open) {
Network::RoomInformation room_information = room->GetRoomInformation();
std::vector<Network::Room::Member> memberlist = room->GetRoomMemberList();
backend->SetRoomInformation(
room_information.guid, room_information.name, room_information.port,
room_information.member_slots, Network::network_version, room->HasPassword(),
room_information.preferred_game, room_information.preferred_game_id);
backend->ClearPlayers();
for (const auto& member : memberlist) {
backend->AddPlayer(member.nickname, member.mac_address, member.game_info.id,
member.game_info.name);
}
future = backend->Announce();
} else {
announce = false;
}
} else {
std::shared_ptr<Network::Room> room = Network::GetRoom().lock();
if (!room) {
announce = false;
continue;
}
if (room->GetState() != Network::Room::State::Open) {
announce = false;
continue;
}
Network::RoomInformation room_information = room->GetRoomInformation();
std::vector<Network::Room::Member> memberlist = room->GetRoomMemberList();
backend->SetRoomInformation(
room_information.uid, room_information.name, room_information.port,
room_information.member_slots, Network::network_version, room->HasPassword(),
room_information.preferred_game, room_information.preferred_game_id);
backend->ClearPlayers();
for (const auto& member : memberlist) {
backend->AddPlayer(member.nickname, member.mac_address, member.game_info.id,
member.game_info.name);
}
future = backend->Announce();
if (future.valid()) {
Common::WebResult result = future.get();
if (result.result_code != Common::WebResult::Success) {
if (result.result_code != Common::WebResult::Code::Success) {
std::lock_guard<std::mutex> lock(callback_mutex);
for (auto callback : error_callbacks) {
(*callback)(result);

View File

@ -23,7 +23,7 @@ void from_json(const nlohmann::json& json, Room::Member& member) {
}
void to_json(nlohmann::json& json, const Room& room) {
json["id"] = room.GUID;
json["id"] = room.UID;
json["port"] = room.port;
json["name"] = room.name;
json["preferredGameName"] = room.preferred_game;
@ -58,12 +58,12 @@ void from_json(const nlohmann::json& json, Room& room) {
namespace WebService {
void RoomJson::SetRoomInformation(const std::string& guid, const std::string& name, const u16 port,
void RoomJson::SetRoomInformation(const std::string& uid, const std::string& name, const u16 port,
const u32 max_player, const u32 net_version,
const bool has_password, const std::string& preferred_game,
const u64 preferred_game_id) {
room.name = name;
room.GUID = guid;
room.UID = uid;
room.port = port;
room.max_player = max_player;
room.net_version = net_version;
@ -105,7 +105,7 @@ std::future<AnnounceMultiplayerRoom::RoomList> RoomJson::GetRoomList(std::functi
void RoomJson::Delete() {
nlohmann::json json;
json["id"] = room.GUID;
json["id"] = room.UID;
DeleteJson(endpoint_url, json.dump(), username, token);
}

View File

@ -20,7 +20,7 @@ public:
RoomJson(const std::string& endpoint_url, const std::string& username, const std::string& token)
: endpoint_url(endpoint_url), username(username), token(token) {}
~RoomJson() = default;
void SetRoomInformation(const std::string& guid, const std::string& name, const u16 port,
void SetRoomInformation(const std::string& uid, const std::string& name, const u16 port,
const u32 max_player, const u32 net_version, const bool has_password,
const std::string& preferred_game,
const u64 preferred_game_id) override;

View File

@ -81,9 +81,8 @@ void TelemetryJson::Complete() {
SerializeSection(Telemetry::FieldType::UserConfig, "UserConfig");
SerializeSection(Telemetry::FieldType::UserSystem, "UserSystem");
// Send the telemetry async but don't handle the errors since the were written to the log
static std::future<Common::WebResult> future =
PostJson(endpoint_url, TopSection().dump(), true, username, token);
// Send the telemetry async but don't handle the errors since they were written to the log
future = PostJson(endpoint_url, TopSection().dump(), true, username, token);
}
} // namespace WebService

View File

@ -5,8 +5,10 @@
#pragma once
#include <array>
#include <future>
#include <string>
#include <json.hpp>
#include "common/announce_multiplayer_room.h"
#include "common/telemetry.h"
namespace WebService {
@ -54,6 +56,7 @@ private:
std::string endpoint_url;
std::string username;
std::string token;
std::future<Common::WebResult> future;
};
} // namespace WebService