move Game.Current{Host,Port} into orderManager

This commit is contained in:
Bob
2010-10-11 16:13:48 +13:00
parent 17990ab8b7
commit 10bf85f57e
4 changed files with 34 additions and 29 deletions

View File

@@ -55,25 +55,18 @@ namespace OpenRA
viewport.Center(loc); viewport.Center(loc);
} }
internal static string CurrentHost = "";
internal static int CurrentPort = 0;
internal static void JoinServer(string host, int port) internal static void JoinServer(string host, int port)
{ {
if (orderManager != null) orderManager.Dispose(); if (orderManager != null) orderManager.Dispose();
CurrentHost = host;
CurrentPort = port;
lastConnectionState = ConnectionState.PreConnecting;
ConnectionStateChanged();
var replayFilename = ChooseReplayFilename(); var replayFilename = ChooseReplayFilename();
string path = Path.Combine( Game.SupportDir, "Replays" ); string path = Path.Combine( Game.SupportDir, "Replays" );
if( !Directory.Exists( path ) ) Directory.CreateDirectory( path ); if( !Directory.Exists( path ) ) Directory.CreateDirectory( path );
var replayFile = File.Create( Path.Combine( path, replayFilename ) ); var replayFile = File.Create( Path.Combine( path, replayFilename ) );
orderManager = new OrderManager( new ReplayRecorderConnection( new NetworkConnection( host, port ), replayFile ) ); orderManager = new OrderManager( host, port, new ReplayRecorderConnection( new NetworkConnection( host, port ), replayFile ) );
lastConnectionState = ConnectionState.PreConnecting;
ConnectionStateChanged(orderManager);
} }
static string ChooseReplayFilename() static string ChooseReplayFilename()
@@ -83,11 +76,10 @@ namespace OpenRA
static void JoinLocal() static void JoinLocal()
{ {
lastConnectionState = ConnectionState.PreConnecting;
ConnectionStateChanged();
if (orderManager != null) orderManager.Dispose(); if (orderManager != null) orderManager.Dispose();
orderManager = new OrderManager(new EchoConnection()); orderManager = new OrderManager("<no server>", -1, new EchoConnection());
lastConnectionState = ConnectionState.PreConnecting;
ConnectionStateChanged( orderManager );
} }
static int lastTime = Environment.TickCount; static int lastTime = Environment.TickCount;
@@ -101,7 +93,7 @@ namespace OpenRA
internal static int LocalTick = 0; internal static int LocalTick = 0;
const int NetTickScale = 3; // 120ms net tick for 40ms local tick const int NetTickScale = 3; // 120ms net tick for 40ms local tick
public static event Action ConnectionStateChanged = () => { }; internal static event Action<OrderManager> ConnectionStateChanged = _ => { };
static ConnectionState lastConnectionState = ConnectionState.PreConnecting; static ConnectionState lastConnectionState = ConnectionState.PreConnecting;
public static int LocalClientId { get { return orderManager.Connection.LocalClientId; } } public static int LocalClientId { get { return orderManager.Connection.LocalClientId; } }
@@ -110,7 +102,7 @@ namespace OpenRA
if (orderManager.Connection.ConnectionState != lastConnectionState) if (orderManager.Connection.ConnectionState != lastConnectionState)
{ {
lastConnectionState = orderManager.Connection.ConnectionState; lastConnectionState = orderManager.Connection.ConnectionState;
ConnectionStateChanged(); ConnectionStateChanged( orderManager );
} }
int t = Environment.TickCount; int t = Environment.TickCount;
@@ -272,19 +264,21 @@ namespace OpenRA
StartGame(modData.Manifest.ShellmapUid); StartGame(modData.Manifest.ShellmapUid);
Game.AfterGameStart += world => Widget.OpenWindow("INGAME_ROOT", new Dictionary<string,object>{{"world", world}}); Game.AfterGameStart += world => Widget.OpenWindow("INGAME_ROOT", new Dictionary<string,object>{{"world", world}});
Game.ConnectionStateChanged += orderManager => Game.ConnectionStateChanged += orderManager =>
{ {
Widget.CloseWindow(); Widget.CloseWindow();
switch( orderManager.Connection.ConnectionState ) switch( orderManager.Connection.ConnectionState )
{ {
case ConnectionState.PreConnecting: case ConnectionState.PreConnecting:
Widget.OpenWindow("MAINMENU_BG"); Widget.OpenWindow("MAINMENU_BG");
break; break;
case ConnectionState.Connecting: case ConnectionState.Connecting:
Widget.OpenWindow( "CONNECTING_BG",
new Dictionary<string, object> { { "host", orderManager.Host }, { "port", orderManager.Port } } ); new Dictionary<string, object> { { "host", orderManager.Host }, { "port", orderManager.Port } } );
break; break;
case ConnectionState.NotConnected: case ConnectionState.NotConnected:
Widget.OpenWindow( "CONNECTION_FAILED_BG",
new Dictionary<string, object> { { "host", orderManager.Host }, { "port", orderManager.Port } } ); new Dictionary<string, object> { { "host", orderManager.Host }, { "port", orderManager.Port } } );
break; break;
case ConnectionState.Connected: case ConnectionState.Connected:

View File

@@ -24,6 +24,9 @@ namespace OpenRA.Network
public Session LobbyInfo = new Session( Game.Settings.Game.Mods ); public Session LobbyInfo = new Session( Game.Settings.Game.Mods );
public Session.Client LocalClient { get { return LobbyInfo.ClientWithIndex( Connection.LocalClientId ); } } public Session.Client LocalClient { get { return LobbyInfo.ClientWithIndex( Connection.LocalClientId ); } }
public readonly string Host;
public readonly int Port;
public int FrameNumber { get; private set; } public int FrameNumber { get; private set; }
public int FramesAhead = 0; public int FramesAhead = 0;
@@ -43,8 +46,10 @@ namespace OpenRA.Network
Connection.Send( new List<Order>().Serialize( i ) ); Connection.Send( new List<Order>().Serialize( i ) );
} }
public OrderManager( IConnection conn ) public OrderManager( string host, int port, IConnection conn )
{ {
this.Host = host;
this.Port = port;
Connection = conn; Connection = conn;
} }

