/// <summary> /// Create new XML with game stat if there is no existing /// </summary> /// <param name="model">last game stat</param> public void WriteXMLFile(GameStatServiceModel model) { XmlDocument doc = new XmlDocument(); if (File.Exists(this._xmlFile)) { doc.Load(this._xmlFile); // <game> XmlElement gameElement = doc.CreateElement(string.Empty, GAME_NODE, string.Empty); doc.DocumentElement.AppendChild(gameElement); // <player_name>{player name as text}</player_name> XmlElement playerNameElement = doc.CreateElement(string.Empty, PLAYER_NAME_NODE, string.Empty); XmlText playerNameAsText = doc.CreateTextNode(model.PlayerName); playerNameElement.AppendChild(playerNameAsText); gameElement.AppendChild(playerNameElement); // <result>{result can be win/lose/draw}</result> XmlElement resultElement = doc.CreateElement(string.Empty, RESULT_NODE, string.Empty); XmlText resultAsText = doc.CreateTextNode(model.Result); resultElement.AppendChild(resultAsText); gameElement.AppendChild(resultElement); } else { // <games_stat> XmlElement gamesStatElement = doc.CreateElement(string.Empty, GAMES_STAT_NODE, string.Empty); doc.AppendChild(gamesStatElement); // <game> XmlElement gameElement = doc.CreateElement(string.Empty, GAME_NODE, string.Empty); gamesStatElement.AppendChild(gameElement); // <player_name>{player name as text}</player_name> XmlElement playerNameElement = doc.CreateElement(string.Empty, PLAYER_NAME_NODE, string.Empty); XmlText playerNameAsText = doc.CreateTextNode(model.PlayerName); playerNameElement.AppendChild(playerNameAsText); gameElement.AppendChild(playerNameElement); // <result>{result can be win/lose/draw}</result> XmlElement resultElement = doc.CreateElement(string.Empty, RESULT_NODE, string.Empty); XmlText resultAsText = doc.CreateTextNode(model.Result); resultElement.AppendChild(resultAsText); gameElement.AppendChild(resultElement); } doc.Save(this._xmlFile); }
/// <summary> /// Save the last game into the XML file /// </summary> public void SaveGame() { var gameStatModel = new GameStatServiceModel { PlayerName = this.Name }; if (this.GameWinner.Key is Winners.Player) { gameStatModel.Result = "win"; } else if (this.GameWinner.Key is Winners.Robot) { gameStatModel.Result = "lose"; } else { gameStatModel.Result = "draw"; } this._xmlService.WriteXMLFile(gameStatModel); }