/// <summary> /// Sends a ping to the game partner /// </summary> public static void Ping() { try { Dictionary<string, object> dict = new Dictionary<string, object>(); MatchMessage message = new MatchMessage(MatchAction.Ping, dict); AppCache.XmppManager.Client.SendRawXML(message.ToSendXML(AppCache.XmppManager.OwnID, AppCache.CurrentMatch.PartnerJID)); } catch (Exception e) { MessageBox.Show(e.Message); } }
/// <summary> /// Parse an XML string and returns a specific BattleshipMessage, if it was valid. /// </summary> /// <param name="strXML">The XML string</param> /// <returns>The specific BattleshipMessage. Returns null, if it was not a valid BattleshipMessage.</returns> public static BattleshipMessage GetBattleshipMessage(String strXML) { try { using (XmlReader reader = XmlReader.Create(new StringReader(strXML))) { Boolean isBattleship = false; Boolean isShoot = false; MatchMessage ShootMessage = null; while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: if (reader.Name.ToLower().Equals("battleship")) { isBattleship = true; } else if ((isBattleship) && reader.Name.ToLower().Equals("queueing")) { String result = reader.GetAttribute("action"); Dictionary<String, Object> dictionary = new Dictionary<String, Object>(); QueuingMessage message = null; if (result != null) { switch (result) { case "request": message = new QueuingMessage(Enum.QueueingAction.request, null); break; case "success": dictionary.Add("id", Convert.ToInt32(reader.GetAttribute("id"))); message = new QueuingMessage(Enum.QueueingAction.success, dictionary); break; case "ping": string id = reader.GetAttribute("id"); if (!String.IsNullOrEmpty(id)) { dictionary.Add("id", Convert.ToInt32(id)); } message = new QueuingMessage(Enum.QueueingAction.ping, dictionary); break; case "assign": dictionary.Add("jid", new JID(reader.GetAttribute("jid"))); dictionary.Add("mid", reader.GetAttribute("mid")); message = new QueuingMessage(Enum.QueueingAction.assign, dictionary); break; case "assigned": dictionary.Add("jid", new JID(reader.GetAttribute("jid"))); dictionary.Add("mid", Convert.ToInt32(reader.GetAttribute("mid"))); message = new QueuingMessage(Enum.QueueingAction.assigned, dictionary); break; } } return message; } else if ((isBattleship) && reader.Name.ToLower().Equals("diceroll")) { int dice = Convert.ToInt32(reader.GetAttribute("dice")); Dictionary<String, Object> dictionary = new Dictionary<String, Object>(); dictionary.Add("dice", dice); MatchMessage message = new MatchMessage(Enum.MatchAction.Diceroll, dictionary); return message; } else if ((isBattleship) && reader.Name.ToLower().Equals("shoot")) { isShoot = true; Dictionary<String, Object> dictionary = new Dictionary<String, Object>(); int x = Convert.ToInt32(reader.GetAttribute("x")); int y = Convert.ToInt32(reader.GetAttribute("y")); string result = reader.GetAttribute("result"); dictionary.Add("x", x); dictionary.Add("y", y); Enum.MatchAction action = Enum.MatchAction.Shot; if (!String.IsNullOrEmpty(result)) { action = Enum.MatchAction.Shotresult; dictionary.Add("result", result); } MatchMessage message = new MatchMessage(action, dictionary); ShootMessage = message; } else if ((isShoot) && reader.Name.ToLower().Equals("ship")) { if (ShootMessage != null) { ShipInfo shipInfo = new ShipInfo(); shipInfo.X = Convert.ToInt32(reader.GetAttribute("x")); shipInfo.Y = Convert.ToInt32(reader.GetAttribute("y")); shipInfo.Destroyed = Convert.ToBoolean(reader.GetAttribute("destroyed")); shipInfo.Size = Convert.ToInt32(reader.GetAttribute("size")); String orientation = reader.GetAttribute("orientation"); if (orientation.ToLower().Equals("horizontal")) shipInfo.Orientation = System.Windows.Controls.Orientation.Horizontal; else shipInfo.Orientation = System.Windows.Controls.Orientation.Vertical; ShootMessage.ShipInfo = shipInfo; } } else if ((isBattleship) && reader.Name.ToLower().Equals("gamestate")) { Dictionary<String, Object> dictionary = new Dictionary<String, Object>(); dictionary.Add("state", reader.GetAttribute("state")); dictionary.Add("looser", reader.GetAttribute("looser")); MatchMessage message = new MatchMessage(Enum.MatchAction.Gamestate, dictionary); return message; } else if ((isBattleship = true) && reader.Name.ToLower().Equals("ping")) { Dictionary<String, Object> dictionary = new Dictionary<String, Object>(); Enum.MatchAction action = Enum.MatchAction.Ping; MatchMessage message = new MatchMessage(action, dictionary); return message; } break; case XmlNodeType.Text: break; case XmlNodeType.XmlDeclaration: case XmlNodeType.ProcessingInstruction: break; case XmlNodeType.Comment: break; case XmlNodeType.EndElement: if (reader.Name.ToLower().Equals("battleship")) { isBattleship = false; if (isShoot && ShootMessage!=null) { return ShootMessage; } } break; } } } } catch (Exception) { } return null; }
/// <summary> /// Sends the game state when a match is finished /// </summary> /// <param name="looser">The Jabber-ID of the looser</param> public static void SendGamestate(String looser) { Dictionary<string, object> dict = new Dictionary<string, object>(); dict.Add("state", "end"); dict.Add("looser", looser); MatchMessage message = new MatchMessage(MatchAction.Gamestate, dict); try { AppCache.XmppManager.Client.SendRawXML(message.ToSendXML(AppCache.XmppManager.OwnID, AppCache.CurrentMatch.PartnerJID)); } catch (Exception e) { MessageBox.Show(e.Message); } }
/// <summary> /// Transfer the results of an incoming shot to the partner /// </summary> /// <param name="x">The X-Coordinate</param> /// <param name="y">The Y-Coordinate</param> /// <param name="isHit">True, if a ship is hit</param> /// <param name="shipInfo">Is not null, if a ship is destroyed</param> public static void TransferShotResult(int x, int y, Boolean isHit, ShipInfo shipInfo) { Dictionary<string, object> dict = new Dictionary<string, object>(); dict.Add("x", x); dict.Add("y", y); if (isHit) { dict.Add("result", "ship"); } else { dict.Add("result", "water"); } MatchMessage message = new MatchMessage(MatchAction.Shotresult, dict); if (shipInfo != null) { message.ShipInfo = shipInfo; } try { AppCache.XmppManager.Client.SendRawXML(message.ToSendXML(AppCache.XmppManager.OwnID, AppCache.CurrentMatch.PartnerJID)); } catch (Exception e) { MessageBox.Show(e.Message); } }
/// <summary> /// Sends a shot to the game partner /// </summary> /// <param name="x">The X-Coordinate</param> /// <param name="y">The Y-Coordinate</param> public static void Shoot(int x, int y) { Dictionary<string, object> dict = new Dictionary<string, object>(); dict.Add("x", x); dict.Add("y", y); MatchMessage message = new MatchMessage(MatchAction.Shot, dict); try { AppCache.XmppManager.Client.SendRawXML(message.ToSendXML(AppCache.XmppManager.OwnID, AppCache.CurrentMatch.PartnerJID)); } catch (Exception e) { MessageBox.Show(e.Message); } }