Exemple #1
0
 public void RemoveSpectator(ISpectator spectator)
 {
     for (int i = _spectators.Count; i >= 0; i--)
     {
         _spectators.Remove(spectator);
     }
 }
Exemple #2
0
        public void TestResumeGameIsOnGameResumedCalled()
        {
            IServer server = CreateServerWithHost();

            server.StartServer();
            IPlayer    player1    = _factory.CreatePlayer(0, "player1", new CountCallTetriNETCallback());
            IPlayer    player2    = _factory.CreatePlayer(1, "player2", new CountCallTetriNETCallback());
            ISpectator spectator1 = _factory.CreateSpectator(0, "spectator1", new CountCallTetriNETCallback());
            ISpectator spectator2 = _factory.CreateSpectator(1, "spectator2", new CountCallTetriNETCallback());

            _host.PlayerManager.Add(player1);
            _host.PlayerManager.Add(player2);
            _host.SpectatorManager.Add(spectator1);
            _host.SpectatorManager.Add(spectator2);
            server.StartGame();
            server.PauseGame();

            server.ResumeGame();

            Assert.AreEqual(server.State, ServerStates.GameStarted);
            Assert.AreEqual((player1.Callback as CountCallTetriNETCallback).GetCallCount("OnGameResumed"), 1);
            Assert.AreEqual((player2.Callback as CountCallTetriNETCallback).GetCallCount("OnGameResumed"), 1);
            Assert.AreEqual((spectator1.Callback as CountCallTetriNETCallback).GetCallCount("OnGameResumed"), 1);
            Assert.AreEqual((spectator2.Callback as CountCallTetriNETCallback).GetCallCount("OnGameResumed"), 1);
        }
Exemple #3
0
 public void AddSpectator(ISpectator spectator)
 {
     if (!_spectators.Contains(spectator))
     {
         _spectators.Add(spectator);
     }
 }
Exemple #4
0
        public void TestStopGameModificationOnServerAndPlayersAndSpectators()
        {
            IServer server = CreateServerWithHost();

            server.StartServer();
            IPlayer    player1    = _factory.CreatePlayer(0, "player1", new CountCallTetriNETCallback());
            IPlayer    player2    = _factory.CreatePlayer(1, "player2", new CountCallTetriNETCallback());
            ISpectator spectator1 = _factory.CreateSpectator(0, "spectator1", new CountCallTetriNETCallback());
            ISpectator spectator2 = _factory.CreateSpectator(1, "spectator2", new CountCallTetriNETCallback());

            _host.PlayerManager.Add(player1);
            _host.PlayerManager.Add(player2);
            _host.SpectatorManager.Add(spectator1);
            _host.SpectatorManager.Add(spectator2);

            server.StartGame();
            server.StopGame();

            Assert.AreEqual(server.State, ServerStates.WaitingStartGame);
            Assert.IsTrue(_host.PlayerManager.Players.All(x => x.State == PlayerStates.Registered));
            Assert.AreEqual((player1.Callback as CountCallTetriNETCallback).GetCallCount("OnGameFinished"), 1);
            Assert.AreEqual((player2.Callback as CountCallTetriNETCallback).GetCallCount("OnGameFinished"), 1);
            Assert.AreEqual((spectator1.Callback as CountCallTetriNETCallback).GetCallCount("OnGameFinished"), 1);
            Assert.AreEqual((spectator2.Callback as CountCallTetriNETCallback).GetCallCount("OnGameFinished"), 1);
        }
