Beispiel #1
0
        /// <summary>
        /// Handles SevrerPacketId.YouAreAlreadyInRoom
        /// </summary>
        private static void Packet_YouAreAlreadyInRoom(byte[] data)
        {
            //Invoke callback on observers
            FoolObservable.OnYouAreAlreadyInRoom();

            Debug.Log("got Packet_YouAreAlreadyInRoom");
        }
Beispiel #2
0
        private static void Packet_NextTurn(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //Skip packetId
            buffer.ReadLong();

            //Read player id
            long firstPlayerId = buffer.ReadLong();
            //Read player slotN
            int slotN = buffer.ReadInteger();
            //Read def player id
            long defendingPlayerId = buffer.ReadLong();
            //Read def player slotN
            int defSlotN = buffer.ReadInteger();
            //Read turn number
            int turnN = buffer.ReadInteger();

            Debug.Log($"Player {firstPlayerId} (slot {slotN}) does turn. Defender: Player {defendingPlayerId} (slot {defendingPlayerId})");

            //Invoke callback on observers
            FoolObservable.OnNextTurn(firstPlayerId, slotN, defendingPlayerId, defSlotN, turnN);
        }
Beispiel #3
0
        private static void Packet_UpdateUserData(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //skip read pakcet id
            buffer.ReadLong();

            long connectionId = buffer.ReadLong();

            long UserId = buffer.ReadLong();

            string Nickname = buffer.ReadStringUnicode();

            double money = buffer.ReadDouble();

            // if avatar string is empty then skip. Otherwise read
            string avatarPath = "";

            if (buffer.Length() > 0)
            {
                int strLen = buffer.ReadInteger(false);
                if (strLen > 0)
                {
                    avatarPath = buffer.ReadString();
                }
            }

            //Invoke callback on observers
            FoolObservable.OnUpdateUserData(connectionId, UserId, Nickname, money, avatarPath);
        }
Beispiel #4
0
        /// <summary>
        /// Handles SevrerPacketId.FaliToJoinFullRoom
        /// </summary>
        private static void Packet_FaliToJoinFullRoom(byte[] data)
        {
            //Invoke callback on observers
            FoolObservable.OnFailedToJoinFullRoom();

            Debug.Log("Failed to join room: it's full");
        }
Beispiel #5
0
        private static void Packet_EndGameFool(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //Skip packetId
            buffer.ReadLong();

            //Read player who fool
            long foolPlayerId = buffer.ReadLong();

            //Read rewards count
            int rewardsN = buffer.ReadInteger();
            //rewards
            Dictionary <long, double> rewards = new Dictionary <long, double>(rewardsN);

            for (int i = 0; i < rewardsN; i++)
            {
                long   player = buffer.ReadLong();
                double reward = buffer.ReadDouble();
                rewards.Add(player, reward);
            }

            //Invoke callback on observers
            FoolObservable.OnEndGameFool(foolPlayerId, rewards);
        }
Beispiel #6
0
 public void DraggedCardDrop(Vector2 mousePos, CardRoot cardRoot)
 {
     //if it was dropped insude table rect
     if (RectTransformUtility.RectangleContainsScreenPoint(TableDropZone, mousePos))
     {
         FoolObservable.OnCardDroppedOnTableByMe(cardRoot);
     }
 }
Beispiel #7
0
        /// <summary>
        /// Handles SevrerPacketId.RoomData
        /// </summary>
        private static void Packet_RoomData(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //Read packet id
            long packetId = buffer.ReadLong();

            //Read maxPlayers
            int maxPlayers = buffer.ReadInteger();

            //Init player array
            StaticRoomData.Players    = new PlayerInRoom[maxPlayers];
            StaticRoomData.MaxPlayers = maxPlayers;

            //Init slots dict
            Dictionary <int, long> slots = new Dictionary <int, long>();

            //Read players count
            int playersCount = buffer.ReadInteger();

            // Read players
            for (int i = 0; i < playersCount; i++)
            {
                //Read player id
                long         playerId = buffer.ReadLong();
                PlayerInRoom player   = new PlayerInRoom(playerId);

                //Read slots number
                int slotN = buffer.ReadInteger();
                player.SlotN = slotN;
                slots.Add(slotN, playerId);

                //if that part of data is about out player then set InRoomSlotNumber to read value
                if (playerId == FoolNetwork.LocalPlayer.ConnectionId)
                {
                    FoolNetwork.LocalPlayer.InRoomSlotNumber = slotN;
                }

                //read player nickname
                player.Nickname = buffer.ReadStringUnicode();

                //read player avatar url
                player.AvatarFile = buffer.ReadString();

                StaticRoomData.Players[i] = player;
            }

            StaticRoomData.ConnectedPlayersCount = playersCount;
            StaticRoomData.OccupiedSlots         = slots;

            //Invoke callback on observers
            FoolObservable.OnRoomDataUpdated();
        }
