示例#1
0
        private void HandleLobbySignal(JObject signalData)
        {
            var signalName = (string)signalData.GetValue("signal");

            // start gry
            if (signalName == PlayerClickedGameStartSerializer.SIGNAL_PLAYER_READY)
            {
                var serializer = new PlayerClickedGameStartSerializer(signalData);
                serializer.Validate();

                Game.Start();
                SendGetHandRequest = true;
            }
            else if (signalName == LobbySignalNewBidSerializer.SIGNAL_NEW_BID)
            {
                var serializer = new LobbySignalNewBidSerializer(signalData);
                serializer.Validate();

                try {
                    var contract = new Contract(
                        (ContractHeight)serializer.Height,
                        (ContractColor)serializer.Color,
                        (PlayerTag)serializer.PlaceTag,
                        serializer.X,
                        serializer.XX
                        );
                    Game.AddBid(contract);
                } catch (WrongBidException) {
                    // TODO: Pobrać całą licytację jeszcze raz
                }
            }
            // Użytkownik usiadł na wybranym miejscu
            else if (signalName == LobbySignalUserSatSerializer.SIGNAL_USER_SAT)
            {
                var serializer = new LobbySignalUserSatSerializer(signalData);
                serializer.Validate();

                if (serializer.Username != Username)
                {
                    Game.AddPlayer(new Player((PlayerTag)serializer.PlaceTag, serializer.Username));
                }
            }
            else if (signalName == LobbySignalUserSittedOutSerializer.SIGNAL_USER_SITTED_OUT)
            {
                var serializer = new LobbySignalUserSittedOutSerializer(signalData);
                serializer.Validate();

                var player = Game.GetPlayerAt((PlayerTag)serializer.PlaceTag);
                Game.RemovePlayer(player);
            }
            else if (signalName == PutCardSignalSerializer.SIGNAL_USER_PUT_CARD)
            {
                var serializer = new PutCardSignalSerializer(signalData);
                serializer.Validate();

                if (serializer.Username != Username)
                {
                    Game.CurrentGame.NextCard(new Card((CardFigure)serializer.CardFigure, (CardColor)serializer.CardColor, (PlayerTag)serializer.OwnerPosition));
                }
            }
            // Następna licytacja
            else if (signalName == LobbySignalGameStartedNextBiddingSerializer.SIGNAL_GAME_STARTED_NEXT_BIDDING)
            {
                var serializer = new LobbySignalGameStartedNextBiddingSerializer(signalData);

                Game.ClearPlayerHands();
                MyCards.Clear();
                if (GrandpaCards != null)
                {
                    GrandpaCards.Clear();
                }
                GrandpaCards       = null;
                SendGetHandRequest = true;

                if (Game.GameState != GameState.BIDDING)
                {
                    Game.GameState = GameState.BIDDING;
                    Game.StartBidding();
                }
            }
            else if (signalName == LobbySignalGameFinishedSerializer.SIGNAL_GAME_FINISHED)
            {
                var serializer = new LobbySignalGameFinishedSerializer(signalData);

                Game.ClearPlayerHands();
                if (MyCards != null)
                {
                    MyCards.Clear();
                }
                if (GrandpaCards != null)
                {
                    GrandpaCards.Clear();
                }
                GrandpaCards = null;

                Game.GameState = GameState.GAME_FINISHED;
            }
        }