Exemple #5
0
        public void TestResetWinListAreOnPublishServerMessageAndOnWinListModifiedCalled()
        {
            IServer server = CreateServerWithHost();

            server.StartServer();
            CountCallTetriNETCallback playerCallback1 = new CountCallTetriNETCallback();
            CountCallTetriNETCallback playerCallback2 = new CountCallTetriNETCallback();
            IPlayer player1 = _factory.CreatePlayer(0, "player1", playerCallback1);
            IPlayer player2 = _factory.CreatePlayer(1, "player2", playerCallback2);
            CountCallTetriNETCallback spectatorCallback1 = new CountCallTetriNETCallback();
            CountCallTetriNETCallback spectatorCallback2 = new CountCallTetriNETCallback();
            ISpectator spectator1 = _factory.CreateSpectator(0, "spectator1", spectatorCallback1);
            ISpectator spectator2 = _factory.CreateSpectator(1, "spectator2", spectatorCallback2);

            _host.PlayerManager.Add(player1);
            _host.PlayerManager.Add(player2);
            _host.SpectatorManager.Add(spectator1);
            _host.SpectatorManager.Add(spectator2);
            server.StartGame();
            _host.GameLost(player1.Callback); // player has lost
            _actionQueue.Process();           // --> stop game because only one alive player left --> gene

            server.ResetWinList();

            Assert.AreEqual(server.WinList.Count, 0);
            Assert.AreEqual(playerCallback1.GetCallCount("OnPublishServerMessage"), 1);
            Assert.AreEqual(playerCallback2.GetCallCount("OnPublishServerMessage"), 1);
            Assert.AreEqual(playerCallback1.GetCallCount("OnWinListModified"), 2);
            Assert.AreEqual(playerCallback2.GetCallCount("OnWinListModified"), 2);
        }
Exemple #6
0
 public GameManager(AppSettings settings, IReplayContextSetter contextSetter, ISpectator spectator, IGameController gameController, IObsController obsController)
 {
     this.settings       = settings ?? throw new ArgumentNullException(nameof(settings));
     this.contextSetter  = contextSetter ?? throw new ArgumentNullException(nameof(contextSetter));
     this.spectator      = spectator ?? throw new ArgumentNullException(nameof(spectator));
     this.gameController = gameController ?? throw new ArgumentNullException(nameof(gameController));
     this.obsController  = obsController ?? throw new ArgumentNullException(nameof(obsController));
 }
Exemple #7
0
        private void OnPublishSpectatorMessage(ISpectator spectator, string msg)
        {
            Log.Default.WriteLine(LogLevels.Info, "PublishSpectatorMessage:{0}:{1}", spectator.Name, msg);

            // Send message to players and spectators
            foreach (IEntity entity in Entities)
            {
                entity.OnPublishPlayerMessage(spectator.Name, msg);
            }
        }
 public bool Remove(ISpectator spectator)
 {
     for (int i = 0; i < MaxSpectators; i++)
     {
         if (_spectators[i] == spectator)
         {
             _spectators[i] = null;
             return(true);
         }
     }
     return(false);
 }
Exemple #9
0
 private static async void RunMatch(ISpectator spectator)
 {
     var ai0 = new Delayed(new SimpleAi.SimpleAi("A", "0"));
     var ai1 = new Delayed(new SimpleAi.SimpleAi("B", "0"));
     var ai2 = new Delayed(new SimpleAi.SimpleAi("C", "0"));
     var ai3 = new Delayed(new SimpleAi.SimpleAi("D", "0"));
     //var ai0 = new Delayed(new KanAi());
     //var ai1 = new Delayed(new KanAi());
     //var ai2 = new Delayed(new KanAi());
     //var ai3 = new Delayed(new KanAi());
     await Match.Start(ai0, ai1, ai2, ai3, spectator);
 }
Exemple #10
0
 private static void ResolveDependencies()
 {
     _leagueInfo       = Container.Resolve <IGetLeagueInfo>();
     _connection       = Container.Resolve <IConnection>();
     _summoner         = Container.Resolve <IGetSummoner>();
     _masters          = Container.Resolve <IGetMasters>();
     _leagueOfSummoner = Container.Resolve <IGetLeagueOfSummonerInformation>();
     _serverService    = Container.Resolve <IServerService>();
     _summonerGame     = Container.Resolve <IGetSummonerGame>();
     _spectator        = Container.Resolve <ISpectator>();
     _lastGames        = Container.Resolve <IGetLastGames>();
     _printMethods     = Container.Resolve <IPrintMethods>();
     _dateHandler      = Container.Resolve <IDateHandler>();
 }