Beispiel #8
0
 /// <summary>
 /// Called on each frame when i drag card
 /// </summary>
 public void DraggedCardUpdate(Vector2 mousePos, CardRoot cardRoot)
 {
     //if it was dragged insude table rect
     if (RectTransformUtility.RectangleContainsScreenPoint(TableDropZone, mousePos))
     {
         FoolObservable.OnDraggedCardUpdate(mousePos, cardRoot, true);
     }
     else
     {
         FoolObservable.OnDraggedCardUpdate(mousePos, cardRoot, false);
     }
 }
Beispiel #9
0
        private static void OnOpen()
        {
            // Notify ConnectionWatchdog that connection is ok
            ConnectionWatchdog.OnOpen();

            // observable
            FoolObservable.OnConnectedToGameServer();

            IsConnectingToGameServer = false;
            IsConnected = true;

            //authorize with token
            ClientSendPackets.Send_Authorize(myToken);
        }
Beispiel #10
0
        private static void Packet_DropCardOnTableErrorCantDropThisCard(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //Skip packetId
            buffer.ReadLong();

            //read card
            string cardCode = buffer.ReadString();

            //Invoke callback on observers
            FoolObservable.OnDropCardOnTableErrorCantDropThisCard(cardCode);
        }
Beispiel #11
0
        /// <summary>
        /// Handles SevrerPacketId.JoinRoomOk
        /// </summary>
        private static void Packet_JoinRoomOk(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            buffer.ReadLong();

            long roomId = buffer.ReadLong();

            //Invoke callback on observers
            FoolObservable.OnJoinRoom(roomId);

            Debug.Log("You joined room: " + roomId);
        }
Beispiel #12
0
        private static void Packet_ErrorBadAuthToken(byte[] data)
        {
            /*ByteBuffer buffer = new ByteBuffer();
             * buffer.WriteBytes(data);
             *
             * //skip read pakcet id
             * buffer.ReadLong();
             *
             * long connectionId = buffer.ReadLong();*/

            Debug.Log("ErrorBadAuthToken");

            //Invoke callback on observers
            FoolObservable.OnErrorBadAuthToken();
        }
Beispiel #13
0
        private static void Packet_Message(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //Skip packetId
            buffer.ReadLong();

            //Read message
            string message = buffer.ReadStringUnicode();

            //Invoke callback on observers
            FoolObservable.OnMessage(message);
        }
Beispiel #14
0
        /// <summary>
        /// Performs write to server stream
        /// </summary>
        /// <param name="data">data to write to server</param>
        public static void WriteToServer(byte[] data)
        {
            if (IsConnected)// && mySocket != null)
            {
                mySocket.Send(data);
            }
            else
            {
                // observable
                FoolObservable.OnSendError();
                UnityEngine.Debug.LogWarning("Can't send data to server: Not connected.");

                FoolNetwork.Reconnect();
            }
        }
Beispiel #15
0
        private static void Packet_OtherPlayerPassed(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //Skip packetId
            buffer.ReadLong();

            //Read player who passed
            long passedPlayerId = buffer.ReadLong();
            //Read slotN
            int slotN = buffer.ReadInteger();

            //Invoke callback on observers
            FoolObservable.OnOtherPlayerPassed(passedPlayerId, slotN);
        }
Beispiel #16
0
        /// <summary>
        /// Handles SevrerPacketId.Packet_OtherPlayerLeftRoom
        /// </summary>
        private static void Packet_OtherPlayerGotNotReady(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //Skip packetId
            buffer.ReadLong();

            //Read id of player
            long playerId = buffer.ReadLong();

            //Read slotN of player
            int slotN = buffer.ReadInteger();

            //Invoke callback on observers
            FoolObservable.OnOtherPlayerGotNotReady(playerId, slotN);
        }
