Nit: use ServerSettings to pass info to the server

This commit is contained in:
Paul Chote
2011-05-13 10:20:03 +12:00
parent f4ea4c5daa
commit 529ed51034
7 changed files with 47 additions and 30 deletions

View File

@@ -372,20 +372,23 @@ namespace OpenRA
return modData.ObjectCreator.CreateObject<T>( name ); return modData.ObjectCreator.CreateObject<T>( name );
} }
public static void CreateServer(string name, string map, int port, bool advertiseOnline, int externalPort) public static void CreateServer(ServerSettings settings)
{ {
server = new Server.Server(IPAddress.Any, port, name, Settings.Game.Mods, map, advertiseOnline, externalPort, modData); server = new Server.Server(IPAddress.Any, settings.ListenPort, Game.Settings.Game.Mods, settings, modData);
} }
public static void CreateLocalServer(string map) public static void CreateLocalServer(string map)
{ {
var settings = new ServerSettings()
{
Name = "Skirmish Game",
AdvertiseOnline = false,
Map = map
};
server = new Server.Server(IPAddress.Loopback, server = new Server.Server(IPAddress.Loopback,
Game.Settings.Server.LoopbackPort, Game.Settings.Server.LoopbackPort,
"Skirmish Game",
Game.Settings.Game.Mods, Game.Settings.Game.Mods,
map, settings,
false,
0,
modData); modData);
} }

View File

@@ -28,7 +28,20 @@ namespace OpenRA.GameRules
public bool AdvertiseOnline = true; public bool AdvertiseOnline = true;
public string MasterServer = "http://master.open-ra.org/"; public string MasterServer = "http://master.open-ra.org/";
public bool AllowCheats = false; public bool AllowCheats = false;
public string LastMap = null; public string Map = null;
public ServerSettings() { }
public ServerSettings(ServerSettings other)
{
Name = other.Name;
LoopbackPort = other.LoopbackPort;
ListenPort = other.ListenPort;
ExternalPort = other.ExternalPort;
AdvertiseOnline = other.AdvertiseOnline;
MasterServer = other.MasterServer;
AllowCheats = other.AllowCheats;
Map = other.Map;
}
} }
public class DebugSettings public class DebugSettings

View File

