2017-10-31 09:02:42 +00:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <future>
|
2018-10-13 11:33:04 +00:00
|
|
|
#include <json.hpp>
|
2018-09-12 16:22:48 +00:00
|
|
|
#include "common/detached_tasks.h"
|
2017-10-31 09:02:42 +00:00
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "web_service/announce_room_json.h"
|
|
|
|
#include "web_service/web_backend.h"
|
|
|
|
|
|
|
|
namespace AnnounceMultiplayerRoom {
|
|
|
|
|
|
|
|
void to_json(nlohmann::json& json, const Room::Member& member) {
|
2018-10-27 07:40:15 +00:00
|
|
|
if (!member.username.empty()) {
|
|
|
|
json["username"] = member.username;
|
|
|
|
}
|
|
|
|
json["nickname"] = member.nickname;
|
|
|
|
if (!member.avatar_url.empty()) {
|
|
|
|
json["avatarUrl"] = member.avatar_url;
|
|
|
|
}
|
2017-10-31 09:02:42 +00:00
|
|
|
json["gameName"] = member.game_name;
|
|
|
|
json["gameId"] = member.game_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void from_json(const nlohmann::json& json, Room::Member& member) {
|
2018-10-27 07:40:15 +00:00
|
|
|
member.nickname = json.at("nickname").get<std::string>();
|
2017-10-31 09:02:42 +00:00
|
|
|
member.game_name = json.at("gameName").get<std::string>();
|
|
|
|
member.game_id = json.at("gameId").get<u64>();
|
2018-10-27 07:40:15 +00:00
|
|
|
try {
|
|
|
|
member.username = json.at("username").get<std::string>();
|
|
|
|
member.avatar_url = json.at("avatarUrl").get<std::string>();
|
2020-07-23 01:51:39 +00:00
|
|
|
} catch (const nlohmann::detail::out_of_range&) {
|
2018-10-27 07:40:15 +00:00
|
|
|
member.username = member.avatar_url = "";
|
|
|
|
LOG_DEBUG(Network, "Member \'{}\' isn't authenticated", member.nickname);
|
|
|
|
}
|
2017-10-31 09:02:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void to_json(nlohmann::json& json, const Room& room) {
|
|
|
|
json["port"] = room.port;
|
|
|
|
json["name"] = room.name;
|
2018-10-27 07:40:15 +00:00
|
|
|
if (!room.description.empty()) {
|
|
|
|
json["description"] = room.description;
|
|
|
|
}
|
2017-10-31 09:02:42 +00:00
|
|
|
json["preferredGameName"] = room.preferred_game;
|
|
|
|
json["preferredGameId"] = room.preferred_game_id;
|
|
|
|
json["maxPlayers"] = room.max_player;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void from_json(const nlohmann::json& json, Room& room) {
|
2018-10-27 07:40:15 +00:00
|
|
|
room.verify_UID = json.at("externalGuid").get<std::string>();
|
2017-10-31 09:02:42 +00:00
|
|
|
room.ip = json.at("address").get<std::string>();
|
|
|
|
room.name = json.at("name").get<std::string>();
|
2018-04-30 07:40:51 +00:00
|
|
|
try {
|
|
|
|
room.description = json.at("description").get<std::string>();
|
2020-07-23 01:51:39 +00:00
|
|
|
} catch (const nlohmann::detail::out_of_range&) {
|
2018-04-30 07:40:51 +00:00
|
|
|
room.description = "";
|
|
|
|
LOG_DEBUG(Network, "Room \'{}\' doesn't contain a description", room.name);
|
|
|
|
}
|
2017-10-31 09:02:42 +00:00
|
|
|
room.owner = json.at("owner").get<std::string>();
|
|
|
|
room.port = json.at("port").get<u16>();
|
|
|
|
room.preferred_game = json.at("preferredGameName").get<std::string>();
|
|
|
|
room.preferred_game_id = json.at("preferredGameId").get<u64>();
|
|
|
|
room.max_player = json.at("maxPlayers").get<u32>();
|
|
|
|
room.net_version = json.at("netVersion").get<u32>();
|
|
|
|
room.has_password = json.at("hasPassword").get<bool>();
|
|
|
|
try {
|
|
|
|
room.members = json.at("players").get<std::vector<Room::Member>>();
|
|
|
|
} catch (const nlohmann::detail::out_of_range& e) {
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_DEBUG(Network, "Out of range {}", e.what());
|
2017-10-31 09:02:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace AnnounceMultiplayerRoom
|
|
|
|
|
|
|
|
namespace WebService {
|
|
|
|
|
2018-10-27 07:40:15 +00:00
|
|
|
void RoomJson::SetRoomInformation(const std::string& name, const std::string& description,
|
|
|
|
const u16 port, const u32 max_player, const u32 net_version,
|
2017-10-31 09:02:42 +00:00
|
|
|
const bool has_password, const std::string& preferred_game,
|
|
|
|
const u64 preferred_game_id) {
|
|
|
|
room.name = name;
|
2018-04-30 07:40:51 +00:00
|
|
|
room.description = description;
|
2017-10-31 09:02:42 +00:00
|
|
|
room.port = port;
|
|
|
|
room.max_player = max_player;
|
|
|
|
room.net_version = net_version;
|
|
|
|
room.has_password = has_password;
|
|
|
|
room.preferred_game = preferred_game;
|
|
|
|
room.preferred_game_id = preferred_game_id;
|
|
|
|
}
|
2018-10-27 07:40:15 +00:00
|
|
|
void RoomJson::AddPlayer(const std::string& username, const std::string& nickname,
|
|
|
|
const std::string& avatar_url,
|
2017-10-31 09:02:42 +00:00
|
|
|
const AnnounceMultiplayerRoom::MacAddress& mac_address, const u64 game_id,
|
|
|
|
const std::string& game_name) {
|
|
|
|
AnnounceMultiplayerRoom::Room::Member member;
|
2018-10-27 07:40:15 +00:00
|
|
|
member.username = username;
|
|
|
|
member.nickname = nickname;
|
|
|
|
member.avatar_url = avatar_url;
|
2017-10-31 09:02:42 +00:00
|
|
|
member.mac_address = mac_address;
|
|
|
|
member.game_id = game_id;
|
|
|
|
member.game_name = game_name;
|
|
|
|
room.members.push_back(member);
|
|
|
|
}
|
|
|
|
|
2018-10-27 07:40:15 +00:00
|
|
|
Common::WebResult RoomJson::Update() {
|
|
|
|
if (room_id.empty()) {
|
|
|
|
LOG_ERROR(WebService, "Room must be registered to be updated");
|
|
|
|
return Common::WebResult{Common::WebResult::Code::LibError, "Room is not registered"};
|
|
|
|
}
|
|
|
|
nlohmann::json json{{"players", room.members}};
|
2018-12-16 02:48:15 +00:00
|
|
|
return client.PostJson(fmt::format("/lobby/{}", room_id), json.dump(), false);
|
2018-10-27 07:40:15 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 14:55:49 +00:00
|
|
|
Common::WebResult RoomJson::Register() {
|
2017-10-31 09:02:42 +00:00
|
|
|
nlohmann::json json = room;
|
2019-04-19 14:55:49 +00:00
|
|
|
auto result = client.PostJson("/lobby", json.dump(), false);
|
|
|
|
if (result.result_code != Common::WebResult::Code::Success) {
|
|
|
|
return result;
|
2018-10-27 07:40:15 +00:00
|
|
|
}
|
2019-04-19 14:55:49 +00:00
|
|
|
auto reply_json = nlohmann::json::parse(result.returned_data);
|
2018-10-27 07:40:15 +00:00
|
|
|
room = reply_json.get<AnnounceMultiplayerRoom::Room>();
|
|
|
|
room_id = reply_json.at("id").get<std::string>();
|
2019-04-19 14:55:49 +00:00
|
|
|
return Common::WebResult{Common::WebResult::Code::Success, "", room.verify_UID};
|
2017-10-31 09:02:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RoomJson::ClearPlayers() {
|
|
|
|
room.members.clear();
|
|
|
|
}
|
|
|
|
|
2018-09-12 16:22:48 +00:00
|
|
|
AnnounceMultiplayerRoom::RoomList RoomJson::GetRoomList() {
|
2018-12-16 02:48:15 +00:00
|
|
|
auto reply = client.GetJson("/lobby", true).returned_data;
|
2018-09-12 16:22:48 +00:00
|
|
|
if (reply.empty()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return nlohmann::json::parse(reply).at("rooms").get<AnnounceMultiplayerRoom::RoomList>();
|
2017-10-31 09:02:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RoomJson::Delete() {
|
2018-10-27 07:40:15 +00:00
|
|
|
if (room_id.empty()) {
|
|
|
|
LOG_ERROR(WebService, "Room must be registered to be deleted");
|
|
|
|
return;
|
|
|
|
}
|
2018-09-12 16:22:48 +00:00
|
|
|
Common::DetachedTasks::AddTask(
|
2018-10-27 07:40:15 +00:00
|
|
|
[host{this->host}, username{this->username}, token{this->token}, room_id{this->room_id}]() {
|
2018-09-12 16:22:48 +00:00
|
|
|
// create a new client here because the this->client might be destroyed.
|
2018-12-16 02:48:15 +00:00
|
|
|
Client{host, username, token}.DeleteJson(fmt::format("/lobby/{}", room_id), "", false);
|
2018-09-12 16:22:48 +00:00
|
|
|
});
|
2017-10-31 09:02:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace WebService
|