Beispiel #17
0
        /// <summary>
        /// Handles SevrerPacketId.Packet_TalonData
        /// </summary>
        private static void Packet_TalonData(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //Skip packetId
            buffer.ReadLong();

            //Read talon len
            int talonLength = buffer.ReadInteger();

            //Read trump card
            string trumpCard = buffer.ReadString();

            //Invoke callback on observers
            FoolObservable.OnTalonData(talonLength, trumpCard);
        }
Beispiel #18
0
        private static void Packet_PlayerWon(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //Skip packetId
            buffer.ReadLong();

            //Read player who won
            long wonPlayerId = buffer.ReadLong();

            //read winner's reward
            double reward = buffer.ReadDouble();

            //Invoke callback on observers
            FoolObservable.OnPlayerWon(wonPlayerId, reward);
        }
Beispiel #19
0
        private static void Packet_DefenderPicksCards(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //Skip packetId
            buffer.ReadLong();

            //Read player who picks
            long playerId = buffer.ReadLong();
            //Read his slotN
            int slotN = buffer.ReadInteger();
            //Read cards on table number
            int cardsN = buffer.ReadInteger();

            //Invoke callback on observers
            FoolObservable.OnDefenderPicksCards(playerId, slotN, cardsN);
        }
Beispiel #20
0
        /// <summary>
        /// Handles SevrerPacketId.JoinRoomOk
        /// </summary>
        private static void Packet_RoomList(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //skip packet id
            buffer.ReadLong();

            //read room count
            int roomN = buffer.ReadInteger();

            //read rooms
            RoomInstance[] rooms = new RoomInstance[roomN];
            for (int i = 0; i < roomN; i++)
            {
                rooms[i] = new RoomInstance();

                //read room id
                rooms[i].RoomId = buffer.ReadLong();

                //read max players in room
                rooms[i].MaxPlayers = buffer.ReadInteger();

                //read deck size
                rooms[i].DeckSize = buffer.ReadInteger();

                //read players count
                int playersN = buffer.ReadInteger();
                rooms[i].ConnectedPlayersN = playersN;

                //read player names
                rooms[i].PlayerNames = new string[playersN];
                for (int j = 0; j < playersN; j++)
                {
                    rooms[i].PlayerNames[j] = buffer.ReadStringUnicode(); //todo bug
                }
            }


            //Invoke callback on observers
            FoolObservable.OnRoomList(rooms);
        }
Beispiel #21
0
        private static void Packet_OtherPlayerDropsCardOnTable(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //Skip packetId
            buffer.ReadLong();

            //Read player who drop
            long playerId = buffer.ReadLong();
            //Read his slot
            int slotN = buffer.ReadInteger();
            //read card
            string cardCode = buffer.ReadString();

            //Invoke callback on observers
            FoolObservable.OnOtherPlayerDropsCardOnTable(playerId, slotN, cardCode);
        }
Beispiel #22
0
        /// <summary>
        /// Handles SevrerPacketId.OtherPlayerJoinedRoom
        /// </summary>
        private static void Packet_OtherPlayerJoinedRoom(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //Skip packetId
            buffer.ReadLong();

            //Read id of newly joined player
            long         joinedId     = buffer.ReadLong();
            PlayerInRoom joinedPlayer = new PlayerInRoom(joinedId);

            //Read slotN of newly joined player
            joinedPlayer.SlotN = buffer.ReadInteger();

            //read name
            joinedPlayer.Nickname = buffer.ReadStringUnicode();

            //read avatar
            joinedPlayer.AvatarFile = buffer.ReadString();


            StaticRoomData.ConnectedPlayersCount++;

            // add to room
            if (StaticRoomData.OccupiedSlots.TryGetValue(joinedPlayer.SlotN, out long n))
            {
                StaticRoomData.OccupiedSlots[joinedPlayer.SlotN] = joinedId;
            }
            else // else create new slot values
            {
                StaticRoomData.OccupiedSlots.Add(joinedPlayer.SlotN, joinedId);
            }

            StaticRoomData.Players[joinedPlayer.SlotN] = joinedPlayer;

            //Observable - room data changed
            FoolObservable.OnRoomDataUpdated();

            //Invoke callback on observers
            FoolObservable.OnOtherPlayerJoinedRoom(joinedPlayer);
        }
