2022-07-25 16:18:30 +01:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-12-25 19:27:52 +00:00
|
|
|
|
|
|
|
#include <future>
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include "common/detached_tasks.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "web_service/announce_room_json.h"
|
|
|
|
#include "web_service/web_backend.h"
|
|
|
|
|
|
|
|
namespace AnnounceMultiplayerRoom {
|
|
|
|
|
2022-07-15 20:11:09 +01:00
|
|
|
static void to_json(nlohmann::json& json, const Member& member) {
|
2021-12-25 19:27:52 +00:00
|
|
|
if (!member.username.empty()) {
|
|
|
|
json["username"] = member.username;
|
|
|
|
}
|
|
|
|
json["nickname"] = member.nickname;
|
|
|
|
if (!member.avatar_url.empty()) {
|
|
|
|
json["avatarUrl"] = member.avatar_url;
|
|
|
|
}
|
2022-07-18 04:53:44 +01:00
|
|
|
json["gameName"] = member.game.name;
|
|
|
|
json["gameId"] = member.game.id;
|
2021-12-25 19:27:52 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 20:11:09 +01:00
|
|
|
static void from_json(const nlohmann::json& json, Member& member) {
|
2021-12-25 19:27:52 +00:00
|
|
|
member.nickname = json.at("nickname").get<std::string>();
|
2022-07-18 04:53:44 +01:00
|
|
|
member.game.name = json.at("gameName").get<std::string>();
|
|
|
|
member.game.id = json.at("gameId").get<u64>();
|
2021-12-25 19:27:52 +00:00
|
|
|
try {
|
|
|
|
member.username = json.at("username").get<std::string>();
|
|
|
|
member.avatar_url = json.at("avatarUrl").get<std::string>();
|
|
|
|
} catch (const nlohmann::detail::out_of_range&) {
|
|
|
|
member.username = member.avatar_url = "";
|
|
|
|
LOG_DEBUG(Network, "Member \'{}\' isn't authenticated", member.nickname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 19:40:57 +01:00
|
|
|
static void to_json(nlohmann::json& json, const Room& room) {
|
2022-07-15 20:11:09 +01:00
|
|
|
json["port"] = room.information.port;
|
|
|
|
json["name"] = room.information.name;
|
|
|
|
if (!room.information.description.empty()) {
|
|
|
|
json["description"] = room.information.description;
|
2021-12-25 19:27:52 +00:00
|
|
|
}
|
2022-07-18 04:53:44 +01:00
|
|
|
json["preferredGameName"] = room.information.preferred_game.name;
|
|
|
|
json["preferredGameId"] = room.information.preferred_game.id;
|
2022-07-15 20:11:09 +01:00
|
|
|
json["maxPlayers"] = room.information.member_slots;
|
2021-12-25 19:27:52 +00:00
|
|
|
json["netVersion"] = room.net_version;
|
|
|
|
json["hasPassword"] = room.has_password;
|
|
|
|
if (room.members.size() > 0) {
|
|
|
|
nlohmann::json member_json = room.members;
|
|
|
|
json["players"] = member_json;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 19:40:57 +01:00
|
|
|
static void from_json(const nlohmann::json& json, Room& room) {
|
2022-07-25 16:08:20 +01:00
|
|
|
room.verify_uid = json.at("externalGuid").get<std::string>();
|
2021-12-25 19:27:52 +00:00
|
|
|
room.ip = json.at("address").get<std::string>();
|
2022-07-15 20:11:09 +01:00
|
|
|
room.information.name = json.at("name").get<std::string>();
|
2021-12-25 19:27:52 +00:00
|
|
|
try {
|
2022-07-15 20:11:09 +01:00
|
|
|
room.information.description = json.at("description").get<std::string>();
|
2021-12-25 19:27:52 +00:00
|
|
|
} catch (const nlohmann::detail::out_of_range&) {
|
2022-07-15 20:11:09 +01:00
|
|
|
room.information.description = "";
|
|
|
|
LOG_DEBUG(Network, "Room \'{}\' doesn't contain a description", room.information.name);
|
2021-12-25 19:27:52 +00:00
|
|
|
}
|
2022-07-15 20:11:09 +01:00
|
|
|
room.information.host_username = json.at("owner").get<std::string>();
|
|
|
|
room.information.port = json.at("port").get<u16>();
|
2022-07-18 04:53:44 +01:00
|
|
|
room.information.preferred_game.name = json.at("preferredGameName").get<std::string>();
|
|
|
|
room.information.preferred_game.id = json.at("preferredGameId").get<u64>();
|
2022-07-15 20:11:09 +01:00
|
|
|
room.information.member_slots = json.at("maxPlayers").get<u32>();
|
2021-12-25 19:27:52 +00:00
|
|
|
room.net_version = json.at("netVersion").get<u32>();
|
|
|
|
room.has_password = json.at("hasPassword").get<bool>();
|
|
|
|
try {
|
2022-07-15 20:11:09 +01:00
|
|
|
room.members = json.at("players").get<std::vector<Member>>();
|
2021-12-25 19:27:52 +00:00
|
|
|
} catch (const nlohmann::detail::out_of_range& e) {
|
|
|
|
LOG_DEBUG(Network, "Out of range {}", e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace AnnounceMultiplayerRoom
|
|
|
|
|
|
|
|
namespace WebService {
|
|
|
|
|
|
|
|
void RoomJson::SetRoomInformation(const std::string& name, const std::string& description,
|
|
|
|
const u16 port, const u32 max_player, const u32 net_version,
|
2022-07-18 04:53:44 +01:00
|
|
|
const bool has_password,
|
|
|
|
const AnnounceMultiplayerRoom::GameInfo& preferred_game) {
|
2022-07-15 20:11:09 +01:00
|
|
|
room.information.name = name;
|
|
|
|
room.information.description = description;
|
|
|
|
room.information.port = port;
|
|
|
|
room.information.member_slots = max_player;
|
2021-12-25 19:27:52 +00:00
|
|
|
room.net_version = net_version;
|
|
|
|
room.has_password = has_password;
|
2022-07-15 20:11:09 +01:00
|
|
|
room.information.preferred_game = preferred_game;
|
2021-12-25 19:27:52 +00:00
|
|
|
}
|
2022-07-15 20:11:09 +01:00
|
|
|
void RoomJson::AddPlayer(const AnnounceMultiplayerRoom::Member& member) {
|
2021-12-25 19:27:52 +00:00
|
|
|
room.members.push_back(member);
|
|
|
|
}
|
|
|
|
|
|
|
|
WebService::WebResult RoomJson::Update() {
|
|
|
|
if (room_id.empty()) {
|
|
|
|
LOG_ERROR(WebService, "Room must be registered to be updated");
|
|
|
|
return WebService::WebResult{WebService::WebResult::Code::LibError,
|
2022-07-08 03:23:16 +01:00
|
|
|
"Room is not registered", ""};
|
2021-12-25 19:27:52 +00:00
|
|
|
}
|
|
|
|
nlohmann::json json{{"players", room.members}};
|
|
|
|
return client.PostJson(fmt::format("/lobby/{}", room_id), json.dump(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
WebService::WebResult RoomJson::Register() {
|
|
|
|
nlohmann::json json = room;
|
|
|
|
auto result = client.PostJson("/lobby", json.dump(), false);
|
|
|
|
if (result.result_code != WebService::WebResult::Code::Success) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
auto reply_json = nlohmann::json::parse(result.returned_data);
|
|
|
|
room = reply_json.get<AnnounceMultiplayerRoom::Room>();
|
|
|
|
room_id = reply_json.at("id").get<std::string>();
|
2022-07-25 16:08:20 +01:00
|
|
|
return WebService::WebResult{WebService::WebResult::Code::Success, "", room.verify_uid};
|
2021-12-25 19:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RoomJson::ClearPlayers() {
|
|
|
|
room.members.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
AnnounceMultiplayerRoom::RoomList RoomJson::GetRoomList() {
|
|
|
|
auto reply = client.GetJson("/lobby", true).returned_data;
|
|
|
|
if (reply.empty()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return nlohmann::json::parse(reply).at("rooms").get<AnnounceMultiplayerRoom::RoomList>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RoomJson::Delete() {
|
|
|
|
if (room_id.empty()) {
|
|
|
|
LOG_ERROR(WebService, "Room must be registered to be deleted");
|
|
|
|
return;
|
|
|
|
}
|
2023-07-19 00:31:35 +01:00
|
|
|
Common::DetachedTasks::AddTask([host_{this->host}, username_{this->username},
|
|
|
|
token_{this->token}, room_id_{this->room_id}]() {
|
|
|
|
// create a new client here because the this->client might be destroyed.
|
|
|
|
Client{host_, username_, token_}.DeleteJson(fmt::format("/lobby/{}", room_id_), "", false);
|
|
|
|
});
|
2021-12-25 19:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace WebService
|