public GameShortModel(Game game) { this.Id = game.Id; this.Name = game.Name; this.Blue = (game.GameState == BullsCows.Models.GameState.WaitingForOpponent) ? "No blue player yet" : game.Blue.Email; this.Red = game.Red.Email; this.GameState = game.GameState.ToString(); this.DateCreated = game.DateCreated; }
//{ // "Id": 1, // "Name": "Battle of the titans", // "DateCreated": "2014-09-22T10:39:37.087", // "Red": "*****@*****.**", // "Blue": "*****@*****.**", // "YourNumber": 1234, // "YourGuesses": [ // { // "Id": 8, // "UserId": "7e1aaf37-d7c3-42e3-8781-e49bce747206", // "Username": "******", // "GameId": 1, // "Number": "1234", // "DateMade": "2014-09-22T14:48:01.16", // "CowsCount": 4, // "BullsCount": 0 // }, // ], // "OpponentGuesses": [ // { // "Id": 9, // "UserId": "12d10b41-fdd4-4d61-8ad5-980af83263d8", // "Username": "******", // "GameId": 1, // "Number": "5432", // "DateMade": "2014-09-22T14:48:14.753", // "CowsCount": 2, // "BullsCount": 1 // }, // ], // "YourColor": "red", // "GameState": "RedInTurn" //} public GameDetailsModel(Game game, string userId) { this.Id = game.Id; this.Name = game.Name; this.Blue = game.Blue.Email; this.Red = game.Red.Email; this.GameState = game.GameState.ToString(); this.DateCreated = game.DateCreated; this.YourNumber = game.BlueId == userId ? game.BlueNumber : game.RedNumber; this.YourColor = game.BlueId == userId ? PlayerColor.Blue.ToString().ToLower() : PlayerColor.Red.ToString().ToLower(); var regGuesses = game.RedGuesses.Select(guess => new GuessModel { Id = guess.Id, UserId = guess.UserId, UserName = guess.User.Email, GameId = guess.GameId, Number = guess.Number, DateMade = guess.DateMade, CowsCount = guess.CowsCount, BullsCount = guess.BullsCount }); var blueGuesses = game.BlueGuesses.Select(guess => new GuessModel { Id = guess.Id, UserId = guess.UserId, UserName = guess.User.Email, GameId = guess.GameId, Number = guess.Number, DateMade = guess.DateMade, CowsCount = guess.CowsCount, BullsCount = guess.BullsCount }); this.YourGuesses = game.BlueId == userId ? blueGuesses : regGuesses; this.OpponentGuesses = game.BlueId == userId ? regGuesses : blueGuesses; }
public IHttpActionResult Post(CreateGameModel request) { var userId = this.User.Identity.GetUserId(); if (request == null || !ModelState.IsValid) { return this.BadRequest(ModelState); } var game = new Game() { Name = request.Name, RedId = userId, RedNumber = request.Number, GameState = GameState.WaitingForOpponent, DateCreated = DateTime.Now }; this.data.Games.Add(game); this.data.Users.Find(userId).Games.Add(game); this.data.SaveChanges(); var gameModel = new GameDetailsModel(game, userId); return this.CreatedAtRoute("DefaultApi", new { id = gameModel.Id }, gameModel); }