Beispiel #23
0
        /// <summary>
        /// Handles SevrerPacketId.Packet_EnemyGotCards
        /// </summary>
        private static void Packet_EnemyGotCardsFromTalon(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //Skip packetId
            buffer.ReadLong();

            //Read player id
            long playerId = buffer.ReadLong();
            //Read player cardsN
            int cardsN = buffer.ReadInteger();
            //Read slotN of player
            int slotN = buffer.ReadInteger();

            //Invoke callback on observers
            FoolObservable.OnEnemyGotCardsFromTalon(playerId, slotN, cardsN);
        }
Beispiel #24
0
        private static void Packet_UpdateUserAvatar(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //Skip packetId
            buffer.ReadLong();

            // read holder's id
            long avatarHolder = buffer.ReadLong();

            // read uri of the avatar
            string avatarPath = buffer.ReadString();

            FoolNetwork.LocalPlayer.AvatarFile = avatarPath;

            //Invoke callback on observers
            FoolObservable.UpdateUserAvatar(avatarHolder, avatarPath);
        }
Beispiel #25
0
        /// <summary>
        /// Handles SevrerPacketId.Packet_OtherPlayerLeftRoom
        /// </summary>
        private static void Packet_OtherPlayerLeftRoom(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //Skip packetId
            buffer.ReadLong();

            //Read id of player who left
            long leftPlayerId = buffer.ReadLong();

            //Read slotN of newly joined player
            int slotN = buffer.ReadInteger();

            Debug.Log("Player left: " + leftPlayerId);

            //Invoke callback on observers
            FoolObservable.OnOtherPlayerLeftRoom(leftPlayerId, slotN);
        }
Beispiel #26
0
        public static void Disconnect(string disconnectReason = null)
        {
            Debug.Log("Disconnected. " + disconnectReason);
            if (mySocket != null)
            {
                // close socket if was open
                // (true if Disconnect() called by user's will)
                // (false if connection was suddenly lost)
                if (mySocket.GetState() == WebSocketState.Open)
                {
                    mySocket.Close(WebSocketCloseCode.Normal);
                }

                mySocket    = null;
                IsConnected = false;
                IsConnectingToGameServer = false;

                //Observable
                FoolObservable.OnDisconnectedFromGameServer(disconnectReason); //вызывает исключение
            }
        }
Beispiel #27
0
        /// <summary>
        /// Handles SevrerPacketId.Packet_YouGotCards
        /// </summary>
        private static void Packet_YouGotCardsFromTalon(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //Skip packetId
            buffer.ReadLong();

            //Read cards number
            int cardsN = buffer.ReadInteger();

            //Read cards
            string[] cards = new string[cardsN];
            for (int i = 0; i < cardsN; i++)
            {
                cards[i] = buffer.ReadString();
            }

            //Invoke callback on observers
            FoolObservable.OnYouGotCardsFromTalon(cards);
        }
Beispiel #28
0
        private static void Packet_OtherPlayerCoversCard(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //Skip packetId
            buffer.ReadLong();

            //Read player who covered
            long coveredPlayerId = buffer.ReadLong();
            //Read his slotN
            int slotN = buffer.ReadInteger();

            //read card on table
            string cardOnTableCode = buffer.ReadString();
            //read card dropped
            string cardDroppedCode = buffer.ReadString();

            //Invoke callback on observers
            FoolObservable
            .OnOtherPlayerCoversCard(coveredPlayerId, slotN, cardOnTableCode, cardDroppedCode);
        }
Beispiel #29
0
        ////////////////////////////DATA PACKETS////////////////////////////
        ////////////////////////////DATA PACKETS////////////////////////////
        ////////////////////////////DATA PACKETS////////////////////////////

        private static void Packet_AuthorizedOk(byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteBytes(data);

            //skip read pakcet id
            buffer.ReadLong();

            //Read my conncetion id
            long connectionId = buffer.ReadLong();

            FoolNetwork.LocalPlayer.ConnectionId = connectionId;

            //Read message
            string msg = buffer.ReadString();

            Debug.Log($"Connected. Your connection id = " + connectionId + ". Server says: " + msg);

            //Invoke callback on observers

            FoolNetwork.LocalPlayer.Authorized = true;
            FoolObservable.OnAuthorizedOk(connectionId);
        }
Beispiel #30
0
 private static void Packet_Beaten(byte[] data)
 {
     //Invoke callback on observers
     FoolObservable.OnBeaten();
 }