diff --git a/OpenRA.Server/Connection.cs b/OpenRA.Server/Connection.cs index bb7246386e..a905ad6a17 100644 --- a/OpenRA.Server/Connection.cs +++ b/OpenRA.Server/Connection.cs @@ -17,6 +17,7 @@ namespace OpenRA.Server /* client data */ public bool IsReady; + public int PlayerIndex; public byte[] PopBytes(int n) { diff --git a/OpenRA.Server/Server.cs b/OpenRA.Server/Server.cs index 9fbc7dd2fa..a719e0a950 100644 --- a/OpenRA.Server/Server.cs +++ b/OpenRA.Server/Server.cs @@ -88,17 +88,37 @@ namespace OpenRA.Server } } + static int ChooseFreePlayerIndex() + { + for (var i = 0; i < 8; i++) + if (conns.All(c => c.PlayerIndex != i)) + return i; + + throw new InvalidOperationException("Already got 8 players"); + } + static void AcceptConnection() { var newConn = new Connection { socket = listener.AcceptSocket() }; - newConn.socket.Blocking = false; - newConn.socket.NoDelay = true; - conns.Add(newConn); + try + { + newConn.socket.Blocking = false; + newConn.socket.NoDelay = true; - /* todo: assign a player number, setup host behavior, etc */ + // assign the player number. + newConn.PlayerIndex = ChooseFreePlayerIndex(); - Console.WriteLine("Accepted connection from {0}.", - newConn.socket.RemoteEndPoint); + conns.Add(newConn); + + DispatchOrdersToClient(newConn, 0, + new ServerOrder(newConn.PlayerIndex, "AssignPlayer", "").Serialize()); + + // todo: tell this client about all the other conns. + + Console.WriteLine("Accepted connection from {0}.", + newConn.socket.RemoteEndPoint); + } + catch (Exception e) { DropClient(newConn, e); } } static bool ReadDataInner(Connection conn) @@ -163,20 +183,23 @@ namespace OpenRA.Server // conn.socket.RemoteEndPoint); } + static void DispatchOrdersToClient(Connection c, int frame, byte[] data) + { + try + { + c.socket.Blocking = true; + c.socket.Send(BitConverter.GetBytes(data.Length + 4)); + c.socket.Send(BitConverter.GetBytes(frame)); + c.socket.Send(data); + c.socket.Blocking = false; + } + catch (Exception e) { DropClient(c, e); } + } + static void DispatchOrders(Connection conn, int frame, byte[] data) { foreach (var c in conns.Except(conn).ToArray()) - { - try - { - c.socket.Blocking = true; - c.socket.Send(BitConverter.GetBytes(data.Length + 4)); - c.socket.Send(BitConverter.GetBytes(frame)); - c.socket.Send(data); - c.socket.Blocking = false; - } - catch (Exception e) { DropClient(c, e); } - } + DispatchOrdersToClient(c, frame, data); if (frame == 0 && conn != null) InterpretServerOrders(conn, data); diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index 630f191d8c..bc99ea7c7d 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -33,7 +33,11 @@ namespace OpenRa.Game public static Dictionary players = new Dictionary(); - public static Player LocalPlayer { get { return players[localPlayerIndex]; } } + public static Player LocalPlayer + { + get { return players[localPlayerIndex]; } + set { localPlayerIndex = value.Index; } + } public static BuildingInfluenceMap BuildingInfluence; public static UnitInfluenceMap UnitInfluence; diff --git a/OpenRa.Game/UnitOrders.cs b/OpenRa.Game/UnitOrders.cs index f4e2d46113..4ce12c33f9 100755 --- a/OpenRa.Game/UnitOrders.cs +++ b/OpenRa.Game/UnitOrders.cs @@ -104,7 +104,9 @@ namespace OpenRa.Game } case "AssignPlayer": { - break; /* todo: set LocalPlayer based on this */ + Game.LocalPlayer = order.Player; + Game.chat.AddLine(Pair.New(order.Player.PlayerName, "is now YOU.")); + break; } case "StartGame": {