summaryrefslogtreecommitdiff
path: root/src/web_service/announce_room_json.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/web_service/announce_room_json.cpp')
-rw-r--r--src/web_service/announce_room_json.cpp157
1 files changed, 157 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..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