Pull out potentially mod-specific player join code (slightly bogus, will fix properly later)
This commit is contained in:
@@ -148,12 +148,6 @@ namespace OpenRA.Server
|
|||||||
throw new InvalidOperationException("Already got 256 players");
|
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()
|
static void AcceptConnection()
|
||||||
{
|
{
|
||||||
Socket newSocket = null;
|
Socket newSocket = null;
|
||||||
@@ -189,33 +183,8 @@ namespace OpenRA.Server
|
|||||||
newConn.socket.Send(BitConverter.GetBytes(newConn.PlayerIndex));
|
newConn.socket.Send(BitConverter.GetBytes(newConn.PlayerIndex));
|
||||||
conns.Add(newConn);
|
conns.Add(newConn);
|
||||||
|
|
||||||
var defaults = new GameRules.PlayerSettings();
|
foreach (var t in ServerTraits.WithInterface<IClientJoined>())
|
||||||
|
t.ClientJoined(newConn);
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
catch (Exception e) { DropClient(newConn, e); }
|
catch (Exception e) { DropClient(newConn, e); }
|
||||||
}
|
}
|
||||||
@@ -280,31 +249,18 @@ namespace OpenRA.Server
|
|||||||
catch (NotImplementedException) { }
|
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)
|
public static void SendChatTo(Connection conn, string text)
|
||||||
{
|
{
|
||||||
DispatchOrdersToClient(conn, 0, 0,
|
DispatchOrdersToClient(conn, 0, 0,
|
||||||
new ServerOrder("Chat", text).Serialize());
|
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());
|
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());
|
DispatchOrders(asConn, 0, new ServerOrder("Disconnected", "").Serialize());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ using OpenRA.FileFormats;
|
|||||||
|
|
||||||
namespace OpenRA.Server.Traits
|
namespace OpenRA.Server.Traits
|
||||||
{
|
{
|
||||||
public class LobbyCommands : IInterpretCommand, IStartServer
|
public class LobbyCommands : IInterpretCommand, IStartServer, IClientJoined
|
||||||
{
|
{
|
||||||
public bool InterpretCommand(Connection conn, string cmd)
|
public bool InterpretCommand(Connection conn, string cmd)
|
||||||
{
|
{
|
||||||
@@ -77,10 +77,8 @@ namespace OpenRA.Server.Traits
|
|||||||
return true;
|
return true;
|
||||||
|
|
||||||
var cl = Server.GetClient(conn);
|
var cl = Server.GetClient(conn);
|
||||||
|
|
||||||
cl.Slot = slotData.Index;
|
cl.Slot = slotData.Index;
|
||||||
|
SyncClientToPlayerReference(cl, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null);
|
||||||
Server.SyncClientToPlayerReference(cl, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null);
|
|
||||||
|
|
||||||
Server.SyncLobbyInfo();
|
Server.SyncLobbyInfo();
|
||||||
return true;
|
return true;
|
||||||
@@ -98,8 +96,7 @@ namespace OpenRA.Server.Traits
|
|||||||
|
|
||||||
var cl = Server.GetClient(conn);
|
var cl = Server.GetClient(conn);
|
||||||
cl.Slot = slot;
|
cl.Slot = slot;
|
||||||
|
SyncClientToPlayerReference(cl, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null);
|
||||||
Server.SyncClientToPlayerReference(cl, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null);
|
|
||||||
|
|
||||||
Server.SyncLobbyInfo();
|
Server.SyncLobbyInfo();
|
||||||
return true;
|
return true;
|
||||||
@@ -201,8 +198,8 @@ namespace OpenRA.Server.Traits
|
|||||||
{
|
{
|
||||||
client.SpawnPoint = 0;
|
client.SpawnPoint = 0;
|
||||||
var slotData = Server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == client.Slot );
|
var slotData = Server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == client.Slot );
|
||||||
if (slotData != null)
|
if (slotData != null && slotData.MapPlayer != null)
|
||||||
Server.SyncClientToPlayerReference(client, Server.Map.Players[slotData.MapPlayer]);
|
SyncClientToPlayerReference(client, Server.Map.Players[slotData.MapPlayer]);
|
||||||
|
|
||||||
client.State = Session.ClientState.NotReady;
|
client.State = Session.ClientState.NotReady;
|
||||||
}
|
}
|
||||||
@@ -266,5 +263,55 @@ namespace OpenRA.Server.Traits
|
|||||||
Bot = null
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ namespace OpenRA.Server.Traits
|
|||||||
// Returns true if order is handled
|
// Returns true if order is handled
|
||||||
public interface IInterpretCommand { bool InterpretCommand(Connection conn, string cmd); }
|
public interface IInterpretCommand { bool InterpretCommand(Connection conn, string cmd); }
|
||||||
public interface IStartServer { void ServerStarted(); }
|
public interface IStartServer { void ServerStarted(); }
|
||||||
|
public interface IClientJoined { void ClientJoined(Connection conn); }
|
||||||
|
|
||||||
public class DebugServerTrait : IInterpretCommand
|
public class DebugServerTrait : IInterpretCommand
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user