Exemple #11
0
        public void TestHostSpectatorLeftCalledOnDisconnection()
        {
            IHost host  = CreateHost();
            int   count = 0;

            host.HostSpectatorLeft += (spectator1, reason) => count++;
            host.RegisterSpectator(new RaiseExceptionTetriNETCallback(), _clientVersion, "spectator1");

            ISpectator spectator = host.SpectatorManager["spectator1"];

            spectator.OnGamePaused(); // -> raise an exception -> disconnecting spectator

            Assert.AreEqual(count, 1);
        }
Exemple #12
0
        private static async Task Start(Decider decider, Board board, Wall wall, ISpectator spectator)
        {
            State state          = new Start();
            var   spectatorBoard = new VisibleBoard(board);

            while (!state.IsFinal)
            {
                state.Update(board, wall);
                spectator.Updated(spectatorBoard);
                await state.Decide(board, decider);

                state = state.Advance();
            }

            spectator.Updated(spectatorBoard);
        }
Exemple #13
0
        public virtual void HeartbeatSpectator(ITetriNETCallback callback)
        {
            //Log.Default.WriteLine(LogLevels.Debug, "Heartbeat");

            ISpectator spectator = SpectatorManager[callback];

            if (spectator != null)
            {
                //Log.Default.WriteLine("Heartbeat from {0}", player.Name);
                spectator.ResetTimeout(); // player alive
            }
            else
            {
                Log.Default.WriteLine(LogLevels.Warning, "HeartbeatSpectator from unknown spectator");
            }
        }
Exemple #14
0
        public virtual void UnregisterSpectator(ITetriNETCallback callback)
        {
            Log.Default.WriteLine(LogLevels.Debug, "UnregisterPlayer");

            ISpectator spectator = SpectatorManager[callback];

            if (spectator != null)
            {
                //
                spectator.ResetTimeout(); // player alive
                //
                HostSpectatorUnregistered?.Invoke(spectator);
            }
            else
            {
                Log.Default.WriteLine(LogLevels.Warning, "UnregisterSpectator from unknown spectator");
            }
        }
Exemple #15
0
        public bool Add(ISpectator spectator)
        {
            if (_spectators.Count >= MaxSpectators)
            {
                Log.Default.WriteLine(LogLevels.Warning, "Too many spectators");
                return(false);
            }

            if (_spectators.ContainsKey(spectator.Callback))
            {
                Log.Default.WriteLine(LogLevels.Warning, "{0} already registered", spectator.Name);
                return(false);
            }
            //
            _spectators.Add(spectator.Callback, spectator);
            //
            return(true);
        }
Exemple #16
0
        public virtual void PublishSpectatorMessage(ITetriNETCallback callback, string msg)
        {
            Log.Default.WriteLine(LogLevels.Debug, "PublishMessage {0}", msg);

            ISpectator spectator = SpectatorManager[callback];

            if (spectator != null)
            {
                //
                spectator.ResetTimeout(); // player alive
                //
                HostSpectatorMessagePublished?.Invoke(spectator, msg);
            }
            else
            {
                Log.Default.WriteLine(LogLevels.Warning, "OnSpectatorMessagePublished from unknown spectator");
            }
        }
Exemple #17
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);
        }
        public TenhouClient(IPlayer player, ISpectator spectator)
        {
            _player    = player;
            _spectator = spectator;
            _lobby     = _player.Lobby;
            _playerId  = _player.Id;

            var folderName = MakeFolderName(_playerId);

            _directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TenhouAi", folderName);
            if (!Directory.Exists(_directory))
            {
                Directory.CreateDirectory(_directory);
            }

            _sessionLogFileName = Path.Combine(_directory, Path.GetRandomFileName() + ".txt");

            _wall         = new FakeWall();
            _board        = new Board(_wall);
            _visibleBoard = new VisibleBoard(_board);
        }
        public bool Add(ISpectator spectator)
        {
            bool alreadyExists = _spectators.Any(x => x != null && (x == spectator || x.Name == spectator.Name));

            if (!alreadyExists)
            {
                // insert in first empty slot
                for (int i = 0; i < MaxSpectators; i++)
                {
                    if (_spectators[i] == null)
                    {
                        _spectators[i] = spectator;
                        return(true);
                    }
                }
            }
            else
            {
                Log.Default.WriteLine(LogLevels.Warning, "{0} already registered", spectator.Name);
            }
            return(false);
        }
