Example #1
0
        private void OnRegisterSpectator(ISpectator spectator, int spectatorId)
        {
            Log.Default.WriteLine(LogLevels.Info, "New spectator:[{0}]{1}", spectatorId, spectator.Name);

            // Send spectator id back to spectator
            spectator.OnSpectatorRegistered(RegistrationResults.RegistrationSuccessful, Version, spectatorId, State == ServerStates.GameStarted || State == ServerStates.GamePaused, Options);

            // Inform new spectator about players
            foreach (IPlayer p in _playerManager.Players)
            {
                spectator.OnPlayerJoined(p.Id, p.Name, p.Team);
            }
            // Inform new spectator about other spectators
            foreach (ISpectator s in _spectatorManager.Spectators.Where(x => x != spectator))
            {
                spectator.OnSpectatorJoined(s.Id, s.Name);
            }

            // Inform players and other spectators about new spectator connected
            foreach (IEntity entity in Entities.Where(x => x != spectator))
            {
                entity.OnSpectatorJoined(spectatorId, spectator.Name);
            }

            // If game is running, send grid for playing player and game lost for dead player
            if (State == ServerStates.GamePaused || State == ServerStates.GameStarted)
            {
                foreach (IPlayer p in _playerManager.Players)
                {
                    if (p.State == PlayerStates.Playing)
                    {
                        spectator.OnGridModified(p.Id, p.Grid);
                    }
                    else if (p.State == PlayerStates.GameLost)
                    {
                        spectator.OnPlayerLost(p.Id);
                    }
                }
            }

            // Send win list
            spectator.OnWinListModified(_winList);
        }