/// <summary> /// Helper method to create a <see cref="PlayerMissionSession"/> in the database /// </summary> /// <param name="playerMissionSession"><see cref="PlayerMissionSession"/> to create</param> /// <returns><see cref="PlayerMissionSession"/> with added database id</returns> private PlayerMissionSession CreatePlayerMissionSession(PlayerMissionSession playerMissionSession) { _addPlayerMissionSession.Parameters[DatabaseUtil.PLAYER_ID_KEY].Value = playerMissionSession.Player.Id; _addPlayerMissionSession.Parameters[DatabaseUtil.MISSION_TO_SESSION_ID_KEY].Value = playerMissionSession.MissionSession.Id; _addPlayerMissionSession.ExecuteNonQuery(); playerMissionSession.Id = GetLastInsertedId(); _logger.DebugFormat("Player mission session inserted into the database with ID: {0}", playerMissionSession.Id); return(playerMissionSession); }
/// <summary> /// Function to update a <see cref="PlayerMissionSession"/> on the database level /// </summary> /// <param name="playerToMissionSession"><see cref="PlayerMissionSession"/> to update</param> public void UpdatePlayerMissionSession(PlayerMissionSession playerToMissionSession) { _updatePlayerMissionSession.Parameters[DatabaseUtil.PLAYED_KEY].Value = playerToMissionSession.Played; _updatePlayerMissionSession.Parameters[DatabaseUtil.LENGTH_KEY].Value = playerToMissionSession.Length; _updatePlayerMissionSession.Parameters[DatabaseUtil.PLAYER_TO_MISSION_TO_SESSION_ID_KEY].Value = playerToMissionSession.Id; _updatePlayerMissionSession.ExecuteNonQuery(); _logger.DebugFormat("Player mission session updated with ID: {0}", playerToMissionSession.Id); if (playerToMissionSession.Player.Updated) { _updatePlayer.Parameters[DatabaseUtil.HAS_CLAN_TAG_KEY].Value = playerToMissionSession.Player.HasClanTag; _updatePlayer.Parameters[DatabaseUtil.PLAYER_ID_KEY].Value = playerToMissionSession.Player.Id; _updatePlayer.ExecuteNonQuery(); _logger.DebugFormat("Player updated with ID: {0}", playerToMissionSession.Player.Id); } }
/// <summary> /// Gets or creates a set of <see cref="PlayerMissionSession"/>s from the given list of <see cref="Player"/>s and the current <see cref="Session"/>. /// Using caching to reduce MySQL calls and speed up runtime /// </summary> /// <param name="players">List of <see cref="Player"/>s to get/create</param> /// <param name="missionSession">Current <see cref="MissionSession"/></param> /// <returns>A set of <see cref="PlayerMissionSession"/> objects, from the database.</returns> public ISet <PlayerMissionSession> GetOrCreatePlayerMissionSessions(IList <Player> players, MissionSession missionSession) { ISet <PlayerMissionSession> playerMissionSessions = new HashSet <PlayerMissionSession>(); foreach (Player player in players) { PlayerMissionSession playerToMissionSession = new PlayerMissionSession(); playerToMissionSession.Player = player; playerToMissionSession.MissionSession = missionSession; string key = player.Name + missionSession.Id; if (_cachedPlayerMissionSessions.ContainsKey(key)) { _cachedPlayerMissionSessions.TryGetValue(key, out playerToMissionSession); _logger.DebugFormat("Retrieved player mission session from the cache with ID: {0}", playerToMissionSession.Id); } else { _getPlayerMissionSession.Parameters[DatabaseUtil.NAME_KEY].Value = player.Name; _getPlayerMissionSession.Parameters[DatabaseUtil.MISSION_TO_SESSION_ID_KEY].Value = missionSession.Id; MySqlDataReader getPlayerResult = _getPlayerMissionSession.ExecuteReader(); if (getPlayerResult.HasRows) { getPlayerResult.Read(); playerToMissionSession.Player.Id = getPlayerResult.GetInt32(0); if (getPlayerResult.GetBoolean(1) != playerToMissionSession.Player.HasClanTag) { playerToMissionSession.Player.Updated = true; } if (getPlayerResult.IsDBNull(2)) { getPlayerResult.Close(); CreatePlayerMissionSession(playerToMissionSession); } else { playerToMissionSession.Id = getPlayerResult.GetInt32(2); playerToMissionSession.Length = getPlayerResult.GetInt32(3); playerToMissionSession.Played = getPlayerResult.GetBoolean(4); _logger.DebugFormat("Retrieved player mission session from the database with ID: {0}", playerToMissionSession.Id); getPlayerResult.Close(); } } else { getPlayerResult.Close(); _addPlayer.Parameters[DatabaseUtil.NAME_KEY].Value = player.Name; _addPlayer.Parameters[DatabaseUtil.HAS_CLAN_TAG_KEY].Value = player.HasClanTag; _addPlayer.ExecuteNonQuery(); playerToMissionSession.Player.Id = GetLastInsertedId(); CreatePlayerMissionSession(playerToMissionSession); } _cachedPlayerMissionSessions.Add(key, playerToMissionSession); } playerMissionSessions.Add(playerToMissionSession); } return(playerMissionSessions); }