announce_multiplayer_room.h 4.8 KB

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