示例#2
0
        protected override BaseSerializer PerformAction(ClientConnection conn, BaseSerializer requestData)
        {
            RequestSerializer  data = (RequestSerializer)requestData;
            ResponseSerializer resp = (ResponseSerializer)InitializeResponseSerializer();

            resp.Status = "OK";
            resp.Color  = data.Color;
            resp.Height = data.Height;
            resp.X      = data.X;
            resp.XX     = data.XX;

            Lobby lobby = (Lobby)conn.Session.Get("joined-lobby");
            Match game  = lobby.Game;

            if (lobby.LobbyState != LobbyState.IN_GAME)
            {
                data.AddError(null, "INVALID_LOBBY_STATE", "Nie można licytować w tym stanie lobby");
                data.ThrowException();
            }

            string username = (string)conn.Session.Get("username");

            int playerIndex = game.PlayerList.FindIndex(x => { return(username == x.Name); });

            // gdy nie ma takiego gracza przy stole => wywal wyjątek
            if (playerIndex == -1)
            {
                data.AddError("PlayerUsername", "INVALID_PLAYER", "Nie ma takiego gracza");
                data.ThrowException();
            }

            var player   = game.PlayerList[playerIndex];
            var contract = new Contract((ContractHeight)data.Height, (ContractColor)data.Color, player.Tag, data.X, data.XX);

            if (!game.CheckAddBid(contract))
            {
                data.AddError(null, "INVALID_CONTRACT", "Nie można zalicytować kontraktu");
                data.ThrowException();
            }
            game.AddBid(contract);

            Console.WriteLine(username + "> Bid: " + contract.ToString());

            // Broadcast do pozostałych graczy
            var broadcastData = new LobbySignalNewBidSerializer()
            {
                Signal   = LobbySignalNewBidSerializer.SIGNAL_NEW_BID,
                Username = username,
                PlaceTag = (int)player.Tag,
                Color    = data.Color,
                Height   = data.Height,
                X        = data.X,
                XX       = data.XX
            };
            var broadcastWrapper = new StandardCommunicateSerializer()
            {
                CommunicateType = StandardCommunicateSerializer.TYPE_LOBBY_SIGNAL,
                Data            = broadcastData.GetApiObject()
            };

            lobby.Broadcast(broadcastWrapper.GetApiObject());

            // Broadcast o przepasowaniu całej licytacji, nowe rozdanie
            if (game.CurrentBidding.IsEnd() && game.CurrentBidding.HighestContract.ContractColor == ContractColor.NONE)
            {
                game.GameState = GameState.BIDDING;
                game.StartBidding();

                var bData = new LobbySignalGameStartedNextBiddingSerializer()
                {
                    Signal = LobbySignalGameStartedNextBiddingSerializer.SIGNAL_GAME_STARTED_NEXT_BIDDING,

                    PointsNSAboveLine = game.PointsNS[1],
                    PointsNSBelowLine = game.PointsNS[0],
                    PointsWEAboveLine = game.PointsWE[1],
                    PointsWEBelowLine = game.PointsWE[0],

                    RoundsNS = game.RoundsNS,
                    RoundsWE = game.RoundsWE
                };
                var bWrapper = new StandardCommunicateSerializer()
                {
                    CommunicateType = StandardCommunicateSerializer.TYPE_LOBBY_SIGNAL,
                    Data            = bData.GetApiObject()
                };
                lobby.Broadcast(bWrapper.GetApiObject());
            }

            return(resp);
        }
