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:
parent
1485093fd9
commit
93742f17b3
6 changed files with 35 additions and 32 deletions
|
@ -13,7 +13,7 @@
|
||||||
|
|
||||||
namespace Common {
|
namespace Common {
|
||||||
struct WebResult {
|
struct WebResult {
|
||||||
enum Code : u32 {
|
enum class Code : u32 {
|
||||||
Success,
|
Success,
|
||||||
InvalidURL,
|
InvalidURL,
|
||||||
CredentialsMissing,
|
CredentialsMissing,
|
||||||
|
@ -39,7 +39,7 @@ struct Room {
|
||||||
u64 game_id;
|
u64 game_id;
|
||||||
};
|
};
|
||||||
std::string name;
|
std::string name;
|
||||||
std::string GUID;
|
std::string UID;
|
||||||
std::string owner;
|
std::string owner;
|
||||||
std::string ip;
|
std::string ip;
|
||||||
u16 port;
|
u16 port;
|
||||||
|
@ -60,8 +60,8 @@ using RoomList = std::vector<Room>;
|
||||||
class Backend : NonCopyable {
|
class Backend : NonCopyable {
|
||||||
public:
|
public:
|
||||||
virtual ~Backend() = default;
|
virtual ~Backend() = default;
|
||||||
virtual void SetRoomInformation(const std::string& guid, const std::string& name,
|
virtual void SetRoomInformation(const std::string& uid, const std::string& name, const u16 port,
|
||||||
const u16 port, const u32 max_player, const u32 net_version,
|
const u32 max_player, const u32 net_version,
|
||||||
const bool has_password, const std::string& preferred_game,
|
const bool has_password, const std::string& preferred_game,
|
||||||
const u64 preferred_game_id) = 0;
|
const u64 preferred_game_id) = 0;
|
||||||
virtual void AddPlayer(const std::string& nickname, const MacAddress& mac_address,
|
virtual void AddPlayer(const std::string& nickname, const MacAddress& mac_address,
|
||||||
|
@ -79,7 +79,7 @@ public:
|
||||||
class NullBackend : public Backend {
|
class NullBackend : public Backend {
|
||||||
public:
|
public:
|
||||||
~NullBackend() = default;
|
~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 u16 /*port*/, const u32 /*max_player*/, const u32 /*net_version*/,
|
||||||
const bool /*has_password*/, const std::string& /*preferred_game*/,
|
const bool /*has_password*/, const std::string& /*preferred_game*/,
|
||||||
const u64 /*preferred_game_id*/) override {}
|
const u64 /*preferred_game_id*/) override {}
|
||||||
|
|
|
@ -77,12 +77,19 @@ void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() {
|
||||||
finished = false;
|
finished = false;
|
||||||
std::future<Common::WebResult> future;
|
std::future<Common::WebResult> future;
|
||||||
while (announce) {
|
while (announce) {
|
||||||
if (std::shared_ptr<Network::Room> room = Network::GetRoom().lock()) {
|
std::shared_ptr<Network::Room> room = Network::GetRoom().lock();
|
||||||
if (room->GetState() == Network::Room::State::Open) {
|
if (!room) {
|
||||||
|
announce = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (room->GetState() != Network::Room::State::Open) {
|
||||||
|
announce = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
Network::RoomInformation room_information = room->GetRoomInformation();
|
Network::RoomInformation room_information = room->GetRoomInformation();
|
||||||
std::vector<Network::Room::Member> memberlist = room->GetRoomMemberList();
|
std::vector<Network::Room::Member> memberlist = room->GetRoomMemberList();
|
||||||
backend->SetRoomInformation(
|
backend->SetRoomInformation(
|
||||||
room_information.guid, room_information.name, room_information.port,
|
room_information.uid, room_information.name, room_information.port,
|
||||||
room_information.member_slots, Network::network_version, room->HasPassword(),
|
room_information.member_slots, Network::network_version, room->HasPassword(),
|
||||||
room_information.preferred_game, room_information.preferred_game_id);
|
room_information.preferred_game, room_information.preferred_game_id);
|
||||||
backend->ClearPlayers();
|
backend->ClearPlayers();
|
||||||
|
@ -91,15 +98,9 @@ void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() {
|
||||||
member.game_info.name);
|
member.game_info.name);
|
||||||
}
|
}
|
||||||
future = backend->Announce();
|
future = backend->Announce();
|
||||||
} else {
|
|
||||||
announce = false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
announce = false;
|
|
||||||
}
|
|
||||||
if (future.valid()) {
|
if (future.valid()) {
|
||||||
Common::WebResult result = future.get();
|
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);
|
std::lock_guard<std::mutex> lock(callback_mutex);
|
||||||
for (auto callback : error_callbacks) {
|
for (auto callback : error_callbacks) {
|
||||||
(*callback)(result);
|
(*callback)(result);
|
||||||
|
|
|
@ -23,7 +23,7 @@ void from_json(const nlohmann::json& json, Room::Member& member) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void to_json(nlohmann::json& json, const Room& room) {
|
void to_json(nlohmann::json& json, const Room& room) {
|
||||||
json["id"] = room.GUID;
|
json["id"] = room.UID;
|
||||||
json["port"] = room.port;
|
json["port"] = room.port;
|
||||||
json["name"] = room.name;
|
json["name"] = room.name;
|
||||||
json["preferredGameName"] = room.preferred_game;
|
json["preferredGameName"] = room.preferred_game;
|
||||||
|
@ -58,12 +58,12 @@ void from_json(const nlohmann::json& json, Room& room) {
|
||||||
|
|
||||||
namespace WebService {
|
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 u32 max_player, const u32 net_version,
|
||||||
const bool has_password, const std::string& preferred_game,
|
const bool has_password, const std::string& preferred_game,
|
||||||
const u64 preferred_game_id) {
|
const u64 preferred_game_id) {
|
||||||
room.name = name;
|
room.name = name;
|
||||||
room.GUID = guid;
|
room.UID = uid;
|
||||||
room.port = port;
|
room.port = port;
|
||||||
room.max_player = max_player;
|
room.max_player = max_player;
|
||||||
room.net_version = net_version;
|
room.net_version = net_version;
|
||||||
|
@ -105,7 +105,7 @@ std::future<AnnounceMultiplayerRoom::RoomList> RoomJson::GetRoomList(std::functi
|
||||||
|
|
||||||
void RoomJson::Delete() {
|
void RoomJson::Delete() {
|
||||||
nlohmann::json json;
|
nlohmann::json json;
|
||||||
json["id"] = room.GUID;
|
json["id"] = room.UID;
|
||||||
DeleteJson(endpoint_url, json.dump(), username, token);
|
DeleteJson(endpoint_url, json.dump(), username, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ public:
|
||||||
RoomJson(const std::string& endpoint_url, const std::string& username, const std::string& token)
|
RoomJson(const std::string& endpoint_url, const std::string& username, const std::string& token)
|
||||||
: endpoint_url(endpoint_url), username(username), token(token) {}
|
: endpoint_url(endpoint_url), username(username), token(token) {}
|
||||||
~RoomJson() = default;
|
~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 u32 max_player, const u32 net_version, const bool has_password,
|
||||||
const std::string& preferred_game,
|
const std::string& preferred_game,
|
||||||
const u64 preferred_game_id) override;
|
const u64 preferred_game_id) override;
|
||||||
|
|
|
@ -81,9 +81,8 @@ void TelemetryJson::Complete() {
|
||||||
SerializeSection(Telemetry::FieldType::UserConfig, "UserConfig");
|
SerializeSection(Telemetry::FieldType::UserConfig, "UserConfig");
|
||||||
SerializeSection(Telemetry::FieldType::UserSystem, "UserSystem");
|
SerializeSection(Telemetry::FieldType::UserSystem, "UserSystem");
|
||||||
|
|
||||||
// Send the telemetry async but don't handle the errors since the were written to the log
|
// Send the telemetry async but don't handle the errors since they were written to the log
|
||||||
static std::future<Common::WebResult> future =
|
future = PostJson(endpoint_url, TopSection().dump(), true, username, token);
|
||||||
PostJson(endpoint_url, TopSection().dump(), true, username, token);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace WebService
|
} // namespace WebService
|
||||||
|
|
|
@ -5,8 +5,10 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <future>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <json.hpp>
|
#include <json.hpp>
|
||||||
|
#include "common/announce_multiplayer_room.h"
|
||||||
#include "common/telemetry.h"
|
#include "common/telemetry.h"
|
||||||
|
|
||||||
namespace WebService {
|
namespace WebService {
|
||||||
|
@ -54,6 +56,7 @@ private:
|
||||||
std::string endpoint_url;
|
std::string endpoint_url;
|
||||||
std::string username;
|
std::string username;
|
||||||
std::string token;
|
std::string token;
|
||||||
|
std::future<Common::WebResult> future;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace WebService
|
} // namespace WebService
|
||||||
|
|
Loading…
Reference in a new issue