// POST: api/Game public HttpResponseMessage Post(Game game) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } //init game initNewGame(game); //update the users data so that they reffer to the new game List<User> users = db.GetUsers(); User player1 = users.Where(x => x.Name == game.Player1).SingleOrDefault(); User player2 = users.Where(x => x.Name == game.Player2).SingleOrDefault(); player1.CurrentGame = game.GameId; player2.CurrentGame = game.GameId; db.SetUsers(users); //add a new game to the games list List<Game> games = db.GetGames(); games.Add(game); db.SetGames(games); return Request.CreateResponse(HttpStatusCode.Created); }
// PUT: api/Game public HttpResponseMessage Put(Game updatedGame) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } //update the game data List<Game> games = db.GetGames(); Game originalGame = games.Where(x => x.GameId == updatedGame.GameId).SingleOrDefault(); int addedValue = (updatedGame.CurrentValue - originalGame.CurrentValue); createInGameMessage(originalGame, String.Format("{0} added {1} to the current value", originalGame.CurrentPlayer, addedValue)); originalGame.CurrentValue = updatedGame.CurrentValue; createInGameMessage(originalGame, String.Format("Current value after adding {0} is: {1}", addedValue, originalGame.CurrentValue)); originalGame.CurrentValue = (originalGame.CurrentValue / 3); createInGameMessage(originalGame, String.Format("Current value after dividing by 3 is : {0}", originalGame.CurrentValue)); if (originalGame.CurrentValue == 1) { createInGameMessage(originalGame, String.Format("Game Over: current value is {0}", originalGame.CurrentValue)); } else { originalGame.CurrentPlayer = (originalGame.CurrentPlayer == originalGame.Player1) ? originalGame.Player2 : originalGame.Player1; createInGameMessage(originalGame, String.Format("{0}'s turn to make a move", originalGame.CurrentPlayer)); } db.SetGames(games); return Request.CreateResponse(HttpStatusCode.OK); }
private void initNewGame(Game game) { game.GameId = String.Concat(game.Player1, game.Player2); if (db.GameExists(game.GameId)) { List<Game> games = db.GetGames(); Game dupGame = games.Where(x => x.GameId == game.GameId).SingleOrDefault(); games.RemoveAt(games.IndexOf(dupGame)); db.SetGames(games); } game.CurrentPlayer = game.Player1; Random random = new Random(); game.CurrentValue = random.Next(1, 100); game.MessageList = new List<Message>(); createInGameMessage(game, "Starting a new game.."); createInGameMessage(game, String.Format("{0} vs {1}..", game.Player1, game.Player2)); createInGameMessage(game, String.Format("Current value is: {0}", game.CurrentValue)); createInGameMessage(game, String.Format("{0}'s turn to make a move", game.CurrentPlayer)); }
private void createInGameMessage(Game game, string content) { Message message = new Message(MessageType.InGameMessage, "", "", content); game.MessageList.Add(message); }