announce_room_json.cpp 5.7 KB

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