StyleCop cleanup

This commit is contained in:
Matthias Mailänder
2013-11-10 07:34:52 +01:00
parent 7de2dd7083
commit bc133d199e
5 changed files with 165 additions and 166 deletions

View File

@@ -386,7 +386,7 @@ namespace OpenRA
System.Threading.Thread.Sleep(100); System.Threading.Thread.Sleep(100);
if ((server.State == Server.ServerState.GameStarted) if ((server.State == Server.ServerState.GameStarted)
&& (server.conns.Count<=1)) && (server.Conns.Count<=1))
{ {
Console.WriteLine("No one is playing, shutting down..."); Console.WriteLine("No one is playing, shutting down...");
server.Shutdown(); server.Shutdown();

View File

@@ -14,8 +14,8 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading; using System.Threading;
using OpenRA.FileFormats; using OpenRA.FileFormats;
@@ -35,35 +35,51 @@ namespace OpenRA.Server
public class Server public class Server
{ {
// Valid player connections
public List<Connection> conns = new List<Connection>();
// Pre-verified player connections
public List<Connection> preConns = new List<Connection>();
TcpListener listener = null;
Dictionary<int, List<Connection>> inFlightFrames
= new Dictionary<int, List<Connection>>();
TypeDictionary ServerTraits = new TypeDictionary();
public Session lobbyInfo;
public readonly IPAddress Ip; public readonly IPAddress Ip;
public readonly int Port; public readonly int Port;
int randomSeed; int randomSeed;
public readonly Thirdparty.Random Random = new Thirdparty.Random(); public readonly Thirdparty.Random Random = new Thirdparty.Random();
// Valid player connections
public List<Connection> Conns = new List<Connection>();
// Pre-verified player connections
public List<Connection> PreConns = new List<Connection>();
TcpListener listener = null;
Dictionary<int, List<Connection>> inFlightFrames
= new Dictionary<int, List<Connection>>();
TypeDictionary serverTraits = new TypeDictionary();
public Session LobbyInfo;
public ServerSettings Settings; public ServerSettings Settings;
public ModData ModData; public ModData ModData;
public Map Map; public Map Map;
XTimer gameTimeout; XTimer gameTimeout;
protected volatile ServerState pState = new ServerState(); public static void SyncClientToPlayerReference(Session.Client c, PlayerReference pr)
{
if (pr == null)
return;
if (pr.LockColor)
c.Color = pr.Color;
else
c.Color = c.PreferredColor;
if (pr.LockRace)
c.Country = pr.Race;
if (pr.LockSpawn)
c.SpawnPoint = pr.Spawn;
if (pr.LockTeam)
c.Team = pr.Team;
}
protected volatile ServerState internalState = new ServerState();
public ServerState State public ServerState State
{ {
get { return pState; } get { return internalState; }
protected set { pState = value; } protected set { internalState = value; }
} }
public List<string> TempBans = new List<string>(); public List<string> TempBans = new List<string>();
@@ -75,7 +91,7 @@ namespace OpenRA.Server
public void EndGame() public void EndGame()
{ {
foreach (var t in ServerTraits.WithInterface<IEndGame>()) foreach (var t in serverTraits.WithInterface<IEndGame>())
t.GameEnded(this); t.GameEnded(this);
} }
@@ -83,7 +99,7 @@ namespace OpenRA.Server
{ {
Log.AddChannel("server", "server.log"); Log.AddChannel("server", "server.log");
pState = ServerState.WaitingPlayers; internalState = ServerState.WaitingPlayers;
listener = new TcpListener(endpoint); listener = new TcpListener(endpoint);
listener.Start(); listener.Start();
var localEndpoint = (IPEndPoint)listener.LocalEndpoint; var localEndpoint = (IPEndPoint)listener.LocalEndpoint;
@@ -99,33 +115,33 @@ namespace OpenRA.Server
UPnP.ForwardPort(); UPnP.ForwardPort();
foreach (var trait in modData.Manifest.ServerTraits) foreach (var trait in modData.Manifest.ServerTraits)
ServerTraits.Add(modData.ObjectCreator.CreateObject<ServerTrait>(trait)); serverTraits.Add(modData.ObjectCreator.CreateObject<ServerTrait>(trait));
lobbyInfo = new Session(mods); LobbyInfo = new Session(mods);
lobbyInfo.GlobalSettings.RandomSeed = randomSeed; LobbyInfo.GlobalSettings.RandomSeed = randomSeed;
lobbyInfo.GlobalSettings.Map = settings.Map; LobbyInfo.GlobalSettings.Map = settings.Map;
lobbyInfo.GlobalSettings.ServerName = settings.Name; LobbyInfo.GlobalSettings.ServerName = settings.Name;
lobbyInfo.GlobalSettings.Dedicated = settings.Dedicated; LobbyInfo.GlobalSettings.Dedicated = settings.Dedicated;
FieldLoader.Load(lobbyInfo.GlobalSettings, modData.Manifest.LobbyDefaults); FieldLoader.Load(LobbyInfo.GlobalSettings, modData.Manifest.LobbyDefaults);
foreach (var t in ServerTraits.WithInterface<INotifyServerStart>()) foreach (var t in serverTraits.WithInterface<INotifyServerStart>())
t.ServerStarted(this); t.ServerStarted(this);
Log.Write("server", "Initial mods: "); Log.Write("server", "Initial mods: ");
foreach (var m in lobbyInfo.GlobalSettings.Mods) foreach (var m in LobbyInfo.GlobalSettings.Mods)
Log.Write("server","- {0}", m); Log.Write("server", "- {0}", m);
Log.Write("server", "Initial map: {0}",lobbyInfo.GlobalSettings.Map); Log.Write("server", "Initial map: {0}", LobbyInfo.GlobalSettings.Map);
new Thread( _ => new Thread(_ =>
{ {
var timeout = ServerTraits.WithInterface<ITick>().Min(t => t.TickTimeout); var timeout = serverTraits.WithInterface<ITick>().Min(t => t.TickTimeout);
for (;;) for (;;)
{ {
var checkRead = new List<Socket>(); var checkRead = new List<Socket>();
checkRead.Add(listener.Server); checkRead.Add(listener.Server);
foreach (var c in conns) checkRead.Add(c.socket); foreach (var c in Conns) checkRead.Add(c.socket);
foreach (var c in preConns) checkRead.Add(c.socket); foreach (var c in PreConns) checkRead.Add(c.socket);
Socket.Select(checkRead, null, null, timeout); Socket.Select(checkRead, null, null, timeout);
if (State == ServerState.ShuttingDown) if (State == ServerState.ShuttingDown)
@@ -136,18 +152,18 @@ namespace OpenRA.Server
foreach (var s in checkRead) foreach (var s in checkRead)
if (s == listener.Server) AcceptConnection(); if (s == listener.Server) AcceptConnection();
else if (preConns.Count > 0) else if (PreConns.Count > 0)
{ {
var p = preConns.SingleOrDefault(c => c.socket == s); var p = PreConns.SingleOrDefault(c => c.socket == s);
if (p != null) p.ReadData(this); if (p != null) p.ReadData(this);
} }
else if (conns.Count > 0) else if (Conns.Count > 0)
{ {
var conn = conns.SingleOrDefault(c => c.socket == s); var conn = Conns.SingleOrDefault(c => c.socket == s);
if (conn != null) conn.ReadData(this); if (conn != null) conn.ReadData(this);
} }
foreach (var t in ServerTraits.WithInterface<ITick>()) foreach (var t in serverTraits.WithInterface<ITick>())
t.Tick(this); t.Tick(this);
if (State == ServerState.ShuttingDown) if (State == ServerState.ShuttingDown)
@@ -158,15 +174,14 @@ namespace OpenRA.Server
} }
} }
foreach (var t in ServerTraits.WithInterface<INotifyServerShutdown>()) foreach (var t in serverTraits.WithInterface<INotifyServerShutdown>())
t.ServerShutdown(this); t.ServerShutdown(this);
preConns.Clear(); PreConns.Clear();
conns.Clear(); Conns.Clear();
try { listener.Stop(); } try { listener.Stop(); }
catch { } catch { }
} ) { IsBackground = true }.Start(); }) { IsBackground = true }.Start();
} }
/* lobby rework TODO: /* lobby rework TODO:
@@ -206,13 +221,13 @@ namespace OpenRA.Server
newConn.PlayerIndex = ChooseFreePlayerIndex(); newConn.PlayerIndex = ChooseFreePlayerIndex();
SendData(newConn.socket, BitConverter.GetBytes(ProtocolVersion.Version)); SendData(newConn.socket, BitConverter.GetBytes(ProtocolVersion.Version));
SendData(newConn.socket, BitConverter.GetBytes(newConn.PlayerIndex)); SendData(newConn.socket, BitConverter.GetBytes(newConn.PlayerIndex));
preConns.Add(newConn); PreConns.Add(newConn);
// Dispatch a handshake order // Dispatch a handshake order
var request = new HandshakeRequest() var request = new HandshakeRequest()
{ {
Map = lobbyInfo.GlobalSettings.Map, Map = LobbyInfo.GlobalSettings.Map,
Mods = lobbyInfo.GlobalSettings.Mods.Select(m => "{0}@{1}".F(m, Mod.AllMods[m].Version)).ToArray() Mods = LobbyInfo.GlobalSettings.Mods.Select(m => "{0}@{1}".F(m, Mod.AllMods[m].Version)).ToArray()
}; };
DispatchOrdersToClient(newConn, 0, 0, new ServerOrder("HandshakeRequest", request.Serialize()).Serialize()); DispatchOrdersToClient(newConn, 0, 0, new ServerOrder("HandshakeRequest", request.Serialize()).Serialize());
} }
@@ -252,14 +267,14 @@ namespace OpenRA.Server
Name = handshake.Client.Name, Name = handshake.Client.Name,
IpAddress = ((IPEndPoint)newConn.socket.RemoteEndPoint).Address.ToString(), IpAddress = ((IPEndPoint)newConn.socket.RemoteEndPoint).Address.ToString(),
Index = newConn.PlayerIndex, Index = newConn.PlayerIndex,
Slot = lobbyInfo.FirstEmptySlot(), Slot = LobbyInfo.FirstEmptySlot(),
PreferredColor = handshake.Client.Color, PreferredColor = handshake.Client.Color,
Color = handshake.Client.Color, Color = handshake.Client.Color,
Country = "random", Country = "random",
SpawnPoint = 0, SpawnPoint = 0,
Team = 0, Team = 0,
State = Session.ClientState.NotReady, State = Session.ClientState.NotReady,
IsAdmin = !lobbyInfo.Clients.Any(c1 => c1.IsAdmin) IsAdmin = !LobbyInfo.Clients.Any(c1 => c1.IsAdmin)
}; };
if (client.Slot != null) if (client.Slot != null)
@@ -269,8 +284,8 @@ namespace OpenRA.Server
// Check that the client has compatible mods // Check that the client has compatible mods
var mods = handshake.Mods; var mods = handshake.Mods;
var validMod = mods.All(m => m.Contains('@')) && //valid format var validMod = mods.All(m => m.Contains('@')) && // valid format
mods.Count() == Game.CurrentMods.Count() && //same number mods.Count() == Game.CurrentMods.Count() && // same number
mods.Select(m => Pair.New(m.Split('@')[0], m.Split('@')[1])).All(kv => Game.CurrentMods.ContainsKey(kv.First)); mods.Select(m => Pair.New(m.Split('@')[0], m.Split('@')[1])).All(kv => Game.CurrentMods.ContainsKey(kv.First));
if (!validMod) if (!validMod)
@@ -286,7 +301,7 @@ namespace OpenRA.Server
var validVersion = mods.Select(m => Pair.New(m.Split('@')[0], m.Split('@')[1])).All( var validVersion = mods.Select(m => Pair.New(m.Split('@')[0], m.Split('@')[1])).All(
kv => kv.Second == Game.CurrentMods[kv.First].Version); kv => kv.Second == Game.CurrentMods[kv.First].Version);
if (!validVersion && !lobbyInfo.GlobalSettings.AllowVersionMismatch) if (!validVersion && !LobbyInfo.GlobalSettings.AllowVersionMismatch)
{ {
Log.Write("server", "Rejected connection from {0}; Not running the same version.", Log.Write("server", "Rejected connection from {0}; Not running the same version.",
newConn.socket.RemoteEndPoint); newConn.socket.RemoteEndPoint);
@@ -307,14 +322,14 @@ namespace OpenRA.Server
} }
// Promote connection to a valid client // Promote connection to a valid client
preConns.Remove(newConn); PreConns.Remove(newConn);
conns.Add(newConn); Conns.Add(newConn);
lobbyInfo.Clients.Add(client); LobbyInfo.Clients.Add(client);
Log.Write("server", "Client {0}: Accepted connection from {1}.", Log.Write("server", "Client {0}: Accepted connection from {1}.",
newConn.PlayerIndex, newConn.socket.RemoteEndPoint); newConn.PlayerIndex, newConn.socket.RemoteEndPoint);
foreach (var t in ServerTraits.WithInterface<IClientJoined>()) foreach (var t in serverTraits.WithInterface<IClientJoined>())
t.ClientJoined(this, newConn); t.ClientJoined(this, newConn);
SyncLobbyInfo(); SyncLobbyInfo();
@@ -323,9 +338,9 @@ namespace OpenRA.Server
// Send initial ping // Send initial ping
SendOrderTo(newConn, "Ping", Environment.TickCount.ToString()); SendOrderTo(newConn, "Ping", Environment.TickCount.ToString());
if (File.Exists("{0}motd_{1}.txt".F(Platform.SupportDir, lobbyInfo.GlobalSettings.Mods[0]))) if (File.Exists("{0}motd_{1}.txt".F(Platform.SupportDir, LobbyInfo.GlobalSettings.Mods[0])))
{ {
var motd = System.IO.File.ReadAllText("{0}motd_{1}.txt".F(Platform.SupportDir, lobbyInfo.GlobalSettings.Mods[0])); var motd = System.IO.File.ReadAllText("{0}motd_{1}.txt".F(Platform.SupportDir, LobbyInfo.GlobalSettings.Mods[0]));
SendOrderTo(newConn, "Message", motd); SendOrderTo(newConn, "Message", motd);
} }
@@ -340,30 +355,14 @@ namespace OpenRA.Server
void SetOrderLag() void SetOrderLag()
{ {
if (lobbyInfo.IsSinglePlayer) if (LobbyInfo.IsSinglePlayer)
lobbyInfo.GlobalSettings.OrderLatency = 1; LobbyInfo.GlobalSettings.OrderLatency = 1;
else else
lobbyInfo.GlobalSettings.OrderLatency = 3; LobbyInfo.GlobalSettings.OrderLatency = 3;
SyncLobbyInfo(); SyncLobbyInfo();
} }
public static void SyncClientToPlayerReference(Session.Client c, PlayerReference pr)
{
if (pr == null)
return;
if (pr.LockColor)
c.Color = pr.Color;
else
c.Color = c.PreferredColor;
if (pr.LockRace)
c.Country = pr.Race;
if (pr.LockSpawn)
c.SpawnPoint = pr.Spawn;
if (pr.LockTeam)
c.Team = pr.Team;
}
public void UpdateInFlightFrames(Connection conn) public void UpdateInFlightFrames(Connection conn)
{ {
if (conn.Frame == 0) if (conn.Frame == 0)
@@ -374,7 +373,7 @@ namespace OpenRA.Server
else else
inFlightFrames[conn.Frame].Add(conn); inFlightFrames[conn.Frame].Add(conn);
if (conns.All(c => inFlightFrames[conn.Frame].Contains(c))) if (Conns.All(c => inFlightFrames[conn.Frame].Contains(c)))
inFlightFrames.Remove(conn.Frame); inFlightFrames.Remove(conn.Frame);
} }
@@ -397,7 +396,7 @@ namespace OpenRA.Server
public void DispatchOrdersToClients(Connection conn, int frame, byte[] data) public void DispatchOrdersToClients(Connection conn, int frame, byte[] data)
{ {
var from = conn != null ? conn.PlayerIndex : 0; var from = conn != null ? conn.PlayerIndex : 0;
foreach (var c in conns.Except(conn).ToArray()) foreach (var c in Conns.Except(conn).ToArray())
DispatchOrdersToClient(c, from, frame, data); DispatchOrdersToClient(c, from, frame, data);
} }
@@ -416,7 +415,7 @@ namespace OpenRA.Server
try try
{ {
for (; ; ) for (;;)
{ {
var so = ServerOrder.Deserialize(br); var so = ServerOrder.Deserialize(br);
if (so == null) return; if (so == null) return;
@@ -443,8 +442,8 @@ namespace OpenRA.Server
{ {
case "Command": case "Command":
bool handled = false; bool handled = false;
foreach (var t in ServerTraits.WithInterface<IInterpretCommand>()) foreach (var t in serverTraits.WithInterface<IInterpretCommand>())
if ((handled = t.InterpretCommand(this, conn, GetClient(conn), so.Data))) if (handled = t.InterpretCommand(this, conn, GetClient(conn), so.Data))
break; break;
if (!handled) if (!handled)
@@ -481,7 +480,7 @@ namespace OpenRA.Server
history.RemoveRange(0, history.Count - 5); history.RemoveRange(0, history.Count - 5);
fromClient.Latency = history.Sum() / history.Count; fromClient.Latency = history.Sum() / history.Count;
fromClient.LatencyJitter = (history.Max() - history.Min())/2; fromClient.LatencyJitter = (history.Max() - history.Min()) / 2;
fromClient.LatencyHistory = history.ToArray(); fromClient.LatencyHistory = history.ToArray();
if (State == ServerState.WaitingPlayers) if (State == ServerState.WaitingPlayers)
@@ -494,18 +493,18 @@ namespace OpenRA.Server
public Session.Client GetClient(Connection conn) public Session.Client GetClient(Connection conn)
{ {
return lobbyInfo.ClientWithIndex(conn.PlayerIndex); return LobbyInfo.ClientWithIndex(conn.PlayerIndex);
} }
public void DropClient(Connection toDrop) public void DropClient(Connection toDrop)
{ {
if (preConns.Contains(toDrop)) if (PreConns.Contains(toDrop))
preConns.Remove(toDrop); PreConns.Remove(toDrop);
else else
{ {
conns.Remove(toDrop); Conns.Remove(toDrop);
var dropClient = lobbyInfo.Clients.FirstOrDefault(c1 => c1.Index == toDrop.PlayerIndex); var dropClient = LobbyInfo.Clients.FirstOrDefault(c1 => c1.Index == toDrop.PlayerIndex);
if (dropClient == null) if (dropClient == null)
return; return;
@@ -517,16 +516,16 @@ namespace OpenRA.Server
// Send disconnected order, even if still in the lobby // Send disconnected order, even if still in the lobby
DispatchOrdersToClients(toDrop, 0, new ServerOrder("Disconnected", "").Serialize()); DispatchOrdersToClients(toDrop, 0, new ServerOrder("Disconnected", "").Serialize());
lobbyInfo.Clients.RemoveAll(c => c.Index == toDrop.PlayerIndex); LobbyInfo.Clients.RemoveAll(c => c.Index == toDrop.PlayerIndex);
// Client was the server admin // Client was the server admin
// TODO: Reassign admin for game in progress via an order // TODO: Reassign admin for game in progress via an order
if (lobbyInfo.GlobalSettings.Dedicated && dropClient.IsAdmin && State == ServerState.WaitingPlayers) if (LobbyInfo.GlobalSettings.Dedicated && dropClient.IsAdmin && State == ServerState.WaitingPlayers)
{ {
// Remove any bots controlled by the admin // Remove any bots controlled by the admin
lobbyInfo.Clients.RemoveAll(c => c.Bot != null && c.BotControllerClientIndex == toDrop.PlayerIndex); LobbyInfo.Clients.RemoveAll(c => c.Bot != null && c.BotControllerClientIndex == toDrop.PlayerIndex);
var nextAdmin = lobbyInfo.Clients.Where(c1 => c1.Bot == null) var nextAdmin = LobbyInfo.Clients.Where(c1 => c1.Bot == null)
.OrderBy(c => c.Index).FirstOrDefault(); .OrderBy(c => c.Index).FirstOrDefault();
if (nextAdmin != null) if (nextAdmin != null)
@@ -536,18 +535,18 @@ namespace OpenRA.Server
} }
} }
DispatchOrders(toDrop, toDrop.MostRecentFrame, new byte[] {0xbf}); DispatchOrders(toDrop, toDrop.MostRecentFrame, new byte[] { 0xbf });
if (!conns.Any()) if (!Conns.Any())
{ {
FieldLoader.Load(lobbyInfo.GlobalSettings, ModData.Manifest.LobbyDefaults); FieldLoader.Load(LobbyInfo.GlobalSettings, ModData.Manifest.LobbyDefaults);
TempBans.Clear(); TempBans.Clear();
} }
if (conns.Any() || lobbyInfo.GlobalSettings.Dedicated) if (Conns.Any() || LobbyInfo.GlobalSettings.Dedicated)
SyncLobbyInfo(); SyncLobbyInfo();
if (!lobbyInfo.GlobalSettings.Dedicated && dropClient.IsAdmin) if (!LobbyInfo.GlobalSettings.Dedicated && dropClient.IsAdmin)
Shutdown(); Shutdown();
} }
@@ -564,9 +563,9 @@ namespace OpenRA.Server
{ {
if (State == ServerState.WaitingPlayers) // Don't do this while the game is running, it breaks things! if (State == ServerState.WaitingPlayers) // Don't do this while the game is running, it breaks things!
DispatchOrders(null, 0, DispatchOrders(null, 0,
new ServerOrder("SyncInfo", lobbyInfo.Serialize()).Serialize()); new ServerOrder("SyncInfo", LobbyInfo.Serialize()).Serialize());
foreach (var t in ServerTraits.WithInterface<INotifySyncLobbyInfo>()) foreach (var t in serverTraits.WithInterface<INotifySyncLobbyInfo>())
t.LobbyInfoSynced(this); t.LobbyInfoSynced(this);
} }
@@ -577,29 +576,29 @@ namespace OpenRA.Server
Console.WriteLine("Game started"); Console.WriteLine("Game started");
foreach (var c in conns) foreach (var c in Conns)
foreach (var d in conns) foreach (var d in Conns)
DispatchOrdersToClient(c, d.PlayerIndex, 0x7FFFFFFF, new byte[] { 0xBF }); DispatchOrdersToClient(c, d.PlayerIndex, 0x7FFFFFFF, new byte[] { 0xBF });
// Drop any unvalidated clients // Drop any unvalidated clients
foreach (var c in preConns.ToArray()) foreach (var c in PreConns.ToArray())
DropClient(c); DropClient(c);
DispatchOrders(null, 0, DispatchOrders(null, 0,
new ServerOrder("StartGame", "").Serialize()); new ServerOrder("StartGame", "").Serialize());
foreach (var t in ServerTraits.WithInterface<IStartGame>()) foreach (var t in serverTraits.WithInterface<IStartGame>())
t.GameStarted(this); t.GameStarted(this);
// Check TimeOut // Check TimeOut
if (Settings.TimeOut > 10000) if (Settings.TimeOut > 10000)
{ {
gameTimeout = new XTimer(Settings.TimeOut); gameTimeout = new XTimer(Settings.TimeOut);
gameTimeout.Elapsed += (_,e) => gameTimeout.Elapsed += (_, e) =>
{ {
Console.WriteLine("Timeout at {0}!!!", e.SignalTime); Console.WriteLine("Timeout at {0}!!!", e.SignalTime);
Environment.Exit(0); Environment.Exit(0);
}; };
gameTimeout.Enabled = true; gameTimeout.Enabled = true;
} }
} }

View File

@@ -22,7 +22,7 @@ namespace OpenRA.Mods.RA.Server
{ {
static bool ValidateSlotCommand(S server, Connection conn, Session.Client client, string arg, bool requiresHost) static bool ValidateSlotCommand(S server, Connection conn, Session.Client client, string arg, bool requiresHost)
{ {
if (!server.lobbyInfo.Slots.ContainsKey(arg)) if (!server.LobbyInfo.Slots.ContainsKey(arg))
{ {
Log.Write("server", "Invalid slot: {0}", arg); Log.Write("server", "Invalid slot: {0}", arg);
return false; return false;
@@ -55,14 +55,14 @@ namespace OpenRA.Mods.RA.Server
void CheckAutoStart(S server, Connection conn, Session.Client client) void CheckAutoStart(S server, Connection conn, Session.Client client)
{ {
var playerClients = server.lobbyInfo.Clients.Where(c => c.Bot == null && c.Slot != null); var playerClients = server.LobbyInfo.Clients.Where(c => c.Bot == null && c.Slot != null);
// Are all players ready? // Are all players ready?
if (playerClients.Count() == 0 || playerClients.Any(c => c.State != Session.ClientState.Ready)) if (playerClients.Count() == 0 || playerClients.Any(c => c.State != Session.ClientState.Ready))
return; return;
// Are the map conditions satisfied? // Are the map conditions satisfied?
if (server.lobbyInfo.Slots.Any(sl => sl.Value.Required && server.lobbyInfo.ClientInSlot(sl.Key) == null)) if (server.LobbyInfo.Slots.Any(sl => sl.Value.Required && server.LobbyInfo.ClientInSlot(sl.Key) == null))
return; return;
server.StartGame(); server.StartGame();
@@ -102,8 +102,8 @@ namespace OpenRA.Mods.RA.Server
return true; return true;
} }
if (server.lobbyInfo.Slots.Any(sl => sl.Value.Required && if (server.LobbyInfo.Slots.Any(sl => sl.Value.Required &&
server.lobbyInfo.ClientInSlot(sl.Key) == null)) server.LobbyInfo.ClientInSlot(sl.Key) == null))
{ {
server.SendOrderTo(conn, "Message", "Unable to start the game until required slots are full."); server.SendOrderTo(conn, "Message", "Unable to start the game until required slots are full.");
return true; return true;
@@ -114,14 +114,14 @@ namespace OpenRA.Mods.RA.Server
{ "slot", { "slot",
s => s =>
{ {
if (!server.lobbyInfo.Slots.ContainsKey(s)) if (!server.LobbyInfo.Slots.ContainsKey(s))
{ {
Log.Write("server", "Invalid slot: {0}", s ); Log.Write("server", "Invalid slot: {0}", s );
return false; return false;
} }
var slot = server.lobbyInfo.Slots[s]; var slot = server.LobbyInfo.Slots[s];
if (slot.Closed || server.lobbyInfo.ClientInSlot(s) != null) if (slot.Closed || server.LobbyInfo.ClientInSlot(s) != null)
return false; return false;
client.Slot = s; client.Slot = s;
@@ -148,14 +148,14 @@ namespace OpenRA.Mods.RA.Server
return false; return false;
// kick any player that's in the slot // kick any player that's in the slot
var occupant = server.lobbyInfo.ClientInSlot(s); var occupant = server.LobbyInfo.ClientInSlot(s);
if (occupant != null) if (occupant != null)
{ {
if (occupant.Bot != null) if (occupant.Bot != null)
server.lobbyInfo.Clients.Remove(occupant); server.LobbyInfo.Clients.Remove(occupant);
else else
{ {
var occupantConn = server.conns.FirstOrDefault( c => c.PlayerIndex == occupant.Index ); var occupantConn = server.Conns.FirstOrDefault( c => c.PlayerIndex == occupant.Index );
if (occupantConn != null) if (occupantConn != null)
{ {
server.SendOrderTo(occupantConn, "ServerError", "Your slot was closed by the host"); server.SendOrderTo(occupantConn, "ServerError", "Your slot was closed by the host");
@@ -164,7 +164,7 @@ namespace OpenRA.Mods.RA.Server
} }
} }
server.lobbyInfo.Slots[s].Closed = true; server.LobbyInfo.Slots[s].Closed = true;
server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
@@ -174,13 +174,13 @@ namespace OpenRA.Mods.RA.Server
if (!ValidateSlotCommand( server, conn, client, s, true )) if (!ValidateSlotCommand( server, conn, client, s, true ))
return false; return false;
var slot = server.lobbyInfo.Slots[s]; var slot = server.LobbyInfo.Slots[s];
slot.Closed = false; slot.Closed = false;
// Slot may have a bot in it // Slot may have a bot in it
var occupant = server.lobbyInfo.ClientInSlot(s); var occupant = server.LobbyInfo.ClientInSlot(s);
if (occupant != null && occupant.Bot != null) if (occupant != null && occupant.Bot != null)
server.lobbyInfo.Clients.Remove(occupant); server.LobbyInfo.Clients.Remove(occupant);
server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
@@ -199,8 +199,8 @@ namespace OpenRA.Mods.RA.Server
if (!ValidateSlotCommand(server, conn, client, parts[0], true)) if (!ValidateSlotCommand(server, conn, client, parts[0], true))
return false; return false;
var slot = server.lobbyInfo.Slots[parts[0]]; var slot = server.LobbyInfo.Slots[parts[0]];
var bot = server.lobbyInfo.ClientInSlot(parts[0]); var bot = server.LobbyInfo.ClientInSlot(parts[0]);
int controllerClientIndex; int controllerClientIndex;
if (!int.TryParse(parts[1], out controllerClientIndex)) if (!int.TryParse(parts[1], out controllerClientIndex))
{ {
@@ -239,7 +239,7 @@ namespace OpenRA.Mods.RA.Server
var lum = (byte)server.Random.Next(51,255); var lum = (byte)server.Random.Next(51,255);
bot.Color = bot.PreferredColor = new HSLColor(hue, sat, lum); bot.Color = bot.PreferredColor = new HSLColor(hue, sat, lum);
server.lobbyInfo.Clients.Add(bot); server.LobbyInfo.Clients.Add(bot);
} }
else else
{ {
@@ -266,8 +266,8 @@ namespace OpenRA.Mods.RA.Server
server.SendOrderTo(conn, "Message", "Map not found"); server.SendOrderTo(conn, "Message", "Map not found");
return true; return true;
} }
server.lobbyInfo.GlobalSettings.Map = s; server.LobbyInfo.GlobalSettings.Map = s;
var oldSlots = server.lobbyInfo.Slots.Keys.ToArray(); var oldSlots = server.LobbyInfo.Slots.Keys.ToArray();
LoadMap(server); LoadMap(server);
SetDefaultDifficulty(server); SetDefaultDifficulty(server);
@@ -275,11 +275,11 @@ namespace OpenRA.Mods.RA.Server
// - Observers remain as observers // - Observers remain as observers
// - Players who now lack a slot are made observers // - Players who now lack a slot are made observers
// - Bots who now lack a slot are dropped // - Bots who now lack a slot are dropped
var slots = server.lobbyInfo.Slots.Keys.ToArray(); var slots = server.LobbyInfo.Slots.Keys.ToArray();
int i = 0; int i = 0;
foreach (var os in oldSlots) foreach (var os in oldSlots)
{ {
var c = server.lobbyInfo.ClientInSlot(os); var c = server.LobbyInfo.ClientInSlot(os);
if (c == null) if (c == null)
continue; continue;
@@ -290,11 +290,11 @@ namespace OpenRA.Mods.RA.Server
{ {
// Remove Bot from slot if slot forbids bots // Remove Bot from slot if slot forbids bots
if (c.Bot != null && !server.Map.Players[c.Slot].AllowBots) if (c.Bot != null && !server.Map.Players[c.Slot].AllowBots)
server.lobbyInfo.Clients.Remove(c); server.LobbyInfo.Clients.Remove(c);
S.SyncClientToPlayerReference(c, server.Map.Players[c.Slot]); S.SyncClientToPlayerReference(c, server.Map.Players[c.Slot]);
} }
else if (c.Bot != null) else if (c.Bot != null)
server.lobbyInfo.Clients.Remove(c); server.LobbyInfo.Clients.Remove(c);
} }
server.SyncLobbyInfo(); server.SyncLobbyInfo();
@@ -315,7 +315,7 @@ namespace OpenRA.Mods.RA.Server
return true; return true;
} }
bool.TryParse(s, out server.lobbyInfo.GlobalSettings.FragileAlliances); bool.TryParse(s, out server.LobbyInfo.GlobalSettings.FragileAlliances);
server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
@@ -334,7 +334,7 @@ namespace OpenRA.Mods.RA.Server
return true; return true;
} }
bool.TryParse(s, out server.lobbyInfo.GlobalSettings.AllowCheats); bool.TryParse(s, out server.LobbyInfo.GlobalSettings.AllowCheats);
server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
@@ -353,7 +353,7 @@ namespace OpenRA.Mods.RA.Server
return true; return true;
} }
bool.TryParse(s, out server.lobbyInfo.GlobalSettings.Shroud); bool.TryParse(s, out server.LobbyInfo.GlobalSettings.Shroud);
server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
@@ -373,7 +373,7 @@ namespace OpenRA.Mods.RA.Server
} }
bool.TryParse(s, out server.lobbyInfo.GlobalSettings.Fog); bool.TryParse(s, out server.LobbyInfo.GlobalSettings.Fog);
server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
@@ -393,11 +393,11 @@ namespace OpenRA.Mods.RA.Server
return true; return true;
} }
var maxTeams = (server.lobbyInfo.Clients.Count(c => c.Slot != null) + 1) / 2; var maxTeams = (server.LobbyInfo.Clients.Count(c => c.Slot != null) + 1) / 2;
teamCount = teamCount.Clamp(0, maxTeams); teamCount = teamCount.Clamp(0, maxTeams);
var players = server.lobbyInfo.Slots var players = server.LobbyInfo.Slots
.Select(slot => server.lobbyInfo.ClientInSlot(slot.Key)) .Select(slot => server.LobbyInfo.ClientInSlot(slot.Key))
.Where(c => c != null && !server.lobbyInfo.Slots[c.Slot].LockTeam); .Where(c => c != null && !server.LobbyInfo.Slots[c.Slot].LockTeam);
var assigned = 0; var assigned = 0;
var playerCount = players.Count(); var playerCount = players.Count();
@@ -433,7 +433,7 @@ namespace OpenRA.Mods.RA.Server
return true; return true;
} }
bool.TryParse(s, out server.lobbyInfo.GlobalSettings.Crates); bool.TryParse(s, out server.LobbyInfo.GlobalSettings.Crates);
server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
@@ -452,7 +452,7 @@ namespace OpenRA.Mods.RA.Server
return true; return true;
} }
bool.TryParse(s, out server.lobbyInfo.GlobalSettings.AllyBuildRadius); bool.TryParse(s, out server.LobbyInfo.GlobalSettings.AllyBuildRadius);
server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
@@ -472,7 +472,7 @@ namespace OpenRA.Mods.RA.Server
return true; return true;
} }
server.lobbyInfo.GlobalSettings.Difficulty = s; server.LobbyInfo.GlobalSettings.Difficulty = s;
server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
@@ -491,7 +491,7 @@ namespace OpenRA.Mods.RA.Server
return true; return true;
} }
server.lobbyInfo.GlobalSettings.StartingUnitsClass = s; server.LobbyInfo.GlobalSettings.StartingUnitsClass = s;
server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
@@ -510,7 +510,7 @@ namespace OpenRA.Mods.RA.Server
return true; return true;
} }
server.lobbyInfo.GlobalSettings.StartingCash = int.Parse(s); server.LobbyInfo.GlobalSettings.StartingCash = int.Parse(s);
server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
@@ -533,7 +533,7 @@ namespace OpenRA.Mods.RA.Server
int kickClientID; int kickClientID;
int.TryParse(split[0], out kickClientID); int.TryParse(split[0], out kickClientID);
var kickConn = server.conns.SingleOrDefault(c => server.GetClient(c) != null && server.GetClient(c).Index == kickClientID); var kickConn = server.Conns.SingleOrDefault(c => server.GetClient(c) != null && server.GetClient(c).Index == kickClientID);
if (kickConn == null) if (kickConn == null)
{ {
server.SendOrderTo(conn, "Message", "Noone in that slot."); server.SendOrderTo(conn, "Message", "Noone in that slot.");
@@ -570,14 +570,14 @@ namespace OpenRA.Mods.RA.Server
s => s =>
{ {
var parts = s.Split(' '); var parts = s.Split(' ');
var targetClient = server.lobbyInfo.ClientWithIndex(int.Parse(parts[0])); var targetClient = server.LobbyInfo.ClientWithIndex(int.Parse(parts[0]));
// Only the host can change other client's info // Only the host can change other client's info
if (targetClient.Index != client.Index && !client.IsAdmin) if (targetClient.Index != client.Index && !client.IsAdmin)
return true; return true;
// Map has disabled race changes // Map has disabled race changes
if (server.lobbyInfo.Slots[targetClient.Slot].LockRace) if (server.LobbyInfo.Slots[targetClient.Slot].LockRace)
return true; return true;
targetClient.Country = parts[1]; targetClient.Country = parts[1];
@@ -588,14 +588,14 @@ namespace OpenRA.Mods.RA.Server
s => s =>
{ {
var parts = s.Split(' '); var parts = s.Split(' ');
var targetClient = server.lobbyInfo.ClientWithIndex(int.Parse(parts[0])); var targetClient = server.LobbyInfo.ClientWithIndex(int.Parse(parts[0]));
// Only the host can change other client's info // Only the host can change other client's info
if (targetClient.Index != client.Index && !client.IsAdmin) if (targetClient.Index != client.Index && !client.IsAdmin)
return true; return true;
// Map has disabled team changes // Map has disabled team changes
if (server.lobbyInfo.Slots[targetClient.Slot].LockTeam) if (server.LobbyInfo.Slots[targetClient.Slot].LockTeam)
return true; return true;
int team; int team;
@@ -613,7 +613,7 @@ namespace OpenRA.Mods.RA.Server
s => s =>
{ {
var parts = s.Split(' '); var parts = s.Split(' ');
var targetClient = server.lobbyInfo.ClientWithIndex(int.Parse(parts[0])); var targetClient = server.LobbyInfo.ClientWithIndex(int.Parse(parts[0]));
// Only the host can change other client's info // Only the host can change other client's info
if (targetClient.Index != client.Index && !client.IsAdmin) if (targetClient.Index != client.Index && !client.IsAdmin)
@@ -624,7 +624,7 @@ namespace OpenRA.Mods.RA.Server
return true; return true;
// Map has disabled spawn changes // Map has disabled spawn changes
if (server.lobbyInfo.Slots[targetClient.Slot].LockSpawn) if (server.LobbyInfo.Slots[targetClient.Slot].LockSpawn)
return true; return true;
int spawnPoint; int spawnPoint;
@@ -634,7 +634,7 @@ namespace OpenRA.Mods.RA.Server
return true; return true;
} }
if (server.lobbyInfo.Clients.Where( cc => cc != client ).Any( cc => (cc.SpawnPoint == spawnPoint) && (cc.SpawnPoint != 0) )) if (server.LobbyInfo.Clients.Where( cc => cc != client ).Any( cc => (cc.SpawnPoint == spawnPoint) && (cc.SpawnPoint != 0) ))
{ {
server.SendOrderTo(conn, "Message", "You can't be at the same spawn point as another player"); server.SendOrderTo(conn, "Message", "You can't be at the same spawn point as another player");
return true; return true;
@@ -648,14 +648,14 @@ namespace OpenRA.Mods.RA.Server
s => s =>
{ {
var parts = s.Split(' '); var parts = s.Split(' ');
var targetClient = server.lobbyInfo.ClientWithIndex(int.Parse(parts[0])); var targetClient = server.LobbyInfo.ClientWithIndex(int.Parse(parts[0]));
// Only the host can change other client's info // Only the host can change other client's info
if (targetClient.Index != client.Index && !client.IsAdmin) if (targetClient.Index != client.Index && !client.IsAdmin)
return true; return true;
// Spectator or map has disabled color changes // Spectator or map has disabled color changes
if (targetClient.Slot == null || server.lobbyInfo.Slots[targetClient.Slot].LockColor) if (targetClient.Slot == null || server.LobbyInfo.Slots[targetClient.Slot].LockColor)
return true; return true;
var ci = parts[1].Split(',').Select(cc => int.Parse(cc)).ToArray(); var ci = parts[1].Split(',').Select(cc => int.Parse(cc)).ToArray();
@@ -701,25 +701,25 @@ namespace OpenRA.Mods.RA.Server
static void LoadMap(S server) static void LoadMap(S server)
{ {
server.Map = new Map(server.ModData.AvailableMaps[server.lobbyInfo.GlobalSettings.Map].Path); server.Map = new Map(server.ModData.AvailableMaps[server.LobbyInfo.GlobalSettings.Map].Path);
server.lobbyInfo.Slots = server.Map.Players server.LobbyInfo.Slots = server.Map.Players
.Select(p => MakeSlotFromPlayerReference(p.Value)) .Select(p => MakeSlotFromPlayerReference(p.Value))
.Where(s => s != null) .Where(s => s != null)
.ToDictionary(s => s.PlayerReference, s => s); .ToDictionary(s => s.PlayerReference, s => s);
server.Map.Options.UpdateServerSettings(server.lobbyInfo.GlobalSettings); server.Map.Options.UpdateServerSettings(server.LobbyInfo.GlobalSettings);
} }
static void SetDefaultDifficulty(S server) static void SetDefaultDifficulty(S server)
{ {
if (!server.Map.Options.Difficulties.Any()) if (!server.Map.Options.Difficulties.Any())
{ {
server.lobbyInfo.GlobalSettings.Difficulty = null; server.LobbyInfo.GlobalSettings.Difficulty = null;
return; return;
} }
if (!server.Map.Options.Difficulties.Contains(server.lobbyInfo.GlobalSettings.Difficulty)) if (!server.Map.Options.Difficulties.Contains(server.LobbyInfo.GlobalSettings.Difficulty))
server.lobbyInfo.GlobalSettings.Difficulty = server.Map.Options.Difficulties.First(); server.LobbyInfo.GlobalSettings.Difficulty = server.Map.Options.Difficulties.First();
} }
} }
} }

View File

@@ -65,10 +65,10 @@ namespace OpenRA.Mods.RA.Server
server.Settings.MasterServer + url.F( server.Settings.MasterServer + url.F(
server.Settings.ExternalPort, Uri.EscapeUriString(server.Settings.Name), server.Settings.ExternalPort, Uri.EscapeUriString(server.Settings.Name),
(int)server.State, (int)server.State,
server.lobbyInfo.Clients.Where(c1 => c1.Bot == null).Count(), server.LobbyInfo.Clients.Where(c1 => c1.Bot == null).Count(),
server.lobbyInfo.Clients.Where(c1 => c1.Bot != null).Count(), server.LobbyInfo.Clients.Where(c1 => c1.Bot != null).Count(),
Game.CurrentMods.Select(f => "{0}@{1}".F(f.Key, f.Value.Version)).JoinWith(","), Game.CurrentMods.Select(f => "{0}@{1}".F(f.Key, f.Value.Version)).JoinWith(","),
server.lobbyInfo.GlobalSettings.Map, server.LobbyInfo.GlobalSettings.Map,
server.Map.PlayerCount)); server.Map.PlayerCount));
if (isInitialPing) if (isInitialPing)

View File

@@ -33,7 +33,7 @@ namespace OpenRA.Mods.RA.Server
{ {
isInitialPing = false; isInitialPing = false;
lastPing = Environment.TickCount; lastPing = Environment.TickCount;
foreach (var p in server.conns) foreach (var p in server.Conns)
server.SendOrderTo(p, "Ping", Environment.TickCount.ToString()); server.SendOrderTo(p, "Ping", Environment.TickCount.ToString());
} }
} }