Pull out potentially mod-specific player join code (slightly bogus, will fix properly later)

This commit is contained in:
Paul Chote
2010-11-08 16:11:36 +13:00
parent 967b16fc0e
commit 836b3a598b
3 changed files with 66 additions and 62 deletions

View File

@@ -148,12 +148,6 @@ namespace OpenRA.Server
throw new InvalidOperationException("Already got 256 players");
}
static int ChooseFreeSlot()
{
return lobbyInfo.Slots.First(s => !s.Closed && s.Bot == null
&& !lobbyInfo.Clients.Any( c => c.Slot == s.Index )).Index;
}
static void AcceptConnection()
{
Socket newSocket = null;
@@ -189,33 +183,8 @@ namespace OpenRA.Server
newConn.socket.Send(BitConverter.GetBytes(newConn.PlayerIndex));
conns.Add(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(),
};
var slotData = lobbyInfo.Slots.FirstOrDefault( x => x.Index == client.Slot );
if (slotData != null)
SyncClientToPlayerReference(client, Map.Players[slotData.MapPlayer]);
lobbyInfo.Clients.Add(client);
Log.Write("server", "Client {0}: Accepted connection from {1}",
newConn.PlayerIndex, newConn.socket.RemoteEndPoint);
SendChat(newConn, "has joined the game.");
SyncLobbyInfo();
foreach (var t in ServerTraits.WithInterface<IClientJoined>())
t.ClientJoined(newConn);
}
catch (Exception e) { DropClient(newConn, e); }
}
@@ -280,31 +249,18 @@ namespace OpenRA.Server
catch (NotImplementedException) { }
}
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;
}
public static void SendChatTo(Connection conn, string text)
{
DispatchOrdersToClient(conn, 0, 0,
new ServerOrder("Chat", text).Serialize());
}
static void SendChat(Connection asConn, string text)
public static void SendChat(Connection asConn, string text)
{
DispatchOrders(asConn, 0, new ServerOrder("Chat", text).Serialize());
}
static void SendDisconnected(Connection asConn)
public static void SendDisconnected(Connection asConn)
{
DispatchOrders(asConn, 0, new ServerOrder("Disconnected", "").Serialize());
}

View File

@@ -16,7 +16,7 @@ using OpenRA.FileFormats;
namespace OpenRA.Server.Traits
{
public class LobbyCommands : IInterpretCommand, IStartServer
public class LobbyCommands : IInterpretCommand, IStartServer, IClientJoined
{
public bool InterpretCommand(Connection conn, string cmd)
{
@@ -77,10 +77,8 @@ namespace OpenRA.Server.Traits
return true;
var cl = Server.GetClient(conn);
cl.Slot = slotData.Index;
Server.SyncClientToPlayerReference(cl, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null);
SyncClientToPlayerReference(cl, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null);
Server.SyncLobbyInfo();
return true;
@@ -98,8 +96,7 @@ namespace OpenRA.Server.Traits
var cl = Server.GetClient(conn);
cl.Slot = slot;
Server.SyncClientToPlayerReference(cl, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null);
SyncClientToPlayerReference(cl, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null);
Server.SyncLobbyInfo();
return true;
@@ -201,8 +198,8 @@ namespace OpenRA.Server.Traits
{
client.SpawnPoint = 0;
var slotData = Server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == client.Slot );
if (slotData != null)
Server.SyncClientToPlayerReference(client, Server.Map.Players[slotData.MapPlayer]);
if (slotData != null && slotData.MapPlayer != null)
SyncClientToPlayerReference(client, Server.Map.Players[slotData.MapPlayer]);
client.State = Session.ClientState.NotReady;
}
@@ -266,5 +263,55 @@ namespace OpenRA.Server.Traits
Bot = null
});
}
public void ClientJoined(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(),
};
var slotData = Server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == client.Slot );
if (slotData != 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()
{
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

@@ -13,6 +13,7 @@ namespace OpenRA.Server.Traits
// Returns true if order is handled
public interface IInterpretCommand { bool InterpretCommand(Connection conn, string cmd); }
public interface IStartServer { void ServerStarted(); }
public interface IClientJoined { void ClientJoined(Connection conn); }
public class DebugServerTrait : IInterpretCommand
{