From 87c665f8a68dd372e965c54209e73dfe4d18277e Mon Sep 17 00:00:00 2001 From: past-due <30942300+past-due@users.noreply.github.com> Date: Tue, 12 Nov 2024 19:08:24 -0500 Subject: [PATCH] cmdinterface: Output room status json on various events --- lib/netplay/netplay.cpp | 22 +++++ lib/netplay/netplay.h | 1 + src/multiint.cpp | 16 ++++ src/multijoin.cpp | 2 + src/multiplay.cpp | 5 + src/stdinreader.cpp | 205 +++++++++++++++++++++++++++++++++++++++- src/stdinreader.h | 2 + 7 files changed, 252 insertions(+), 1 deletion(-) diff --git a/lib/netplay/netplay.cpp b/lib/netplay/netplay.cpp index d3e3e1d1dd7..2a74a231660 100644 --- a/lib/netplay/netplay.cpp +++ b/lib/netplay/netplay.cpp @@ -412,6 +412,11 @@ void NET_setLobbyDisabled(const std::string& infoLinkURL) lobby_disabled_info_link_url = infoLinkURL; } +uint32_t NET_getCurrentHostedLobbyGameId() +{ + return gamestruct.gameId; +} + // Sets if the game is password protected or not void NETGameLocked(bool flag) { @@ -991,12 +996,19 @@ static void NETplayerLeaving(UDWORD index, bool quietSocketClose) sync_counter.left++; bool wasSpectator = NetPlay.players[index].isSpectator; MultiPlayerLeave(index); // more cleanup + bool resetReadyCalled = false; if (ingame.localJoiningInProgress) // Only if game hasn't actually started yet. { NET_DestroyPlayer(index); // sets index player's array to false if (!wasSpectator) { resetReadyStatus(false); // reset ready status for all players + resetReadyCalled = true; + } + + if (!resetReadyCalled) + { + wz_command_interface_output_room_status_json(); } } } @@ -1020,6 +1032,7 @@ static void NETplayerDropped(UDWORD index) sync_counter.drops++; bool wasSpectator = NetPlay.players[index].isSpectator; MultiPlayerLeave(id); // more cleanup + bool resetReadyCalled = false; if (ingame.localJoiningInProgress) // Only if game hasn't actually started yet. { // Send message type specifically for dropped / disconnects @@ -1031,6 +1044,12 @@ static void NETplayerDropped(UDWORD index) if (!wasSpectator) { resetReadyStatus(false); // reset ready status for all players + resetReadyCalled = true; + } + + if (!resetReadyCalled) + { + wz_command_interface_output_room_status_json(); } } @@ -4361,6 +4380,9 @@ static void NETallowJoining() { ASSERT(false, "wzFiles is uninitialized?? (Player: %" PRIu8 ")", index); } + + wz_command_interface_output_room_status_json(); + continue; // continue to next tmp_socket } diff --git a/lib/netplay/netplay.h b/lib/netplay/netplay.h index 04c56310fc6..6cfaf64846c 100644 --- a/lib/netplay/netplay.h +++ b/lib/netplay/netplay.h @@ -487,6 +487,7 @@ void NET_clearDownloadingWZFiles(); bool NET_getLobbyDisabled(); const std::string& NET_getLobbyDisabledInfoLinkURL(); void NET_setLobbyDisabled(const std::string& infoLinkURL); +uint32_t NET_getCurrentHostedLobbyGameId(); bool NETGameIsLocked(); void NETGameLocked(bool flag); diff --git a/src/multiint.cpp b/src/multiint.cpp index 0106a881f7b..533c2856154 100644 --- a/src/multiint.cpp +++ b/src/multiint.cpp @@ -2615,6 +2615,8 @@ static bool SendReadyRequest(UBYTE player, bool bReady) std::string playerName = NetPlay.players[player].name; std::string playerNameB64 = base64Encode(std::vector(playerName.begin(), playerName.end())); wz_command_interface_output("WZEVENT: readyStatus=%d: %" PRIu32 " %s %s %s %s %s\n", bReady ? 1 : 0, player, playerPublicKeyB64.c_str(), playerIdentityHash.c_str(), playerVerifiedStatus.c_str(), playerNameB64.c_str(), NetPlay.players[player].IPtextAddress); + + wz_command_interface_output_room_status_json(); } return changedValue; } @@ -2681,6 +2683,8 @@ bool recvReadyRequest(NETQUEUE queue) std::string playerName = NetPlay.players[player].name; std::string playerNameB64 = base64Encode(std::vector(playerName.begin(), playerName.end())); wz_command_interface_output("WZEVENT: readyStatus=%d: %" PRIu32 " %s %s %s %s %s\n", bReady ? 1 : 0, player, playerPublicKeyB64.c_str(), playerIdentityHash.c_str(), playerVerifiedStatus.c_str(), playerNameB64.c_str(), NetPlay.players[player].IPtextAddress); + + wz_command_interface_output_room_status_json(); } return changedValue; } @@ -7318,6 +7322,18 @@ void WzMultiplayerOptionsTitleUI::frontendMultiMessages(bool running) NETuint32_t(&player_id); NETend(); + if (player_id >= MAX_CONNECTED_PLAYERS) + { + debug(LOG_ERROR, "Bad NET_PLAYERRESPONDING received, ID is %d", (int)player_id); + break; + } + + if (whosResponsible(player_id) != queue.index && queue.index != NetPlay.hostPlayer) + { + HandleBadParam("NET_PLAYERRESPONDING given incorrect params.", player_id, queue.index); + break; + } + ingame.JoiningInProgress[player_id] = false; ingame.DataIntegrity[player_id] = false; break; diff --git a/src/multijoin.cpp b/src/multijoin.cpp index 9454216a6b7..fc6ec32be1b 100644 --- a/src/multijoin.cpp +++ b/src/multijoin.cpp @@ -449,6 +449,8 @@ void recvPlayerLeft(NETQUEUE queue) debug(LOG_INFO, "** player %u has dropped, in-game! (gameTime: %" PRIu32 ")", playerIndex, gameTime); ActivityManager::instance().updateMultiplayGameData(game, ingame, NETGameIsLocked()); + + wz_command_interface_output_room_status_json(); } // //////////////////////////////////////////////////////////////////////////// diff --git a/src/multiplay.cpp b/src/multiplay.cpp index 96a0a03f166..dc0cab35fea 100644 --- a/src/multiplay.cpp +++ b/src/multiplay.cpp @@ -461,6 +461,7 @@ bool multiPlayerLoop() } ingame.lastPlayerDataCheck2 = std::chrono::steady_clock::now(); wz_command_interface_output("WZEVENT: allPlayersJoined\n"); + wz_command_interface_output_room_status_json(); } if (NetPlay.bComms) { @@ -1460,6 +1461,8 @@ bool recvMessage() std::string playerName = NetPlay.players[player_id].name; std::string playerNameB64 = base64Encode(std::vector(playerName.begin(), playerName.end())); wz_command_interface_output("WZEVENT: playerResponding: %" PRIu32 " %s %s %s %s %s\n", player_id, playerPublicKeyB64.c_str(), playerIdentityHash.c_str(), playerVerifiedStatus.c_str(), playerNameB64.c_str(), NetPlay.players[player_id].IPtextAddress); + + wz_command_interface_output_room_status_json(); } } break; @@ -2530,6 +2533,8 @@ void resetReadyStatus(bool bSendOptions, bool ignoreReadyReset) changeReadyStatus(i, false); } } + + wz_command_interface_output_room_status_json(); } } diff --git a/src/stdinreader.cpp b/src/stdinreader.cpp index 7b05b94c857..25c4b221bfb 100644 --- a/src/stdinreader.cpp +++ b/src/stdinreader.cpp @@ -1,6 +1,6 @@ /* This file is part of Warzone 2100. - Copyright (C) 2020-2021 Warzone 2100 Project + Copyright (C) 2020-2024 Warzone 2100 Project Warzone 2100 is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -26,6 +26,7 @@ #include "multistat.h" #include "multilobbycommands.h" #include "clparse.h" +#include "main.h" #include #include @@ -1451,3 +1452,205 @@ void configSetCmdInterface(WZ_Command_Interface mode, std::string value) } wz_cmd_interface_param = value; } + +// MARK: - Output Room Status JSON + +static void WzCmdInterfaceDumpHumanPlayerVarsImpl(uint32_t player, bool gameHasFiredUp, nlohmann::ordered_json& j) +{ + PLAYER const &p = NetPlay.players[player]; + + j["name"] = p.name; + + if (!gameHasFiredUp) + { + // in lobby, output "ready" status + j["ready"] = static_cast(p.ready); + } + else + { + // once game has fired up, output loading / connection status + if (p.allocated) + { + if (ingame.JoiningInProgress[player]) + { + j["status"] = "loading"; + } + else + { + j["status"] = "active"; + } + if (ingame.PendingDisconnect[player]) + { + j["status"] = "pendingleave"; + } + } + else + { + j["status"] = "left"; + } + } + + const auto& identity = getMultiStats(player).identity; + if (!identity.empty()) + { + j["pk"] = base64Encode(identity.toBytes(EcKey::Public)); + } + j["ip"] = NetPlay.players[player].IPtextAddress; + + if (ingame.PingTimes[player] != PING_LIMIT) + { + j["ping"] = ingame.PingTimes[player]; + } + + j["admin"] = static_cast(NetPlay.players[player].isAdmin); +} + +void wz_command_interface_output_room_status_json() +{ + if (!wz_command_interface_enabled()) + { + return; + } + + bool gameHasFiredUp = (GetGameMode() == GS_NORMAL); + + auto root = nlohmann::ordered_json::object(); + root["ver"] = 1; + + auto data = nlohmann::ordered_json::object(); + if (gameHasFiredUp) + { + if (ingame.TimeEveryoneIsInGame.has_value()) + { + data["state"] = "started"; + } + else + { + data["state"] = "starting"; + } + } + else + { + data["state"] = "lobby"; + } + if (NetPlay.isHost) + { + auto lobbyGameId = NET_getCurrentHostedLobbyGameId(); + if (lobbyGameId != 0) + { + data["lobbyid"] = lobbyGameId; + } + } + data["map"] = game.map; + + root["data"] = std::move(data); + + if (NetPlay.isHost) + { + auto players = nlohmann::ordered_json::array(); + for (uint8_t player = 0; player < game.maxPlayers; ++player) + { + PLAYER const &p = NetPlay.players[player]; + auto j = nlohmann::ordered_json::object(); + + j["pos"] = p.position; + j["team"] = p.team; + j["col"] = p.colour; + j["fact"] = static_cast(p.faction); + + if (p.ai == AI_CLOSED) + { + // closed slot + j["type"] = "closed"; + } + else if (p.ai == AI_OPEN) + { + if (!gameHasFiredUp && !p.allocated) + { + // available / open slot (in lobby) + j["type"] = "open"; + } + else + { + if (!p.allocated) + { + // if game has fired up and this slot is no longer allocated, skip it entirely if it wasn't initially a human player + if (p.difficulty != AIDifficulty::HUMAN) + { + continue; + } + } + + // human (or host) slot + if (player == NetPlay.hostPlayer) + { + j["type"] = "host"; + } + else + { + j["type"] = (p.isSpectator) ? "spec" : "player"; + } + + WzCmdInterfaceDumpHumanPlayerVarsImpl(player, gameHasFiredUp, j); + } + } + else + { + // bot player + j["type"] = "bot"; + + j["name"] = getAIName(player); + j["difficulty"] = static_cast(NetPlay.players[player].difficulty); + } + + players.push_back(std::move(j)); + } + root["players"] = std::move(players); + + auto spectators = nlohmann::ordered_json::array(); + for (uint32_t i = MAX_PLAYER_SLOTS; i < MAX_CONNECTED_PLAYERS; ++i) + { + PLAYER const &p = NetPlay.players[i]; + if (p.ai == AI_CLOSED) + { + continue; + } + + auto j = nlohmann::ordered_json::object(); + if (!p.allocated) + { + if (!gameHasFiredUp) + { + // available / open spectator slot + j["type"] = "open"; + } + else + { + // no spectator connected to this slot - skip + continue; + } + } + else + { + // human (or host) slot + if (i == NetPlay.hostPlayer) + { + j["type"] = "host"; + } + else + { + j["type"] = (p.isSpectator) ? "spec" : "player"; + } + + WzCmdInterfaceDumpHumanPlayerVarsImpl(i, gameHasFiredUp, j); + } + + spectators.push_back(std::move(j)); + } + root["specs"] = std::move(spectators); + } + + std::string statusJSONStr = std::string("__WZROOMSTATUS__") + root.dump(-1, ' ', false, nlohmann::ordered_json::error_handler_t::replace) + "__ENDWZROOMSTATUS__"; + statusJSONStr.append("\n"); + wz_command_interface_output_str(statusJSONStr.c_str()); +} diff --git a/src/stdinreader.h b/src/stdinreader.h index 0f53a1e9950..8e9331a3fed 100644 --- a/src/stdinreader.h +++ b/src/stdinreader.h @@ -45,3 +45,5 @@ void wz_command_interface_output(const char *str, ...) WZ_DECL_FORMAT(printf, 1, #endif void wz_command_interface_output_str(const char *str); + +void wz_command_interface_output_room_status_json();