Fix unnecessary duplication and get a better UI out of it

This commit is contained in:
alzeih
2010-06-14 12:49:01 +12:00
parent 889d736d35
commit 9425fb1def
2 changed files with 47 additions and 106 deletions

View File

@@ -264,19 +264,7 @@ namespace OpenRA.Server
}}, }},
{ "name", { "name",
s => s =>
{ {
if (GetClient(conn).State == Session.ClientState.Ready)
{
SendChatTo( conn, "You can't change your name once you are marked as ready" );
return true;
}
if (GameStarted)
{
SendChatTo( conn, "You can't change your name after the game has started" );
return true;
}
if (s.Trim() == "") if (s.Trim() == "")
{ {
SendChatTo( conn, "Blank names are not permitted." ); SendChatTo( conn, "Blank names are not permitted." );
@@ -302,19 +290,7 @@ namespace OpenRA.Server
}}, }},
{ "race", { "race",
s => s =>
{ {
if (GetClient(conn).State == Session.ClientState.Ready)
{
SendChatTo( conn, "You can't change your race once you are marked as ready" );
return true;
}
if (GameStarted)
{
SendChatTo( conn, "You can't change your race after the game has started" );
return true;
}
GetClient(conn).Country = s; GetClient(conn).Country = s;
SyncLobbyInfo(); SyncLobbyInfo();
return true; return true;
@@ -322,18 +298,6 @@ namespace OpenRA.Server
{ "team", { "team",
s => s =>
{ {
if (GetClient(conn).State == Session.ClientState.Ready)
{
SendChatTo( conn, "You can't change your team once you are marked as ready" );
return true;
}
if (GameStarted)
{
SendChatTo( conn, "You can't change your team after the game has started" );
return true;
}
int team; int team;
if (!int.TryParse(s, out team)) { Console.WriteLine("Invalid team: {0}", s ); return false; } if (!int.TryParse(s, out team)) { Console.WriteLine("Invalid team: {0}", s ); return false; }
@@ -344,18 +308,6 @@ namespace OpenRA.Server
{ "spawn", { "spawn",
s => s =>
{ {
if (GetClient(conn).State == Session.ClientState.Ready)
{
SendChatTo( conn, "You can't change your spawn point once you are marked as ready" );
return true;
}
if (GameStarted)
{
SendChatTo( conn, "You can't change your spawn point after the game has started" );
return true;
}
int spawnPoint; int spawnPoint;
if (!int.TryParse(s, out spawnPoint) || spawnPoint < 0 || spawnPoint > 8) //TODO: SET properly! if (!int.TryParse(s, out spawnPoint) || spawnPoint < 0 || spawnPoint > 8) //TODO: SET properly!
{ {
@@ -376,17 +328,6 @@ namespace OpenRA.Server
{ "pal", { "pal",
s => s =>
{ {
if (GetClient(conn).State == Session.ClientState.Ready)
{
SendChatTo( conn, "You can't change your color once you are marked as ready" );
return true;
}
if (GameStarted)
{
SendChatTo( conn, "You can't change your color after the game has started" );
return true;
}
int pali; int pali;
if (!int.TryParse(s, out pali)) if (!int.TryParse(s, out pali))
@@ -413,16 +354,12 @@ namespace OpenRA.Server
SendChatTo( conn, "Only the host can change the map" ); SendChatTo( conn, "Only the host can change the map" );
return true; return true;
} }
if (GameStarted)
{
SendChatTo( conn, "You can't change the map after the game has started" );
return true;
}
lobbyInfo.GlobalSettings.Map = s; lobbyInfo.GlobalSettings.Map = s;
foreach(var client in lobbyInfo.Clients) foreach(var client in lobbyInfo.Clients)
{
client.SpawnPoint = 0; client.SpawnPoint = 0;
client.State = Session.ClientState.NotReady;
}
SyncLobbyInfo(); SyncLobbyInfo();
return true; return true;
@@ -430,12 +367,6 @@ namespace OpenRA.Server
{ "addpkg", { "addpkg",
s => s =>
{ {
if (GameStarted)
{
SendChatTo( conn, "You can't change packages after the game has started" );
return true;
}
Console.WriteLine("** Added package: `{0}`", s); Console.WriteLine("** Added package: `{0}`", s);
try try
{ {
@@ -455,11 +386,6 @@ namespace OpenRA.Server
{ "mods", { "mods",
s => s =>
{ {
if (GameStarted)
{
SendChatTo( conn, "You can't change mods after the game has started" );
return true;
}
var args = s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList(); var args = s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
lobbyInfo.GlobalSettings.Mods = args.GetRange(0,args.Count - 1).ToArray(); lobbyInfo.GlobalSettings.Mods = args.GetRange(0,args.Count - 1).ToArray();
lobbyInfo.GlobalSettings.Map = args.Last(); lobbyInfo.GlobalSettings.Map = args.Last();
@@ -495,20 +421,35 @@ namespace OpenRA.Server
switch (so.Name) switch (so.Name)
{ {
case "Chat": case "Chat":
if (so.Data.StartsWith("/")) if (!HandleAsCommand(conn, so))
{ foreach (var c in conns.Except(conn).ToArray())
if (!InterpretCommand(conn, so.Data.Substring(1))) DispatchOrdersToClient(c, GetClient(conn).Index, 0, so.Serialize());
{ break;
Console.WriteLine("Bad server command: {0}", so.Data.Substring(1)); //TODO: send to those with the same team
SendChatTo(conn, "Bad server command."); case "TeamChat":
} if (!HandleAsCommand(conn, so))
}
else
foreach (var c in conns.Except(conn).ToArray()) foreach (var c in conns.Except(conn).ToArray())
DispatchOrdersToClient(c, GetClient(conn).Index, 0, so.Serialize()); DispatchOrdersToClient(c, GetClient(conn).Index, 0, so.Serialize());
break; break;
} }
} }
static bool HandleAsCommand(Connection conn, ServerOrder so)
{
if (!so.Data.StartsWith("/")) return false;
var cmd = so.Data.Substring(1);
if(GameStarted)
SendChatTo(conn, "Cannot change state when game started.");
else if (GetClient(conn).State == Session.ClientState.Ready && cmd != "ready")
SendChatTo(conn, "Cannot change state when marked as ready.");
else if (!InterpretCommand(conn, cmd))
{
Console.WriteLine("Bad server command: {0}", cmd);
SendChatTo(conn, "Bad server command.");
}
return true;
}
static Session.Client GetClient(Connection conn) static Session.Client GetClient(Connection conn)
{ {

View File

@@ -63,14 +63,12 @@ namespace OpenRA.Widgets.Delegates
foreach(var client in Game.LobbyInfo.Clients) foreach(var client in Game.LobbyInfo.Clients)
{ {
var c = client; var c = client;
var template = (client.Index == Game.LocalClient.Index)? LocalPlayerTemplate.Clone() : RemotePlayerTemplate.Clone(); Widget template;
template.Id = "PLAYER_{0}".F(c.Index); if(client.Index == Game.LocalClient.Index && c.State != Session.ClientState.Ready)
template.Parent = Players;
template.GetWidget<LabelWidget>("NAME").GetText = () => c.Name;
if(client.Index == Game.LocalClient.Index)
{ {
template = LocalPlayerTemplate.Clone();
var color = template.GetWidget<ButtonWidget>("COLOR"); var color = template.GetWidget<ButtonWidget>("COLOR");
color.OnMouseUp = CyclePalette; color.OnMouseUp = CyclePalette;
@@ -95,6 +93,8 @@ namespace OpenRA.Widgets.Delegates
} }
else else
{ {
template = RemotePlayerTemplate.Clone();
var color = template.GetWidget<ColorBlockWidget>("COLOR"); var color = template.GetWidget<ColorBlockWidget>("COLOR");
color.GetColor = () => Game.world.PlayerColors()[c.PaletteIndex % Game.world.PlayerColors().Count].Color; color.GetColor = () => Game.world.PlayerColors()[c.PaletteIndex % Game.world.PlayerColors().Count].Color;
@@ -110,8 +110,13 @@ namespace OpenRA.Widgets.Delegates
var status = template.GetWidget<CheckboxWidget>("STATUS"); var status = template.GetWidget<CheckboxWidget>("STATUS");
status.Checked = () => c.State == Session.ClientState.Ready; status.Checked = () => c.State == Session.ClientState.Ready;
if (client.Index == Game.LocalClient.Index) status.OnMouseDown = CycleReady;
} }
template.Id = "PLAYER_{0}".F(c.Index);
template.Parent = Players;
template.GetWidget<LabelWidget>("NAME").GetText = () => c.Name;
template.Bounds = new Rectangle(0, offset, template.Bounds.Width, template.Bounds.Height); template.Bounds = new Rectangle(0, offset, template.Bounds.Width, template.Bounds.Height);
template.IsVisible = () => true; template.IsVisible = () => true;
Players.AddChild(template); Players.AddChild(template);
@@ -124,9 +129,7 @@ namespace OpenRA.Widgets.Delegates
bool SpawnPointAvailable(int index) { return (index == 0) || Game.LobbyInfo.Clients.All(c => c.SpawnPoint != index); } bool SpawnPointAvailable(int index) { return (index == 0) || Game.LobbyInfo.Clients.All(c => c.SpawnPoint != index); }
bool CyclePalette(MouseInput mi) bool CyclePalette(MouseInput mi)
{ {
if (Game.LocalClient.State == Session.ClientState.Ready) return false;
var d = (mi.Button == MouseButton.Left) ? +1 : Game.world.PlayerColors().Count() - 1; var d = (mi.Button == MouseButton.Left) ? +1 : Game.world.PlayerColors().Count() - 1;
var newIndex = ((int)Game.LocalClient.PaletteIndex + d) % Game.world.PlayerColors().Count(); var newIndex = ((int)Game.LocalClient.PaletteIndex + d) % Game.world.PlayerColors().Count();
@@ -141,9 +144,7 @@ namespace OpenRA.Widgets.Delegates
} }
bool CycleRace(MouseInput mi) bool CycleRace(MouseInput mi)
{ {
if (Game.LocalClient.State == Session.ClientState.Ready) return false;
var countries = new[] { "Random" }.Concat(Game.world.GetCountries().Select(c => c.Name)); var countries = new[] { "Random" }.Concat(Game.world.GetCountries().Select(c => c.Name));
if (mi.Button == MouseButton.Right) if (mi.Button == MouseButton.Right)
@@ -164,14 +165,15 @@ namespace OpenRA.Widgets.Delegates
bool CycleReady(MouseInput mi) bool CycleReady(MouseInput mi)
{ {
//HACK: Can't set this as part of the fuction as LocalClient/State not initalised yet
Chrome.rootWidget.GetWidget("SERVER_LOBBY").GetWidget<ButtonWidget>("CHANGEMAP_BUTTON").Visible
= (Game.IsHost && Game.LocalClient.State == Session.ClientState.Ready);
Game.IssueOrder(Order.Chat("/ready")); Game.IssueOrder(Order.Chat("/ready"));
return true; return true;
} }
bool CycleSpawnPoint(MouseInput mi) bool CycleSpawnPoint(MouseInput mi)
{ {
if (Game.LocalClient.State == Session.ClientState.Ready) return false;
var d = (mi.Button == MouseButton.Left) ? +1 : Game.world.Map.SpawnPoints.Count(); var d = (mi.Button == MouseButton.Left) ? +1 : Game.world.Map.SpawnPoints.Count();
var newIndex = (Game.LocalClient.SpawnPoint + d) % (Game.world.Map.SpawnPoints.Count()+1); var newIndex = (Game.LocalClient.SpawnPoint + d) % (Game.world.Map.SpawnPoints.Count()+1);
@@ -185,9 +187,7 @@ namespace OpenRA.Widgets.Delegates
} }
bool CycleTeam(MouseInput mi) bool CycleTeam(MouseInput mi)
{ {
if (Game.LocalClient.State == Session.ClientState.Ready) return false;
var d = (mi.Button == MouseButton.Left) ? +1 : Game.world.Map.PlayerCount; var d = (mi.Button == MouseButton.Left) ? +1 : Game.world.Map.PlayerCount;
var newIndex = (Game.LocalClient.Team + d) % (Game.world.Map.PlayerCount+1); var newIndex = (Game.LocalClient.Team + d) % (Game.world.Map.PlayerCount+1);