/// <summary>Oyunu Komple Yükler ve de bazı verileri de pratiklik olsun diye set eder</summary> public static bool GameView(ciFramework frameW, csGameView game, short key) { csPlayers players = null; if (!King.UtilPlayers.Load(frameW, ref players)) return false; XDocument docGames = null; if (!King.Utils.LoadDb(frameW, ref docGames, "Games")) return false; XElement nodeGame = null; if (!King.Utils.FindGameNode(frameW, ref nodeGame, docGames, key)) return false; // game.Key = key; game.Date = Convert.ToString(nodeGame.Attribute("date").Value); game.IsKing = nodeGame.Attribute("king") != null && nodeGame.Attribute("king").Value == "1"; // Players foreach (XElement nodePlayer in nodeGame.Element("players").Descendants("player")) { var player = new csGamePlayer();// player.Key = Convert.ToInt16(nodePlayer.Attribute("key").Value); player.Name = players.FirstOrDefault(f => f.Key == player.Key).Name; game.Players.Add(player); // } // oynanan turnler var nodeTurns = nodeGame.Element("turns"); if (nodeTurns != null) { foreach (XElement nodeTurn in nodeTurns.Descendants("turn")) { var turn = new csGameTurn();// turn.Key = Convert.ToInt16(nodeTurn.Attribute("key").Value); turn.Type = (enmGameType)Convert.ToByte(nodeTurn.Attribute("type").Value); turn.Player = Convert.ToInt16(nodeTurn.Attribute("player").Value); turn.IsKing = nodeTurn.Attribute("king") != null && nodeTurn.Attribute("king").Value == "1"; // kimler almış elleri foreach (XElement nodeMove in nodeTurn.Element("moves").Descendants("move")) { var move = new csGameTurn.csGameMove();// move.Player = Convert.ToInt16(nodeMove.Attribute("player").Value); move.Quantity = Convert.ToByte(nodeMove.Attribute("quantity").Value); turn.Moves.Add(move); // } game.Turns.Add(turn); // } } // olası OyunTiplerini de ekleyelim var gameTypes = GameTypes(); foreach (var gameType in gameTypes) { game.AvailTurns.Add(new csAvailTurn { GameType = gameType }); } // oynanmış turnlerden data bulalım foreach (var turn in game.Turns) { var gameType = game.AvailTurns.FirstOrDefault(f => f.GameType.Key == turn.Type); gameType.Played += 1; // oynayan kim var playerDef = game.Players.FirstOrDefault(f => f.Key == turn.Player); playerDef.Played += 1; if (gameType.GameType.Key == enmGameType.Trump) { playerDef.PlayedTrump += 1; } else { playerDef.PlayedPenalty += 1; } // her oyundaki her eli playerlara paylaştıralım foreach (var move in turn.Moves) { var playerMove = game.Players.FirstOrDefault(f => f.Key == move.Player); if (gameType.GameType.Key == enmGameType.Trump) { playerMove.TotalUp += move.Quantity * gameType.GameType.Value; } else { playerMove.TotalDown += move.Quantity * gameType.GameType.Value; } playerMove.Total = playerMove.TotalUp - playerMove.TotalDown; } } return true; }
public static bool GameTurnSave(ciFramework frameW, short gameKey, csGameTurn turn) { // toplam el ile seçilen uyuyor mu diye bakalım var gameTypeDef = GameType(turn.Type); int totalMove = 0; foreach (var itemMove in turn.Moves) { totalMove += itemMove.Quantity; } if (totalMove != gameTypeDef.HowMany) return frameW.ErrorAdd("Toplam " + gameTypeDef.HowMany + " El Seçilmeli"); // XDocument docGames = null; if (!King.Utils.LoadDb(frameW, ref docGames, "Games")) return false; XElement nodeGame = null; if (!King.Utils.FindGameNode(frameW, ref nodeGame, docGames, gameKey)) return false; //turns node var nodeTurns = nodeGame.Element("turns"); if (nodeTurns == null) { nodeTurns = new XElement("turns"); nodeGame.Add(nodeTurns); } // turn oluştur veya bul XElement nodeTurn = null; if (turn.Key == 0) { turn.Key = nodeTurns.Nodes().Count() + 1; nodeTurn = new XElement("turn"); nodeTurn.Add(new XAttribute("key", turn.Key)); nodeTurns.Add(nodeTurn); } else { if (!King.Utils.FindTurnNode(frameW, ref nodeTurn, nodeGame, turn.Key)) return false; } // update veya insert King.Utils.UpdateAttribute(nodeTurn, "player", Convert.ToString(turn.Player)); King.Utils.UpdateAttribute(nodeTurn, "type", Convert.ToByte(turn.Type).ToString()); King.Utils.UpdateAttribute(nodeTurn, "king", (turn.IsKing ? 1 : 0).ToString()); //eski move varsa da silelim if (nodeTurn.Element("moves") != null) nodeTurn.Element("moves").Remove(); // var nodeMoves = new XElement("moves"); nodeTurn.Add(nodeMoves); foreach (var itemMove in turn.Moves) { if (itemMove.Quantity > 0) { var nodeMove = new XElement("move"); nodeMoves.Add(nodeMove); nodeMove.Add(new XAttribute("player", itemMove.Player.ToString())); nodeMove.Add(new XAttribute("quantity", itemMove.Quantity.ToString())); } } King.Utils.SaveDb(docGames, "Games"); return true; }