Pass the client into InterpretCommand

This commit is contained in:
Paul Chote
2010-11-08 17:07:27 +13:00
parent b77dcd476c
commit e83838e9ff
4 changed files with 28 additions and 31 deletions

View File

@@ -263,7 +263,7 @@ namespace OpenRA.Server
{ {
bool handled = false; bool handled = false;
foreach (var t in ServerTraits.WithInterface<IInterpretCommand>()) foreach (var t in ServerTraits.WithInterface<IInterpretCommand>())
if ((handled = t.InterpretCommand(conn, so.Data))) if ((handled = t.InterpretCommand(conn, GetClient(conn), so.Data)))
break; break;
if (!handled) if (!handled)

View File

@@ -18,7 +18,7 @@ namespace OpenRA.Server.Traits
{ {
public class LobbyCommands : IInterpretCommand, IStartServer, IClientJoined public class LobbyCommands : IInterpretCommand, IStartServer, IClientJoined
{ {
public bool InterpretCommand(Connection conn, string cmd) public bool InterpretCommand(Connection conn, Session.Client client, string cmd)
{ {
var dict = new Dictionary<string, Func<string, bool>> var dict = new Dictionary<string, Func<string, bool>>
{ {
@@ -26,8 +26,6 @@ namespace OpenRA.Server.Traits
s => s =>
{ {
// if we're downloading, we can't ready up. // if we're downloading, we can't ready up.
var client = Server.GetClient(conn);
if (client.State == Session.ClientState.NotReady) if (client.State == Session.ClientState.NotReady)
client.State = Session.ClientState.Ready; client.State = Session.ClientState.Ready;
else if (client.State == Session.ClientState.Ready) else if (client.State == Session.ClientState.Ready)
@@ -39,7 +37,7 @@ namespace OpenRA.Server.Traits
Server.SyncLobbyInfo(); Server.SyncLobbyInfo();
if (Server.conns.Count > 0 && Server.conns.All(c => Server.GetClient(c).State == Session.ClientState.Ready)) if (Server.conns.Count > 0 && Server.conns.All(c => Server.GetClient(c).State == Session.ClientState.Ready))
InterpretCommand(conn, "startgame"); InterpretCommand(conn, client, "startgame");
return true; return true;
}}, }},
@@ -68,9 +66,8 @@ namespace OpenRA.Server.Traits
if (slotData == null) if (slotData == null)
return true; return true;
var cl = Server.GetClient(conn); client.Slot = slotData.Index;
cl.Slot = slotData.Index; SyncClientToPlayerReference(client, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null);
SyncClientToPlayerReference(cl, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null);
Server.SyncLobbyInfo(); Server.SyncLobbyInfo();
return true; return true;
@@ -86,9 +83,8 @@ namespace OpenRA.Server.Traits
|| Server.lobbyInfo.Clients.Any( c => c.Slot == slot )) || Server.lobbyInfo.Clients.Any( c => c.Slot == slot ))
return false; return false;
var cl = Server.GetClient(conn); client.Slot = slot;
cl.Slot = slot; SyncClientToPlayerReference(client, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null);
SyncClientToPlayerReference(cl, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null);
Server.SyncLobbyInfo(); Server.SyncLobbyInfo();
return true; return true;
@@ -186,14 +182,14 @@ namespace OpenRA.Server.Traits
Server.lobbyInfo.GlobalSettings.Map = s; Server.lobbyInfo.GlobalSettings.Map = s;
LoadMap(); LoadMap();
foreach(var client in Server.lobbyInfo.Clients) foreach(var c in Server.lobbyInfo.Clients)
{ {
client.SpawnPoint = 0; c.SpawnPoint = 0;
var slotData = Server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == client.Slot ); var slotData = Server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == c.Slot );
if (slotData != null && slotData.MapPlayer != null) if (slotData != null && slotData.MapPlayer != null)
SyncClientToPlayerReference(client, Server.Map.Players[slotData.MapPlayer]); SyncClientToPlayerReference(c, Server.Map.Players[slotData.MapPlayer]);
client.State = Session.ClientState.NotReady; c.State = Session.ClientState.NotReady;
} }
Server.SyncLobbyInfo(); Server.SyncLobbyInfo();

View File

@@ -18,7 +18,7 @@ namespace OpenRA.Server.Traits
{ {
public class PlayerCommands : IInterpretCommand public class PlayerCommands : IInterpretCommand
{ {
public bool InterpretCommand(Connection conn, string cmd) public bool InterpretCommand(Connection conn, Session.Client client, string cmd)
{ {
var dict = new Dictionary<string, Func<string, bool>> var dict = new Dictionary<string, Func<string, bool>>
{ {
@@ -26,14 +26,14 @@ namespace OpenRA.Server.Traits
s => s =>
{ {
Log.Write("server", "Player@{0} is now known as {1}", conn.socket.RemoteEndPoint, s); Log.Write("server", "Player@{0} is now known as {1}", conn.socket.RemoteEndPoint, s);
Server.GetClient(conn).Name = s; client.Name = s;
Server.SyncLobbyInfo(); Server.SyncLobbyInfo();
return true; return true;
}}, }},
{ "race", { "race",
s => s =>
{ {
Server.GetClient(conn).Country = s; client.Country = s;
Server.SyncLobbyInfo(); Server.SyncLobbyInfo();
return true; return true;
}}, }},
@@ -43,7 +43,7 @@ namespace OpenRA.Server.Traits
int team; int team;
if (!int.TryParse(s, out team)) { Log.Write("server", "Invalid team: {0}", s ); return false; } if (!int.TryParse(s, out team)) { Log.Write("server", "Invalid team: {0}", s ); return false; }
Server.GetClient(conn).Team = team; client.Team = team;
Server.SyncLobbyInfo(); Server.SyncLobbyInfo();
return true; return true;
}}, }},
@@ -57,13 +57,13 @@ namespace OpenRA.Server.Traits
return false; return false;
} }
if (Server.lobbyInfo.Clients.Where( c => c != Server.GetClient(conn) ).Any( c => (c.SpawnPoint == spawnPoint) && (c.SpawnPoint != 0) )) if (Server.lobbyInfo.Clients.Where( c => c != client ).Any( c => (c.SpawnPoint == spawnPoint) && (c.SpawnPoint != 0) ))
{ {
Server.SendChatTo( conn, "You can't be at the same spawn point as another player" ); Server.SendChatTo( conn, "You can't be at the same spawn point as another player" );
return true; return true;
} }
Server.GetClient(conn).SpawnPoint = spawnPoint; client.SpawnPoint = spawnPoint;
Server.SyncLobbyInfo(); Server.SyncLobbyInfo();
return true; return true;
}}, }},
@@ -71,8 +71,8 @@ namespace OpenRA.Server.Traits
s => s =>
{ {
var c = s.Split(',').Select(cc => int.Parse(cc)).ToArray(); var c = s.Split(',').Select(cc => int.Parse(cc)).ToArray();
Server.GetClient(conn).Color1 = Color.FromArgb(c[0],c[1],c[2]); client.Color1 = Color.FromArgb(c[0],c[1],c[2]);
Server.GetClient(conn).Color2 = Color.FromArgb(c[3],c[4],c[5]); client.Color2 = Color.FromArgb(c[3],c[4],c[5]);
Server.SyncLobbyInfo(); Server.SyncLobbyInfo();
return true; return true;
}} }}

