Skip to content

Commit

Permalink
Minor Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBetteridge committed Oct 6, 2016
1 parent cfe6d41 commit 94954e8
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions Connect4/Controllers/ChangeGameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using Connect4.Bots;
Expand All @@ -27,21 +28,33 @@ public async Task<ActionResult> Index(ChangeGameViewModel model)
var thisPlayer = allPlayers.SingleOrDefault(a => a.Name == model.PlayerName);
if (thisPlayer == null) return RedirectToAction("Index", "Home");

var game = new Game();
game.ID = Guid.NewGuid();
game.YellowPlayerID = model.OtherPlayerID.Value;
game.RedPlayerID = thisPlayer.ID;
var currentGameID = thisPlayer.CurrentGameID;

var newGame = new Game();
newGame.ID = Guid.NewGuid();
newGame.YellowPlayerID = model.OtherPlayerID.Value;
newGame.RedPlayerID = thisPlayer.ID;

var otherPlayer = await database.LoadPlayer(model.OtherPlayerID.Value);
if (otherPlayer.SystemBot)
{
var bot = BaseBot.GetBot(model.OtherPlayerID.Value);
bot.MakeMove(game);
bot.MakeMove(newGame);
}

await this.database.SaveGame(game);
using (var tx = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
// Create the new game
await database.SaveGame(newGame);

// and delete the old game
if (currentGameID.HasValue)
await database.DeleteGame(currentGameID.Value);

tx.Complete();
}

return RedirectToAction("Index", "DisplayGame", new { gameID = game.ID });
return RedirectToAction("Index", "DisplayGame", new { gameID = newGame.ID });
}

// GET: ChangeGame
Expand Down

0 comments on commit 94954e8

Please sign in to comment.