@@ -37,13 +37,11 @@ namespace OpenRA.Server
TypeDictionary ServerTraits = new TypeDictionary(); TypeDictionary ServerTraits = new TypeDictionary();
public Session lobbyInfo; public Session lobbyInfo;
public bool GameStarted = false; public bool GameStarted = false;
public string Name; public readonly IPAddress Ip;
public IPAddress Ip {get; private set;} public readonly int Port;
public int Port {get; private set;}
public int ExternalPort {get; private set;}
public bool AdvertiseOnline {get; private set;}
int randomSeed; int randomSeed;
public ServerSettings Settings;
public ModData ModData; public ModData ModData;
public Map Map; public Map Map;
@@ -53,25 +51,23 @@ namespace OpenRA.Server
shutdown = true; shutdown = true;
} }
public Server(IPAddress ip, int port, string serverName, string[] mods, string map, bool advertiseOnline, int externalPort, ModData modData) public Server(IPAddress ip, int port, string[] mods, ServerSettings settings, ModData modData)
{ {
Log.AddChannel("server", "server.log"); Log.AddChannel("server", "server.log");
Settings = settings;
Ip = ip; Ip = ip;
Port = port; Port = port;
ExternalPort = externalPort;
listener = new TcpListener(ip, port); listener = new TcpListener(ip, port);
Name = serverName;
randomSeed = (int)DateTime.Now.ToBinary(); randomSeed = (int)DateTime.Now.ToBinary();
ModData = modData; ModData = modData;
AdvertiseOnline = advertiseOnline;
foreach (var trait in modData.Manifest.ServerTraits) foreach (var trait in modData.Manifest.ServerTraits)
ServerTraits.Add( modData.ObjectCreator.CreateObject<ServerTrait>(trait) ); ServerTraits.Add( modData.ObjectCreator.CreateObject<ServerTrait>(trait) );
lobbyInfo = new Session( mods ); lobbyInfo = new Session( mods );
lobbyInfo.GlobalSettings.RandomSeed = randomSeed; lobbyInfo.GlobalSettings.RandomSeed = randomSeed;
lobbyInfo.GlobalSettings.Map = map; lobbyInfo.GlobalSettings.Map = settings.Map;
lobbyInfo.GlobalSettings.ServerName = serverName; lobbyInfo.GlobalSettings.ServerName = settings.Name;
foreach (var t in ServerTraits.WithInterface<INotifyServerStart>()) foreach (var t in ServerTraits.WithInterface<INotifyServerStart>())
t.ServerStarted(this); t.ServerStarted(this);

View File

@@ -193,11 +193,11 @@ namespace OpenRA.Mods.Cnc.Widgets
var onSelect = new Action<Map>(m => var onSelect = new Action<Map>(m =>
{ {
orderManager.IssueOrder(Order.Command("map " + m.Uid)); orderManager.IssueOrder(Order.Command("map " + m.Uid));
Game.Settings.Server.LastMap = m.Uid; Game.Settings.Server.Map = m.Uid;
Game.Settings.Save(); Game.Settings.Save();
}); });
Widget.OpenWindow( "MAPCHOOSER_PANEL", new WidgetArgs() Widget.OpenWindow("MAPCHOOSER_PANEL", new WidgetArgs()
{ {
{ "initialMap", Map.Uid }, { "initialMap", Map.Uid },
{ "onExit", () => {} }, { "onExit", () => {} },

View File

@@ -13,6 +13,7 @@ using System.Net;
using OpenRA.Widgets; using OpenRA.Widgets;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using OpenRA.GameRules;
namespace OpenRA.Mods.Cnc.Widgets namespace OpenRA.Mods.Cnc.Widgets
{ {
@@ -38,7 +39,7 @@ namespace OpenRA.Mods.Cnc.Widgets
panel.GetWidget<CncMenuButtonWidget>("MAP_BUTTON").OnClick = () => panel.GetWidget<CncMenuButtonWidget>("MAP_BUTTON").OnClick = () =>
{ {
Widget.OpenWindow( "MAPCHOOSER_PANEL", new WidgetArgs() Widget.OpenWindow("MAPCHOOSER_PANEL", new WidgetArgs()
{ {
{ "initialMap", map.Uid }, { "initialMap", map.Uid },
{ "onExit", () => {} }, { "onExit", () => {} },
@@ -46,7 +47,7 @@ namespace OpenRA.Mods.Cnc.Widgets
}); });
}; };
if (string.IsNullOrEmpty(Game.Settings.Server.LastMap) || !Game.modData.AvailableMaps.TryGetValue(Game.Settings.Server.LastMap, out map)) if (string.IsNullOrEmpty(Game.Settings.Server.Map) || !Game.modData.AvailableMaps.TryGetValue(Game.Settings.Server.Map, out map))
map = Game.modData.AvailableMaps.FirstOrDefault(m => m.Value.Selectable).Value; map = Game.modData.AvailableMaps.FirstOrDefault(m => m.Value.Selectable).Value;
panel.GetWidget<MapPreviewWidget>("MAP_PREVIEW").Map = () => map; panel.GetWidget<MapPreviewWidget>("MAP_PREVIEW").Map = () => map;
@@ -84,11 +85,14 @@ namespace OpenRA.Mods.Cnc.Widgets
Game.Settings.Server.ListenPort = listenPort; Game.Settings.Server.ListenPort = listenPort;
Game.Settings.Server.ExternalPort = externalPort; Game.Settings.Server.ExternalPort = externalPort;
Game.Settings.Server.AdvertiseOnline = advertiseOnline; Game.Settings.Server.AdvertiseOnline = advertiseOnline;
Game.Settings.Server.LastMap = map.Uid; Game.Settings.Server.Map = map.Uid;
Game.Settings.Save(); Game.Settings.Save();
// Take a copy so that subsequent changes don't affect the server
var settings = new ServerSettings(Game.Settings.Server);
// Create and join the server // Create and join the server
Game.CreateServer(name, map.Uid, listenPort, advertiseOnline, externalPort); Game.CreateServer(settings);
Widget.CloseWindow(); Widget.CloseWindow();
CncConnectingLogic.Connect(IPAddress.Loopback.ToString(), Game.Settings.Server.ListenPort, onCreate, onExit); CncConnectingLogic.Connect(IPAddress.Loopback.ToString(), Game.Settings.Server.ListenPort, onCreate, onExit);
} }

View File

@@ -44,7 +44,7 @@ namespace OpenRA.Mods.RA.Server
Queue<string> masterServerMessages = new Queue<string>(); Queue<string> masterServerMessages = new Queue<string>();
public void PingMasterServer(S server) public void PingMasterServer(S server)
{ {
if (isBusy || !server.AdvertiseOnline) return; if (isBusy || !server.Settings.AdvertiseOnline) return;
lastPing = Environment.TickCount; lastPing = Environment.TickCount;
isBusy = true; isBusy = true;
@@ -60,8 +60,8 @@ namespace OpenRA.Mods.RA.Server
{ {
wc.Proxy = null; wc.Proxy = null;
wc.DownloadData( wc.DownloadData(
Game.Settings.Server.MasterServer + url.F( server.Settings.MasterServer + url.F(
server.ExternalPort, Uri.EscapeUriString(server.Name), server.Settings.ExternalPort, Uri.EscapeUriString(server.Settings.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(",", Game.CurrentMods.Select(f => "{0}@{1}".F(f.Key, f.Value.Version)).ToArray()), string.Join(",", Game.CurrentMods.Select(f => "{0}@{1}".F(f.Key, f.Value.Version)).ToArray()),

View File

@@ -11,6 +11,7 @@
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using OpenRA.Widgets; using OpenRA.Widgets;
using OpenRA.GameRules;
namespace OpenRA.Mods.RA.Widgets.Delegates namespace OpenRA.Mods.RA.Widgets.Delegates
{ {
@@ -27,14 +28,14 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
}; };
cs.GetWidget("BUTTON_START").OnMouseUp = mi => { cs.GetWidget("BUTTON_START").OnMouseUp = mi => {
var map = Game.modData.AvailableMaps.FirstOrDefault(m => m.Value.Selectable).Key;
settings.Server.Name = cs.GetWidget<TextFieldWidget>("GAME_TITLE").Text; settings.Server.Name = cs.GetWidget<TextFieldWidget>("GAME_TITLE").Text;
settings.Server.ListenPort = int.Parse(cs.GetWidget<TextFieldWidget>("LISTEN_PORT").Text); settings.Server.ListenPort = int.Parse(cs.GetWidget<TextFieldWidget>("LISTEN_PORT").Text);
settings.Server.ExternalPort = int.Parse(cs.GetWidget<TextFieldWidget>("EXTERNAL_PORT").Text); settings.Server.ExternalPort = int.Parse(cs.GetWidget<TextFieldWidget>("EXTERNAL_PORT").Text);
settings.Server.Map = Game.modData.AvailableMaps.FirstOrDefault(m => m.Value.Selectable).Key;
settings.Save(); settings.Save();
Game.CreateServer(settings.Server.Name, map,settings.Server.ListenPort, settings.Server.AdvertiseOnline, settings.Server.ExternalPort); // Take a copy so that subsequent settings changes don't affect the server
Game.CreateServer(new ServerSettings(Game.Settings.Server));
Game.JoinServer(IPAddress.Loopback.ToString(), settings.Server.ListenPort); Game.JoinServer(IPAddress.Loopback.ToString(), settings.Server.ListenPort);
return true; return true;
}; };