summaryrefslogtreecommitdiff
path: root/src/network
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-07-11 21:57:05 +0200
committerGitHub <noreply@github.com>2021-07-11 21:57:05 +0200
commitb6a116a2479eb62aece540633c41d0f54a481fd6 (patch)
tree977ffda77d342bf5adc9105f25eed7e13e5d993f /src/network
parent8bb9c3f6466c6d15a1a02e963b06b95f9ffb6397 (diff)
downloadopenttd-b6a116a2479eb62aece540633c41d0f54a481fd6.tar.xz
Add: allow setting your server visibility to "invite-only" (#9434)
In this mode you do register to the Game Coordinator, but your server will not show up in the public server listing. You can give your friends the invite code of the server with which they can join.
Diffstat (limited to 'src/network')
-rw-r--r--src/network/network.cpp25
-rw-r--r--src/network/network_coordinator.cpp19
-rw-r--r--src/network/network_func.h1
-rw-r--r--src/network/network_gui.cpp38
-rw-r--r--src/network/network_type.h1
5 files changed, 60 insertions, 24 deletions
diff --git a/src/network/network.cpp b/src/network/network.cpp
index f8138bbbc..d35fbe746 100644
--- a/src/network/network.cpp
+++ b/src/network/network.cpp
@@ -934,7 +934,7 @@ bool NetworkServerStart()
NetworkInitGameInfo();
- if (_settings_client.network.server_advertise) {
+ if (_settings_client.network.server_game_type != SERVER_GAME_TYPE_LOCAL) {
_network_coordinator_client.Register();
}
@@ -1000,6 +1000,29 @@ void NetworkDisconnect(bool blocking, bool close_admins)
}
/**
+ * The setting server_game_type was updated; possibly we need to take some
+ * action.
+ */
+void NetworkUpdateServerGameType()
+{
+ if (!_networking) return;
+
+ switch (_settings_client.network.server_game_type) {
+ case SERVER_GAME_TYPE_LOCAL:
+ _network_coordinator_client.CloseConnection();
+ break;
+
+ case SERVER_GAME_TYPE_INVITE_ONLY:
+ case SERVER_GAME_TYPE_PUBLIC:
+ _network_coordinator_client.Register();
+ break;
+
+ default:
+ NOT_REACHED();
+ }
+}
+
+/**
* Receives something from the network.
* @return true if everything went fine, false when the connection got closed.
*/
diff --git a/src/network/network_coordinator.cpp b/src/network/network_coordinator.cpp
index 8bd81b6f6..22b303f00 100644
--- a/src/network/network_coordinator.cpp
+++ b/src/network/network_coordinator.cpp
@@ -94,8 +94,8 @@ bool ClientNetworkCoordinatorSocketHandler::Receive_GC_ERROR(Packet *p)
SetDParamStr(0, detail);
ShowErrorMessage(STR_NETWORK_ERROR_COORDINATOR_REGISTRATION_FAILED, STR_JUST_RAW_STRING, WL_ERROR);
- /* To prevent that we constantly try to reconnect, switch to private game. */
- _settings_client.network.server_advertise = false;
+ /* To prevent that we constantly try to reconnect, switch to local game. */
+ _settings_client.network.server_game_type = SERVER_GAME_TYPE_LOCAL;
this->CloseConnection();
return false;
@@ -153,9 +153,18 @@ bool ClientNetworkCoordinatorSocketHandler::Receive_GC_REGISTER_ACK(Packet *p)
default: connection_type = "Unknown"; break; // Should never happen, but don't fail if it does.
}
+ std::string game_type;
+ switch (_settings_client.network.server_game_type) {
+ case SERVER_GAME_TYPE_INVITE_ONLY: game_type = "Invite only"; break;
+ case SERVER_GAME_TYPE_PUBLIC: game_type = "Public"; break;
+
+ case SERVER_GAME_TYPE_LOCAL: // Impossible to register local servers.
+ default: game_type = "Unknown"; break; // Should never happen, but don't fail if it does.
+ }
+
Debug(net, 3, "----------------------------------------");
Debug(net, 3, "Your server is now registered with the Game Coordinator:");
- Debug(net, 3, " Game type: Public");
+ Debug(net, 3, " Game type: {}", game_type);
Debug(net, 3, " Connection type: {}", connection_type);
Debug(net, 3, " Invite code: {}", _network_server_invite_code);
Debug(net, 3, "----------------------------------------");
@@ -298,7 +307,7 @@ void ClientNetworkCoordinatorSocketHandler::Register()
Packet *p = new Packet(PACKET_COORDINATOR_SERVER_REGISTER);
p->Send_uint8(NETWORK_COORDINATOR_VERSION);
- p->Send_uint8(SERVER_GAME_TYPE_PUBLIC);
+ p->Send_uint8(_settings_client.network.server_game_type);
p->Send_uint16(_settings_client.network.server_port);
if (_settings_client.network.server_invite_code.empty() || _settings_client.network.server_invite_code_secret.empty()) {
p->Send_string("");
@@ -467,7 +476,7 @@ void ClientNetworkCoordinatorSocketHandler::CloseAllTokens()
void ClientNetworkCoordinatorSocketHandler::SendReceive()
{
/* Private games are not listed via the Game Coordinator. */
- if (_network_server && !_settings_client.network.server_advertise) {
+ if (_network_server && _settings_client.network.server_game_type == SERVER_GAME_TYPE_LOCAL) {
if (this->sock != INVALID_SOCKET) {
this->CloseConnection();
}
diff --git a/src/network/network_func.h b/src/network/network_func.h
index 6da8fb5cc..c0f68f915 100644
--- a/src/network/network_func.h
+++ b/src/network/network_func.h
@@ -39,6 +39,7 @@ bool NetworkValidateOurClientName();
bool NetworkValidateClientName(std::string &client_name);
bool NetworkValidateServerName(std::string &server_name);
void NetworkUpdateClientName(const std::string &client_name);
+void NetworkUpdateServerGameType();
bool NetworkCompanyHasClients(CompanyID company);
std::string NetworkChangeCompanyPassword(CompanyID company_id, std::string password);
void NetworkReboot();
diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp
index a0d2d47c8..d2fefb5cc 100644
--- a/src/network/network_gui.cpp
+++ b/src/network/network_gui.cpp
@@ -61,16 +61,6 @@ static ClientID _admin_client_id = INVALID_CLIENT_ID; ///< For what client a con
static CompanyID _admin_company_id = INVALID_COMPANY; ///< For what company a confirmation window is open.
/**
- * Visibility of the server. Public servers advertise, where private servers
- * do not.
- */
-static const StringID _server_visibility_dropdown[] = {
- STR_NETWORK_SERVER_VISIBILITY_LOCAL,
- STR_NETWORK_SERVER_VISIBILITY_PUBLIC,
- INVALID_STRING_ID
-};
-
-/**
* Update the network new window because a new server is
* found on the network.
*/
@@ -79,6 +69,17 @@ void UpdateNetworkGameWindow()
InvalidateWindowData(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME, 0);
}
+static DropDownList BuildVisibilityDropDownList()
+{
+ DropDownList list;
+
+ list.emplace_back(new DropDownListStringItem(STR_NETWORK_SERVER_VISIBILITY_LOCAL, SERVER_GAME_TYPE_LOCAL, false));
+ list.emplace_back(new DropDownListStringItem(STR_NETWORK_SERVER_VISIBILITY_INVITE_ONLY, SERVER_GAME_TYPE_INVITE_ONLY, false));
+ list.emplace_back(new DropDownListStringItem(STR_NETWORK_SERVER_VISIBILITY_PUBLIC, SERVER_GAME_TYPE_PUBLIC, false));
+
+ return list;
+}
+
typedef GUIList<NetworkGameList*, StringFilter&> GUIGameServerList;
typedef int ServerListPosition;
static const ServerListPosition SLP_INVALID = -1;
@@ -1015,7 +1016,7 @@ struct NetworkStartServerWindow : public Window {
{
switch (widget) {
case WID_NSS_CONNTYPE_BTN:
- SetDParam(0, _server_visibility_dropdown[_settings_client.network.server_advertise]);
+ SetDParam(0, STR_NETWORK_SERVER_VISIBILITY_LOCAL + _settings_client.network.server_game_type);
break;
case WID_NSS_CLIENTS_TXT:
@@ -1036,7 +1037,7 @@ struct NetworkStartServerWindow : public Window {
{
switch (widget) {
case WID_NSS_CONNTYPE_BTN:
- *size = maxdim(GetStringBoundingBox(_server_visibility_dropdown[0]), GetStringBoundingBox(_server_visibility_dropdown[1]));
+ *size = maxdim(maxdim(GetStringBoundingBox(STR_NETWORK_SERVER_VISIBILITY_LOCAL), GetStringBoundingBox(STR_NETWORK_SERVER_VISIBILITY_PUBLIC)), GetStringBoundingBox(STR_NETWORK_SERVER_VISIBILITY_INVITE_ONLY));
size->width += padding.width;
size->height += padding.height;
break;
@@ -1066,7 +1067,7 @@ struct NetworkStartServerWindow : public Window {
break;
case WID_NSS_CONNTYPE_BTN: // Connection type
- ShowDropDownMenu(this, _server_visibility_dropdown, _settings_client.network.server_advertise, WID_NSS_CONNTYPE_BTN, 0, 0); // do it for widget WID_NSS_CONNTYPE_BTN
+ ShowDropDownList(this, BuildVisibilityDropDownList(), _settings_client.network.server_game_type, WID_NSS_CONNTYPE_BTN);
break;
case WID_NSS_CLIENTS_BTND: case WID_NSS_CLIENTS_BTNU: // Click on up/down button for number of clients
@@ -1144,7 +1145,7 @@ struct NetworkStartServerWindow : public Window {
{
switch (widget) {
case WID_NSS_CONNTYPE_BTN:
- _settings_client.network.server_advertise = (index != 0);
+ _settings_client.network.server_game_type = (ServerGameType)index;
break;
default:
NOT_REACHED();
@@ -2041,7 +2042,7 @@ public:
{
switch (widget) {
case WID_CL_SERVER_VISIBILITY:
- *size = maxdim(GetStringBoundingBox(_server_visibility_dropdown[0]), GetStringBoundingBox(_server_visibility_dropdown[1]));
+ *size = maxdim(maxdim(GetStringBoundingBox(STR_NETWORK_SERVER_VISIBILITY_LOCAL), GetStringBoundingBox(STR_NETWORK_SERVER_VISIBILITY_PUBLIC)), GetStringBoundingBox(STR_NETWORK_SERVER_VISIBILITY_INVITE_ONLY));
size->width += padding.width;
size->height += padding.height;
break;
@@ -2073,7 +2074,7 @@ public:
break;
case WID_CL_SERVER_VISIBILITY:
- SetDParam(0, _server_visibility_dropdown[_settings_client.network.server_advertise]);
+ SetDParam(0, STR_NETWORK_SERVER_VISIBILITY_LOCAL + _settings_client.network.server_game_type);
break;
case WID_CL_SERVER_INVITE_CODE: {
@@ -2117,7 +2118,7 @@ public:
case WID_CL_SERVER_VISIBILITY:
if (!_network_server) break;
- ShowDropDownMenu(this, _server_visibility_dropdown, _settings_client.network.server_advertise, WID_CL_SERVER_VISIBILITY, 0, 0);
+ ShowDropDownList(this, BuildVisibilityDropDownList(), _settings_client.network.server_game_type, WID_CL_SERVER_VISIBILITY);
break;
case WID_CL_MATRIX: {
@@ -2183,7 +2184,8 @@ public:
case WID_CL_SERVER_VISIBILITY:
if (!_network_server) break;
- _settings_client.network.server_advertise = (index != 0);
+ _settings_client.network.server_game_type = (ServerGameType)index;
+ NetworkUpdateServerGameType();
break;
case WID_CL_MATRIX: {
diff --git a/src/network/network_type.h b/src/network/network_type.h
index 6e6fe33de..a3fb217fe 100644
--- a/src/network/network_type.h
+++ b/src/network/network_type.h
@@ -42,6 +42,7 @@ enum NetworkVehicleType {
enum ServerGameType : uint8 {
SERVER_GAME_TYPE_LOCAL = 0,
SERVER_GAME_TYPE_PUBLIC,
+ SERVER_GAME_TYPE_INVITE_ONLY,
};
/** 'Unique' identifier to be given to clients */