View File

@@ -15,7 +15,10 @@ namespace OpenRA.Widgets.Delegates
public class ConnectionDialogsDelegate : IWidgetDelegate public class ConnectionDialogsDelegate : IWidgetDelegate
{ {
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public ConnectionDialogsDelegate( [ObjectCreator.Param] Widget widget ) public ConnectionDialogsDelegate(
[ObjectCreator.Param] Widget widget,
[ObjectCreator.Param] string host,
[ObjectCreator.Param] int port )
{ {
widget.GetWidget("CONNECTION_BUTTON_ABORT").OnMouseUp = mi => { widget.GetWidget("CONNECTION_BUTTON_ABORT").OnMouseUp = mi => {
widget.GetWidget("CONNECTION_BUTTON_ABORT").Parent.Visible = false; widget.GetWidget("CONNECTION_BUTTON_ABORT").Parent.Visible = false;
@@ -24,14 +27,17 @@ namespace OpenRA.Widgets.Delegates
}; };
widget.GetWidget<LabelWidget>("CONNECTING_DESC").GetText = () => widget.GetWidget<LabelWidget>("CONNECTING_DESC").GetText = () =>
"Connecting to {0}:{1}...".F(Game.CurrentHost, Game.CurrentPort); "Connecting to {0}:{1}...".F(host, port);
} }
} }
public class ConnectionFailedDelegate : IWidgetDelegate public class ConnectionFailedDelegate : IWidgetDelegate
{ {
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public ConnectionFailedDelegate( [ObjectCreator.Param] Widget widget ) public ConnectionFailedDelegate(
[ObjectCreator.Param] Widget widget,
[ObjectCreator.Param] string host,
[ObjectCreator.Param] int port )
{ {
widget.GetWidget("CONNECTION_BUTTON_CANCEL").OnMouseUp = mi => { widget.GetWidget("CONNECTION_BUTTON_CANCEL").OnMouseUp = mi => {
widget.GetWidget("CONNECTION_BUTTON_CANCEL").Parent.Visible = false; widget.GetWidget("CONNECTION_BUTTON_CANCEL").Parent.Visible = false;
@@ -39,12 +45,12 @@ namespace OpenRA.Widgets.Delegates
return true; return true;
}; };
widget.GetWidget("CONNECTION_BUTTON_RETRY").OnMouseUp = mi => { widget.GetWidget("CONNECTION_BUTTON_RETRY").OnMouseUp = mi => {
Game.JoinServer(Game.CurrentHost, Game.CurrentPort); Game.JoinServer(host, port);
return true; return true;
}; };
widget.GetWidget<LabelWidget>("CONNECTION_FAILED_DESC").GetText = () => widget.GetWidget<LabelWidget>("CONNECTION_FAILED_DESC").GetText = () =>
"Could not connect to {0}:{1}".F(Game.CurrentHost, Game.CurrentPort); "Could not connect to {0}:{1}".F(host, port);
} }
} }
} }

View File

@@ -200,9 +200,9 @@ namespace OpenRA.Widgets.Delegates
Game.IssueOrder(Order.Command("color {0},{1},{2},{3},{4},{5}".F(c1.R,c1.G,c1.B,c2.R,c2.G,c2.B))); Game.IssueOrder(Order.Command("color {0},{1},{2},{3},{4},{5}".F(c1.R,c1.G,c1.B,c2.R,c2.G,c2.B)));
} }
void ResetConnectionState() void ResetConnectionState( OrderManager orderManager )
{ {
if (Game.orderManager.Connection.ConnectionState == ConnectionState.PreConnecting) if( orderManager.Connection.ConnectionState == ConnectionState.PreConnecting )
hasJoined = false; hasJoined = false;
} }