示例#3
0
        protected override BaseSerializer PerformAction(ClientConnection conn, BaseSerializer requestData)
        {
            RequestSerializer  data = (RequestSerializer)requestData;
            ResponseSerializer resp = (ResponseSerializer)InitializeResponseSerializer();

            resp.Status = "OK";

            Lobby lobby = (Lobby)conn.Session.Get("joined-lobby");
            Match game  = lobby.Game;

            if (lobby.LobbyState != LobbyState.IN_GAME)
            {
                data.AddError(null, "INVALID_LOBBY_STATE", "Nie można wyłożyć karty w tym stanie lobby");
                data.ThrowException();
            }

            if (game.GameState != GameState.PLAYING)
            {
                data.AddError(null, "INVALID_GAME_STATE", "Gra nie jest w poprawnym stanie do tej akcji");
                data.ThrowException();
            }

            var username = conn.Session.Get <string>("username");

            Console.WriteLine(username + "> Card attempt: " + data.CardOwnerPosition + " " + data.Figure + " " + ((CardColor)data.Color).ToString());
            if (!conn.Session.Has("player"))
            {
                data.AddError(null, "INVALID_USER", "Podany użytkownik nie jest uczestnikiem rozgrywki");
                data.ThrowException();
            }
            var player    = conn.Session.Get <Player>("player");
            var cardOwner = game.GetPlayerAt((PlayerTag)data.CardOwnerPosition);

            // Inny gracz niz my (gracz musi byc dziadkiem)
            if (player != game.GetPlayerAt((PlayerTag)data.CardOwnerPosition))
            {
                if (player.Tag != game.CurrentBidding.Declarer)
                {
                    data.AddError(null, "INVALID_HAND", "Nie można wyłożyć karty z ręki tego gracza");
                    data.ThrowException();
                }

                if (((int)player.Tag + 2) % 4 == (int)game.CurrentBidding.Declarer)
                {
                    // Próbujemy wyłożyć kartę kogoś innego niż dziadka
                    data.AddError(null, "INVALID_HAND", "Nie można wyłożyć karty jako dziadek");
                    data.ThrowException();
                }
            }
            else
            {
                if (((int)player.Tag + 2) % 4 == (int)game.CurrentBidding.Declarer)
                {
                    // Jesteśmy dziadkiem
                    data.AddError(null, "INVALID_HAND", "Nie można wyłożyć karty jako dziadek");
                    data.ThrowException();
                }
            }

            // czy gracz ma taką kartę na ręce
            bool cardFound = false;
            Card card      = null;

            for (int i = 0; i < cardOwner.Hand.Length; i++)
            {
                if (cardOwner.Hand[i].Color == (CardColor)data.Color && cardOwner.Hand[i].Figure == (CardFigure)data.Figure)
                {
                    cardFound = true;
                    card      = cardOwner.Hand[i];
                    break;
                }
            }

            // gracz nie ma takiej karty na ręce
            if (!cardFound)
            {
                data.AddError(null, "INVALID_CARD", "Gracz nie posiada takiej karty na ręce");
                data.ThrowException();
            }

            bool canPlayerPutCard = game.CheckNextCard(cardOwner.Tag, card.Color, card.Figure);

            // gracz nie może wyłożyć tej karty
            if (!canPlayerPutCard)
            {
                data.AddError(null, "INVALID_CARD", "Nie można wyłożyć tej karty");
                data.ThrowException();
            }

            game.NextCard((PlayerTag)data.CardOwnerPosition, card.Color, card.Figure);

            Console.WriteLine(username + "> Card: " + data.CardOwnerPosition + " " + (int)card.Figure + " " + card.Color.ToString());

            var broadcastData = new PutCardSignalSerializer()
            {
                Signal        = PutCardSignalSerializer.SIGNAL_USER_PUT_CARD,
                Username      = username,
                OwnerPosition = data.CardOwnerPosition,
                CardFigure    = data.Figure,
                CardColor     = data.Color
            };
            var broadcastWrapper = new StandardCommunicateSerializer()
            {
                CommunicateType = StandardCommunicateSerializer.TYPE_LOBBY_SIGNAL,
                Data            = broadcastData.GetApiObject()
            };

            lobby.Broadcast(broadcastWrapper.GetApiObject());

            if (game.GameState == GameState.BIDDING)
            {
                var bData = new LobbySignalGameStartedNextBiddingSerializer()
                {
                    Signal = LobbySignalGameStartedNextBiddingSerializer.SIGNAL_GAME_STARTED_NEXT_BIDDING,

                    PointsNSAboveLine = game.PointsNS[1],
                    PointsNSBelowLine = game.PointsNS[0],
                    PointsWEAboveLine = game.PointsWE[1],
                    PointsWEBelowLine = game.PointsWE[0],

                    RoundsNS = game.RoundsNS,
                    RoundsWE = game.RoundsWE
                };
                var bWrapper = new StandardCommunicateSerializer()
                {
                    CommunicateType = StandardCommunicateSerializer.TYPE_LOBBY_SIGNAL,
                    Data            = bData.GetApiObject()
                };
                lobby.Broadcast(bWrapper.GetApiObject());
            }
            else if (game.GameState == GameState.GAME_FINISHED)
            {
                var bData = new LobbySignalGameFinishedSerializer()
                {
                    Signal   = LobbySignalGameFinishedSerializer.SIGNAL_GAME_FINISHED,
                    Winner   = (game.RoundsNS == 2) ? (short)0 : (short)1,
                    PointsNS = game.PointsNS[0] + game.PointsNS[1],
                    PointsWE = game.PointsWE[0] + game.PointsWE[1],
                    RoundsNS = game.RoundsNS,
                    RoundsWE = game.RoundsWE
                };
                var bWrapper = new StandardCommunicateSerializer()
                {
                    CommunicateType = StandardCommunicateSerializer.TYPE_LOBBY_SIGNAL,
                    Data            = bData.GetApiObject()
                };
                lobby.Broadcast(bWrapper.GetApiObject());
            }

            resp.CardFigure    = (int)card.Figure;
            resp.CardColor     = (int)card.Color;
            resp.OwnerPosition = (int)data.CardOwnerPosition;
            return(resp);
        }