public void Stop() { StopVote(); spring.ExitGame(); tas.ChangeMyUserStatus(false, false); tas.LeaveBattle(); }
/// <summary> /// singleton, dont use, internal for designer /// </summary> internal BattleBar() { InitializeComponent(); picoChat.ChatBackgroundColor = TextColor.background; //same color as Program.Conf.BgColor picoChat.IRCForeColor = 14; //mirc grey. Unknown use picoChat.DefaultTooltip = "Last lines from room chat, click to enter full screen chat"; client = Program.TasClient; spring = new Spring(Program.SpringPaths); try { // silly way to create speech and voice engines on runtime - needed due to mono crash speech = Activator.CreateInstance(Type.GetType("ZeroKLobby.ChatToSpeech"), spring); } catch (Exception ex) { Trace.TraceWarning("Failed to init VoiceCommands:{0}", ex.Message); } spring.SpringExited += (s, e) => { client.ChangeMyUserStatus(isInGame: false); if (e.Data) { Program.MainWindow.InvokeFunc(() => { var defaultButton = MessageBoxDefaultButton.Button2; var icon = MessageBoxIcon.None; if ( MessageBox.Show("Do you want me to set Low details?\n(will effect: lups.cfg and springsettings.cfg)\n\nIf you wish to file a bug report, please include a copy of infolog.txt in your game data folder (accessible through Settings).\nUpload it to a text sharing site such as pastebin.com.", "Spring engine has crashed, update your video and audio drivers please!", MessageBoxButtons.YesNo, icon, defaultButton) == DialogResult.Yes) { Program.Conf.UseSafeMode = true; Program.EngineConfigurator.Configure(true, 0); } }); } }; spring.SpringStarted += (s, e) => { Program.MainWindow.SwitchMusicOnOff(false); client.ChangeMyUserStatus(isInGame: true); }; client.Rang += (s, e) => { if (e.User == GlobalConst.NightwatchName) //Nightwatch RING is from UserController.cs (website code) MainWindow.Instance.NotifyUser("chat/zkadmin", "New report arrive at zkadmin channel", true, true); else { MainWindow.Instance.NotifyUser("chat/battle", "Someone demands your attention in battle room!", true, true); AutoRespond(); } }; client.BattleJoined += (s, e) => { if (!isVisible) ManualBattleStarted(); if (IsHostGameRunning()) barContainer.btnDetail.Text = "Rejoin"; else barContainer.btnDetail.Text = "Start"; //client.ChangeMyUserStatus(false, false); var battle = client.MyBattle; lastBattleFounder = battle.Founder.Name; if (battle.Founder.Name.StartsWith("PlanetWars") || battle.Founder.Name.StartsWith("Zk")) ChangeDesiredSpectatorState(false); // TODO pw unpsec hack, remove later if (battle.IsQueue) { barContainer.Title = string.Format("Joined {0} Quick Match Queue", battle.QueueName); barContainer.TitleTooltip = "Please await people, game will start automatically"; lbQueue.Visible = true; radioPlay.Visible = false; radioSpec.Visible = false; barContainer.btnDetail.Visible = false; } else { barContainer.Title = string.Format("Joined battle room hosted by {0}", battle.Founder.Name); barContainer.TitleTooltip = "Use button on the left side to start a game"; lbQueue.Visible = false; radioPlay.Visible = true; radioSpec.Visible = true; barContainer.btnDetail.Visible = true; } Program.Downloader.GetResource(DownloadType.MAP, battle.MapName); Program.Downloader.GetResource(DownloadType.MOD, battle.ModName); engineVersionNeeded = battle.EngineVersion; if (engineVersionNeeded != Program.SpringPaths.SpringVersion) Program.Downloader.GetAndSwitchEngine(engineVersionNeeded); else engineVersionNeeded = null; if (gameBox.Image != null) gameBox.Image.Dispose(); DpiMeasurement.DpiXYMeasurement(this); int scaledIconHeight = DpiMeasurement.ScaleValueY(BattleIcon.Height); int scaledIconWidth = DpiMeasurement.ScaleValueX(BattleIcon.Width); gameBox.Image = new Bitmap(scaledIconWidth, scaledIconHeight); using (var g = Graphics.FromImage(gameBox.Image)) { g.FillRectangle(Brushes.White, 0, 0, scaledIconWidth, scaledIconHeight); var bi = Program.BattleIconManager.GetBattleIcon(battle.BattleID); g.DrawImageUnscaled(bi.Image, 0, 0); } gameBox.Invalidate(); RefreshTooltip(); var team = battle.GetFreeTeamID(client.UserName); client.ChangeMyBattleStatus(desiredSpectatorState, HasAllResources() ? SyncStatuses.Synced : SyncStatuses.Unsynced, 0, team); }; client.MyBattleMapChanged += (s, e) => { if (client.MyBattle != null && !Program.SpringScanner.HasResource(client.MyBattle.MapName)) { client.ChangeMyBattleStatus(syncStatus: SyncStatuses.Unsynced); Program.Downloader.GetResource(DownloadType.MAP, client.MyBattle.MapName); } RefreshTooltip(); }; client.MyBattleHostExited += (s, e) => { barContainer.btnDetail.Text = "Start"; }; client.MyBattleStarted += (s, e) => { try { barContainer.btnDetail.Text = "Rejoin"; if (client.MyBattleStatus.SyncStatus == SyncStatuses.Synced) { if (Utils.VerifySpringInstalled()) { if (spring.IsRunning) spring.ExitGame(); lastScript = spring.ConnectGame(client.MyBattle.Ip, client.MyBattle.HostPort, client.UserName, client.MyBattle.Users[client.UserName].ScriptPassword); //use MT tag when in spectator slot } } } catch (Exception ex) { MessageBox.Show("Error starting spring: " + ex.Message); } RefreshTooltip(); }; client.BattleMyUserStatusChanged += (s, e) => { if (client.MyBattleStatus != null) { barContainer.btnDetail.Enabled = client.MyBattleStatus.SyncStatus == SyncStatuses.Synced; if (client.MyBattleStatus.IsSpectator && radioPlay.Checked) ChangeGuiSpectatorWithoutEvent(false); // i was spectated if (!client.MyBattleStatus.IsSpectator && radioSpec.Checked) ChangeGuiSpectatorWithoutEvent(true); //i was unspectated } }; client.BattleClosed += (s, e) => { barContainer.btnDetail.Text = "Start"; if (gameBox.Image != null) gameBox.Image.Dispose(); gameBox.Image = null; RefreshTooltip(); Stop(); }; client.MyBattleRemoved += (s, e) => { var t = new Timer(); var tryCount = 0; t.Interval = 1000; t.Tick += (s2, e2) => { tryCount++; if (tryCount > 15) { t.Stop(); t.Dispose(); } else if (client.IsLoggedIn && client.MyBattle == null) { var bat = client.ExistingBattles.Values.FirstOrDefault(x => x.Founder.Name == lastBattleFounder && !x.IsPassworded); if (bat != null) { ActionHandler.JoinBattle(bat.BattleID, null); t.Stop(); t.Dispose(); } } }; t.Start(); }; client.ConnectionLost += (s, e) => { if (gameBox.Image != null) gameBox.Image.Dispose(); gameBox.Image = null; RefreshTooltip(); Stop(); }; // process special queue message to display in label client.Said += (s, e) => { if (e.Place == SayPlace.Battle && client.MyBattle != null && client.MyBattle.Founder.Name == e.UserName && e.Text.StartsWith("Queue")) { var t = e.Text.Substring(6); queueLabelFormatter = Regex.Replace(t, "([0-9]+)s", m => { var queueSeconds = int.Parse(m.Groups[1].Value); queueTarget = DateTime.Now.AddSeconds(queueSeconds); return "{0}s"; }); lbQueue.Text = string.Format(queueLabelFormatter, Math.Round(queueTarget.Subtract(DateTime.Now).TotalSeconds)); } }; timer.Tick += (s, e) => { if (client.IsLoggedIn) { if (WindowsApi.IdleTime.TotalMinutes > Program.Conf.IdleTime) { if (!client.MyUser.IsAway) client.ChangeMyUserStatus(isAway: true); } else { if (client.MyUser.IsAway) client.ChangeMyUserStatus(isAway: false); } CheckMyBattle(); } if (client.MyBattle != null && client.MyBattle.IsQueue) { lbQueue.Text = string.Format(queueLabelFormatter, Math.Round(queueTarget.Subtract(DateTime.Now).TotalSeconds)); } }; timer.Interval = 1000; timer.Start(); Program.BattleIconManager.BattleChanged += BattleIconManager_BattleChanged; //picoChat.Font = new Font(Program.Conf.ChatFont.FontFamily, Program.Conf.ChatFont.Size*0.8f); picoChat.ShowHistory = false; picoChat.ShowJoinLeave = false; //picoChat.HideScroll = true; BattleChatControl.BattleLine += (s, e) => picoChat.AddLine(e.Data); picoChat.MouseClick += (s, e) => NavigationControl.Instance.Path = "chat/battle"; }
/// <summary> /// singleton, dont use, internal for designer /// </summary> internal BattleBar() { InitializeComponent(); Program.ToolTip.SetText(btnLeave, "Leave this battle"); picoChat.ChatBackgroundColor = TextColor.background; //same color as Program.Conf.BgColor picoChat.IRCForeColor = 14; //mirc grey. Unknown use picoChat.DefaultTooltip = "Last lines from room chat, click to enter full screen chat"; gameBox.BackColor = Color.Transparent; btnStart.Image = Buttons.fight.GetResizedWithCache(38, 38); btnStart.ImageAlign = ContentAlignment.MiddleCenter; btnStart.TextImageRelation = TextImageRelation.ImageAboveText; btnStart.Text = "Play"; btnLeave.Image = Buttons.exit.GetResizedWithCache(38, 38); btnLeave.ImageAlign = ContentAlignment.MiddleCenter; btnLeave.TextImageRelation = TextImageRelation.ImageAboveText; btnLeave.Text = "Leave"; Program.ToolTip?.SetText(btnStart, "Start battle"); Program.ToolTip?.SetText(btnLeave, "Quit battle"); picoChat.Visible = false; btnStart.Click += btnStart_Click; btnLeave.Click += BtnLeaveClick; client = Program.TasClient; spring = new Spring(Program.SpringPaths); try { // silly way to create speech and voice engines on runtime - needed due to mono crash speech = Activator.CreateInstance(Type.GetType("ZeroKLobby.ChatToSpeech"), spring); } catch (Exception ex) { Trace.TraceWarning("Failed to init VoiceCommands:{0}", ex.Message); } spring.SpringExited += (s, e) => { client.ChangeMyUserStatus(isInGame: false); if (e.IsCrash) { Program.MainWindow.InvokeFunc(() => { var defaultButton = MessageBoxDefaultButton.Button2; var icon = MessageBoxIcon.None; if ( MessageBox.Show(this, "Do you want me to set Low details?\n(will effect: lups.cfg and springsettings.cfg)\n\nIf you wish to file a bug report, please include a copy of infolog.txt in your game data folder (accessible through Settings).\nUpload it to a text sharing site such as pastebin.com.", "Spring engine has crashed, update your video and audio drivers please!", MessageBoxButtons.YesNo, icon, defaultButton) == DialogResult.Yes) { Program.Conf.UseSafeMode = true; Program.EngineConfigurator.Configure(true, 0); } }); } }; spring.SpringStarted += (s, e) => { Program.MainWindow.SwitchMusicOnOff(false); client.ChangeMyUserStatus(isInGame: true); }; client.Rang += (s, e) => { if (e.Place == SayPlace.Channel) MainWindow.Instance.NotifyUser($"chat/{e.Target}", $"Attention needed in {e.Target} channel", true, true); else { MainWindow.Instance.NotifyUser("chat/battle", "Someone demands your attention in battle room!", true, true); AutoRespond(); } }; client.BattleJoined += (s, e) => { if (!isVisible) ManualBattleStarted(); if (IsHostGameRunning()) btnStart.Text = "Rejoin"; else btnStart.Text = "Start"; //client.ChangeMyUserStatus(false, false); var battle = client.MyBattle; lastBattleFounder = battle.FounderName; //Title = string.Format("Joined battle room hosted by {0}", battle.Founder.Name); //TitleTooltip = "Use button on the left side to start a game"; radioPlay.Visible = true; radioSpec.Visible = true; btnStart.Visible = true; Program.Downloader.GetResource(DownloadType.MAP, battle.MapName); Program.Downloader.GetResource(DownloadType.MOD, battle.ModName); Program.Downloader.GetResource(DownloadType.ENGINE, battle.EngineVersion); if (gameBox.Image != null) gameBox.Image.Dispose(); CreateBattleIcon(Program.BattleIconManager.GetBattleIcon(battle.BattleID)); RefreshTooltip(); if (Program.TasClient.MyBattle != null) NavigationControl.Instance.Path = "chat/battle"; client.ChangeMyBattleStatus(desiredSpectatorState, HasAllResources() ? SyncStatuses.Synced : SyncStatuses.Unsynced, 0); }; client.MyBattleMapChanged += (s, e) => { if (client.MyBattle != null && !Program.SpringScanner.HasResource(client.MyBattle.MapName)) { client.ChangeMyBattleStatus(syncStatus: SyncStatuses.Unsynced); Program.Downloader.GetResource(DownloadType.MAP, client.MyBattle.MapName); } RefreshTooltip(); }; client.MyBattleHostExited += (s, e) => { btnStart.Text = "Start"; }; client.ConnectSpringReceived += (s, e) => { try { btnStart.Text = "Rejoin"; List<Download> downloads = new List<Download>(); downloads.Add(Program.Downloader.GetResource(DownloadType.ENGINE, e.Engine)); downloads.Add(Program.Downloader.GetResource(DownloadType.MOD, e.Game)); downloads.Add(Program.Downloader.GetResource(DownloadType.MAP, e.Map)); downloads = downloads.Where(x => x != null).ToList(); if (downloads.Count > 0) { var dd = new WaitDownloadDialog(downloads); if (dd.ShowDialog(Program.MainWindow) == DialogResult.Cancel) return; } if (spring.IsRunning) spring.ExitGame(); spring.ConnectGame(e.Ip, e.Port, client.UserName, e.ScriptPassword, e.Engine); } catch (Exception ex) { MessageBox.Show(this, "Error starting spring: " + ex.Message); } RefreshTooltip(); }; client.BattleMyUserStatusChanged += (s, e) => { if (client.MyBattleStatus != null) { //btnStart.Enabled = client.MyBattleStatus.SyncStatus == SyncStatuses.Synced; if (client.MyBattleStatus.IsSpectator && radioPlay.Checked) ChangeGuiSpectatorWithoutEvent(false); // i was spectated if (!client.MyBattleStatus.IsSpectator && radioSpec.Checked) ChangeGuiSpectatorWithoutEvent(true); //i was unspectated } }; client.BattleClosed += (s, e) => { btnStart.Text = "Start"; if (gameBox.Image != null) gameBox.Image.Dispose(); gameBox.Image = null; RefreshTooltip(); Stop(); }; client.MyBattleRemoved += (s, e) => { var t = new Timer(); var tryCount = 0; t.Interval = 1000; t.Tick += (s2, e2) => { tryCount++; if (tryCount > 15) { t.Stop(); t.Dispose(); } else if (client.IsLoggedIn && client.MyBattle == null) { var bat = client.ExistingBattles.Values.FirstOrDefault(x => x.FounderName == lastBattleFounder && !x.IsPassworded); if (bat != null) { ActionHandler.JoinBattle(bat.BattleID, null); t.Stop(); t.Dispose(); } } }; t.Start(); }; client.ConnectionLost += (s, e) => { if (gameBox.Image != null) gameBox.Image.Dispose(); gameBox.Image = null; RefreshTooltip(); Stop(); }; timer.Tick += (s, e) => { if (client.IsLoggedIn) { if (WindowsApi.IdleTime.TotalMinutes > Program.Conf.IdleTime) { if (!client.MyUser.IsAway) client.ChangeMyUserStatus(isAway: true); } else { if (client.MyUser.IsAway) client.ChangeMyUserStatus(isAway: false); } CheckMyBattle(); } }; Program.MainWindow.navigationControl.PageChanged += s => { if (s != "chat/battle") { picoChat.Visible = true; } else picoChat.Visible = false; }; timer.Interval = 1000; timer.Start(); Program.BattleIconManager.BattleChanged += BattleIconManager_BattleChanged; //picoChat.Font = new Font(Program.Conf.ChatFont.FontFamily, Program.Conf.ChatFont.Size*0.8f); picoChat.ShowHistory = false; picoChat.ShowJoinLeave = false; //picoChat.HideScroll = true; BattleChatControl.BattleLine += (s, e) => picoChat.AddLine(e.Data); picoChat.MouseClick += (s, e) => NavigationControl.Instance.Path = "chat/battle"; }
/// <summary> /// singleton, dont use, internal for designer /// </summary> internal BattleBar() { InitializeComponent(); picoChat.ChatBackgroundColor = TextColor.background; //same color as Program.Conf.BgColor picoChat.IRCForeColor = 14; //mirc grey. Unknown use picoChat.DefaultTooltip = "Last lines from room chat, click to enter full screen chat"; client = Program.TasClient; spring = new Spring(Program.SpringPaths); try { // silly way to create speech and voice engines on runtime - needed due to mono crash speech = Activator.CreateInstance(Type.GetType("ZeroKLobby.ChatToSpeech"), spring); } catch (Exception ex) { Trace.TraceWarning("Failed to init VoiceCommands:{0}", ex.Message); } spring.SpringExited += (s, e) => { client.ChangeMyUserStatus(isInGame: false); if (e.Data) { Program.MainWindow.InvokeFunc(() => { var defaultButton = MessageBoxDefaultButton.Button2; var icon = MessageBoxIcon.None; if ( MessageBox.Show("Do you want me to set Low details?\n(will effect: lups.cfg and springsettings.cfg)\n\nIf you wish to file a bug report, please include a copy of infolog.txt in your game data folder (accessible through Settings).\nUpload it to a text sharing site such as pastebin.com.", "Spring engine has crashed, update your video and audio drivers please!", MessageBoxButtons.YesNo, icon, defaultButton) == DialogResult.Yes) { Program.Conf.UseSafeMode = true; Program.EngineConfigurator.Configure(true, 0); } }); } }; spring.SpringStarted += (s, e) => { client.ChangeMyUserStatus(isInGame: true); }; client.Rang += (s, e) => { if (e.User == GlobalConst.NightwatchName) { //Nightwatch RING is from UserController.cs (website code) MainWindow.Instance.NotifyUser("chat/zkadmin", "New report arrive at zkadmin channel", true, true); } else { MainWindow.Instance.NotifyUser("chat/battle", "Someone demands your attention in battle room!", true, true); AutoRespond(); } }; client.BattleJoined += (s, e) => { if (!isVisible) { ManualBattleStarted(); } if (IsHostGameRunning()) { barContainer.btnDetail.Text = "Rejoin"; } else { barContainer.btnDetail.Text = "Start"; } //client.ChangeMyUserStatus(false, false); var battle = client.MyBattle; lastBattleFounder = battle.Founder.Name; if (battle.Founder.Name.StartsWith("PlanetWars") || battle.Founder.Name.StartsWith("Zk")) { ChangeDesiredSpectatorState(false); // TODO pw unpsec hack, remove later } if (battle.IsQueue) { barContainer.Title = string.Format("Joined {0} Quick Match Queue", battle.QueueName); barContainer.TitleTooltip = "Please await people, game will start automatically"; lbQueue.Visible = true; radioPlay.Visible = false; radioSpec.Visible = false; barContainer.btnDetail.Visible = false; } else { barContainer.Title = string.Format("Joined battle room hosted by {0}", battle.Founder.Name); barContainer.TitleTooltip = "Use button on the left side to start a game"; lbQueue.Visible = false; radioPlay.Visible = true; radioSpec.Visible = true; barContainer.btnDetail.Visible = true; } Program.Downloader.GetResource(DownloadType.MAP, battle.MapName); Program.Downloader.GetResource(DownloadType.MOD, battle.ModName); engineVersionNeeded = battle.EngineVersion; if (engineVersionNeeded != Program.SpringPaths.SpringVersion) { Program.Downloader.GetAndSwitchEngine(engineVersionNeeded); } else { engineVersionNeeded = null; } if (gameBox.Image != null) { gameBox.Image.Dispose(); } DpiMeasurement.DpiXYMeasurement(this); int scaledIconHeight = DpiMeasurement.ScaleValueY(BattleIcon.Height); int scaledIconWidth = DpiMeasurement.ScaleValueX(BattleIcon.Width); gameBox.Image = new Bitmap(scaledIconWidth, scaledIconHeight); using (var g = Graphics.FromImage(gameBox.Image)) { g.FillRectangle(Brushes.White, 0, 0, scaledIconWidth, scaledIconHeight); var bi = Program.BattleIconManager.GetBattleIcon(battle.BattleID); g.DrawImageUnscaled(bi.Image, 0, 0); } gameBox.Invalidate(); RefreshTooltip(); var alliance = Enumerable.Range(0, TasClient.MaxAlliances - 1) .FirstOrDefault(allyTeam => !battle.Users.Values.Any(user => user.AllyNumber == allyTeam)); var team = battle.GetFreeTeamID(client.UserName); client.ChangeMyBattleStatus(desiredSpectatorState, HasAllResources() ? SyncStatuses.Synced : SyncStatuses.Unsynced, alliance, team); }; client.MyBattleMapChanged += (s, e) => { if (client.MyBattle != null && !Program.SpringScanner.HasResource(client.MyBattle.MapName)) { client.ChangeMyBattleStatus(syncStatus: SyncStatuses.Unsynced); Program.Downloader.GetResource(DownloadType.MAP, client.MyBattle.MapName); } RefreshTooltip(); }; client.MyBattleHostExited += (s, e) => { barContainer.btnDetail.Text = "Start"; }; client.MyBattleStarted += (s, e) => { try { barContainer.btnDetail.Text = "Rejoin"; if (client.MyBattleStatus.SyncStatus == SyncStatuses.Synced) { if (Utils.VerifySpringInstalled()) { if (spring.IsRunning) { spring.ExitGame(); } lastScript = spring.StartGame(client, null, null, null, Program.Conf.UseSafeMode, client.MyBattleStatus.IsSpectator ? Program.Conf.UseMtEngine : false); //use MT tag when in spectator slot } } } catch (Exception ex) { MessageBox.Show("Error starting spring: " + ex.Message); } RefreshTooltip(); }; client.BattleMyUserStatusChanged += (s, e) => { if (client.MyBattleStatus != null) { barContainer.btnDetail.Enabled = client.MyBattleStatus.SyncStatus == SyncStatuses.Synced; if (client.MyBattleStatus.IsSpectator && radioPlay.Checked) { ChangeGuiSpectatorWithoutEvent(false); // i was spectated } if (!client.MyBattleStatus.IsSpectator && radioSpec.Checked) { ChangeGuiSpectatorWithoutEvent(true); //i was unspectated } } }; client.BattleClosed += (s, e) => { barContainer.btnDetail.Text = "Start"; if (gameBox.Image != null) { gameBox.Image.Dispose(); } gameBox.Image = null; RefreshTooltip(); Stop(); }; client.MyBattleRemoved += (s, e) => { var t = new Timer(); var tryCount = 0; t.Interval = 1000; t.Tick += (s2, e2) => { tryCount++; if (tryCount > 15) { t.Stop(); t.Dispose(); } else if (client.IsLoggedIn && client.MyBattle == null) { var bat = client.ExistingBattles.Values.FirstOrDefault(x => x.Founder.Name == lastBattleFounder && !x.IsPassworded); if (bat != null) { ActionHandler.JoinBattle(bat.BattleID, null); t.Stop(); t.Dispose(); } } }; t.Start(); }; client.ConnectionLost += (s, e) => { if (gameBox.Image != null) { gameBox.Image.Dispose(); } gameBox.Image = null; RefreshTooltip(); Stop(); }; // process special queue message to display in label client.Said += (s, e) => { if (e.Place == SayPlace.Battle && client.MyBattle != null && client.MyBattle.Founder.Name == e.UserName && e.Text.StartsWith("Queue")) { var t = e.Text.Substring(6); queueLabelFormatter = Regex.Replace(t, "([0-9]+)s", m => { var queueSeconds = int.Parse(m.Groups[1].Value); queueTarget = DateTime.Now.AddSeconds(queueSeconds); return("{0}s"); }); lbQueue.Text = string.Format(queueLabelFormatter, Math.Round(queueTarget.Subtract(DateTime.Now).TotalSeconds)); } }; timer.Tick += (s, e) => { if (client.IsLoggedIn) { if (WindowsApi.IdleTime.TotalMinutes > Program.Conf.IdleTime) { if (!client.MyUser.IsAway) { client.ChangeMyUserStatus(isAway: true); } } else { if (client.MyUser.IsAway) { client.ChangeMyUserStatus(isAway: false); } } CheckMyBattle(); } if (client.MyBattle != null && client.MyBattle.IsQueue) { lbQueue.Text = string.Format(queueLabelFormatter, Math.Round(queueTarget.Subtract(DateTime.Now).TotalSeconds)); } }; timer.Interval = 1000; timer.Start(); Program.BattleIconManager.BattleChanged += BattleIconManager_BattleChanged; //picoChat.Font = new Font(Program.Conf.ChatFont.FontFamily, Program.Conf.ChatFont.Size*0.8f); picoChat.ShowHistory = false; picoChat.ShowJoinLeave = false; //picoChat.HideScroll = true; BattleChatControl.BattleLine += (s, e) => picoChat.AddLine(e.Data); picoChat.MouseClick += (s, e) => NavigationControl.Instance.Path = "chat/battle"; }
/// <summary> /// singleton, dont use, internal for designer /// </summary> internal BattleBar() { InitializeComponent(); picoChat.ChatBackgroundColor = TextColor.background; //same color as Program.Conf.BgColor picoChat.IRCForeColor = 14; //mirc grey. Unknown use Program.ToolTip.SetText(cbSide, "Choose the faction you wish to play."); client = Program.TasClient; spring = new Spring(Program.SpringPaths); try { // silly way to create speech and voice engines on runtime - needed due to mono crash speech = Activator.CreateInstance(Type.GetType("ZeroKLobby.ChatToSpeech"), spring); if (Program.Conf.EnableVoiceCommands) { voice = Activator.CreateInstance(Type.GetType("ZeroKLobby.VoiceCommand.VoiceCommandEngine"), client, spring); } } catch (Exception ex) { Trace.TraceWarning("Failed to init VoiceCommands:{0}", ex.Message); } spring.SpringExited += (s, e) => { client.ChangeMyUserStatus(isInGame: false); client.ChangeMyBattleStatus(ready: true); if (e.Data) { Program.MainWindow.InvokeFunc(() => { var defaultButton = MessageBoxDefaultButton.Button2; var icon = MessageBoxIcon.None; if ( MessageBox.Show("Do you want me to set Low details?\n(will effect: lups.cfg and springsettings.cfg)\n\nIf you wish to file a bug report, please include a copy of infolog.txt in your game data folder (accessible through Settings).\nUpload it to a text sharing site such as pastebin.com.", "Spring engine has crashed, update your video and audio drivers please!", MessageBoxButtons.YesNo, icon, defaultButton) == DialogResult.Yes) { Program.Conf.UseSafeMode = true; Program.EngineConfigurator.Configure(true, 0); } }); } }; spring.SpringStarted += (s, e) => { client.ChangeMyUserStatus(isInGame: true); }; client.Rang += (s, e) => { MainWindow.Instance.NotifyUser("chat/battle", "Someone demands your attention in battle room!", true, true); AutoRespond(); }; client.BattleJoined += (s, e) => { if (!isVisible) { ManualBattleStarted(); } if (IsHostGameRunning()) { barContainer.btnDetail.Text = "Rejoin"; } else { barContainer.btnDetail.Text = "Start"; } //client.ChangeMyUserStatus(false, false); var battle = client.MyBattle; lastBattleFounder = battle.Founder.Name; if (battle.Founder.Name.StartsWith("PlanetWars")) { ChangeDesiredSpectatorState(false); // TODO pw unpsec hack, remove later } Program.SpringScanner.MetaData.GetModAsync(battle.ModName, (mod) => { if (!Program.CloseOnNext) { Program.MainWindow.InvokeFunc(() => { var previousSide = cbSide.SelectedItem != null ? cbSide.SelectedItem.ToString() : null; cbSide.Items.Clear(); var cnt = 0; foreach (var side in mod.Sides) { cbSide.Items.Add(new SideItem(side, mod.SideIcons[cnt++])); } var pickedItem = cbSide.Items.OfType <SideItem>() .FirstOrDefault(x => x.Side == previousSide); suppressSideChangeEvent = true; cbSide.Visible = mod.Sides.Length > 1; if (cbSide.Visible) { if (pickedItem != null) { cbSide.SelectedItem = pickedItem; } else { cbSide.SelectedIndex = random.Next(cbSide.Items.Count); } } suppressSideChangeEvent = false; }); } }, (ex) => { }, Program.SpringPaths.SpringVersion); Program.Downloader.GetResource(DownloadType.MAP, battle.MapName); Program.Downloader.GetResource(DownloadType.MOD, battle.ModName); engineVersionNeeded = battle.EngineVersion; if (engineVersionNeeded != Program.SpringPaths.SpringVersion) { Program.Downloader.GetAndSwitchEngine(engineVersionNeeded); } else { engineVersionNeeded = null; } if (battle != previousBattle) { previousBattle = battle; if (gameBox.Image != null) { gameBox.Image.Dispose(); } DpiMeasurement.DpiXYMeasurement(this); int scaledIconHeight = DpiMeasurement.ScaleValueY(BattleIcon.Height); int scaledIconWidth = DpiMeasurement.ScaleValueX(BattleIcon.Width); gameBox.Image = new Bitmap(scaledIconWidth, scaledIconHeight); using (var g = Graphics.FromImage(gameBox.Image)) { g.FillRectangle(Brushes.White, 0, 0, scaledIconWidth, scaledIconHeight); var bi = Program.BattleIconManager.GetBattleIcon(battle.BattleID); g.DrawImageUnscaled(bi.Image, 0, 0); } gameBox.Invalidate(); } RefreshTooltip(); }; cbSide.DrawMode = DrawMode.OwnerDrawFixed; cbSide.DrawItem += cbSide_DrawItem; client.MyBattleMapChanged += (s, e) => { if (client.MyBattle != null && !Program.SpringScanner.HasResource(client.MyBattle.MapName)) { client.ChangeMyBattleStatus(syncStatus: SyncStatuses.Unsynced); Program.Downloader.GetResource(DownloadType.MAP, client.MyBattle.MapName); } RefreshTooltip(); }; client.MyBattleHostExited += (s, e) => { barContainer.btnDetail.Text = "Start"; }; client.RequestBattleStatus += (s, e) => { var battle = client.MyBattle; var alliance = Enumerable.Range(0, TasClient.MaxAlliances - 1) .FirstOrDefault(allyTeam => !battle.Users.Any(user => user.AllyNumber == allyTeam)); var team = battle.GetFreeTeamID(client.UserName); /* if (battle) * { * var b = tas.MyBattle; * return hostedMod.MissionSlots.Where(x => x.IsHuman).OrderByDescending(x => x.IsRequired).Where( * x => !b.Users.Any(y => y.AllyNumber == x.AllyID && y.TeamNumber == x.TeamID && !y.IsSpectator)); * * var slot = GetFreeSlots().FirstOrDefault(); * if (slot != null) * { * tas.ForceAlly(u.Name, slot.AllyID); * tas.ForceTeam(u.Name, slot.TeamID); * } * else tas.ForceSpectator(u.Name); * }*/ var status = new UserBattleStatus { AllyNumber = alliance, TeamNumber = team, SyncStatus = HasAllResources() ? SyncStatuses.Synced : SyncStatuses.Unsynced, IsSpectator = desiredSpectatorState, Side = cbSide.SelectedIndex >= 0 ? cbSide.SelectedIndex : 0, TeamColor = Program.Conf.DefaultPlayerColorInt, IsReady = true, }; client.SendMyBattleStatus(status); }; client.MyBattleStarted += (s, e) => { try { barContainer.btnDetail.Text = "Rejoin"; if (client.MyBattleStatus.SyncStatus == SyncStatuses.Synced) { if (Utils.VerifySpringInstalled()) { if (spring.IsRunning) { spring.ExitGame(); } lastScript = spring.StartGame(client, null, null, null, Program.Conf.UseSafeMode, client.MyBattleStatus.IsSpectator?Program.Conf.UseMtEngine:false); //use MT tag when in spectator slot } } } catch (Exception ex) { MessageBox.Show("Error starting spring: " + ex.Message); } RefreshTooltip(); }; client.BattleMyUserStatusChanged += (s, e) => { if (client.MyBattleStatus != null) { barContainer.btnDetail.Enabled = client.MyBattleStatus.SyncStatus == SyncStatuses.Synced; if (client.MyBattleStatus.IsSpectator && radioPlay.Checked) { ChangeGuiSpectatorWithoutEvent(false); // i was spectated } if (!client.MyBattleStatus.IsSpectator && radioSpec.Checked) { ChangeGuiSpectatorWithoutEvent(true); //i was unspectated } } }; client.BattleClosed += (s, e) => { barContainer.btnDetail.Text = "Start"; if (gameBox.Image != null) { gameBox.Image.Dispose(); } gameBox.Image = null; cbSide.Visible = false; RefreshTooltip(); Stop(); }; client.MyBattleEnded += (s, e) => { var t = new Timer(); var tryCount = 0; t.Interval = 1000; t.Tick += (s2, e2) => { tryCount++; if (tryCount > 15) { t.Stop(); } else if (client.IsLoggedIn && client.MyBattle == null) { var bat = client.ExistingBattles.Values.FirstOrDefault(x => x.Founder.Name == lastBattleFounder && !x.IsPassworded); if (bat != null) { ActionHandler.JoinBattle(bat.BattleID, null); t.Stop(); } } }; t.Start(); }; client.ConnectionLost += (s, e) => { if (gameBox.Image != null) { gameBox.Image.Dispose(); } gameBox.Image = null; cbSide.Visible = false; RefreshTooltip(); Stop(); }; timer.Tick += (s, e) => { if (client.IsLoggedIn) { if (WindowsApi.IdleTime.TotalMinutes > Program.Conf.IdleTime) { client.ChangeMyUserStatus(isAway: true); } else { client.ChangeMyUserStatus(isAway: false); } CheckMyBattle(); } }; timer.Interval = 2500; timer.Start(); Program.BattleIconManager.BattleChanged += BattleIconManager_BattleChanged; //picoChat.Font = new Font(Program.Conf.ChatFont.FontFamily, Program.Conf.ChatFont.Size*0.8f); picoChat.ShowHistory = false; picoChat.ShowJoinLeave = false; //picoChat.HideScroll = true; BattleChatControl.BattleLine += (s, e) => picoChat.AddLine(e.Data); picoChat.MouseClick += (s, e) => NavigationControl.Instance.Path = "chat/battle"; }
/// <summary> /// singleton, dont use, internal for designer /// </summary> internal BattleBar() { InitializeComponent(); Program.ToolTip.SetText(btnLeave, "Leave this battle"); picoChat.ChatBackgroundColor = TextColor.background; //same color as Program.Conf.BgColor picoChat.IRCForeColor = 14; //mirc grey. Unknown use picoChat.DefaultTooltip = "Last lines from room chat, click to enter full screen chat"; gameBox.BackColor = Color.Transparent; btnStart.Image = Buttons.fight.GetResizedWithCache(38, 38); btnStart.ImageAlign = ContentAlignment.MiddleCenter; btnStart.TextImageRelation = TextImageRelation.ImageAboveText; btnStart.Text = "Play"; btnLeave.Image = Buttons.exit.GetResizedWithCache(38, 38); btnLeave.ImageAlign = ContentAlignment.MiddleCenter; btnLeave.TextImageRelation = TextImageRelation.ImageAboveText; btnLeave.Text = "Leave"; Program.ToolTip?.SetText(btnStart, "Start battle"); Program.ToolTip?.SetText(btnLeave, "Quit battle"); picoChat.Visible = false; btnStart.Click += btnStart_Click; btnLeave.Click += BtnLeaveClick; client = Program.TasClient; spring = new Spring(Program.SpringPaths); try { // silly way to create speech and voice engines on runtime - needed due to mono crash speech = Activator.CreateInstance(Type.GetType("ZeroKLobby.ChatToSpeech"), spring); } catch (Exception ex) { Trace.TraceWarning("Failed to init VoiceCommands:{0}", ex.Message); } spring.SpringExited += (s, e) => { client.ChangeMyUserStatus(isInGame: false); if (e.IsCrash) { Program.MainWindow.InvokeFunc(() => { var defaultButton = MessageBoxDefaultButton.Button2; var icon = MessageBoxIcon.None; if ( MessageBox.Show(this, "Do you want me to set Low details?\n(will effect: lups.cfg and springsettings.cfg)\n\nIf you wish to file a bug report, please include a copy of infolog.txt in your game data folder (accessible through Settings).\nUpload it to a text sharing site such as pastebin.com.", "Spring engine has crashed, update your video and audio drivers please!", MessageBoxButtons.YesNo, icon, defaultButton) == DialogResult.Yes) { Program.Conf.UseSafeMode = true; Program.EngineConfigurator.Configure(true, 0); } }); } }; spring.SpringStarted += (s, e) => { Program.MainWindow.SwitchMusicOnOff(false); client.ChangeMyUserStatus(isInGame: true); }; client.Rang += (s, e) => { if (e.Place == SayPlace.Channel) { MainWindow.Instance.NotifyUser($"chat/{e.Target}", $"Attention needed in {e.Target} channel", true, true); } else { MainWindow.Instance.NotifyUser("chat/battle", "Someone demands your attention in battle room!", true, true); AutoRespond(); } }; client.BattleJoinSuccess += (s, e) => { if (!isVisible) { ManualBattleStarted(); } if (IsHostGameRunning()) { btnStart.Text = "Rejoin"; } else { btnStart.Text = "Start"; } //client.ChangeMyUserStatus(false, false); var battle = client.MyBattle; lastBattleFounder = battle.FounderName; //Title = string.Format("Joined battle room hosted by {0}", battle.Founder.Name); //TitleTooltip = "Use button on the left side to start a game"; radioPlay.Visible = true; radioSpec.Visible = true; btnStart.Visible = true; Program.Downloader.GetResource(DownloadType.MAP, battle.MapName); Program.Downloader.GetResource(DownloadType.RAPID, battle.ModName); Program.Downloader.GetResource(DownloadType.ENGINE, battle.EngineVersion); if (gameBox.Image != null) { gameBox.Image.Dispose(); } CreateBattleIcon(Program.BattleIconManager.GetBattleIcon(battle.BattleID)); RefreshTooltip(); if (Program.TasClient.MyBattle != null) { NavigationControl.Instance.Path = "chat/battle"; } client.ChangeMyBattleStatus(desiredSpectatorState, HasAllResources() ? SyncStatuses.Synced : SyncStatuses.Unsynced, 0); }; client.MyBattleMapChanged += (s, e) => { if (client.MyBattle != null && !Program.SpringScanner.HasResource(client.MyBattle.MapName)) { client.ChangeMyBattleStatus(syncStatus: SyncStatuses.Unsynced); Program.Downloader.GetResource(DownloadType.MAP, client.MyBattle.MapName); } RefreshTooltip(); }; client.MyBattleHostExited += (s, e) => { btnStart.Text = "Start"; }; client.ConnectSpringReceived += (s, e) => { try { btnStart.Text = "Rejoin"; List <Download> downloads = new List <Download>(); downloads.Add(Program.Downloader.GetResource(DownloadType.ENGINE, e.Engine)); downloads.Add(Program.Downloader.GetResource(DownloadType.RAPID, e.Game)); downloads.Add(Program.Downloader.GetResource(DownloadType.MAP, e.Map)); downloads = downloads.Where(x => x != null).ToList(); if (downloads.Count > 0) { var dd = new WaitDownloadDialog(downloads); if (dd.ShowDialog(Program.MainWindow) == DialogResult.Cancel) { return; } } if (spring.IsRunning) { spring.ExitGame(); } spring.ConnectGame(e.Ip, e.Port, client.UserName, e.ScriptPassword, e.Engine); } catch (Exception ex) { MessageBox.Show(this, "Error starting spring: " + ex.Message); } RefreshTooltip(); }; client.BattleMyUserStatusChanged += (s, e) => { if (client.MyBattleStatus != null) { //btnStart.Enabled = client.MyBattleStatus.SyncStatus == SyncStatuses.Synced; if (client.MyBattleStatus.IsSpectator && radioPlay.Checked) { ChangeGuiSpectatorWithoutEvent(false); // i was spectated } if (!client.MyBattleStatus.IsSpectator && radioSpec.Checked) { ChangeGuiSpectatorWithoutEvent(true); //i was unspectated } } }; client.BattleClosed += (s, e) => { btnStart.Text = "Start"; if (gameBox.Image != null) { gameBox.Image.Dispose(); } gameBox.Image = null; RefreshTooltip(); Stop(); }; client.MyBattleRemoved += (s, e) => { var t = new Timer(); var tryCount = 0; t.Interval = 1000; t.Tick += (s2, e2) => { tryCount++; if (tryCount > 15) { t.Stop(); t.Dispose(); } else if (client.IsLoggedIn && client.MyBattle == null) { var bat = client.ExistingBattles.Values.FirstOrDefault(x => x.FounderName == lastBattleFounder && !x.IsPassworded); if (bat != null) { ActionHandler.JoinBattle(bat.BattleID, null); t.Stop(); t.Dispose(); } } }; t.Start(); }; client.ConnectionLost += (s, e) => { if (gameBox.Image != null) { gameBox.Image.Dispose(); } gameBox.Image = null; RefreshTooltip(); Stop(); }; timer.Tick += (s, e) => { if (client.IsLoggedIn) { if (WindowsApi.IdleTime.TotalMinutes > Program.Conf.IdleTime) { if (!client.MyUser.IsAway) { client.ChangeMyUserStatus(isAway: true); } } else { if (client.MyUser.IsAway) { client.ChangeMyUserStatus(isAway: false); } } CheckMyBattle(); } }; Program.MainWindow.navigationControl.PageChanged += s => { if (s != "chat/battle") { picoChat.Visible = true; } else { picoChat.Visible = false; } }; timer.Interval = 2000; timer.Start(); Program.BattleIconManager.BattleChanged += BattleIconManager_BattleChanged; //picoChat.Font = new Font(Program.Conf.ChatFont.FontFamily, Program.Conf.ChatFont.Size*0.8f); picoChat.ShowHistory = false; picoChat.ShowJoinLeave = false; //picoChat.HideScroll = true; BattleChatControl.BattleLine += (s, e) => picoChat.AddLine(e.Data); picoChat.MouseClick += (s, e) => NavigationControl.Instance.Path = "chat/battle"; }
public void RunNub(int num) { var tas = new TasClient("Nubotron"); var maps = AutoRegistrator.RegistratorRes.campaignMaps.Split('\n'); var name = "TestNub" + num; var ord = num / 16; //tas.Input += (sender, args) => { Console.WriteLine(" < {0}", args); }; //tas.Output += (sender, args) => { Console.WriteLine(" > {0}", args); }; tas.Connected += (sender, args) => { tas.Login(name, "dummy"); }; tas.ConnectionLost += (sender, args) => { tas.Connect(GlobalConst.LobbyServerHost, GlobalConst.LobbyServerPort); Console.WriteLine("disconnected"); }; tas.LoginAccepted += (sender, args) => { Console.WriteLine(name + " accepted"); }; tas.LoginDenied += (sender, args) => { tas.Register(name, "dummy"); }; tas.RegistrationAccepted += (sender, args) => { tas.Login(name, "dummy"); }; tas.RegistrationDenied += (sender, response) => { Console.WriteLine(name + "registration denied"); }; tas.Connect(GlobalConst.LobbyServerHost, GlobalConst.LobbyServerPort); Task.Factory.StartNew(async() => { while (true) { await Task.Delay(rand.Next(10000)); if (tas.IsLoggedIn) { await tas.LeaveBattle(); if (tas.ExistingBattles.Count < 20) { await tas.OpenBattle(new BattleHeader() { Title = "" + name, MaxPlayers = 16, Mode = AutohostMode.None, Engine = tas.ServerWelcome.Engine, Game = tas.ServerWelcome.Game, Map = maps[rand.Next(maps.Length)], }); } else { var bats = tas.ExistingBattles.Values.ToList(); if (bats.Count > 0) { var bat = bats[rand.Next(bats.Count)]; if (bat != null) { tas.JoinBattle(bat.BattleID); } } } } } }, TaskCreationOptions.LongRunning); Task.Factory.StartNew(async() => { while (true) { await Task.Delay(rand.Next(50000)); if (tas.IsLoggedIn) { tas.Say(SayPlace.Channel, "zk", sent.GetNext(), false); } } }, TaskCreationOptions.LongRunning); Task.Factory.StartNew(async() => { bool cycler = false; while (true) { await Task.Delay(rand.Next(5000)); if (tas.IsLoggedIn) { await tas.ChangeMyUserStatus(cycler, cycler); //await tas.ChangeMyBattleStatus(cycler, SyncStatuses.Synced, 1); cycler = !cycler; } } }, TaskCreationOptions.LongRunning); }