Exemple #20
0
        private void OnSpectatorLeft(ISpectator spectator, LeaveReasons reason)
        {
            Log.Default.WriteLine(LogLevels.Info, "Spectator left:{0} {1}", spectator.Name, reason);

            // Remove from spectator
            lock (_spectatorManager.LockObject)
            {
                // Remove spectator from spectator list
                _spectatorManager.Remove(spectator);
            }

            // Clean host tables
            foreach (IHost host in _hosts)
            {
                host.RemoveSpectator(spectator);
            }

            // Inform players and spectators except disconnected spectator
            foreach (IEntity entity in Entities.Where(x => x != spectator))
            {
                entity.OnSpectatorLeft(spectator.Id, spectator.Name, reason);
            }
        }
 public virtual void OnChatMessage(ISpectator spectator, string message)
 {
 }
Exemple #22
0
 void ISessionEventListener.OnChatMessage(ISpectator spectator, string message)
 {
     ConsoleHelper.SessionEvent("Chat: {0}: {1}", spectator.Name, message);
 }
 public virtual void OnSpectatorJoinedSession(ISpectator spectator)
 {
 }
 public virtual void OnSpectatorLeftSession(ISpectator spectator)
 {
 }
Exemple #25
0
 public bool Remove(ISpectator spectator)
 {
     return(_spectators.Remove(spectator.Callback));
 }
 public void OnSpectatorLeftSession(ISpectator spectator)
 {
     foreach(ISessionEventListener listener  in mainListeners.ToArray())
         listener.OnSpectatorLeftSession(spectator);
 }
 private SpectatorNode GetSpectatorNode(ISpectator spectator)
 {
     int spectatorId = spectator.ID;
     try
     {
         return spectatorNodes[spectatorId];
     }
     catch(KeyNotFoundException)
     {
         return spectatorNodes[spectatorId] = new SpectatorNode(spectator);
     }
 }
Exemple #28
0
 public override void OnChatMessage(ISpectator spectator, string message)
 {
     Gdk.Threads.Enter();
     Gtk.TextBuffer buffer = parent.chatView.Buffer;
     Gtk.TextIter ti = buffer.EndIter;
     buffer.InsertWithTagsByName(ref ti, spectator.Name + ": ", "spectator");
     buffer.Insert(ref ti, message + "\n");
     parent.NotifyUrgent();
     Gdk.Threads.Leave();
 }
Exemple #29
0
 void ISessionEventListener.OnChatMessage(ISpectator spectator, string message)
 {
 }
Exemple #30
0
 public override void RemoveSpectator(ISpectator spectator)
 {
     // NOP
 }
Exemple #31
0
        private void OnUnregisterSpectator(ISpectator spectator)
        {
            Log.Default.WriteLine(LogLevels.Info, "Unregister spectator:{0}", spectator.Name);

            OnSpectatorLeft(spectator, LeaveReasons.Disconnected);
        }
Exemple #32
0
 void ISessionEventListener.OnSpectatorLeftSession(ISpectator spectator)
 {
 }
Exemple #33
0
 void ISessionEventListener.OnSpectatorJoinedSession(ISpectator spectator)
 {
 }
Exemple #34
0
 void ISessionEventListener.OnSpectatorJoinedSession(ISpectator spectator)
 {
     ConsoleHelper.SessionEvent("Spectator #{0} '{1}' joined the session.", spectator.ID, spectator.Name);
 }
 private void RemoveSpectatorNode(ISpectator spectator)
 {
     int spectatorId = spectator.ID;
     spectatorNodes.Remove(spectatorId);
 }
