public async Task SetTicTacToeStepAsync(TicTacToe game, int cellIndex) { TicTacToe.CellData cell = game !.Cells[cellIndex] !; var command = _games.UpdateOne() .Filter(b => b .Match(g => g.Id == game.Id && g.GameType == game.GameType && g.Status == GameStatus.InGame && g.Version == game.Version) .Match($"{{ 'Cells.{cellIndex}': {{ $type: 10 }} }}")) // $type 10 is null .Update(b => b .Modify($"{{ $set : {{ 'Cells.{cellIndex}' : {{ 'Number': {game.TickedCellsCount} }} }} }}") .Modify(g => g.Inc(g => g.Version, 1))); if (game.Status == GameStatus.Finished) { var bsonPlayers = game.ToBsonDocument().GetElement("Players").Value.ToString(); command = command .Update(b => b .Modify($"{{ $set : {{ 'Status' : {(int)game.Status} }} }}") .Modify($"{{ $set : {{ 'EndedAt' : '{DateTime.Now.ToIso()}' }} }}") .Modify($"{{ $set : {{ 'Players' : {bsonPlayers} }} }}")); } var updateResult = await command.ExecuteAsync(); if (updateResult.MatchedCount == 0 || updateResult.ModifiedCount == 0) { throw new UpdateException(); } }
public TicTacToeBuilder TickCell(int cellIndex) { _cells[cellIndex] = new TicTacToe.CellData() { Number = _stepNumber++ }; return(this); }
public TicTacToeBuilder TickOtherCell(params int[] butNotTheseCellIndexes) { var random = new Random(); int cellIndex; do { cellIndex = random.Next(0, 9); // first value is inclusive, second is exclusive } while (butNotTheseCellIndexes.Contains(cellIndex)); _cells[cellIndex] = new TicTacToe.CellData() { Number = _stepNumber++ }; return(this); }