Shift Client creation to the client, sent in the handshake response. Fixes the `Newbie' bug and removes a lot of fragmented behaviour on player join.

This commit is contained in:
Paul Chote
2010-12-31 12:51:19 +13:00
parent 8f9e32dcc0
commit e2d1eec56e
6 changed files with 84 additions and 108 deletions

View File

@@ -18,7 +18,7 @@ using S = OpenRA.Server.Server;
namespace OpenRA.Mods.RA.Server
{
public class LobbyCommands : ServerTrait, IInterpretCommand, INotifyServerStart, IClientJoined
public class LobbyCommands : ServerTrait, IInterpretCommand, INotifyServerStart
{
public static int MaxSpectators = 4; // How many spectators to allow // @todo Expose this as an option
@@ -82,7 +82,7 @@ namespace OpenRA.Mods.RA.Server
return true;
client.Slot = slotData.Index;
SyncClientToPlayerReference(client, slotData.MapPlayer != null ? server.Map.Players[slotData.MapPlayer] : null);
S.SyncClientToPlayerReference(client, slotData.MapPlayer != null ? server.Map.Players[slotData.MapPlayer] : null);
server.SyncLobbyInfo();
return true;
@@ -99,7 +99,7 @@ namespace OpenRA.Mods.RA.Server
return false;
client.Slot = slot;
SyncClientToPlayerReference(client, slotData.MapPlayer != null ? server.Map.Players[slotData.MapPlayer] : null);
S.SyncClientToPlayerReference(client, slotData.MapPlayer != null ? server.Map.Players[slotData.MapPlayer] : null);
server.SyncLobbyInfo();
return true;
@@ -202,7 +202,7 @@ namespace OpenRA.Mods.RA.Server
c.SpawnPoint = 0;
var slotData = server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == c.Slot );
if (slotData != null && slotData.MapPlayer != null)
SyncClientToPlayerReference(c, server.Map.Players[slotData.MapPlayer]);
S.SyncClientToPlayerReference(c, server.Map.Players[slotData.MapPlayer]);
c.State = Session.ClientState.NotReady;
}
@@ -291,55 +291,5 @@ namespace OpenRA.Mods.RA.Server
Bot = null
});
}
public void ClientJoined(S server, Connection newConn)
{
var defaults = new GameRules.PlayerSettings();
var client = new Session.Client()
{
Index = newConn.PlayerIndex,
Color1 = defaults.Color1,
Color2 = defaults.Color2,
Name = defaults.Name,
Country = "random",
State = Session.ClientState.NotReady,
SpawnPoint = 0,
Team = 0,
Slot = ChooseFreeSlot(server),
};
var slotData = server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == client.Slot );
if (slotData != null && slotData.MapPlayer != null)
SyncClientToPlayerReference(client, server.Map.Players[slotData.MapPlayer]);
server.lobbyInfo.Clients.Add(client);
Log.Write("server", "Client {0}: Accepted connection from {1}",
newConn.PlayerIndex, newConn.socket.RemoteEndPoint);
server.SendChat(newConn, "has joined the game.");
server.SyncLobbyInfo();
}
static int ChooseFreeSlot(S server)
{
return server.lobbyInfo.Slots.First(s => !s.Closed && s.Bot == null
&& !server.lobbyInfo.Clients.Any( c => c.Slot == s.Index )).Index;
}
public static void SyncClientToPlayerReference(Session.Client c, PlayerReference pr)
{
if (pr == null)
return;
if (pr.LockColor)
{
c.Color1 = pr.Color;
c.Color2 = pr.Color2;
}
if (pr.LockRace)
c.Country = pr.Race;
}
}
}

View File

@@ -115,9 +115,6 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
// Todo: Only show if the map requirements are met for player slots
startGameButton.IsVisible = () => Game.IsHost;
Game.LobbyInfoChanged += JoinedServer;
Game.ConnectionStateChanged += ResetConnectionState;
Game.LobbyInfoChanged += UpdatePlayerList;
Game.AddChatLine += lobby.GetWidget<ChatDisplayWidget>("CHAT_DISPLAY").AddLine;
@@ -171,29 +168,6 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
title.Text = "OpenRA Multiplayer Lobby - " + orderManager.LobbyInfo.GlobalSettings.ServerName;
}
bool hasJoined = false;
void JoinedServer()
{
if (hasJoined)
return;
hasJoined = true;
if (orderManager.LocalClient.Name != Game.Settings.Player.Name)
orderManager.IssueOrder(Order.Command("name " + Game.Settings.Player.Name));
var c1 = Game.Settings.Player.Color1;
var c2 = Game.Settings.Player.Color2;
if (orderManager.LocalClient.Color1 != c1 || orderManager.LocalClient.Color2 != c2)
orderManager.IssueOrder(Order.Command("color {0},{1},{2},{3},{4},{5}".F(c1.R,c1.G,c1.B,c2.R,c2.G,c2.B)));
}
void ResetConnectionState( OrderManager orderManager )
{
if( orderManager.Connection.ConnectionState == ConnectionState.PreConnecting )
hasJoined = false;
}
Session.Client GetClientInSlot(Session.Slot slot)
{
return orderManager.LobbyInfo.ClientInSlot( slot );