public static async Task <Guid> HostGame(HostedGame req) { // Try to kill every other game this asshole started before this one. var others = _gameListener.Games.Where(x => x.HostUser.Equals(req.HostUser)) .ToArray(); foreach (var g in others) { try { g.KillGame(); } catch (InvalidOperationException ex) { Log.Error($"{nameof(HostGame)}: Error killing game. See inner exception for more details.", ex); } } var bport = AppConfig.Instance.GameBroadcastPort; req.Id = Guid.NewGuid(); req.HostAddress = AppConfig.Instance.HostName + ":" + _networkHelper.NextPort.ToString(); var waitTask = _gameListener.WaitForGame(req.Id); var gameProcess = new HostedGameProcess(req, Service.IsDebug, false, AppConfig.Instance.GameBroadcastPort); gameProcess.Start(); await waitTask; return(req.Id); }
async Task StartLocalGame(DataNew.Entities.Game game, string name, string password) { var hg = new HostedGame() { Id = Guid.NewGuid(), Name = name, HostUser = Program.LobbyClient?.Me, GameName = game.Name, GameId = game.Id, GameVersion = game.Version.ToString(), HostAddress = $"0.0.0.0:{HostPort}", Password = password, GameIconUrl = game.IconUrl, Spectators = true, }; if (Program.LobbyClient?.Me != null) { hg.HostUserIconUrl = ApiUserCache.Instance.ApiUser(Program.LobbyClient.Me).IconUrl; } // We don't use a userid here becuase we're doing a local game. var hs = new HostedGameProcess(hg, X.Instance.Debug, true); hs.Start(); Program.GameSettings.UseTwoSidedTable = HostGame.UseTwoSidedTable; Program.IsHost = true; Program.GameEngine = new GameEngine(game, Prefs.Nickname, false, password, true); var ip = IPAddress.Parse("127.0.0.1"); for (var i = 0; i < 5; i++) { try { Program.Client = new Octgn.Networking.ClientSocket(ip, HostPort); await Program.Client.Connect(); return; } catch (Exception e) { Log.Warn("Start local game error", e); if (i == 4) { throw; } } Thread.Sleep(2000); } throw new UserMessageException("Cannot start local game. You may be missing a file."); }
async Task StartLocalGame(DataNew.Entities.Game game, string name, string password) { var hostport = new Random().Next(5000, 6000); while (!NetworkHelper.IsPortAvailable(hostport)) { hostport++; } var hg = new HostedGame() { Id = Guid.NewGuid(), Name = name, HostUser = Program.LobbyClient?.User ?? new User(hostport.ToString(), Username), GameName = game.Name, GameId = game.Id, GameVersion = game.Version.ToString(), HostAddress = $"0.0.0.0:{hostport}", Password = password, GameIconUrl = game.IconUrl, Spectators = true, }; if (Program.LobbyClient?.User != null) { hg.HostUserIconUrl = ApiUserCache.Instance.ApiUser(Program.LobbyClient.User)?.IconUrl; } // Since it's a local game, we want to use the username instead of a userid, since that won't exist. var hs = new HostedGameProcess(hg, X.Instance.Debug, true); hs.Start(); Prefs.Nickname = hg.HostUser.DisplayName; Program.GameEngine = new GameEngine(game, Username, false, password, true); Program.CurrentOnlineGameName = name; Program.IsHost = true; var ip = IPAddress.Parse("127.0.0.1"); for (var i = 0; i < 5; i++) { try { Program.Client = new ClientSocket(ip, hostport); await Program.Client.Connect(); SuccessfulHost = true; return; } catch (Exception e) { Log.Warn("Start local game error", e); if (i == 4) { throw; } } Thread.Sleep(2000); } }