예제 #1
0
        /// <summary>
        /// Creates a new player record based on a player name.
        /// </summary>
        /// <param name="name">Name of the player to create</param>
        /// <returns>Player record.</returns>
        public static Player Create(string name)
        {
            // Initialize the new player.
            var createDate = DateTime.Now;
            var player     = new Player
            {
                Name             = name,
                JoinDate         = createDate,
                LastLoggedInDate = createDate
            };

            PlayerRepository.Create(player);
            var createdPlayer = PlayerRepository.GetByName(player.Name);

            // Initialize the new player's Net Worth
            var netWorth = new NetWorth
            {
                PlayerId        = createdPlayer.Id,
                Value           = 0,
                LastUpdatedDate = createDate
            };

            NetWorthRepository.Create(netWorth);
            var createdNetWorth = NetWorthRepository.GetByPlayerId(createdPlayer.Id);

            // Reputations will be initialized the first time they change.

            // Build the player.
            createdPlayer.NetWorth = createdNetWorth;
            createdPlayer.Bounties = new List <Bounty>();

            return(createdPlayer);
        }
예제 #2
0
        /// <summary>
        /// Gets an existing player record by name.
        /// </summary>
        /// <param name="name">Name to look for.</param>
        /// <returns>Player if found, otherwise null.</returns>
        public static Player GetByName(string name)
        {
            var existingPlayer = PlayerRepository.GetByName(name);

            if (existingPlayer == null)
            {
                return(null);
            }

            var existingNetWorth = NetWorthRepository.GetByPlayerId(existingPlayer.Id);

            if (existingNetWorth == null)
            {
                throw new ApplicationException($"Could not find a NetWorths record for Player ID #{existingPlayer.Id}");
            }

            var existingBounties = BountyRepository.GetByTargetPlayerId(existingPlayer.Id);

            if (existingBounties == null)
            {
                existingBounties = new List <Bounty>();
            }

            var existingReputations = ReputationRepository.GetByPlayerId(existingPlayer.Id);

            if (existingReputations == null)
            {
                existingReputations = new List <Reputation>();
            }

            existingPlayer.NetWorth    = existingNetWorth;
            existingPlayer.Bounties    = existingBounties;
            existingPlayer.Reputations = existingReputations;

            return(existingPlayer);
        }
예제 #3
0
 /// <summary>
 /// Updates a player record from the in-memory object.
 /// </summary>
 /// <param name="player">Player to update</param>
 public static void Update(Player player)
 {
     PlayerRepository.Update(player);
     NetWorthRepository.Update(player.NetWorth);
 }