Пример #1
0
        /// <summary>
        /// takes messages from the shared queue, converts them to C# classes, and sends them to up
        /// берет сообщения из общей очереди, конвертирует их в классы C# и отправляет на верх
        /// </summary>
        public void Converter()
        {
            while (true)
            {
                try
                {
                    if (_isDisposed)
                    {
                        return;
                    }

                    if (!_newMessage.IsEmpty)
                    {
                        string data;

                        if (_newMessage.TryDequeue(out data))
                        {
                            if (data.StartsWith("<pits>") || data.StartsWith("<sec_info"))
                            {
                                continue;
                            }

                            if (data.StartsWith("<server_status"))
                            {
                                ServerStatus status = Deserialize <ServerStatus>(data);

                                if (status.Connected == "true")
                                {
                                    IsConnected = true;
                                    Connected?.Invoke();
                                }
                                else if (status.Connected == "false")
                                {
                                    IsConnected = false;
                                    Disconnected?.Invoke();
                                }
                                else if (status.Connected == "error")
                                {
                                    SendLogMessage(status.Text, LogMessageType.Error);
                                }
                            }
                            else if (data.StartsWith("<securities>"))
                            {
                                var securities = _deserializer.Deserialize <List <Security> >(new RestResponse()
                                {
                                    Content = data
                                });

                                UpdatePairs?.Invoke(securities);
                            }
                            else if (data.StartsWith("<united_portfolio"))
                            {
                                UnitedPortfolio unitedPortfolio = Deserialize <UnitedPortfolio>(data);

                                UpdatePortfolio?.Invoke(unitedPortfolio);
                            }
                            else if (data.StartsWith("<positions"))
                            {
                                var positions = Deserialize <TransaqPositions>(data);

                                UpdatePositions?.Invoke(positions);
                            }
                            else if (data.StartsWith("<clientlimits"))
                            {
                                var limits = Deserialize <Clientlimits>(data);

                                UpdateMonoPortfolio?.Invoke(limits);
                            }
                            else if (data.StartsWith("<client"))
                            {
                                var clientInfo = _deserializer.Deserialize <Client>(new RestResponse()
                                {
                                    Content = data
                                });

                                ClientsInfo?.Invoke(clientInfo);
                            }
                            else if (data.StartsWith("<alltrades>"))
                            {
                                var allTrades = _deserializer.Deserialize <List <Trade> >(new RestResponse()
                                {
                                    Content = data
                                });

                                NewTradesEvent?.Invoke(allTrades);
                            }
                            else if (data.StartsWith("<quotes>"))
                            {
                                var quotes = _deserializer.Deserialize <List <Quote> >(new RestResponse()
                                {
                                    Content = data
                                });

                                UpdateMarketDepth?.Invoke(quotes);
                            }
                            else if (data.StartsWith("<orders>"))
                            {
                                var orders = _deserializer.Deserialize <List <Order> >(new RestResponse()
                                {
                                    Content = data
                                });

                                MyOrderEvent?.Invoke(orders);
                            }
                            else if (data.StartsWith("<trades>"))
                            {
                                var myTrades = _deserializer.Deserialize <List <Trade> >(new RestResponse()
                                {
                                    Content = data
                                });

                                MyTradeEvent?.Invoke(myTrades);
                            }
                            else if (data.StartsWith("<candles"))
                            {
                                Candles newCandles = Deserialize <Candles>(data);

                                NewCandles?.Invoke(newCandles);
                            }
                            else if (data.StartsWith("<messages>"))
                            {
                                if (data.Contains("Время действия Вашего пароля истекло"))
                                {
                                    NeedChangePassword?.Invoke();
                                }
                            }
                        }
                    }
                }

                catch (Exception exception)
                {
                    SendLogMessage(exception.ToString(), LogMessageType.Error);
                }
                Thread.Sleep(1);
            }
        }
Пример #2
0
        private static void UpdateRoom(PongRoom room)
        {
            switch (room.BallState)
            {
            case PongBallState.InMotion:
                room.BallX += room.BallVX;
                room.BallY += room.BallVY;

                if (room.BallY - BallHeight / 2 < 0)
                {       // bounce top
                    room.BallY  = BallHeight / 2;
                    room.BallVY = -room.BallVY;
                }
                if (room.BallY + BallHeight / 2 > RoomHeight)
                {       // bounce bottom
                    room.BallY  = RoomHeight - BallHeight / 2;
                    room.BallVY = -room.BallVY;
                }

                if (room.BallX - BallWidth / 2 < PaddleWidth)
                {       // bounce left
                    double lowerBound = room.Paddle1 - BallHeight / 3;
                    double upperBound = room.Paddle1 + BallHeight * 4 / 3;
                    if (lowerBound < room.BallY && room.BallY < upperBound)
                    {
                        room.BallX = PaddleWidth + BallWidth / 2;

                        room.BallVY = VerticalUpdateHelper(room.BallVY);
                        room.BallVX = -room.BallVX + rnd.NextDouble() / 2;
                        if (room.BallVX < 0)
                        {
                            room.BallVX = 3;
                        }
                    }
                    else
                    {
                        room.BallState = PongBallState.Player1;
                        room.Score2++;
                        room.BallVX = 0;
                        room.BallVY = 0;
                        if (UpdateScore != null)
                        {
                            UpdateScore.Invoke(null, Tuple.Create(
                                                   room.RoomID,
                                                   room.Score1,
                                                   room.Score2));
                        }
                    }
                }
                if (room.BallX + BallWidth / 2 > RoomWidth - PaddleWidth)
                {       // bounce right
                    double lowerBound = room.Paddle1 - BallHeight / 3;
                    double upperBound = room.Paddle1 + BallHeight * 4 / 3;
                    if (lowerBound < room.BallY && room.BallY < upperBound)
                    {
                        room.BallX = RoomWidth - PaddleWidth - BallWidth / 2;

                        room.BallVY = VerticalUpdateHelper(room.BallVY);
                        room.BallVX = -room.BallVX - rnd.NextDouble() / 2;
                        if (room.BallVX > 0)
                        {
                            room.BallVX = -3;
                        }
                    }
                    else
                    {
                        room.BallState = PongBallState.Player2;
                        room.Score1++;
                        room.BallVX = 0;
                        room.BallVY = 0;
                        if (UpdateScore != null)
                        {
                            UpdateScore.Invoke(null, Tuple.Create(
                                                   room.RoomID,
                                                   room.Score1,
                                                   room.Score2));
                        }
                    }
                }
                break;

            case PongBallState.Player1:
                room.BallX = PaddleWidth + BallWidth / 2;
                room.BallY = room.Paddle1 + PaddleHeight / 2;
                break;

            case PongBallState.Player2:
                room.BallX = RoomWidth - PaddleWidth + BallWidth / 2;
                room.BallY = room.Paddle2 + PaddleHeight / 2;
                break;
            }
            if (UpdatePositions != null)
            {
                UpdatePositions.Invoke(null, Tuple.Create(
                                           room.RoomID,
                                           room.Paddle1,
                                           room.Paddle2,
                                           room.BallX,
                                           room.BallY));
            }
        }