1
0
mirror of https://github.com/OpenTTD/OpenTTD.git synced 2025-08-20 13:09:15 +00:00

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.
This commit is contained in:
Patric Stout
2021-07-11 21:57:05 +02:00
committed by GitHub
parent 8bb9c3f646
commit b6a116a247
10 changed files with 86 additions and 31 deletions

View File

@@ -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();
}
@@ -999,6 +999,29 @@ void NetworkDisconnect(bool blocking, bool close_admins)
NetworkUDPInitialize();
}
/**
* 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.

View File

@@ -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();
}

View File

@@ -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();

View File

@@ -60,16 +60,6 @@ static const int NETWORK_LIST_REFRESH_DELAY = 30; ///< Time, in seconds, between
static ClientID _admin_client_id = INVALID_CLIENT_ID; ///< For what client a confirmation window is open.
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: {

View File

@@ -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 */