PageRenderTime 701ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/other/ZeroKLobby/ActionHandler.cs

http://zero-k.googlecode.com/
C# | 345 lines | 258 code | 47 blank | 40 comment | 32 complexity | f3e929b86f4bee92df8cbe20cac615b3 MD5 | raw file
Possible License(s): GPL-2.0, MIT, BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Windows.Forms;
  7. using JetBrains.Annotations;
  8. using LobbyClient;
  9. using PlasmaShared;
  10. using PlasmaShared.ContentService;
  11. using PlasmaShared.UnitSyncLib;
  12. using ZeroKLobby.MicroLobby;
  13. using ZeroKLobby.Notifications;
  14. namespace ZeroKLobby
  15. {
  16. /// <summary>
  17. /// Central place for gui-centric actions invloving one or more controls
  18. /// </summary>
  19. public static class ActionHandler
  20. {
  21. /// <summary>
  22. /// Changes user's desired spectator state of battle - does not actually send tasclient state change
  23. /// </summary>
  24. /// <param name="state">new desired state</param>
  25. /// <returns>true if change allowed</returns>
  26. public static bool ChangeDesiredSpectatorState(bool state)
  27. {
  28. return Program.BattleBar.ChangeDesiredSpectatorState(state);
  29. }
  30. /// <summary>
  31. /// Closes a channel in the chat tab (any tab on the left)
  32. /// </summary>
  33. public static void CloseChannel(string key)
  34. {
  35. Program.MainWindow.ChatTab.CloseTab(key);
  36. }
  37. /// <summary>
  38. /// Hides the next PM that has a specific string as message
  39. /// </summary>
  40. public static void HidePM(string text)
  41. {
  42. EventHandler<CancelEventArgs<TasSayEventArgs>> hideMessage = null;
  43. hideMessage = (s, e) =>
  44. {
  45. if (e.Data.Place == TasSayEventArgs.Places.Normal && e.Data.Text == text)
  46. {
  47. e.Cancel = true;
  48. Program.TasClient.PreviewSaid -= hideMessage;
  49. }
  50. };
  51. Program.TasClient.PreviewSaid += hideMessage;
  52. }
  53. /// <summary>
  54. /// Make this client join an ally team, join a free team, and unspec
  55. /// </summary>
  56. /// <param name="allyTeam"></param>
  57. public static void JoinAllyTeam(int allyTeam)
  58. {
  59. if (ChangeDesiredSpectatorState(false))
  60. {
  61. var newStatus = Program.TasClient.MyBattleStatus.Clone();
  62. newStatus.AllyNumber = allyTeam;
  63. newStatus.TeamNumber = Program.TasClient.MyBattle.GetFreeTeamID(Program.TasClient.UserName);
  64. newStatus.IsSpectator = false;
  65. Program.TasClient.SendMyBattleStatus(newStatus);
  66. }
  67. }
  68. /// <summary>
  69. /// Joins battle manually
  70. /// </summary>
  71. public static void JoinBattle(int battleID, string password)
  72. {
  73. EventHandler<EventArgs<Battle>> battleJoinHandler = null;
  74. EventHandler<TasEventArgs> battleJoinFailedHandler = null;
  75. battleJoinHandler = ((s, e) =>
  76. {
  77. Program.TasClient.BattleJoined -= battleJoinHandler;
  78. Program.TasClient.JoinBattleFailed -= battleJoinFailedHandler;
  79. NavigationControl.Instance.Path = "chat/battle";
  80. });
  81. battleJoinFailedHandler = ((s, e) =>
  82. {
  83. Program.TasClient.BattleJoined -= battleJoinHandler;
  84. Program.TasClient.JoinBattleFailed -= battleJoinFailedHandler;
  85. MessageBox.Show(PlasmaShared.Utils.Glue(e.ServerParams.ToArray()), "Battle joining failed");
  86. });
  87. Program.TasClient.BattleJoined += battleJoinHandler;
  88. Program.TasClient.JoinBattleFailed += battleJoinFailedHandler;
  89. //Program.JugglerBar.Deactivate();
  90. Program.BattleBar.StartManualBattle(battleID, password);
  91. }
  92. /// <summary>
  93. /// Joins same battle as player
  94. /// </summary>
  95. public static void JoinPlayer(string name)
  96. {
  97. var client = Program.TasClient;
  98. if (!client.IsLoggedIn) return;
  99. User user;
  100. if (client.ExistingUsers.TryGetValue(name, out user) && user.IsInBattleRoom)
  101. {
  102. var bat = client.ExistingBattles.Values.FirstOrDefault(x => x.Users.Any(y => y.Name == name));
  103. if (bat != null) JoinBattle(bat.BattleID, null);
  104. }
  105. }
  106. public static void JoinSlot(MissionSlot slot)
  107. {
  108. if (ChangeDesiredSpectatorState(false))
  109. {
  110. var newStatus = Program.TasClient.MyBattleStatus.Clone();
  111. newStatus.AllyNumber = slot.AllyID;
  112. newStatus.TeamNumber = slot.TeamID;
  113. newStatus.IsSpectator = false;
  114. newStatus.TeamColor = slot.Color;
  115. Program.TasClient.SendMyBattleStatus(newStatus);
  116. }
  117. }
  118. /// <summary>
  119. /// Selects Next Button
  120. /// </summary>
  121. public static void NextButton()
  122. {
  123. Program.MainWindow.navigationControl.Path = Program.MainWindow.ChatTab.GetNextTabPath();
  124. }
  125. public static void PerformAction(string actionString)
  126. {
  127. if (!String.IsNullOrEmpty(actionString))
  128. {
  129. var idx = actionString.IndexOf(':');
  130. var command = actionString;
  131. var arg = "";
  132. if (idx > -1)
  133. {
  134. command = actionString.Substring(0, idx);
  135. arg = actionString.Substring(idx + 1);
  136. }
  137. switch (command)
  138. {
  139. case "logout":
  140. Program.TasClient.Disconnect();
  141. Program.Conf.LobbyPlayerName = "";
  142. Program.ConnectBar.TryToConnectTasClient();
  143. break;
  144. case "start_mission":
  145. StartMission(arg);
  146. break;
  147. case "start_replay":
  148. var parts = arg.Split(',');
  149. StartReplay(parts[0],parts[1],parts[2],parts[3]);
  150. break;
  151. case "host_mission":
  152. SpawnAutohost(arg, String.Format("{0}'s {1}", Program.Conf.LobbyPlayerName, arg), null, null);
  153. break;
  154. case "start_script_mission":
  155. StartScriptMission(arg);
  156. break;
  157. case "select_map":
  158. if (Program.TasClient.MyBattle != null) Program.TasClient.Say(TasClient.SayPlace.Battle, null, "!map " + arg, false);
  159. else
  160. {
  161. var name = String.Format("{0}'s game", Program.Conf.LobbyPlayerName);
  162. SpawnAutohost(KnownGames.List.First(x => x.IsPrimary).RapidTag, name, null, new List<string> { "!map " + arg });
  163. }
  164. break;
  165. case "add_friend":
  166. Program.FriendManager.AddFriend(arg);
  167. break;
  168. case "join_battle":
  169. JoinPlayer(arg);
  170. break;
  171. case "join_player":
  172. JoinPlayer(arg);
  173. break;
  174. }
  175. }
  176. }
  177. /// <summary>
  178. /// Selects Previous Button
  179. /// </summary>
  180. public static void PrevButton()
  181. {
  182. Program.MainWindow.navigationControl.Path = Program.MainWindow.ChatTab.GetPrevTabPath();
  183. }
  184. /// <summary>
  185. /// Displays a window with the debug log
  186. /// </summary>
  187. public static void ShowLog()
  188. {
  189. Program.MainWindow.DisplayLog();
  190. }
  191. public static void SpawnAutohost(string gameName, string battleTitle, string password, IEnumerable<string> springieCommands)
  192. {
  193. var hostSpawnerName = SpringieCommand.GetHostSpawnerName(gameName);
  194. var spawnCommand = SpringieCommand.Spawn(gameName, battleTitle, password);
  195. var waitingBar = WarningBar.DisplayWarning("Waiting for AutoHost to start");
  196. EventHandler<CancelEventArgs<TasSayEventArgs>> joinGame = null;
  197. joinGame = (s, e) =>
  198. {
  199. if (e.Data.Place == TasSayEventArgs.Places.Normal && e.Data.Origin == TasSayEventArgs.Origins.Player && (e.Data.Text == spawnCommand.Reply))
  200. {
  201. e.Cancel = true;
  202. Program.NotifySection.RemoveBar(waitingBar);
  203. Program.TasClient.PreviewSaid -= joinGame;
  204. var myHostName = e.Data.UserName;
  205. var battle = Program.TasClient.ExistingBattles.Values.First(b => b.Founder.Name == myHostName);
  206. EventHandler<EventArgs<Battle>> battleJoined = null;
  207. battleJoined = (s2, e2) =>
  208. {
  209. if (e2.Data.BattleID == battle.BattleID)
  210. {
  211. if (springieCommands != null)
  212. {
  213. foreach (var command in springieCommands)
  214. {
  215. HidePM(command);
  216. Program.TasClient.Say(TasClient.SayPlace.User, myHostName, command, false);
  217. }
  218. }
  219. Program.TasClient.BattleJoined -= battleJoined;
  220. }
  221. };
  222. Program.TasClient.BattleJoined += battleJoined;
  223. JoinBattle(battle.BattleID, password);
  224. NavigationControl.Instance.Path = "chat/battle";
  225. }
  226. };
  227. Program.TasClient.PreviewSaid += joinGame;
  228. HidePM(spawnCommand.Command);
  229. Program.TasClient.Say(TasClient.SayPlace.User, hostSpawnerName, spawnCommand.Command, false);
  230. }
  231. /// <summary>
  232. /// Set this client as spectator
  233. /// </summary>
  234. public static void Spectate()
  235. {
  236. if (ChangeDesiredSpectatorState(true))
  237. {
  238. var newStatus = Program.TasClient.MyBattleStatus.Clone();
  239. newStatus.IsSpectator = true;
  240. Program.TasClient.SendMyBattleStatus(newStatus);
  241. }
  242. }
  243. public static void StartMission(string name)
  244. {
  245. Program.NotifySection.AddBar(new MissionBar(name));
  246. }
  247. public static void StartReplay(string url, string mod, string map, string engine)
  248. {
  249. Program.NotifySection.AddBar(new ReplayBar(url, mod, map, engine));
  250. }
  251. public static void StartScriptMission(string name)
  252. {
  253. try
  254. {
  255. var serv = new ContentService() { Proxy = null };
  256. SinglePlayerBar.DownloadAndStartMission(serv.GetScriptMissionData(name));
  257. }
  258. catch (WebException ex)
  259. {
  260. Trace.TraceWarning("Problem starting script mission {0}: {1}", name, ex);
  261. }
  262. catch (Exception ex)
  263. {
  264. Trace.TraceError("Error starting mission {0}: {1}", name, ex);
  265. }
  266. }
  267. public static void StopBattle()
  268. {
  269. Program.BattleBar.Stop();
  270. }
  271. public static void StartQuickMatch() {
  272. Program.JugglerBar.Activate();
  273. }
  274. public static void StopQuickMatch()
  275. {
  276. Program.JugglerBar.Deactivate();
  277. }
  278. public static void ToggleQuickMatch()
  279. {
  280. Program.JugglerBar.SwitchState();
  281. }
  282. /// <summary>
  283. /// Unspec this client
  284. /// </summary>
  285. public static void UnSpec()
  286. {
  287. if (ChangeDesiredSpectatorState(false))
  288. {
  289. if (Program.TasClient.MyBattle != null)
  290. {
  291. var newStatus = Program.TasClient.MyBattleStatus.Clone();
  292. newStatus.IsSpectator = false;
  293. Program.TasClient.SendMyBattleStatus(newStatus);
  294. }
  295. }
  296. }
  297. }
  298. }