Пример #1
0
        /// <summary>
        /// Потеря соединения
        /// </summary>
        /// <param name="c"></param>
        private void ConnectionLostHandler(SBRemoteClient c)
        {
            if (c.Name != null)
            {
                this.SendToAll(new MsgChatMessage()
                {
                    Text = c.Name + " соединение потеряно"
                });

                if (c.State != KnownClientState.InGame)
                {
                    this.SendToAll(new MsgRemoveKnownClient()
                    {
                        Id = c.Id
                    });

                    using (_knownClients.Write())
                    {
                        _knownClients.Object.Remove(c.Id);
                    }
                }
            }
            else
            {
                using (_unnamedClients.Write())
                {
                    _unnamedClients.Object.Remove(c);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Начало игры
        /// </summary>
        /// <param name="c"></param>
        /// <param name="opponentId"></param>
        /// <param name="password"></param>
        private void BeginGameHandler(SBRemoteClient c, Guid opponentId, string password)
        {
            SBRemoteClient opponent;

            if (c.State == KnownClientState.Free || c.State == KnownClientState.Ready)
            {
                using (_knownClients.Read())
                {
                    if (!_knownClients.Object.TryGetValue(opponentId, out opponent))
                    {
                        opponent = null;
                    }
                }

                if (opponent != null)
                {
                    if (opponent.Password == password)
                    {
                        c.SetState(KnownClientState.InGame);
                        opponent.SetState(KnownClientState.InGame);

                        var g = new SBGame(c, opponent);
                        g.OnScoreChanged += gc => GameScoreChangedHalder(gc);
                        g.OnGameFinished += () => GameFinishedHandler(g);

                        using (_games.Write())
                        {
                            _games.Object.Add(g.Id, g);
                        }

                        this.SendToAll(new MsgNewGame()
                        {
                            Id      = g.Id,
                            PlayerA = g.PlayerA,
                            PlayerB = g.PlayerB
                        });
                    }
                    else
                    {
                        c.Send(new MsgChatMessage()
                        {
                            Text = "Неверный пароль!"
                        });
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Создание нового соединения
        /// </summary>
        /// <param name="cnn"></param>
        private void NewConnectionHandler(Connection cnn)
        {
            var c = new SBRemoteClient(cnn);

            c.OnRegisterName   += name => RegisterNameHandler(c);
            c.OnRestoreSession += id => RestoreSessionHandler(id, cnn);
            c.OnChatMessage    += msg => ChatMessageHandler(c, msg);
            c.OnStateChanged   += st => StateChangedHandler(c, st);
            c.OnBeginGame      += (opponentId, pwd) => BeginGameHandler(c, opponentId, pwd);
            c.OnConnectionLost += () => ConnectionLostHandler(c);
            c.Start();

            using (_unnamedClients.Write())
            {
                _unnamedClients.Object.AddLast(c);
            }

            OnNewConnection(c, cnn);
        }