announce_room_json.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <future>
  4. #include <nlohmann/json.hpp>
  5. #include "common/detached_tasks.h"
  6. #include "common/logging/log.h"
  7. #include "web_service/announce_room_json.h"
  8. #include "web_service/web_backend.h"
  9. namespace AnnounceMultiplayerRoom {
  10. static void to_json(nlohmann::json& json, const Member& member) {
  11. if (!member.username.empty()) {
  12. json["username"] = member.username;
  13. }
  14. json["nickname"] = member.nickname;
  15. if (!member.avatar_url.empty()) {
  16. json["avatarUrl"] = member.avatar_url;
  17. }
  18. json["gameName"] = member.game.name;
  19. json["gameId"] = member.game.id;
  20. }
  21. static void from_json(const nlohmann::json& json, Member& member) {
  22. member.nickname = json.at("nickname").get<std::string>();
  23. member.game.name = json.at("gameName").get<std::string>();
  24. member.game.id = json.at("gameId").get<u64>();
  25. try {
  26. member.username = json.at("username").get<std::string>();
  27. member.avatar_url = json.at("avatarUrl").get<std::string>();
  28. } catch (const nlohmann::detail::out_of_range&) {
  29. member.username = member.avatar_url = "";
  30. LOG_DEBUG(Network, "Member \'{}\' isn't authenticated", member.nickname);
  31. }
  32. }
  33. static void to_json(nlohmann::json& json, const Room& room) {
  34. json["port"] = room.information.port;
  35. json["name"] = room.information.name;
  36. if (!room.information.description.empty()) {
  37. json["description"] = room.information.description;
  38. }
  39. json["preferredGameName"] = room.information.preferred_game.name;
  40. json["preferredGameId"] = room.information.preferred_game.id;
  41. json["maxPlayers"] = room.information.member_slots;
  42. json["netVersion"] = room.net_version;
  43. json["hasPassword"] = room.has_password;
  44. if (room.members.size() > 0) {
  45. nlohmann::json member_json = room.members;
  46. json["players"] = member_json;
  47. }
  48. }
  49. static void from_json(const nlohmann::json& json, Room& room) {
  50. room.verify_uid = json.at("externalGuid").get<std::string>();
  51. room.ip = json.at("address").get<std::string>();
  52. room.information.name = json.at("name").get<std::string>();
  53. try {
  54. room.information.description = json.at("description").get<std::string>();
  55. } catch (const nlohmann::detail::out_of_range&) {
  56. room.information.description = "";
  57. LOG_DEBUG(Network, "Room \'{}\' doesn't contain a description", room.information.name);
  58. }
  59. room.information.host_username = json.at("owner").get<std::string>();
  60. room.information.port = json.at("port").get<u16>();
  61. room.information.preferred_game.name = json.at("preferredGameName").get<std::string>();
  62. room.information.preferred_game.id = json.at("preferredGameId").get<u64>();
  63. room.information.member_slots = json.at("maxPlayers").get<u32>();
  64. room.net_version = json.at("netVersion").get<u32>();
  65. room.has_password = json.at("hasPassword").get<bool>();
  66. try {
  67. room.members = json.at("players").get<std::vector<Member>>();
  68. } catch (const nlohmann::detail::out_of_range& e) {
  69. LOG_DEBUG(Network, "Out of range {}", e.what());
  70. }
  71. }
  72. } // namespace AnnounceMultiplayerRoom
  73. namespace WebService {
  74. void RoomJson::SetRoomInformation(const std::string& name, const std::string& description,
  75. const u16 port, const u32 max_player, const u32 net_version,
  76. const bool has_password,
  77. const AnnounceMultiplayerRoom::GameInfo& preferred_game) {
  78. room.information.name = name;
  79. room.information.description = description;
  80. room.information.port = port;
  81. room.information.member_slots = max_player;
  82. room.net_version = net_version;
  83. room.has_password = has_password;
  84. room.information.preferred_game = preferred_game;
  85. }
  86. void RoomJson::AddPlayer(const AnnounceMultiplayerRoom::Member& member) {
  87. room.members.push_back(member);
  88. }
  89. WebService::WebResult RoomJson::Update() {
  90. if (room_id.empty()) {
  91. LOG_ERROR(WebService, "Room must be registered to be updated");
  92. return WebService::WebResult{WebService::WebResult::Code::LibError,
  93. "Room is not registered", ""};
  94. }
  95. nlohmann::json json{{"players", room.members}};
  96. return client.PostJson(fmt::format("/lobby/{}", room_id), json.dump(), false);
  97. }
  98. WebService::WebResult RoomJson::Register() {
  99. nlohmann::json json = room;
  100. auto result = client.PostJson("/lobby", json.dump(), false);
  101. if (result.result_code != WebService::WebResult::Code::Success) {
  102. return result;
  103. }
  104. auto reply_json = nlohmann::json::parse(result.returned_data);
  105. room = reply_json.get<AnnounceMultiplayerRoom::Room>();
  106. room_id = reply_json.at("id").get<std::string>();
  107. return WebService::WebResult{WebService::WebResult::Code::Success, "", room.verify_uid};
  108. }
  109. void RoomJson::ClearPlayers() {
  110. room.members.clear();
  111. }
  112. AnnounceMultiplayerRoom::RoomList RoomJson::GetRoomList() {
  113. auto reply = client.GetJson("/lobby", true).returned_data;
  114. if (reply.empty()) {
  115. return {};
  116. }
  117. return nlohmann::json::parse(reply).at("rooms").get<AnnounceMultiplayerRoom::RoomList>();
  118. }
  119. void RoomJson::Delete() {
  120. if (room_id.empty()) {
  121. LOG_ERROR(WebService, "Room must be registered to be deleted");
  122. return;
  123. }
  124. Common::DetachedTasks::AddTask([host_{this->host}, username_{this->username},
  125. token_{this->token}, room_id_{this->room_id}]() {
  126. // create a new client here because the this->client might be destroyed.
  127. Client{host_, username_, token_}.DeleteJson(fmt::format("/lobby/{}", room_id_), "", false);
  128. });
  129. }
  130. } // namespace WebService