// From a Game object, and the current user, construct the information needed // to render a particular play layout for a user's game. public GameModel(Game gameIn, UserProfile currentUserIn) { game = gameIn; currentUser = currentUserIn; // Find out the Player object and // playerId of the current user int playerId = 0; bool found = false; int index = 0; int indexOfCurrentPlayer = 0; while (! found) { Player player = game.Players[index]; if (player.UserId == currentUser.UserId) { playerId = player.PlayerId; indexOfCurrentPlayer = index; found = true; } index++; } if (!found) throw new InvalidOperationException(); currentPlayer = game.Players[indexOfCurrentPlayer]; userPlayerId = playerId; }
// Create a brand new game given an array of user profiles who // will be the players. public static GameModel NewGame(UserProfile[] playerProfiles) { Player[] players = new Player[2]; int count = 0; foreach (UserProfile profile in playerProfiles) { players[count++] = Player.FindOrCreate(profile.UserId, profile.UserName); } Game game = new Game(players); game.StartGame(); return new GameModel(game, playerProfiles[0]); }
public PlayerModel(UserProfile userProfile) { Profile = userProfile; player = Player.FindOrCreate(userProfile.UserId, userProfile.UserName); }
// Retrieves a game already in progress. public static GameModel GetByID(int id, UserProfile currentUser) { Game game = new Game(id); return new GameModel(game, currentUser); }