summaryrefslogtreecommitdiff
path: root/src/web_service
diff options
context:
space:
mode:
authorGravatar FearlessTobi2021-12-25 20:27:52 +0100
committerGravatar FearlessTobi2022-07-25 21:59:28 +0200
commit705f7db84dd85555a6aef1e136cf251725cef293 (patch)
treee110c6482a11d711d18515afce4fc50adcee76e7 /src/web_service
parentnetwork: Add initial files and enet dependency (diff)
downloadyuzu-705f7db84dd85555a6aef1e136cf251725cef293.tar.gz
yuzu-705f7db84dd85555a6aef1e136cf251725cef293.tar.xz
yuzu-705f7db84dd85555a6aef1e136cf251725cef293.zip
yuzu: Add ui files for multiplayer rooms
Diffstat (limited to 'src/web_service')
-rw-r--r--src/web_service/CMakeLists.txt4
-rw-r--r--src/web_service/announce_room_json.cpp157
-rw-r--r--src/web_service/announce_room_json.h46
3 files changed, 206 insertions, 1 deletions
diff --git a/src/web_service/CMakeLists.txt b/src/web_service/CMakeLists.txt
index ae85a72ea..aff81f26d 100644
--- a/src/web_service/CMakeLists.txt
+++ b/src/web_service/CMakeLists.txt
@@ -1,4 +1,6 @@
1add_library(web_service STATIC 1add_library(web_service STATIC
2 announce_room_json.cpp
3 announce_room_json.h
2 telemetry_json.cpp 4 telemetry_json.cpp
3 telemetry_json.h 5 telemetry_json.h
4 verify_login.cpp 6 verify_login.cpp
@@ -9,4 +11,4 @@ add_library(web_service STATIC
9) 11)
10 12
11create_target_directory_groups(web_service) 13create_target_directory_groups(web_service)
12target_link_libraries(web_service PRIVATE common nlohmann_json::nlohmann_json httplib) 14target_link_libraries(web_service PRIVATE common network nlohmann_json::nlohmann_json httplib)
diff --git a/src/web_service/announce_room_json.cpp b/src/web_service/announce_room_json.cpp
new file mode 100644
index 000000000..31804d2b1
--- /dev/null
+++ b/src/web_service/announce_room_json.cpp
@@ -0,0 +1,157 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <future>
6#include <nlohmann/json.hpp>
7#include "common/detached_tasks.h"
8#include "common/logging/log.h"
9#include "web_service/announce_room_json.h"
10#include "web_service/web_backend.h"
11
12namespace AnnounceMultiplayerRoom {
13
14void to_json(nlohmann::json& json, const Room::Member& member) {
15 if (!member.username.empty()) {
16 json["username"] = member.username;
17 }
18 json["nickname"] = member.nickname;
19 if (!member.avatar_url.empty()) {
20 json["avatarUrl"] = member.avatar_url;
21 }
22 json["gameName"] = member.game_name;
23 json["gameId"] = member.game_id;
24}
25
26void from_json(const nlohmann::json& json, Room::Member& member) {
27 member.nickname = json.at("nickname").get<std::string>();
28 member.game_name = json.at("gameName").get<std::string>();
29 member.game_id = json.at("gameId").get<u64>();
30 try {
31 member.username = json.at("username").get<std::string>();
32 member.avatar_url = json.at("avatarUrl").get<std::string>();
33 } catch (const nlohmann::detail::out_of_range&) {
34 member.username = member.avatar_url = "";
35 LOG_DEBUG(Network, "Member \'{}\' isn't authenticated", member.nickname);
36 }
37}
38
39void to_json(nlohmann::json& json, const Room& room) {
40 json["port"] = room.port;
41 json["name"] = room.name;
42 if (!room.description.empty()) {
43 json["description"] = room.description;
44 }
45 json["preferredGameName"] = room.preferred_game;
46 json["preferredGameId"] = room.preferred_game_id;
47 json["maxPlayers"] = room.max_player;
48 json["netVersion"] = room.net_version;
49 json["hasPassword"] = room.has_password;
50 if (room.members.size() > 0) {
51 nlohmann::json member_json = room.members;
52 json["players"] = member_json;
53 }
54}
55
56void from_json(const nlohmann::json& json, Room& room) {
57 room.verify_UID = json.at("externalGuid").get<std::string>();
58 room.ip = json.at("address").get<std::string>();
59 room.name = json.at("name").get<std::string>();
60 try {
61 room.description = json.at("description").get<std::string>();
62 } catch (const nlohmann::detail::out_of_range&) {
63 room.description = "";
64 LOG_DEBUG(Network, "Room \'{}\' doesn't contain a description", room.name);
65 }
66 room.owner = json.at("owner").get<std::string>();
67 room.port = json.at("port").get<u16>();
68 room.preferred_game = json.at("preferredGameName").get<std::string>();
69 room.preferred_game_id = json.at("preferredGameId").get<u64>();
70 room.max_player = json.at("maxPlayers").get<u32>();
71 room.net_version = json.at("netVersion").get<u32>();
72 room.has_password = json.at("hasPassword").get<bool>();
73 try {
74 room.members = json.at("players").get<std::vector<Room::Member>>();
75 } catch (const nlohmann::detail::out_of_range& e) {
76 LOG_DEBUG(Network, "Out of range {}", e.what());
77 }
78}
79
80} // namespace AnnounceMultiplayerRoom
81
82namespace WebService {
83
84void RoomJson::SetRoomInformation(const std::string& name, const std::string& description,
85 const u16 port, const u32 max_player, const u32 net_version,
86 const bool has_password, const std::string& preferred_game,
87 const u64 preferred_game_id) {
88 room.name = name;
89 room.description = description;
90 room.port = port;
91 room.max_player = max_player;
92 room.net_version = net_version;
93 room.has_password = has_password;
94 room.preferred_game = preferred_game;
95 room.preferred_game_id = preferred_game_id;
96}
97void RoomJson::AddPlayer(const std::string& username_, const std::string& nickname_,
98 const std::string& avatar_url,
99 const AnnounceMultiplayerRoom::MacAddress& mac_address, const u64 game_id,
100 const std::string& game_name) {
101 AnnounceMultiplayerRoom::Room::Member member;
102 member.username = username_;
103 member.nickname = nickname_;
104 member.avatar_url = avatar_url;
105 member.mac_address = mac_address;
106 member.game_id = game_id;
107 member.game_name = game_name;
108 room.members.push_back(member);
109}
110
111WebService::WebResult RoomJson::Update() {
112 if (room_id.empty()) {
113 LOG_ERROR(WebService, "Room must be registered to be updated");
114 return WebService::WebResult{WebService::WebResult::Code::LibError,
115 "Room is not registered"};
116 }
117 nlohmann::json json{{"players", room.members}};
118 return client.PostJson(fmt::format("/lobby/{}", room_id), json.dump(), false);
119}
120
121WebService::WebResult RoomJson::Register() {
122 nlohmann::json json = room;
123 auto result = client.PostJson("/lobby", json.dump(), false);
124 if (result.result_code != WebService::WebResult::Code::Success) {
125 return result;
126 }
127 auto reply_json = nlohmann::json::parse(result.returned_data);
128 room = reply_json.get<AnnounceMultiplayerRoom::Room>();
129 room_id = reply_json.at("id").get<std::string>();
130 return WebService::WebResult{WebService::WebResult::Code::Success, "", room.verify_UID};
131}
132
133void RoomJson::ClearPlayers() {
134 room.members.clear();
135}
136
137AnnounceMultiplayerRoom::RoomList RoomJson::GetRoomList() {
138 auto reply = client.GetJson("/lobby", true).returned_data;
139 if (reply.empty()) {
140 return {};
141 }
142 return nlohmann::json::parse(reply).at("rooms").get<AnnounceMultiplayerRoom::RoomList>();
143}
144
145void RoomJson::Delete() {
146 if (room_id.empty()) {
147 LOG_ERROR(WebService, "Room must be registered to be deleted");
148 return;
149 }
150 Common::DetachedTasks::AddTask(
151 [host{this->host}, username{this->username}, token{this->token}, room_id{this->room_id}]() {
152 // create a new client here because the this->client might be destroyed.
153 Client{host, username, token}.DeleteJson(fmt::format("/lobby/{}", room_id), "", false);
154 });
155}
156
157} // namespace WebService
diff --git a/src/web_service/announce_room_json.h b/src/web_service/announce_room_json.h
new file mode 100644
index 000000000..ac02af5b1
--- /dev/null
+++ b/src/web_service/announce_room_json.h
@@ -0,0 +1,46 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <functional>
8#include <string>
9#include "common/announce_multiplayer_room.h"
10#include "web_service/web_backend.h"
11
12namespace WebService {
13
14/**
15 * Implementation of AnnounceMultiplayerRoom::Backend that (de)serializes room information into/from
16 * JSON, and submits/gets it to/from the yuzu web service
17 */
18class RoomJson : public AnnounceMultiplayerRoom::Backend {
19public:
20 RoomJson(const std::string& host, const std::string& username, const std::string& token)
21 : client(host, username, token), host(host), username(username), token(token) {}
22 ~RoomJson() = default;
23 void SetRoomInformation(const std::string& name, const std::string& description, const u16 port,
24 const u32 max_player, const u32 net_version, const bool has_password,
25 const std::string& preferred_game,
26 const u64 preferred_game_id) override;
27 void AddPlayer(const std::string& username_, const std::string& nickname_,
28 const std::string& avatar_url,
29 const AnnounceMultiplayerRoom::MacAddress& mac_address, const u64 game_id,
30 const std::string& game_name) override;
31 WebResult Update() override;
32 WebResult Register() override;
33 void ClearPlayers() override;
34 AnnounceMultiplayerRoom::RoomList GetRoomList() override;
35 void Delete() override;
36
37private:
38 AnnounceMultiplayerRoom::Room room;
39 Client client;
40 std::string host;
41 std::string username;
42 std::string token;
43 std::string room_id;
44};
45
46} // namespace WebService