Exemple #36
0
 void ISessionEventListener.OnSpectatorLeftSession(ISpectator spectator)
 {
     ConsoleHelper.SessionEvent("Spectator #{0} '{1}' left the session.", spectator.ID, spectator.Name);
 }
 public override void OnSpectatorLeftSession(ISession session, ISpectator spectator)
 {
     if(session == parent.session)
     {
         Gdk.Threads.Enter();
         SpectatorNode node = parent.GetSpectatorNode(spectator);
         parent.spectatorStore.RemoveNode(node);
         parent.RemoveSpectatorNode(spectator);
         node.Dispose();
         Gdk.Threads.Leave();
     }
 }
Exemple #38
0
 public abstract void RemoveSpectator(ISpectator spectator);
 public SpectatorNode(ISpectator spectator)
 {
     this.spectator = spectator;
     Update();
 }
Exemple #40
0
        public virtual void RegisterSpectator(ITetriNETCallback callback, Versioning clientVersion, string spectatorName)
        {
            Log.Default.WriteLine(LogLevels.Debug, "RegisterSpectator {0}", spectatorName);

            // TODO: check ban list

            RegistrationResults result    = RegistrationResults.RegistrationSuccessful;
            ISpectator          spectator = null;
            int  id    = -1;
            bool added = false;

            // TODO: smarter compatibility check
            if (clientVersion == null ||
                (_serverVersion.Major != clientVersion.Major && _serverVersion.Minor != clientVersion.Minor))
            {
                result = RegistrationResults.IncompatibleVersion;
            }
            else
            {
                lock (SpectatorManager.LockObject)
                {
                    if (String.IsNullOrEmpty(spectatorName) || spectatorName.Length > 20)
                    {
                        result = RegistrationResults.RegistrationFailedInvalidName;
                        Log.Default.WriteLine(LogLevels.Warning, "Cannot register {0} because name is invalid", spectatorName);
                    }
                    else if (SpectatorManager[spectatorName] != null || PlayerManager[spectatorName] != null)
                    {
                        result = RegistrationResults.RegistrationFailedPlayerAlreadyExists;
                        Log.Default.WriteLine(LogLevels.Warning, "Cannot register {0} because it already exists", spectatorName);
                    }
                    else if (SpectatorManager.SpectatorCount >= SpectatorManager.MaxSpectators)
                    {
                        result = RegistrationResults.RegistrationFailedTooManyPlayers;
                        Log.Default.WriteLine(LogLevels.Warning, "Cannot register {0} because too many spectators are already connected", spectatorName);
                    }
                    else
                    {
                        id = SpectatorManager.FirstAvailableId;
                        if (id != -1)
                        {
                            spectator = Factory.CreateSpectator(id, spectatorName, callback);
                            //
                            spectator.ConnectionLost += OnEntityConnectionLost;
                            //
                            added = SpectatorManager.Add(spectator);
                        }
                    }
                }
            }
            if (added && id != -1 && result == RegistrationResults.RegistrationSuccessful)
            {
                //
                spectator.ResetTimeout(); // spectator alive
                //
                HostSpectatorRegistered?.Invoke(spectator, id);
            }
            else
            {
                Log.Default.WriteLine(LogLevels.Info, "Register failed for spectator {0}", spectatorName);
                //
                callback.OnSpectatorRegistered(result, _serverVersion, -1, false, null);
            }
        }
Exemple #41
0
        public static async Task Start(IPlayer p0, IPlayer p1, IPlayer p2, IPlayer p3, ISpectator spectator)
        {
            var wall    = new Wall();
            var board   = new Board(wall);
            var decider = new Decider(board, new[] { p0, p1, p2, p3 });

            await Start(decider, board, wall, spectator);
        }
Exemple #42
0
 void IServerEventListener.OnSpectatorLeftSession(ISession session, ISpectator spectator)
 {
     ConsoleHelper.ServerEvent("Spectator #{0} '{1}' left session #{2}.", spectator.ID, spectator.Name, session.ID);
 }
 public void OnChatMessage(ISpectator spectator, string message)
 {
     foreach(ISessionEventListener listener  in mainListeners.ToArray())
         listener.OnChatMessage(spectator, message);
 }