Ejemplo n.º 1
0
        /// <summary>
        /// Begins reporting on a session, should handle DAOs here
        /// </summary>
        public void StartReporting()
        {
            // define DAO objects to interact with databases
            MySqlConnection connection = null;
            PlayerDAO       playerDAO  = null;
            MissionDAO      missionDAO = null;
            SessionDAO      sessionDAO = null;

            try {
                // create DAO objects to interact with databases
                BuildDAOsIfNeeded(ref connection, ref playerDAO, ref missionDAO, ref sessionDAO);

                ServerInfoService serverInfoService = new ServerInfoService();

                // variables needed to track session playthough
                Session        session = null;
                MissionSession currentMissionSession = null;
                ISet <PlayerMissionSession> currentPlayersToMissionSession = new HashSet <PlayerMissionSession>();
                int  missionCount = 0;
                bool inGame       = false;

                Stopwatch runTime = new Stopwatch();
                runTime.Start();
                try {
                    while (session == null && CheckTimeThreshold(runTime.ElapsedMilliseconds))
                    {
                        try {
                            _logger.Debug("Trying to set up session");
                            session = SetUpSession(serverInfoService, sessionDAO, Settings.Default.armaServerAddress,
                                                   Settings.Default.armaServerPort, ref inGame);
                        } catch (MySqlException e) {
                            _logger.Error("Problem setting up session: ", e);
                            BuildDAOsIfNeeded(ref connection, ref playerDAO, ref missionDAO, ref sessionDAO);
                        }
                        Thread.Sleep(Settings.Default.pollRate);
                    }

                    while (CheckMissionThreshold(missionCount, inGame) && CheckTimeThreshold(runTime.ElapsedMilliseconds))
                    {
                        try {
                            _logger.Debug("Trying to update session details");
                            ServerInfo serverInfo = serverInfoService.GetServerInfo(Settings.Default.armaServerAddress, Settings.Default.armaServerPort);
                            inGame = CheckServerRunningState(serverInfo.ServerState);

                            if (inGame)
                            {
                                if (session.MaxPing < serverInfo.Ping)
                                {
                                    session.MaxPing = serverInfo.Ping;
                                }
                                else if (session.MinPing > serverInfo.Ping)
                                {
                                    session.MinPing = serverInfo.Ping;
                                }

                                if (session.MaxPlayers < serverInfo.Players.Count)
                                {
                                    session.MaxPlayers = serverInfo.Players.Count;
                                }

                                MissionSession missionSession = missionDAO.GetOrCreateMissionSession(serverInfo.MapName, serverInfo.Mission, session);
                                if (currentMissionSession != null && currentMissionSession.Id != missionSession.Id)   // handle case where missions changed between polls
                                {
                                    sessionDAO.UpdateSession(session);
                                    missionDAO.UpdateMissionSession(currentMissionSession);
                                    playerDAO.UpdatePlayerMissionSessions(currentPlayersToMissionSession);

                                    currentPlayersToMissionSession.Clear();
                                }

                                missionSession.Length += (Settings.Default.pollRate / 1000);
                                if (missionSession.Played == false && CheckPlayedThreshold(missionSession.Length))
                                {
                                    missionSession.Played = true;
                                    missionCount++;
                                }

                                currentMissionSession = missionSession;

                                ISet <PlayerMissionSession> playersToMissionSession = playerDAO.GetOrCreatePlayerMissionSessions(serverInfo.Players, missionSession);

                                foreach (PlayerMissionSession playerToMissionSession in playersToMissionSession)
                                {
                                    playerToMissionSession.Length += (Settings.Default.pollRate / 1000);
                                    if (playerToMissionSession.Played == false && CheckPlayedThreshold(playerToMissionSession.Length))
                                    {
                                        playerToMissionSession.Played = true;
                                    }
                                }

                                currentPlayersToMissionSession.UnionWith(playersToMissionSession);
                            }
                            else if (currentMissionSession != null)
                            {
                                sessionDAO.UpdateSession(session);
                                missionDAO.UpdateMissionSession(currentMissionSession);
                                playerDAO.UpdatePlayerMissionSessions(currentPlayersToMissionSession);

                                currentMissionSession = null;
                                currentPlayersToMissionSession.Clear();
                            }
                        } catch (MySqlException e) {
                            _logger.Error("Problem updating session details: ", e);
                            BuildDAOsIfNeeded(ref connection, ref playerDAO, ref missionDAO, ref sessionDAO);
                        }
                        Thread.Sleep(Settings.Default.pollRate);
                    }
                } catch (NoServerInfoException nsie) {
                    _logger.Error("Error reporting", nsie);
                }
            }
            finally {
                // Ensure disposable objects are disposed
                if (playerDAO != null)
                {
                    playerDAO.Dispose();
                }
                if (missionDAO != null)
                {
                    missionDAO.Dispose();
                }
                if (sessionDAO != null)
                {
                    sessionDAO.Dispose();
                }
                if (connection != null)
                {
                    connection.Dispose();
                }
            }
        }