/// <summary> /// Gets or creates a set of <see cref="PlayerSessionToMissionSession"/> objects for the given <see cref="MissionSession"/> and set of <see cref="PlayerSession"/> objects /// Using caching to reduce MySQL calls and speed up runtime /// </summary> /// <param name="missionSession"><see cref="MissionSession"/> object used for lookup/creation</param> /// <param name="playerSessions">Set of <see cref="PlayerSession"/> objects used for lookup/creation</param> /// <returns>Set of <see cref="PlayerSession"/>s from the database</returns> public ISet<PlayerSessionToMissionSession> GetOrCreatePSTMS(MissionSession missionSession, ISet<PlayerSession> playerSessions) { ISet<PlayerSessionToMissionSession> pstmses = new HashSet<PlayerSessionToMissionSession>(); // our cached mission session id is invalid, reset the caches if (_cachedMissionSessionId != missionSession.Id) { _cachedMissionSessionId = missionSession.Id; _cachedPlayerSessionsToPSTMS = new Dictionary<int, PlayerSessionToMissionSession>(); } foreach (PlayerSession playerSession in playerSessions) { PlayerSessionToMissionSession pstms = new PlayerSessionToMissionSession(); pstms.PlayerSessionId = playerSession.Id; pstms.MissionSessionId = missionSession.Id; if (_cachedPlayerSessionsToPSTMS.ContainsKey(playerSession.Id)) { _cachedPlayerSessionsToPSTMS.TryGetValue(playerSession.Id, out pstms); _logger.DebugFormat("PSTMS retrieved from cache with id: {0}", pstms.Id); } else { //get _getPSTMS.Parameters[DatabaseUtil.PLAYER_TO_SESSION_ID_KEY].Value = playerSession.Id; _getPSTMS.Parameters[DatabaseUtil.MISSION_TO_SESSION_ID_KEY].Value = missionSession.Id; MySqlDataReader getPTSTMTSResult = _getPSTMS.ExecuteReader(); if (getPTSTMTSResult.HasRows) { getPTSTMTSResult.Read(); pstms.Id = getPTSTMTSResult.GetInt32(0); pstms.Length = getPTSTMTSResult.GetInt32(1); pstms.Played = getPTSTMTSResult.GetBoolean(2); _logger.DebugFormat("PSTMS retrieved from database with id: {0}", pstms.Id); getPTSTMTSResult.Close(); } else { //add getPTSTMTSResult.Close(); _addPSTMS.Parameters[DatabaseUtil.PLAYER_TO_SESSION_ID_KEY].Value = playerSession.Id; _addPSTMS.Parameters[DatabaseUtil.MISSION_TO_SESSION_ID_KEY].Value = missionSession.Id; _addPSTMS.ExecuteNonQuery(); pstms.Id = GetLastInsertedId(); _logger.DebugFormat("PSTMS inserted into the database with id: {0}", pstms.Id); } } pstmses.Add(pstms); } return pstmses; }
/// <summary> /// Helper function to update <see cref="PlayerSessionToMissionSession"/> data in the database /// </summary> /// <param name="pstmsDAO">DAO for <see cref="PlayerSessionToMissionSession"/>s</param> /// <param name="missionSession">The current <see cref="MissionSession"/></param> /// <param name="playerSessions">The current set of <see cref="PlayerSession"/>s</param> private void UpdatePTSTMTSData(PlayerSessionToMissionSessionDAO pstmsDAO, MissionSession missionSession, ISet<PlayerSession> playerSessions) { ISet<PlayerSessionToMissionSession> pstmses = pstmsDAO.GetOrCreatePSTMS(missionSession, playerSessions); foreach (PlayerSessionToMissionSession pstms in pstmses) { pstms.Updated = true; pstms.Length += (Settings.Default.pollRate / 1000); if (pstms.Played == false && CheckPlayedThreshold(pstms.Length)) { pstms.Played = true; } } pstmsDAO.UpdatePSTMS(pstmses); }
/// <summary> /// Helper method to update <see cref="Player"/> data in the database /// </summary> /// <param name="playerDAO">DAO for <see cref="Player"/>s</param> /// <param name="session">The current game <see cref="Session"/></param> /// <param name="serverInfo">The current <see cref="ServerInfo"/></param> /// <param name="missionSession">The current <see cref="MissionSession"/></param> /// <returns>The current set of <see cref="PlayerSession"/>s</returns> private ISet<PlayerSession> UpdatePlayerData(PlayerDAO playerDAO, Session session, ServerInfo serverInfo, MissionSession missionSession) { ISet<PlayerSession> playerSessions = playerDAO.GetOrCreatePlayerSessions(serverInfo.Players, session); foreach (PlayerSession playerSession in playerSessions) { playerSession.Updated = true; playerSession.Length += (Settings.Default.pollRate / 1000); if (playerSession.Played == false && CheckPlayedThreshold(playerSession.Length)) { playerSession.Played = true; } } playerDAO.UpdatePlayerSessions(playerSessions); return playerSessions; }