added server-driven local player choosing
This commit is contained in:
@@ -17,6 +17,7 @@ namespace OpenRA.Server
|
||||
|
||||
/* client data */
|
||||
public bool IsReady;
|
||||
public int PlayerIndex;
|
||||
|
||||
public byte[] PopBytes(int n)
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -33,7 +33,11 @@ namespace OpenRa.Game
|
||||
|
||||
public static Dictionary<int, Player> players = new Dictionary<int, Player>();
|
||||
|
||||
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;
|
||||
|
||||
|
||||
@@ -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":
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user