Core: Added trait 'SurrenderOnDisconnect' and the core changes required to make this work

This commit is contained in:
geckosoft
2010-10-25 17:19:48 +02:00
committed by Chris Forbes
parent e7c61fac5c
commit f5b8b18d86
8 changed files with 256 additions and 184 deletions

View File

@@ -21,20 +21,21 @@ namespace OpenRA.Network
public List<Slot> Slots = new List<Slot>(); public List<Slot> Slots = new List<Slot>();
public Global GlobalSettings = new Global(); public Global GlobalSettings = new Global();
public Client ClientWithIndex( int clientID ) public Client ClientWithIndex(int clientID)
{ {
return Clients.SingleOrDefault( c => c.Index == clientID ); return Clients.SingleOrDefault(c => c.Index == clientID);
} }
public Client ClientInSlot( Slot slot ) public Client ClientInSlot(Slot slot)
{ {
return Clients.SingleOrDefault( c => c.Slot == slot.Index ); return Clients.SingleOrDefault(c => c.Slot == slot.Index);
} }
public enum ClientState public enum ClientState
{ {
NotReady, NotReady,
Ready Ready,
Disconnected = 1000
} }
public class Client public class Client
@@ -71,7 +72,7 @@ namespace OpenRA.Network
public bool AllowCheats = false; public bool AllowCheats = false;
} }
public Session( string[] mods ) public Session(string[] mods)
{ {
this.GlobalSettings.Mods = mods.ToArray(); this.GlobalSettings.Mods = mods.ToArray();
} }
@@ -80,27 +81,27 @@ namespace OpenRA.Network
{ {
var clientData = new List<MiniYamlNode>(); var clientData = new List<MiniYamlNode>();
foreach( var client in Clients ) foreach (var client in Clients)
clientData.Add( new MiniYamlNode( "Client@{0}".F( client.Index ), FieldSaver.Save( client ) ) ); clientData.Add(new MiniYamlNode("Client@{0}".F(client.Index), FieldSaver.Save(client)));
foreach( var slot in Slots ) foreach (var slot in Slots)
clientData.Add( new MiniYamlNode( "Slot@{0}".F( slot.Index ), FieldSaver.Save( slot ) ) ); clientData.Add(new MiniYamlNode("Slot@{0}".F(slot.Index), FieldSaver.Save(slot)));
clientData.Add( new MiniYamlNode( "GlobalSettings", FieldSaver.Save( GlobalSettings ) ) ); clientData.Add(new MiniYamlNode("GlobalSettings", FieldSaver.Save(GlobalSettings)));
return clientData.WriteToString(); return clientData.WriteToString();
} }
public static Session Deserialize(string data) public static Session Deserialize(string data)
{ {
var session = new Session( Game.Settings.Game.Mods ); var session = new Session(Game.Settings.Game.Mods);
var ys = MiniYaml.FromString(data); var ys = MiniYaml.FromString(data);
foreach (var y in ys) foreach (var y in ys)
{ {
var yy = y.Key.Split('@'); var yy = y.Key.Split('@');
switch( yy[0] ) switch (yy[0])
{ {
case "GlobalSettings": case "GlobalSettings":
FieldLoader.Load(session.GlobalSettings, y.Value); FieldLoader.Load(session.GlobalSettings, y.Value);
@@ -111,7 +112,7 @@ namespace OpenRA.Network
break; break;
case "Slot": case "Slot":
session.Slots.Add(FieldLoader.Load<Session.Slot>(y.Value )); session.Slots.Add(FieldLoader.Load<Session.Slot>(y.Value));
break; break;
} }
} }

View File

@@ -16,7 +16,7 @@ namespace OpenRA.Network
{ {
static class UnitOrders static class UnitOrders
{ {
static Player FindPlayerByClient( this World world, Session.Client c) static Player FindPlayerByClient(this World world, Session.Client c)
{ {
/* todo: this is still a hack. /* todo: this is still a hack.
* the cases we're trying to avoid are the extra players on the host's client -- Neutral, other MapPlayers, * the cases we're trying to avoid are the extra players on the host's client -- Neutral, other MapPlayers,
@@ -25,7 +25,7 @@ namespace OpenRA.Network
p => p.ClientIndex == c.Index && p.PlayerName == c.Name); p => p.ClientIndex == c.Index && p.PlayerName == c.Name);
} }
public static void ProcessOrder( OrderManager orderManager, World world, int clientId, Order order ) public static void ProcessOrder(OrderManager orderManager, World world, int clientId, Order order)
{ {
if (world != null) if (world != null)
{ {
@@ -34,25 +34,36 @@ namespace OpenRA.Network
return; return;
} }
switch( order.OrderString ) switch (order.OrderString)
{ {
case "Chat": case "Chat":
{ {
var client = orderManager.LobbyInfo.ClientWithIndex( clientId ); var client = orderManager.LobbyInfo.ClientWithIndex(clientId);
if (Game.IsHost && Game.Settings.Server.Extension != null && !Game.Settings.Server.Extension.OnIngameChat(client, order.TargetString, false)) if (Game.IsHost && Game.Settings.Server.Extension != null && !Game.Settings.Server.Extension.OnIngameChat(client, order.TargetString, false))
break; break;
if (client != null) if (client != null)
{ {
var player = world != null ? world.FindPlayerByClient(client) : null; var player = world != null ? world.FindPlayerByClient(client) : null;
var suffix = (player != null && player.WinState == WinState.Lost) ? " (Dead)" : ""; var suffix = (player != null && player.WinState == WinState.Lost) ? " (Dead)" : "";
Game.AddChatLine(client.Color1, client.Name+suffix, order.TargetString); Game.AddChatLine(client.Color1, client.Name + suffix, order.TargetString);
} }
else else
Game.AddChatLine(Color.White, "(player {0})".F(clientId), order.TargetString); Game.AddChatLine(Color.White, "(player {0})".F(clientId), order.TargetString);
break; break;
} }
case "Disconnected": /* reports that the target player disconnected */
{
var client = orderManager.LobbyInfo.ClientWithIndex(clientId);
if (client != null)
{
client.State = Session.ClientState.Disconnected;
}
break;
}
case "TeamChat": case "TeamChat":
{ {
var client = orderManager.LobbyInfo.ClientWithIndex(clientId); var client = orderManager.LobbyInfo.ClientWithIndex(clientId);
@@ -72,12 +83,16 @@ namespace OpenRA.Network
{ {
var player = world.FindPlayerByClient(client); var player = world.FindPlayerByClient(client);
var display = player != null var display = player != null
&& (world.LocalPlayer != null && player.Stances[world.LocalPlayer] == Stance.Ally &&
(world.LocalPlayer != null &&
player.Stances[world.LocalPlayer] == Stance.Ally
|| player.WinState == WinState.Lost); || player.WinState == WinState.Lost);
if (display) if (display)
{ {
var suffix = (player != null && player.WinState == WinState.Lost) ? " (Dead)" : " (Team)"; var suffix = (player != null && player.WinState == WinState.Lost)
? " (Dead)"
: " (Team)";
Game.AddChatLine(client.Color1, client.Name + suffix, order.TargetString); Game.AddChatLine(client.Color1, client.Name + suffix, order.TargetString);
} }
} }
@@ -92,21 +107,21 @@ namespace OpenRA.Network
} }
case "SyncInfo": case "SyncInfo":
{ {
orderManager.LobbyInfo = Session.Deserialize( order.TargetString ); orderManager.LobbyInfo = Session.Deserialize(order.TargetString);
if( orderManager.FramesAhead != orderManager.LobbyInfo.GlobalSettings.OrderLatency if (orderManager.FramesAhead != orderManager.LobbyInfo.GlobalSettings.OrderLatency
&& !orderManager.GameStarted ) && !orderManager.GameStarted)
{ {
orderManager.FramesAhead = orderManager.LobbyInfo.GlobalSettings.OrderLatency; orderManager.FramesAhead = orderManager.LobbyInfo.GlobalSettings.OrderLatency;
Game.Debug( "Order lag is now {0} frames.".F( orderManager.LobbyInfo.GlobalSettings.OrderLatency ) ); Game.Debug(
"Order lag is now {0} frames.".F(orderManager.LobbyInfo.GlobalSettings.OrderLatency));
} }
Game.SyncLobbyInfo(); Game.SyncLobbyInfo();
break; break;
} }
case "SetStance": case "SetStance":
{ {
var targetPlayer = order.Player.World.players[order.TargetLocation.X]; var targetPlayer = order.Player.World.players[order.TargetLocation.X];
var newStance = (Stance)order.TargetLocation.Y; var newStance = (Stance)order.TargetLocation.Y;
@@ -126,7 +141,7 @@ namespace OpenRA.Network
} }
default: default:
{ {
if( !order.IsImmediate ) if (!order.IsImmediate)
foreach (var t in order.Subject.TraitsImplementing<IResolveOrder>()) foreach (var t in order.Subject.TraitsImplementing<IResolveOrder>())
t.ResolveOrder(order.Subject, order); t.ResolveOrder(order.Subject, order);
break; break;

View File

@@ -43,13 +43,13 @@ namespace OpenRA
public ShroudRenderer Shroud; public ShroudRenderer Shroud;
public World World { get; private set; } public World World { get; private set; }
public Player( World world, PlayerReference pr, int index ) public Player(World world, PlayerReference pr, int index)
{ {
World = world; World = world;
Shroud = new ShroudRenderer(this, world.Map); Shroud = new ShroudRenderer(this, world.Map);
Index = index; Index = index;
Palette = "player"+index; Palette = "player" + index;
Color = pr.Color; Color = pr.Color;
Color2 = pr.Color2; Color2 = pr.Color2;
@@ -63,28 +63,29 @@ namespace OpenRA
PlayerRef = pr; PlayerRef = pr;
PlayerActor = world.CreateActor("Player", new TypeDictionary{ new OwnerInit( this ) }); PlayerActor = world.CreateActor("Player", new TypeDictionary { new OwnerInit(this) });
} }
public Player( World world, Session.Client client, PlayerReference pr, int index ) public Player(World world, Session.Client client, PlayerReference pr, int index)
{ {
World = world; World = world;
Shroud = new ShroudRenderer(this, world.Map); Shroud = new ShroudRenderer(this, world.Map);
Index = index; Index = index;
Palette = "player"+index; Palette = "player" + index;
Color = client.Color1; Color = client.Color1;
Color2 = client.Color2; Color2 = client.Color2;
PlayerName = client.Name; PlayerName = client.Name;
InternalName = pr.Name; InternalName = pr.Name;
Country = world.GetCountries() Country = world.GetCountries()
.FirstOrDefault(c => client != null && client.Country == c.Race ) .FirstOrDefault(c => client != null && client.Country == c.Race)
?? world.GetCountries().Random(world.SharedRandom); ?? world.GetCountries().Random(world.SharedRandom);
ClientIndex = client.Index; ClientIndex = client.Index;
PlayerRef = pr; PlayerRef = pr;
PlayerActor = world.CreateActor("Player", new TypeDictionary{ new OwnerInit( this ) }); PlayerActor = world.CreateActor("Player", new TypeDictionary { new OwnerInit(this) });
} }
public void GiveAdvice(string advice) public void GiveAdvice(string advice)

View File

@@ -661,15 +661,20 @@ namespace OpenRA.Server
DispatchOrders(asConn, 0, new ServerOrder("Chat", text).Serialize()); DispatchOrders(asConn, 0, new ServerOrder("Chat", text).Serialize());
} }
static void SendDisconnected(Connection asConn)
{
DispatchOrders(asConn, 0, new ServerOrder("Disconnected", "").Serialize());
}
static void InterpretServerOrder(Connection conn, ServerOrder so) static void InterpretServerOrder(Connection conn, ServerOrder so)
{ {
switch (so.Name) switch (so.Name)
{ {
case "Command": case "Command":
{ {
if(GameStarted) if (GameStarted)
SendChatTo(conn, "Cannot change state when game started."); SendChatTo(conn, "Cannot change state when game started.");
else if (GetClient(conn).State == Session.ClientState.Ready && !(so.Data == "ready" || so.Data == "startgame") ) else if (GetClient(conn).State == Session.ClientState.Ready && !(so.Data == "ready" || so.Data == "startgame"))
SendChatTo(conn, "Cannot change state when marked as ready."); SendChatTo(conn, "Cannot change state when marked as ready.");
else if (!InterpretCommand(conn, so.Data)) else if (!InterpretCommand(conn, so.Data))
{ {
@@ -681,6 +686,7 @@ namespace OpenRA.Server
case "Chat": case "Chat":
case "TeamChat": case "TeamChat":
case "Disconnected":
if (E(e => e.OnChat(conn, so.Data, so.Name == "TeamChat"))) if (E(e => e.OnChat(conn, so.Data, so.Name == "TeamChat")))
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());
@@ -698,6 +704,9 @@ namespace OpenRA.Server
conns.Remove(toDrop); conns.Remove(toDrop);
SendChat(toDrop, "Connection Dropped"); SendChat(toDrop, "Connection Dropped");
if (GameStarted)
SendDisconnected(toDrop); /* Report disconnection */
lobbyInfo.Clients.RemoveAll(c => c.Index == toDrop.PlayerIndex); lobbyInfo.Clients.RemoveAll(c => c.Index == toDrop.PlayerIndex);
DispatchOrders( toDrop, toDrop.MostRecentFrame, new byte[] { 0xbf } ); DispatchOrders( toDrop, toDrop.MostRecentFrame, new byte[] { 0xbf } );

View File

@@ -84,6 +84,7 @@
<Compile Include="AttackMove.cs" /> <Compile Include="AttackMove.cs" />
<Compile Include="Buildable.cs" /> <Compile Include="Buildable.cs" />
<Compile Include="Combat.cs" /> <Compile Include="Combat.cs" />
<Compile Include="Player\SurrenderOnDisconnect.cs" />
<Compile Include="Crates\CloakCrateAction.cs" /> <Compile Include="Crates\CloakCrateAction.cs" />
<Compile Include="Crates\GiveMcvCrateAction.cs" /> <Compile Include="Crates\GiveMcvCrateAction.cs" />
<Compile Include="Crates\GiveUnitCrateAction.cs" /> <Compile Include="Crates\GiveUnitCrateAction.cs" />

View File

@@ -0,0 +1,44 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see LICENSE.
*/
#endregion
using OpenRA.Network;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class SurrenderOnDisconnectInfo : TraitInfo<SurrenderOnDisconnect>
{
}
class SurrenderOnDisconnect : ITick
{
private bool Disconnected = false;
public void Tick(Actor self)
{
if (Disconnected) return;
var p = self.Owner;
if (p.WinState == WinState.Lost || p.WinState == WinState.Won) return; /* already won or lost */
var client = p.World.LobbyInfo.ClientWithIndex(p.ClientIndex);
if (client == null)
return;
if (client.State == Session.ClientState.Disconnected)
{
Disconnected = true; /* dont call this multiple times! */
self.World.IssueOrder(new Order("Surrender", self));
}
}
}
}

View File

@@ -68,7 +68,7 @@ Player:
PlayerColorPalette: PlayerColorPalette:
BasePalette: terrain BasePalette: terrain
PaletteFormat: cnc PaletteFormat: cnc
SurrenderOnDisconnect:
World: World:
OpenWidgetAtGameStart: OpenWidgetAtGameStart:
Widget: INGAME_ROOT Widget: INGAME_ROOT

View File

@@ -124,6 +124,7 @@ Player:
3tnk: 0% 3tnk: 0%
PlayerColorPalette: PlayerColorPalette:
BasePalette: terrain BasePalette: terrain
SurrenderOnDisconnect:
World: World:
OpenWidgetAtGameStart: OpenWidgetAtGameStart: