added server-driven local player choosing
This commit is contained in:
@@ -17,6 +17,7 @@ namespace OpenRA.Server
|
|||||||
|
|
||||||
/* client data */
|
/* client data */
|
||||||
public bool IsReady;
|
public bool IsReady;
|
||||||
|
public int PlayerIndex;
|
||||||
|
|
||||||
public byte[] PopBytes(int n)
|
public byte[] PopBytes(int n)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -88,18 +88,38 @@ 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()
|
static void AcceptConnection()
|
||||||
{
|
{
|
||||||
var newConn = new Connection { socket = listener.AcceptSocket() };
|
var newConn = new Connection { socket = listener.AcceptSocket() };
|
||||||
|
try
|
||||||
|
{
|
||||||
newConn.socket.Blocking = false;
|
newConn.socket.Blocking = false;
|
||||||
newConn.socket.NoDelay = true;
|
newConn.socket.NoDelay = true;
|
||||||
|
|
||||||
|
// assign the player number.
|
||||||
|
newConn.PlayerIndex = ChooseFreePlayerIndex();
|
||||||
|
|
||||||
conns.Add(newConn);
|
conns.Add(newConn);
|
||||||
|
|
||||||
/* todo: assign a player number, setup host behavior, etc */
|
DispatchOrdersToClient(newConn, 0,
|
||||||
|
new ServerOrder(newConn.PlayerIndex, "AssignPlayer", "").Serialize());
|
||||||
|
|
||||||
|
// todo: tell this client about all the other conns.
|
||||||
|
|
||||||
Console.WriteLine("Accepted connection from {0}.",
|
Console.WriteLine("Accepted connection from {0}.",
|
||||||
newConn.socket.RemoteEndPoint);
|
newConn.socket.RemoteEndPoint);
|
||||||
}
|
}
|
||||||
|
catch (Exception e) { DropClient(newConn, e); }
|
||||||
|
}
|
||||||
|
|
||||||
static bool ReadDataInner(Connection conn)
|
static bool ReadDataInner(Connection conn)
|
||||||
{
|
{
|
||||||
@@ -163,9 +183,7 @@ namespace OpenRA.Server
|
|||||||
// conn.socket.RemoteEndPoint);
|
// conn.socket.RemoteEndPoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void DispatchOrders(Connection conn, int frame, byte[] data)
|
static void DispatchOrdersToClient(Connection c, int frame, byte[] data)
|
||||||
{
|
|
||||||
foreach (var c in conns.Except(conn).ToArray())
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -178,6 +196,11 @@ namespace OpenRA.Server
|
|||||||
catch (Exception e) { DropClient(c, e); }
|
catch (Exception e) { DropClient(c, e); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void DispatchOrders(Connection conn, int frame, byte[] data)
|
||||||
|
{
|
||||||
|
foreach (var c in conns.Except(conn).ToArray())
|
||||||
|
DispatchOrdersToClient(c, frame, data);
|
||||||
|
|
||||||
if (frame == 0 && conn != null)
|
if (frame == 0 && conn != null)
|
||||||
InterpretServerOrders(conn, data);
|
InterpretServerOrders(conn, data);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,11 @@ namespace OpenRa.Game
|
|||||||
|
|
||||||
public static Dictionary<int, Player> players = new Dictionary<int, Player>();
|
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 BuildingInfluenceMap BuildingInfluence;
|
||||||
public static UnitInfluenceMap UnitInfluence;
|
public static UnitInfluenceMap UnitInfluence;
|
||||||
|
|
||||||
|
|||||||
@@ -104,7 +104,9 @@ namespace OpenRa.Game
|
|||||||
}
|
}
|
||||||
case "AssignPlayer":
|
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":
|
case "StartGame":
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user