announce_multiplayer_room.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <functional>
  6. #include <string>
  7. #include <vector>
  8. #include "common/common_types.h"
  9. #include "common/socket_types.h"
  10. #include "web_service/web_result.h"
  11. namespace AnnounceMultiplayerRoom {
  12. struct GameInfo {
  13. std::string name{""};
  14. u64 id{0};
  15. std::string version{""};
  16. };
  17. struct Member {
  18. std::string username;
  19. std::string nickname;
  20. std::string display_name;
  21. std::string avatar_url;
  22. Network::IPv4Address fake_ip;
  23. GameInfo game;
  24. };
  25. struct RoomInformation {
  26. std::string name; ///< Name of the server
  27. std::string description; ///< Server description
  28. u32 member_slots; ///< Maximum number of members in this room
  29. u16 port; ///< The port of this room
  30. GameInfo preferred_game; ///< Game to advertise that you want to play
  31. std::string host_username; ///< Forum username of the host
  32. bool enable_yuzu_mods; ///< Allow yuzu Moderators to moderate on this room
  33. };
  34. struct Room {
  35. RoomInformation information;
  36. std::string id;
  37. std::string verify_uid; ///< UID used for verification
  38. std::string ip;
  39. u32 net_version;
  40. bool has_password;
  41. std::vector<Member> members;
  42. };
  43. using RoomList = std::vector<Room>;
  44. /**
  45. * A AnnounceMultiplayerRoom interface class. A backend to submit/get to/from a web service should
  46. * implement this interface.
  47. */
  48. class Backend {
  49. public:
  50. virtual ~Backend() = default;
  51. /**
  52. * Sets the Information that gets used for the announce
  53. * @param uid The Id of the room
  54. * @param name The name of the room
  55. * @param description The room description
  56. * @param port The port of the room
  57. * @param net_version The version of the libNetwork that gets used
  58. * @param has_password True if the room is passowrd protected
  59. * @param preferred_game The preferred game of the room
  60. * @param preferred_game_id The title id of the preferred game
  61. */
  62. virtual void SetRoomInformation(const std::string& name, const std::string& description,
  63. const u16 port, const u32 max_player, const u32 net_version,
  64. const bool has_password, const GameInfo& preferred_game) = 0;
  65. /**
  66. * Adds a player information to the data that gets announced
  67. * @param member The player to add
  68. */
  69. virtual void AddPlayer(const Member& member) = 0;
  70. /**
  71. * Updates the data in the announce service. Re-register the room when required.
  72. * @result The result of the update attempt
  73. */
  74. virtual WebService::WebResult Update() = 0;
  75. /**
  76. * Registers the data in the announce service
  77. * @result The result of the register attempt. When the result code is Success, A global Guid of
  78. * the room which may be used for verification will be in the result's returned_data.
  79. */
  80. virtual WebService::WebResult Register() = 0;
  81. /**
  82. * Empties the stored players
  83. */
  84. virtual void ClearPlayers() = 0;
  85. /**
  86. * Get the room information from the announce service
  87. * @result A list of all rooms the announce service has
  88. */
  89. virtual RoomList GetRoomList() = 0;
  90. /**
  91. * Sends a delete message to the announce service
  92. */
  93. virtual void Delete() = 0;
  94. };
  95. /**
  96. * Empty implementation of AnnounceMultiplayerRoom interface that drops all data. Used when a
  97. * functional backend implementation is not available.
  98. */
  99. class NullBackend : public Backend {
  100. public:
  101. ~NullBackend() = default;
  102. void SetRoomInformation(const std::string& /*name*/, const std::string& /*description*/,
  103. const u16 /*port*/, const u32 /*max_player*/, const u32 /*net_version*/,
  104. const bool /*has_password*/,
  105. const GameInfo& /*preferred_game*/) override {}
  106. void AddPlayer(const Member& /*member*/) override {}
  107. WebService::WebResult Update() override {
  108. return WebService::WebResult{WebService::WebResult::Code::NoWebservice,
  109. "WebService is missing", ""};
  110. }
  111. WebService::WebResult Register() override {
  112. return WebService::WebResult{WebService::WebResult::Code::NoWebservice,
  113. "WebService is missing", ""};
  114. }
  115. void ClearPlayers() override {}
  116. RoomList GetRoomList() override {
  117. return RoomList{};
  118. }
  119. void Delete() override {}
  120. };
  121. } // namespace AnnounceMultiplayerRoom