common: multiplayer: Use GameInfo type

This commit is contained in:
german77 2022-07-17 22:53:44 -05:00 committed by FearlessTobi
parent 4b404191cf
commit 899c8bb330
11 changed files with 60 additions and 62 deletions

View file

@ -15,14 +15,18 @@ namespace AnnounceMultiplayerRoom {
using MacAddress = std::array<u8, 6>;
struct GameInfo {
std::string name{""};
u64 id{0};
};
struct Member {
std::string username;
std::string nickname;
std::string display_name;
std::string avatar_url;
MacAddress mac_address;
std::string game_name;
u64 game_id;
GameInfo game;
};
struct RoomInformation {
@ -30,17 +34,11 @@ struct RoomInformation {
std::string description; ///< Server description
u32 member_slots; ///< Maximum number of members in this room
u16 port; ///< The port of this room
std::string preferred_game; ///< Game to advertise that you want to play
u64 preferred_game_id; ///< Title ID for the advertised game
GameInfo preferred_game; ///< Game to advertise that you want to play
std::string host_username; ///< Forum username of the host
bool enable_yuzu_mods; ///< Allow yuzu Moderators to moderate on this room
};
struct GameInfo {
std::string name{""};
u64 id{0};
};
struct Room {
RoomInformation information;
@ -75,8 +73,7 @@ public:
*/
virtual void SetRoomInformation(const std::string& name, const std::string& description,
const u16 port, const u32 max_player, const u32 net_version,
const bool has_password, const std::string& preferred_game,
const u64 preferred_game_id) = 0;
const bool has_password, const GameInfo& preferred_game) = 0;
/**
* Adds a player information to the data that gets announced
* @param nickname The nickname of the player
@ -125,8 +122,8 @@ public:
~NullBackend() = default;
void SetRoomInformation(const std::string& /*name*/, const std::string& /*description*/,
const u16 /*port*/, const u32 /*max_player*/, const u32 /*net_version*/,
const bool /*has_password*/, const std::string& /*preferred_game*/,
const u64 /*preferred_game_id*/) override {}
const bool /*has_password*/,
const GameInfo& /*preferred_game*/) override {}
void AddPlayer(const Member& /*member*/) override {}
WebService::WebResult Update() override {
return WebService::WebResult{WebService::WebResult::Code::NoWebservice,

View file

@ -89,10 +89,10 @@ AnnounceMultiplayerSession::~AnnounceMultiplayerSession() {
void AnnounceMultiplayerSession::UpdateBackendData(std::shared_ptr<Network::Room> room) {
Network::RoomInformation room_information = room->GetRoomInformation();
std::vector<AnnounceMultiplayerRoom::Member> memberlist = room->GetRoomMemberList();
backend->SetRoomInformation(
room_information.name, room_information.description, room_information.port,
room_information.member_slots, Network::network_version, room->HasPassword(),
room_information.preferred_game, room_information.preferred_game_id);
backend->SetRoomInformation(room_information.name, room_information.description,
room_information.port, room_information.member_slots,
Network::network_version, room->HasPassword(),
room_information.preferred_game);
backend->ClearPlayers();
for (const auto& member : memberlist) {
backend->AddPlayer(member);

View file

@ -811,7 +811,7 @@ void Room::RoomImpl::BroadcastRoomInformation() {
packet << room_information.description;
packet << room_information.member_slots;
packet << room_information.port;
packet << room_information.preferred_game;
packet << room_information.preferred_game.name;
packet << room_information.host_username;
packet << static_cast<u32>(members.size());
@ -1013,7 +1013,7 @@ Room::~Room() = default;
bool Room::Create(const std::string& name, const std::string& description,
const std::string& server_address, u16 server_port, const std::string& password,
const u32 max_connections, const std::string& host_username,
const std::string& preferred_game, u64 preferred_game_id,
const GameInfo preferred_game,
std::unique_ptr<VerifyUser::Backend> verify_backend,
const Room::BanList& ban_list, bool enable_yuzu_mods) {
ENetAddress address;
@ -1036,7 +1036,6 @@ bool Room::Create(const std::string& name, const std::string& description,
room_impl->room_information.member_slots = max_connections;
room_impl->room_information.port = server_port;
room_impl->room_information.preferred_game = preferred_game;
room_impl->room_information.preferred_game_id = preferred_game_id;
room_impl->room_information.host_username = host_username;
room_impl->room_information.enable_yuzu_mods = enable_yuzu_mods;
room_impl->password = password;
@ -1076,8 +1075,7 @@ std::vector<Member> Room::GetRoomMemberList() const {
member.display_name = member_impl.user_data.display_name;
member.avatar_url = member_impl.user_data.avatar_url;
member.mac_address = member_impl.mac_address;
member.game_name = member_impl.game_info.name;
member.game_id = member_impl.game_info.id;
member.game = member_impl.game_info;
member_list.push_back(member);
}
return member_list;

View file

@ -125,8 +125,7 @@ public:
const std::string& server = "", u16 server_port = DefaultRoomPort,
const std::string& password = "",
const u32 max_connections = MaxConcurrentConnections,
const std::string& host_username = "", const std::string& preferred_game = "",
u64 preferred_game_id = 0,
const std::string& host_username = "", const GameInfo = {},
std::unique_ptr<VerifyUser::Backend> verify_backend = nullptr,
const BanList& ban_list = {}, bool enable_yuzu_mods = false);

View file

@ -303,7 +303,7 @@ void RoomMember::RoomMemberImpl::HandleRoomInformationPacket(const ENetEvent* ev
packet >> info.description;
packet >> info.member_slots;
packet >> info.port;
packet >> info.preferred_game;
packet >> info.preferred_game.name;
packet >> info.host_username;
room_information.name = info.name;
room_information.description = info.description;

View file

@ -19,14 +19,14 @@ static void to_json(nlohmann::json& json, const Member& member) {
if (!member.avatar_url.empty()) {
json["avatarUrl"] = member.avatar_url;
}
json["gameName"] = member.game_name;
json["gameId"] = member.game_id;
json["gameName"] = member.game.name;
json["gameId"] = member.game.id;
}
static void from_json(const nlohmann::json& json, Member& member) {
member.nickname = json.at("nickname").get<std::string>();
member.game_name = json.at("gameName").get<std::string>();
member.game_id = json.at("gameId").get<u64>();
member.game.name = json.at("gameName").get<std::string>();
member.game.id = json.at("gameId").get<u64>();
try {
member.username = json.at("username").get<std::string>();
member.avatar_url = json.at("avatarUrl").get<std::string>();
@ -42,8 +42,8 @@ static void to_json(nlohmann::json& json, const Room& room) {
if (!room.information.description.empty()) {
json["description"] = room.information.description;
}
json["preferredGameName"] = room.information.preferred_game;
json["preferredGameId"] = room.information.preferred_game_id;
json["preferredGameName"] = room.information.preferred_game.name;
json["preferredGameId"] = room.information.preferred_game.id;
json["maxPlayers"] = room.information.member_slots;
json["netVersion"] = room.net_version;
json["hasPassword"] = room.has_password;
@ -65,8 +65,8 @@ static void from_json(const nlohmann::json& json, Room& room) {
}
room.information.host_username = json.at("owner").get<std::string>();
room.information.port = json.at("port").get<u16>();
room.information.preferred_game = json.at("preferredGameName").get<std::string>();
room.information.preferred_game_id = json.at("preferredGameId").get<u64>();
room.information.preferred_game.name = json.at("preferredGameName").get<std::string>();
room.information.preferred_game.id = json.at("preferredGameId").get<u64>();
room.information.member_slots = json.at("maxPlayers").get<u32>();
room.net_version = json.at("netVersion").get<u32>();
room.has_password = json.at("hasPassword").get<bool>();
@ -83,8 +83,8 @@ namespace WebService {
void RoomJson::SetRoomInformation(const std::string& name, const std::string& description,
const u16 port, const u32 max_player, const u32 net_version,
const bool has_password, const std::string& preferred_game,
const u64 preferred_game_id) {
const bool has_password,
const AnnounceMultiplayerRoom::GameInfo& preferred_game) {
room.information.name = name;
room.information.description = description;
room.information.port = port;
@ -92,7 +92,6 @@ void RoomJson::SetRoomInformation(const std::string& name, const std::string& de
room.net_version = net_version;
room.has_password = has_password;
room.information.preferred_game = preferred_game;
room.information.preferred_game_id = preferred_game_id;
}
void RoomJson::AddPlayer(const AnnounceMultiplayerRoom::Member& member) {
room.members.push_back(member);

View file

@ -22,8 +22,7 @@ public:
~RoomJson() = default;
void SetRoomInformation(const std::string& name, const std::string& description, const u16 port,
const u32 max_player, const u32 net_version, const bool has_password,
const std::string& preferred_game,
const u64 preferred_game_id) override;
const AnnounceMultiplayerRoom::GameInfo& preferred_game) override;
void AddPlayer(const AnnounceMultiplayerRoom::Member& member) override;
WebResult Update() override;
WebResult Register() override;

View file

@ -10,6 +10,8 @@
namespace WebService {
std::string GetPublicKey(const std::string& host);
class VerifyUserJWT final : public Network::VerifyUser::Backend {
public:
VerifyUserJWT(const std::string& host);

View file

@ -132,21 +132,24 @@ void HostRoomWindow::Host() {
}
ui->host->setDisabled(true);
auto game_name = ui->game_list->currentData(Qt::DisplayRole).toString();
auto game_id = ui->game_list->currentData(GameListItemPath::ProgramIdRole).toLongLong();
auto port = ui->port->isModified() ? ui->port->text().toInt() : Network::DefaultRoomPort;
auto password = ui->password->text().toStdString();
const AnnounceMultiplayerRoom::GameInfo game{
.name = ui->game_list->currentData(Qt::DisplayRole).toString().toStdString(),
.id = ui->game_list->currentData(GameListItemPath::ProgramIdRole).toULongLong(),
};
const auto port =
ui->port->isModified() ? ui->port->text().toInt() : Network::DefaultRoomPort;
const auto password = ui->password->text().toStdString();
const bool is_public = ui->host_type->currentIndex() == 0;
Network::Room::BanList ban_list{};
if (ui->load_ban_list->isChecked()) {
ban_list = UISettings::values.multiplayer_ban_list;
}
if (auto room = Network::GetRoom().lock()) {
bool created = room->Create(
ui->room_name->text().toStdString(),
const bool created =
room->Create(ui->room_name->text().toStdString(),
ui->room_description->toPlainText().toStdString(), "", port, password,
ui->max_player->value(), Settings::values.yuzu_username.GetValue(),
game_name.toStdString(), game_id, CreateVerifyBackend(is_public), ban_list);
game, CreateVerifyBackend(is_public), ban_list);
if (!created) {
NetworkMessage::ErrorManager::ShowError(
NetworkMessage::ErrorManager::COULD_NOT_CREATE_ROOM);

View file

@ -214,7 +214,7 @@ void Lobby::OnRefreshLobby() {
for (int r = 0; r < game_list->rowCount(); ++r) {
auto index = game_list->index(r, 0);
auto game_id = game_list->data(index, GameListItemPath::ProgramIdRole).toULongLong();
if (game_id != 0 && room.information.preferred_game_id == game_id) {
if (game_id != 0 && room.information.preferred_game.id == game_id) {
smdh_icon = game_list->data(index, Qt::DecorationRole).value<QPixmap>();
}
}
@ -223,8 +223,8 @@ void Lobby::OnRefreshLobby() {
for (auto member : room.members) {
QVariant var;
var.setValue(LobbyMember{QString::fromStdString(member.username),
QString::fromStdString(member.nickname), member.game_id,
QString::fromStdString(member.game_name)});
QString::fromStdString(member.nickname), member.game.id,
QString::fromStdString(member.game.name)});
members.append(var);
}
@ -232,8 +232,9 @@ void Lobby::OnRefreshLobby() {
auto row = QList<QStandardItem*>({
first_item,
new LobbyItemName(room.has_password, QString::fromStdString(room.information.name)),
new LobbyItemGame(room.information.preferred_game_id,
QString::fromStdString(room.information.preferred_game), smdh_icon),
new LobbyItemGame(room.information.preferred_game.id,
QString::fromStdString(room.information.preferred_game.name),
smdh_icon),
new LobbyItemHost(QString::fromStdString(room.information.host_username),
QString::fromStdString(room.ip), room.information.port,
QString::fromStdString(room.verify_UID)),

View file

@ -105,12 +105,12 @@ struct Values {
// multiplayer settings
Settings::Setting<QString> multiplayer_nickname{QStringLiteral("yuzu"), "nickname"};
Settings::Setting<QString> multiplayer_ip{{}, "ip"};
Settings::SwitchableSetting<uint> multiplayer_port{24872, 0, 65535, "port"};
Settings::SwitchableSetting<uint, true> multiplayer_port{24872, 0, 65535, "port"};
Settings::Setting<QString> multiplayer_room_nickname{{}, "room_nickname"};
Settings::Setting<QString> multiplayer_room_name{{}, "room_name"};
Settings::SwitchableSetting<uint> multiplayer_max_player{8, 0, 8, "max_player"};
Settings::SwitchableSetting<uint> multiplayer_room_port{24872, 0, 65535, "room_port"};
Settings::SwitchableSetting<uint> multiplayer_host_type{0, 0, 1, "host_type"};
Settings::SwitchableSetting<uint, true> multiplayer_max_player{8, 0, 8, "max_player"};
Settings::SwitchableSetting<uint, true> multiplayer_room_port{24872, 0, 65535, "room_port"};
Settings::SwitchableSetting<uint, true> multiplayer_host_type{0, 0, 1, "host_type"};
Settings::Setting<qulonglong> multiplayer_game_id{{}, "game_id"};
Settings::Setting<QString> multiplayer_room_description{{}, "room_description"};
std::pair<std::vector<std::string>, std::vector<std::string>> multiplayer_ban_list;