summaryrefslogtreecommitdiff
path: root/src/web_service/announce_room_json.cpp
diff options
context:
space:
mode:
authorGravatar liamwhite2022-07-25 18:31:45 -0400
committerGravatar GitHub2022-07-25 18:31:45 -0400
commit1e67d2b59f6dfd561768db3fb9a8e0c6a16ec9f2 (patch)
tree999411f1ca76390654d1034c6d0bd2c47c3f101c /src/web_service/announce_room_json.cpp
parentMerge pull request #8564 from lat9nq/dinner-fork (diff)
parentnetwork: Address review comments (diff)
downloadyuzu-1e67d2b59f6dfd561768db3fb9a8e0c6a16ec9f2.tar.gz
yuzu-1e67d2b59f6dfd561768db3fb9a8e0c6a16ec9f2.tar.xz
yuzu-1e67d2b59f6dfd561768db3fb9a8e0c6a16ec9f2.zip
Merge pull request #8541 from FearlessTobi/multiplayer-part1
yuzu, network: Add room service and UI configuration
Diffstat (limited to '')
-rw-r--r--src/web_service/announce_room_json.cpp145
1 files changed, 145 insertions, 0 deletions
diff --git a/src/web_service/announce_room_json.cpp b/src/web_service/announce_room_json.cpp
new file mode 100644
index 000000000..4c3195efd
--- /dev/null
+++ b/src/web_service/announce_room_json.cpp
@@ -0,0 +1,145 @@
1// SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#include <future>
5#include <nlohmann/json.hpp>
6#include "common/detached_tasks.h"
7#include "common/logging/log.h"
8#include "web_service/announce_room_json.h"
9#include "web_service/web_backend.h"
10
11namespace AnnounceMultiplayerRoom {
12
13static void to_json(nlohmann::json& json, const Member& member) {
14 if (!member.username.empty()) {
15 json["username"] = member.username;
16 }
17 json["nickname"] = member.nickname;
18 if (!member.avatar_url.empty()) {
19 json["avatarUrl"] = member.avatar_url;
20 }
21 json["gameName"] = member.game.name;
22 json["gameId"] = member.game.id;
23}
24
25static void from_json(const nlohmann::json& json, Member& member) {
26 member.nickname = json.at("nickname").get<std::string>();
27 member.game.name = json.at("gameName").get<std::string>();
28 member.game.id = json.at("gameId").get<u64>();
29 try {
30 member.username = json.at("username").get<std::string>();
31 member.avatar_url = json.at("avatarUrl").get<std::string>();
32 } catch (const nlohmann::detail::out_of_range&) {
33 member.username = member.avatar_url = "";
34 LOG_DEBUG(Network, "Member \'{}\' isn't authenticated", member.nickname);
35 }
36}
37
38static void to_json(nlohmann::json& json, const Room& room) {
39 json["port"] = room.information.port;
40 json["name"] = room.information.name;
41 if (!room.information.description.empty()) {
42 json["description"] = room.information.description;
43 }
44 json["preferredGameName"] = room.information.preferred_game.name;
45 json["preferredGameId"] = room.information.preferred_game.id;
46 json["maxPlayers"] = room.information.member_slots;
47 json["netVersion"] = room.net_version;
48 json["hasPassword"] = room.has_password;
49 if (room.members.size() > 0) {
50 nlohmann::json member_json = room.members;
51 json["players"] = member_json;
52 }
53}
54
55static void from_json(const nlohmann::json& json, Room& room) {
56 room.verify_uid = json.at("externalGuid").get<std::string>();
57 room.ip = json.at("address").get<std::string>();
58 room.information.name = json.at("name").get<std::string>();
59 try {
60 room.information.description = json.at("description").get<std::string>();
61 } catch (const nlohmann::detail::out_of_range&) {
62 room.information.description = "";
63 LOG_DEBUG(Network, "Room \'{}\' doesn't contain a description", room.information.name);
64 }
65 room.information.host_username = json.at("owner").get<std::string>();
66 room.information.port = json.at("port").get<u16>();
67 room.information.preferred_game.name = json.at("preferredGameName").get<std::string>();
68 room.information.preferred_game.id = json.at("preferredGameId").get<u64>();
69 room.information.member_slots = json.at("maxPlayers").get<u32>();
70 room.net_version = json.at("netVersion").get<u32>();
71 room.has_password = json.at("hasPassword").get<bool>();
72 try {
73 room.members = json.at("players").get<std::vector<Member>>();
74 } catch (const nlohmann::detail::out_of_range& e) {
75 LOG_DEBUG(Network, "Out of range {}", e.what());
76 }
77}
78
79} // namespace AnnounceMultiplayerRoom
80
81namespace WebService {
82
83void RoomJson::SetRoomInformation(const std::string& name, const std::string& description,
84 const u16 port, const u32 max_player, const u32 net_version,
85 const bool has_password,
86 const AnnounceMultiplayerRoom::GameInfo& preferred_game) {
87 room.information.name = name;
88 room.information.description = description;
89 room.information.port = port;
90 room.information.member_slots = max_player;
91 room.net_version = net_version;
92 room.has_password = has_password;
93 room.information.preferred_game = preferred_game;
94}
95void RoomJson::AddPlayer(const AnnounceMultiplayerRoom::Member& member) {
96 room.members.push_back(member);
97}
98
99WebService::WebResult RoomJson::Update() {
100 if (room_id.empty()) {
101 LOG_ERROR(WebService, "Room must be registered to be updated");
102 return WebService::WebResult{WebService::WebResult::Code::LibError,
103 "Room is not registered", ""};
104 }
105 nlohmann::json json{{"players", room.members}};
106 return client.PostJson(fmt::format("/lobby/{}", room_id), json.dump(), false);
107}
108
109WebService::WebResult RoomJson::Register() {
110 nlohmann::json json = room;
111 auto result = client.PostJson("/lobby", json.dump(), false);
112 if (result.result_code != WebService::WebResult::Code::Success) {
113 return result;
114 }
115 auto reply_json = nlohmann::json::parse(result.returned_data);
116 room = reply_json.get<AnnounceMultiplayerRoom::Room>();
117 room_id = reply_json.at("id").get<std::string>();
118 return WebService::WebResult{WebService::WebResult::Code::Success, "", room.verify_uid};
119}
120
121void RoomJson::ClearPlayers() {
122 room.members.clear();
123}
124
125AnnounceMultiplayerRoom::RoomList RoomJson::GetRoomList() {
126 auto reply = client.GetJson("/lobby", true).returned_data;
127 if (reply.empty()) {
128 return {};
129 }
130 return nlohmann::json::parse(reply).at("rooms").get<AnnounceMultiplayerRoom::RoomList>();
131}
132
133void RoomJson::Delete() {
134 if (room_id.empty()) {
135 LOG_ERROR(WebService, "Room must be registered to be deleted");
136 return;
137 }
138 Common::DetachedTasks::AddTask(
139 [host{this->host}, username{this->username}, token{this->token}, room_id{this->room_id}]() {
140 // create a new client here because the this->client might be destroyed.
141 Client{host, username, token}.DeleteJson(fmt::format("/lobby/{}", room_id), "", false);
142 });
143}
144
145} // namespace WebService