Move ServerTraits into Mods.

This commit is contained in:
Paul Chote
2010-11-10 09:57:11 +13:00
parent 8e007131c9
commit dfa14f16d3
7 changed files with 82 additions and 74 deletions

View File

@@ -192,10 +192,7 @@
<Compile Include="Traits\ActorStance.cs" /> <Compile Include="Traits\ActorStance.cs" />
<Compile Include="Traits\Armor.cs" /> <Compile Include="Traits\Armor.cs" />
<Compile Include="Graphics\CursorProvider.cs" /> <Compile Include="Graphics\CursorProvider.cs" />
<Compile Include="ServerTraits\TraitInterfaces.cs" /> <Compile Include="Server\TraitInterfaces.cs" />
<Compile Include="ServerTraits\LobbyCommands.cs" />
<Compile Include="ServerTraits\PlayerCommands.cs" />
<Compile Include="ServerTraits\MasterServerPinger.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj"> <ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -20,7 +20,6 @@ using System.Threading;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.GameRules; using OpenRA.GameRules;
using OpenRA.Network; using OpenRA.Network;
using OpenRA.Server.Traits;
namespace OpenRA.Server namespace OpenRA.Server
{ {

View File

@@ -10,7 +10,7 @@
using System; using System;
using OpenRA.Network; using OpenRA.Network;
namespace OpenRA.Server.Traits namespace OpenRA.Server
{ {
// Returns true if order is handled // Returns true if order is handled
public interface IInterpretCommand { bool InterpretCommand(Connection conn, Session.Client client, string cmd); } public interface IInterpretCommand { bool InterpretCommand(Connection conn, Session.Client client, string cmd); }

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -292,6 +292,9 @@
<Compile Include="Scripting\Media.cs" /> <Compile Include="Scripting\Media.cs" />
<Compile Include="OpenWidgetAtGameStart.cs" /> <Compile Include="OpenWidgetAtGameStart.cs" />
<Compile Include="World\WorldGameOver.cs" /> <Compile Include="World\WorldGameOver.cs" />
<Compile Include="ServerTraits\MasterServerPinger.cs" />
<Compile Include="ServerTraits\PlayerCommands.cs" />
<Compile Include="ServerTraits\LobbyCommands.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj"> <ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">
@@ -318,4 +321,7 @@
copy "$(TargetPath)" "$(SolutionDir)mods/ra/" copy "$(TargetPath)" "$(SolutionDir)mods/ra/"
cd "$(SolutionDir)"</PostBuildEvent> cd "$(SolutionDir)"</PostBuildEvent>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Folder Include="ServerTraits\" />
</ItemGroup>
</Project> </Project>

View File

@@ -13,8 +13,10 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRA.Network; using OpenRA.Network;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.Server;
using server = OpenRA.Server.Server;
namespace OpenRA.Server.Traits namespace OpenRA.Mods.RA.Server
{ {
public class LobbyCommands : ServerTrait, IInterpretCommand, INotifyServerStart, IClientJoined public class LobbyCommands : ServerTrait, IInterpretCommand, INotifyServerStart, IClientJoined
{ {
@@ -22,14 +24,14 @@ namespace OpenRA.Server.Traits
public bool InterpretCommand(Connection conn, Session.Client client, string cmd) public bool InterpretCommand(Connection conn, Session.Client client, string cmd)
{ {
if (Server.GameStarted) if (server.GameStarted)
{ {
Server.SendChatTo(conn, "Cannot change state when game started. ({0})".F(cmd)); server.SendChatTo(conn, "Cannot change state when game started. ({0})".F(cmd));
return false; return false;
} }
else if (client.State == Session.ClientState.Ready && !(cmd == "ready" || cmd == "startgame")) else if (client.State == Session.ClientState.Ready && !(cmd == "ready" || cmd == "startgame"))
{ {
Server.SendChatTo(conn, "Cannot change state when marked as ready."); server.SendChatTo(conn, "Cannot change state when marked as ready.");
return false; return false;
} }
@@ -47,9 +49,9 @@ namespace OpenRA.Server.Traits
Log.Write("server", "Player @{0} is {1}", Log.Write("server", "Player @{0} is {1}",
conn.socket.RemoteEndPoint, client.State); conn.socket.RemoteEndPoint, client.State);
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, client, "startgame"); InterpretCommand(conn, client, "startgame");
return true; return true;
@@ -57,7 +59,7 @@ namespace OpenRA.Server.Traits
{ "startgame", { "startgame",
s => s =>
{ {
Server.StartGame(); server.StartGame();
return true; return true;
}}, }},
{ "lag", { "lag",
@@ -68,21 +70,21 @@ namespace OpenRA.Server.Traits
Log.Write("server", "Order lag is now {0} frames.", lag); Log.Write("server", "Order lag is now {0} frames.", lag);
Server.lobbyInfo.GlobalSettings.OrderLatency = lag; server.lobbyInfo.GlobalSettings.OrderLatency = lag;
Server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
{ "spectator", { "spectator",
s => s =>
{ {
var slotData = Server.lobbyInfo.Slots.Where(ax => ax.Spectator && !Server.lobbyInfo.Clients.Any(l => l.Slot == ax.Index)).FirstOrDefault(); var slotData = server.lobbyInfo.Slots.Where(ax => ax.Spectator && !server.lobbyInfo.Clients.Any(l => l.Slot == ax.Index)).FirstOrDefault();
if (slotData == null) if (slotData == null)
return true; return true;
client.Slot = slotData.Index; client.Slot = slotData.Index;
SyncClientToPlayerReference(client, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null); SyncClientToPlayerReference(client, slotData.MapPlayer != null ? server.Map.Players[slotData.MapPlayer] : null);
Server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
{ "slot", { "slot",
@@ -91,15 +93,15 @@ namespace OpenRA.Server.Traits
int slot; int slot;
if (!int.TryParse(s, out slot)) { Log.Write("server", "Invalid slot: {0}", s ); return false; } if (!int.TryParse(s, out slot)) { Log.Write("server", "Invalid slot: {0}", s ); return false; }
var slotData = Server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == slot ); var slotData = server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == slot );
if (slotData == null || slotData.Closed || slotData.Bot != null if (slotData == null || slotData.Closed || slotData.Bot != null
|| Server.lobbyInfo.Clients.Any( c => c.Slot == slot )) || server.lobbyInfo.Clients.Any( c => c.Slot == slot ))
return false; return false;
client.Slot = slot; client.Slot = slot;
SyncClientToPlayerReference(client, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null); SyncClientToPlayerReference(client, slotData.MapPlayer != null ? server.Map.Players[slotData.MapPlayer] : null);
Server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
{ "slot_close", { "slot_close",
@@ -108,13 +110,13 @@ namespace OpenRA.Server.Traits
int slot; int slot;
if (!int.TryParse(s, out slot)) { Log.Write("server", "Invalid slot: {0}", s ); return false; } if (!int.TryParse(s, out slot)) { Log.Write("server", "Invalid slot: {0}", s ); return false; }
var slotData = Server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == slot ); var slotData = server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == slot );
if (slotData == null) if (slotData == null)
return false; return false;
if (conn.PlayerIndex != 0) if (conn.PlayerIndex != 0)
{ {
Server.SendChatTo( conn, "Only the host can alter slots" ); server.SendChatTo( conn, "Only the host can alter slots" );
return true; return true;
} }
@@ -122,15 +124,15 @@ namespace OpenRA.Server.Traits
slotData.Bot = null; slotData.Bot = null;
/* kick any player that's in the slot */ /* kick any player that's in the slot */
var occupant = Server.lobbyInfo.Clients.FirstOrDefault( c => c.Slot == slotData.Index ); var occupant = server.lobbyInfo.Clients.FirstOrDefault( c => c.Slot == slotData.Index );
if (occupant != null) if (occupant != null)
{ {
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.DropClient( occupantConn, new Exception() ); server.DropClient( occupantConn, new Exception() );
} }
Server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
{ "slot_open", { "slot_open",
@@ -139,20 +141,20 @@ namespace OpenRA.Server.Traits
int slot; int slot;
if (!int.TryParse(s, out slot)) { Log.Write("server", "Invalid slot: {0}", s ); return false; } if (!int.TryParse(s, out slot)) { Log.Write("server", "Invalid slot: {0}", s ); return false; }
var slotData = Server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == slot ); var slotData = server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == slot );
if (slotData == null) if (slotData == null)
return false; return false;
if (conn.PlayerIndex != 0) if (conn.PlayerIndex != 0)
{ {
Server.SendChatTo( conn, "Only the host can alter slots" ); server.SendChatTo( conn, "Only the host can alter slots" );
return true; return true;
} }
slotData.Closed = false; slotData.Closed = false;
slotData.Bot = null; slotData.Bot = null;
Server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
{ "slot_bot", { "slot_bot",
@@ -162,26 +164,26 @@ namespace OpenRA.Server.Traits
if (parts.Length != 2) if (parts.Length != 2)
{ {
Server.SendChatTo( conn, "Malformed slot_bot command" ); server.SendChatTo( conn, "Malformed slot_bot command" );
return true; return true;
} }
int slot; int slot;
if (!int.TryParse(parts[0], out slot)) { Log.Write("server", "Invalid slot: {0}", s ); return false; } if (!int.TryParse(parts[0], out slot)) { Log.Write("server", "Invalid slot: {0}", s ); return false; }
var slotData = Server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == slot ); var slotData = server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == slot );
if (slotData == null) if (slotData == null)
return false; return false;
if (conn.PlayerIndex != 0) if (conn.PlayerIndex != 0)
{ {
Server.SendChatTo( conn, "Only the host can alter slots" ); server.SendChatTo( conn, "Only the host can alter slots" );
return true; return true;
} }
slotData.Bot = parts[1]; slotData.Bot = parts[1];
Server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
{ "map", { "map",
@@ -189,23 +191,23 @@ namespace OpenRA.Server.Traits
{ {
if (conn.PlayerIndex != 0) if (conn.PlayerIndex != 0)
{ {
Server.SendChatTo( conn, "Only the host can change the map" ); server.SendChatTo( conn, "Only the host can change the map" );
return true; return true;
} }
Server.lobbyInfo.GlobalSettings.Map = s; server.lobbyInfo.GlobalSettings.Map = s;
LoadMap(); LoadMap();
foreach(var c in Server.lobbyInfo.Clients) foreach(var c in server.lobbyInfo.Clients)
{ {
c.SpawnPoint = 0; c.SpawnPoint = 0;
var slotData = Server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == c.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(c, Server.Map.Players[slotData.MapPlayer]); SyncClientToPlayerReference(c, server.Map.Players[slotData.MapPlayer]);
c.State = Session.ClientState.NotReady; c.State = Session.ClientState.NotReady;
} }
Server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
{ "lockteams", { "lockteams",
@@ -213,12 +215,12 @@ namespace OpenRA.Server.Traits
{ {
if (conn.PlayerIndex != 0) if (conn.PlayerIndex != 0)
{ {
Server.SendChatTo( conn, "Only the host can set that option" ); server.SendChatTo( conn, "Only the host can set that option" );
return true; return true;
} }
bool.TryParse(s, out Server.lobbyInfo.GlobalSettings.LockTeams); bool.TryParse(s, out server.lobbyInfo.GlobalSettings.LockTeams);
Server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
}; };
@@ -247,8 +249,8 @@ namespace OpenRA.Server.Traits
public static void LoadMap() public static void LoadMap()
{ {
Server.Map = new Map(Server.ModData.AvailableMaps[Server.lobbyInfo.GlobalSettings.Map]); server.Map = new Map(server.ModData.AvailableMaps[server.lobbyInfo.GlobalSettings.Map]);
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)
.Select((s, i) => { s.Index = i; return s; }) .Select((s, i) => { s.Index = i; return s; })
@@ -256,10 +258,10 @@ namespace OpenRA.Server.Traits
// Generate slots for spectators // Generate slots for spectators
for (int i = 0; i < MaxSpectators; i++) for (int i = 0; i < MaxSpectators; i++)
Server.lobbyInfo.Slots.Add(new Session.Slot server.lobbyInfo.Slots.Add(new Session.Slot
{ {
Spectator = true, Spectator = true,
Index = Server.lobbyInfo.Slots.Count(), Index = server.lobbyInfo.Slots.Count(),
MapPlayer = null, MapPlayer = null,
Bot = null Bot = null
}); });
@@ -282,23 +284,23 @@ namespace OpenRA.Server.Traits
Slot = ChooseFreeSlot(), Slot = ChooseFreeSlot(),
}; };
var slotData = Server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == client.Slot ); var slotData = server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == client.Slot );
if (slotData != null) if (slotData != null)
SyncClientToPlayerReference(client, Server.Map.Players[slotData.MapPlayer]); SyncClientToPlayerReference(client, server.Map.Players[slotData.MapPlayer]);
Server.lobbyInfo.Clients.Add(client); server.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);
Server.SendChat(newConn, "has joined the game."); server.SendChat(newConn, "has joined the game.");
Server.SyncLobbyInfo(); server.SyncLobbyInfo();
} }
static int ChooseFreeSlot() static int ChooseFreeSlot()
{ {
return Server.lobbyInfo.Slots.First(s => !s.Closed && s.Bot == null return server.lobbyInfo.Slots.First(s => !s.Closed && s.Bot == null
&& !Server.lobbyInfo.Clients.Any( c => c.Slot == s.Index )).Index; && !server.lobbyInfo.Clients.Any( c => c.Slot == s.Index )).Index;
} }

View File

@@ -11,8 +11,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;
using OpenRA.Server;
using server = OpenRA.Server.Server;
namespace OpenRA.Server.Traits namespace OpenRA.Mods.RA.Server
{ {
public class MasterServerPinger : ServerTrait, ITick, INotifySyncLobbyInfo, IStartGame public class MasterServerPinger : ServerTrait, ITick, INotifySyncLobbyInfo, IStartGame
{ {
@@ -26,7 +28,7 @@ namespace OpenRA.Server.Traits
else else
lock (masterServerMessages) lock (masterServerMessages)
while (masterServerMessages.Count > 0) while (masterServerMessages.Count > 0)
Server.SendChat(null, masterServerMessages.Dequeue()); server.SendChat(null, masterServerMessages.Dequeue());
} }
@@ -61,11 +63,11 @@ namespace OpenRA.Server.Traits
{ {
wc.DownloadData( wc.DownloadData(
masterServerUrl + url.F( masterServerUrl + url.F(
externalPort, Uri.EscapeUriString(Server.Name), externalPort, Uri.EscapeUriString(server.Name),
Server.GameStarted ? 2 : 1, // todo: post-game states, etc. server.GameStarted ? 2 : 1, // todo: post-game states, etc.
Server.lobbyInfo.Clients.Count, server.lobbyInfo.Clients.Count,
string.Join(",", Server.lobbyInfo.GlobalSettings.Mods), string.Join(",", server.lobbyInfo.GlobalSettings.Mods),
Server.lobbyInfo.GlobalSettings.Map)); server.lobbyInfo.GlobalSettings.Map));
if (isInitialPing) if (isInitialPing)
{ {

View File

@@ -13,21 +13,23 @@ using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using OpenRA.Network; using OpenRA.Network;
using OpenRA.Server;
using server = OpenRA.Server.Server;
namespace OpenRA.Server.Traits namespace OpenRA.Mods.RA.Server
{ {
public class PlayerCommands : ServerTrait, IInterpretCommand public class PlayerCommands : ServerTrait, IInterpretCommand
{ {
public bool InterpretCommand(Connection conn, Session.Client client, string cmd) public bool InterpretCommand(Connection conn, Session.Client client, string cmd)
{ {
if (Server.GameStarted) if (server.GameStarted)
{ {
Server.SendChatTo(conn, "Cannot change state when game started. ({0})".F(cmd)); server.SendChatTo(conn, "Cannot change state when game started. ({0})".F(cmd));
return false; return false;
} }
else if (client.State == Session.ClientState.Ready && !(cmd == "ready" || cmd == "startgame")) else if (client.State == Session.ClientState.Ready && !(cmd == "ready" || cmd == "startgame"))
{ {
Server.SendChatTo(conn, "Cannot change state when marked as ready."); server.SendChatTo(conn, "Cannot change state when marked as ready.");
return false; return false;
} }
@@ -38,14 +40,14 @@ namespace OpenRA.Server.Traits
{ {
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);
client.Name = s; client.Name = s;
Server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
{ "race", { "race",
s => s =>
{ {
client.Country = s; client.Country = s;
Server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
{ "team", { "team",
@@ -55,7 +57,7 @@ namespace OpenRA.Server.Traits
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; }
client.Team = team; client.Team = team;
Server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
{ "spawn", { "spawn",
@@ -68,14 +70,14 @@ namespace OpenRA.Server.Traits
return false; return false;
} }
if (Server.lobbyInfo.Clients.Where( c => c != client ).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;
} }
client.SpawnPoint = spawnPoint; client.SpawnPoint = spawnPoint;
Server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},
{ "color", { "color",
@@ -84,7 +86,7 @@ namespace OpenRA.Server.Traits
var c = s.Split(',').Select(cc => int.Parse(cc)).ToArray(); var c = s.Split(',').Select(cc => int.Parse(cc)).ToArray();
client.Color1 = Color.FromArgb(c[0],c[1],c[2]); client.Color1 = Color.FromArgb(c[0],c[1],c[2]);
client.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;
}} }}
}; };