public void SetFirstMoveOfPlayer(Game game, string playerId) { var random = new Random(int.Parse(Guid.NewGuid().ToString().Substring(0, 8), System.Globalization.NumberStyles.HexNumber)); var player = game.Players.Find(p => string.Equals(p.FacebookId, playerId)); var firstMoveTileIndex = random.Next(player.Tiles.Count); game.Moves = new List<PlayerMove>() { new PlayerMove() { Col = 9, Row = 9, PlayerId = player.FacebookId, Tile = player.Tiles[firstMoveTileIndex] } }; player.Tiles.RemoveAt(firstMoveTileIndex); player.Points = 1; game.TurnOfPlayer = game.Players.Find(p => !string.Equals(p.FacebookId, playerId)).FacebookId; game.LastTurn = new LastTurn() { PlayerFacebookId = playerId, PlayerMoves = game.Moves, Points = 1, }; }
public Game CreateGame() { Game game = new Game() { Status = "Invited", Moves = new List<PlayerMove>(), AddDate = DateTime.UtcNow, ModifiedDate = DateTime.UtcNow, }; game.Players = new List<Player>() { new Player(), new Player() }; var random = new Random(int.Parse(Guid.NewGuid().ToString().Substring(0, 8), System.Globalization.NumberStyles.HexNumber)); var addedTiles = new List<int>(); foreach (var player in game.Players) { player.Tiles = new List<string>(); while (player.Tiles.Count < (_tiles.Count/2)) { var index = random.Next(_tiles.Count); if (!addedTiles.Contains(index)) { player.Tiles.Add(_tiles[index]); addedTiles.Add(index); } } player.FirstName = "Waiting..."; } return game; }
public bool SaveGame(Game game) { try { var settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects, }; game.ModifiedDate = DateTime.UtcNow; string gameString = JsonConvert.SerializeObject(game, settings); File.WriteAllText(string.Format(@"{0}\{1}.json", _filePath, game.GameId), gameString); } catch (Exception ex) { Logger.LogException(ex, _source, "SaveGame", Severity.Major); return false; } return true; }
private Game ConvertToElasticType(Game game) { Game eGame = new Game() { Status = game.Status, Winner = game.Winner, GameId = game.GameId, AddDate = game.AddDate, ModifiedDate = game.ModifiedDate, }; if (game.Players != null) { List<Player> ePlayers = new List<Player>(); foreach (var player in game.Players) { ePlayers.Add(new Player() { FacebookId = player.FacebookId, FirstName = player.FirstName, LastName = player.LastName, }); } eGame.Players = ePlayers; } return eGame; }
public Game SaveGame(Game game) { var exGame = string.IsNullOrWhiteSpace(game.GameId) ? null : GetGame(game.GameId); if (exGame != null) { game.GameId = exGame.GameId; } var eGame = ConvertToElasticType(game); var result = _client.Index(eGame); IsServerError(result); game.GameId = result.Id; _client.Refresh(); return game; }