View File

@@ -8,25 +8,26 @@
*/ */
#endregion #endregion
using System; using System;
using OpenRA.Network;
namespace OpenRA.Server.Traits 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, Session.Client client, string cmd); }
public interface INotifySyncLobbyInfo { void LobbyInfoSynced(); }
public interface IStartServer { void ServerStarted(); }
public interface IStartGame { void GameStarted(); }
public interface IClientJoined { void ClientJoined(Connection conn); }
public interface ITick public interface ITick
{ {
void Tick(); void Tick();
int TickTimeout { get; } int TickTimeout { get; }
} }
public interface INotifySyncLobbyInfo { void LobbyInfoSynced(); }
public interface IStartServer { void ServerStarted(); }
public interface IStartGame { void GameStarted(); }
public interface IClientJoined { void ClientJoined(Connection conn); }
public class DebugServerTrait : IInterpretCommand, IStartGame, INotifySyncLobbyInfo public class DebugServerTrait : IInterpretCommand, IStartGame, INotifySyncLobbyInfo
{ {
public bool InterpretCommand(Connection conn, string cmd) public bool InterpretCommand(Connection conn, Session.Client client, string cmd)
{ {
Console.WriteLine("Server received command from player {1}: {0}",cmd, conn.PlayerIndex); Console.WriteLine("Server received command from player {1}: {0}",cmd, conn.PlayerIndex);
return false; return false;