Exemplo n.º 1
0
    public override void OnRecvPacket(GSPacketIn pkg)
    {
        this.m_lastRecv = pkg.Code;
        this.m_recvCount++;
        // Debug.Log("player ["+this.m_account+"]:"+DateTime.Now+" receive: "+pkg.Code+" (ePackageType."+(ePackageType)pkg.Code+")");

        /*if (this.m_log != null)
         * {
         *  this.m_log.WriteLine(Marshal.ToHexDump(string.Format("player [{0}]:{1} recive:", this.m_account, DateTime.Now), pkg.Buffer, 0, pkg.Length));
         * }*/
        Debug.Log("OnRecvPacket: " + (ePackageType)pkg.Code);
        switch (pkg.Code)
        {
        case (byte)ePlayerPackageType.LOGIN: {
            //LOGIN
            if (pkg.ReadByte() != 0)
            {
                string str = pkg.ReadString();
                Debug.Log(this.m_account + " login socket failed: " + str);
                this.m_state   = ePlayerState.Stopped;
                this.LastError = "SocketError:" + str;
                break;
            }
            this.UserId = pkg.ClientID;
            pkg.ReadInt();     //4
            this.m_playerId          = pkg.Parameter1;
            localPlayerInfo          = new PlayerInfo();
            localPlayerInfo.id       = this.m_playerId;
            localPlayerInfo.nickname = this.NickName;
            ClientRecvPreparer.PlayerInfo_LOGIN(ref localPlayerInfo, ref pkg);

            Debug.Log("login socket success! pid " + this.m_playerId + " uid: " + this.UserId);
            // Debug.Log("login info: "+ localPlayerInfo.ToString());
            // Debug.Log("localPlayerInfo style: " + localPlayerInfo.style);
            UnityThread.executeInUpdate(() =>
                {
                    //Call UpdateLocalPlayerPreview in ConnectorManager
                    connectorManager.UpdateLocalPlayerPreview();
                });
            break;
        }

        case 2: {
            //KIT_USER
            Debug.Log("CMDType 2 KIT_USER");
            this.m_state   = ePlayerState.Stopped;
            this.LastError = "KickReason:" + pkg.ReadString();
            Debug.Log(this.LastError);
            break;
        }

        case 3: {
            //SYS_MESS
            Debug.Log("CMDType 3 - SYS MESS");
            int val = pkg.ReadInt();
            Debug.Log("CMDType 3 val: " + val.ToString());
            this.LastMsg = pkg.ReadString();
            Debug.Log(this.LastMsg);
            break;
        }

        case (byte)ePlayerPackageType.GRID_GOODS: {
            // MainBag = 0, PropBag = 1, TaskBag = 2,
            //TempBag = 4, CaddyBag = 5, Bank    = 11, Store   =12, Card=15
            int             bagType    = pkg.ReadInt();
            int             slotLength = pkg.ReadInt();
            List <ItemInfo> bagBag     = new List <ItemInfo>();
            Debug.Log("bagtype: " + bagType + " - " + (eBagType)bagType + " slotLEngth: " + slotLength);
            for (int ind = 0; ind < slotLength; ind++)
            {
                pkg.ReadInt();
                bagBag.Add(new ItemInfo(ref pkg, true));
                // Debug.Log(bagBag[ind].ToString());
            }
            UnityThread.executeInUpdate(() =>
                {
                    //Call StartGameHandler in ConnectorManager
                    connectorManager.GridGoodsHandler(bagType, bagBag);
                });
            break;
        }

        case (byte)ePackageType.GAME_ROOM_CREATE: {
            RoomInfo ri = new RoomInfo();
            ri.id          = pkg.ReadInt();
            ri.roomType    = (eRoomType)pkg.ReadByte();
            ri.hardLevel   = (eHardLevel)pkg.ReadByte();
            ri.timeMode    = pkg.ReadByte();
            ri.playerCount = pkg.ReadByte();
            ri.placescount = pkg.ReadByte();
            ri.isPasswd    = pkg.ReadBoolean();
            ri.mapid       = pkg.ReadInt();
            ri.isplaying   = pkg.ReadBoolean();
            ri.roomname    = pkg.ReadString();
            ri.gametype    = (eGameType)pkg.ReadByte();
            ri.levelLimits = pkg.ReadInt();
            pkg.ReadBoolean();    //pkg.WriteBoolean(false);
            Debug.Log(ri.ToString());
            this.m_state  = ePlayerState.Room;
            this.m_isHost = true;
            this.m_roomCount++;
            this.Act(new PlayerExecutable(this.StartGame));
            break;
        }

        case (byte)ePlayerPackageType.ITEM_EQUIP: {
            {
                pkg.ReadInt();     //id
                pkg.ReadInt();     //agi
                pkg.ReadInt();     //Attack
                pkg.ReadString();  //Colors
                pkg.ReadString();  //Skin
                pkg.ReadInt();     //Defence
                pkg.ReadInt();     // GP
                pkg.ReadInt();     //Grade
                pkg.ReadInt();     //Luck
                pkg.ReadInt();     //Hide
                pkg.ReadInt();     //Repute
                pkg.ReadBoolean(); // Sex
                pkg.ReadString();  // Style
                pkg.ReadInt();     //Offer
                pkg.ReadString();  //NickName
                pkg.ReadBoolean(); pkg.ReadInt();
                pkg.ReadInt();     //Win
                pkg.ReadInt();     //Total
                pkg.ReadInt();     //Escape
                pkg.ReadInt(); pkg.ReadString(); pkg.ReadInt(); pkg.ReadInt(); pkg.ReadBoolean();
                pkg.ReadInt(); pkg.ReadString(); pkg.ReadString(); pkg.ReadInt();
                pkg.ReadInt(); //FightPower
                pkg.ReadInt(); pkg.ReadInt(); pkg.ReadString(); pkg.ReadInt(); pkg.ReadString();
                //AchievementPoint
                pkg.ReadInt(); pkg.ReadString(); pkg.ReadDateTime();
            }
            int             iCount = pkg.ReadInt(); //itemCount
            List <ItemInfo> items  = new List <ItemInfo>();
            for (int ind = 0; ind < iCount; ind++)
            {
                items.Add(new ItemInfo(ref pkg, false));
                Debug.Log(items[ind].ToString());
            }
            break;
        }

        case (byte)ePlayerPackageType.UPDATE_PRIVATE_INFO: {
            localPlayerInfo.money     = pkg.ReadInt();
            localPlayerInfo.medal     = pkg.ReadInt();
            localPlayerInfo.gold      = pkg.ReadInt();
            localPlayerInfo.giftToken = pkg.ReadInt();
            break;
        }

        case (byte)ePlayerPackageType.UPDATE_PlAYER_INFO: {
            localPlayerInfo.nickname = this.m_account;
            localPlayerInfo.id       = this.m_playerId;
            localPlayerInfo.grade    = this.m_grade;
            localPlayerInfo.gp       = pkg.ReadInt();   //info.GP);
            pkg.ReadInt();                              //info.Offer);
            pkg.ReadInt();                              //info.RichesOffer);
            pkg.ReadInt();                              //info.RichesRob);
            localPlayerInfo.win        = pkg.ReadInt(); //info.Win);
            localPlayerInfo.totalMatch = pkg.ReadInt(); //info.Total);
            pkg.ReadInt();                              //info.Escape);

            localPlayerInfo.attack  = pkg.ReadInt();    //info.Attack);
            localPlayerInfo.defence = pkg.ReadInt();    //info.Defence);
            localPlayerInfo.agility = pkg.ReadInt();    //info.Agility);
            localPlayerInfo.luck    = pkg.ReadInt();    //info.Luck);
            pkg.ReadInt();                              //info.Hide);
            localPlayerInfo.style = pkg.ReadString();   //info.Style);
            localPlayerInfo.color = pkg.ReadString();   //info.Colors);
            localPlayerInfo.skin  = pkg.ReadString();   //info.Skin);

            pkg.ReadInt();                              //info.ConsortiaID);
            pkg.ReadString();                           //info.ConsortiaName);
            pkg.ReadInt();                              //info.ConsortiaLevel);
            pkg.ReadInt();                              //info.ConsortiaRepute);

            pkg.ReadInt();                              //info.Nimbus);
            pkg.ReadString();                           //info.PvePermission);
            pkg.ReadString();                           //"1");
            localPlayerInfo.fightPower = pkg.ReadInt(); //info.FightPower);
            // pkg.ReadInt();//1);
            // pkg.ReadInt();//-1);
            // pkg.ReadString();//"ss");
            // pkg.ReadInt();//1);
            // pkg.ReadString();//"ss");
            // ////AchievementPoint
            // pkg.ReadInt();//0);
            // ////honor
            // pkg.ReadString();//"honor");
            // // //LastSpaDate
            // // if ();//info.ExpendDate != null)
            // //     pkg.ReadDateTime();//();//DateTime)info.ExpendDate);
            // // else { pkg.ReadDateTime();//DateTime.MinValue); }
            // //charmgp
            // pkg.ReadInt();//100);
            // //consortiaCharmGP
            // pkg.ReadInt();//100);

            // pkg.ReadDateTime();//DateTime.MinValue);
            // ////DeputyWeaponID
            // pkg.ReadInt();//10001);
            // pkg.ReadInt();//0);
            // // box gi ko biet
            // pkg.ReadInt();//info.AnswerSite);
            // // pkg.ReadInt();//0);
            Debug.Log(localPlayerInfo.ToString());
            UnityThread.executeInUpdate(() =>
                {
                    //Call Update mainplayer info in ConnectorManager
                    connectorManager.UpdateLocalPlayerPreview();
                    connectorManager.UpdateStatsDisplay();
                });
            break;
        }

        case (byte)ePlayerPackageType.GAME_CMD: {
            //GAME_CMD
            //Debug.Log("GAME_CMD");
            eTankCmdType type     = (eTankCmdType)pkg.ReadByte();
            int          pId      = pkg.Parameter1;
            int          LifeTime = pkg.Parameter2;
            Debug.Log("[CMD] " + ((eTankCmdType)type).ToString() + " - lifeTime: " + LifeTime.ToString());
            switch (type)
            {
            case eTankCmdType.TAKE_CARD:
            // Debug.Log("TAKE_CARD ");
            // return;
            case eTankCmdType.GAME_OVER:
                // this.Act(new PlayerExecutable(this.EnterWaitingRoom));
                // this.Act(new PlayerExecutable(this.CreateRoom));
                // this.Act(new PlayerExecutable(this.StartGame));
                MatchSummary ms = ClientRecvPreparer.GameOver_MatchSummary(ref pkg, this.m_playerId);
                UnityThread.executeInUpdate(() =>
                    {
                        //Call StartGameHandler in ConnectorManager
                        connectorManager.GameOverHandler(ms);
                    });
                return;

            case eTankCmdType.GAME_TIME:
                this.m_lifeTime = pkg.ReadInt();
                Debug.Log("GAME_TIME sync: " + this.m_lifeTime.ToString());
                return;

            case eTankCmdType.START_GAME:
                // Game.Logic/PVPGame.cs
                int size = pkg.ReadInt();         //number of players
                // List<PlayerInfo> Players = new List<PlayerInfo>();
                for (int i = 0; i < size; i++)
                {
                    int tmpId = pkg.ReadInt();
                    for (int j = 0; j < size; j++)
                    {
                        if (tmpId != playersList[j].id)
                        {
                            continue;
                        }
                        playersList[j].x         = pkg.ReadInt();
                        playersList[j].y         = pkg.ReadInt();
                        playersList[j].direction = pkg.ReadInt();
                        playersList[j].blood     = pkg.ReadInt();
                        pkg.ReadInt();             //2
                        pkg.ReadInt();             //34
                        playersList[j].isMainPlayer = false;
                        //check if player is this user
                        //Debug.Log("thisPid: "+ this.m_playerId.ToString() + " - Pid: " + Players[i].id.ToString());
                        // if (playersList[j].id == this.m_playerId){
                        if (playersList[j].nickname == this.m_account ||
                            playersList[j].nickname == this.NickName)
                        {
                            playersList[j].isMainPlayer = true;
                            this.m_playerId             = playersList[j].id;
                            this.m_posX = playersList[j].x;
                            this.m_posY = playersList[j].y;
                        }
                        playersList[j].dander      = pkg.ReadInt();
                        playersList[j].effectCount = pkg.ReadInt();
                        playersList[j].property3   = new List <int>();
                        playersList[j].property4   = new List <int>();
                        for (int k = 0; k < playersList[j].effectCount; k++)
                        {
                            playersList[j].property3.Add(pkg.ReadInt());
                            playersList[j].property4.Add(pkg.ReadInt());
                        }
                        break;
                    }
                }
                UnityThread.executeInUpdate(() =>
                    {
                        //Call StartGameHandler in ConnectorManager
                        connectorManager.StartGameHandler(playersList);
                    });
                this.m_state = ePlayerState.StartGame;
                return;

            case eTankCmdType.GAME_CREATE:
                this.m_gameCount++;
                pkg.ReadInt();         //Roomtype
                pkg.ReadInt();         //gametype
                pkg.ReadInt();         //Timetype
                localPlayerInfo.nickname = this.m_account;
                this.playersList         = ClientRecvPreparer.GameCreate_PlayerList(ref localPlayerInfo, ref pkg);
                this.m_playerId          = localPlayerInfo.id;
                this.m_state             = ePlayerState.CreateGame;
                UnityThread.executeInUpdate(() =>
                    {
                        //Call GameCreateHandler in ConnectorManager
                        connectorManager.GameCreateHandler(this.playersList);
                    });
                return;

            case eTankCmdType.GAME_LOAD:

                pkg.ReadInt();         //maxTime
                int mapId = pkg.ReadInt();
                UnityThread.executeInUpdate(() =>
                    {
                        //Call LoadMapHandler in ConnectorManager
                        connectorManager.GameLoadHandler(mapId);
                    });
                return;

            case eTankCmdType.LOAD:

                this.m_state = ePlayerState.Loading;
                // this.Act(new PlayerExecutable(this.SendLoadingComplete));
                return;

            case eTankCmdType.GAME_MISSION_INFO:
                MissionInfo mi = ClientRecvPreparer.GetPVEMissionInfo(ref pkg);
                UnityThread.executeInUpdate(() =>
                    {
                        connectorManager.PVEMissionPrepare(mi);
                    });
                return;

            case eTankCmdType.GAME_UI_DATA:
                // update turn index and kill count;
                int TurnIndex = pkg.ReadInt();
                int count = pkg.ReadInt();
                int param3 = pkg.ReadInt(), param4 = pkg.ReadInt();
                UnityThread.executeInUpdate(() =>
                    {
                        Debug.Log(string.Format("GAME_UI_DATA : TurnIndex:{0} - count:{1} - param3,4:{2},{3}",
                                                TurnIndex, count, param3, param4));
                    });
                return;

            case eTankCmdType.ADD_LIVING:
                // update turn index and kill count;
                LivingInfo li = ClientRecvPreparer.AddLivingInfo(ref pkg);
                UnityThread.executeInUpdate(() =>
                    {
                        Debug.Log("ADD_LIVING : " + li.ToString());
                    });
                return;

            case eTankCmdType.TURN:
                Debug.Log("pId turn: " + pkg.Parameter1.ToString());
                int wind = pkg.ReadInt();
                pkg.ReadBoolean();
                pkg.ReadByte();
                pkg.ReadByte();
                pkg.ReadByte();
                bool isHiding = pkg.ReadBoolean();

                //turnTime
                // TimeType
                int            timeType      = pkg.ReadInt();
                List <BoxInfo> newBoxesList  = new List <BoxInfo>();
                int            newBoxesCount = pkg.ReadInt();
                for (int j = 0; j < newBoxesCount; j++)
                {
                    newBoxesList.Add(new BoxInfo());
                    newBoxesList[j].id = pkg.ReadInt();
                    newBoxesList[j].x  = pkg.ReadInt();
                    newBoxesList[j].y  = pkg.ReadInt();
                    pkg.ReadInt();
                    // pkg.ReadBoolean();
                }
                List <PlayerInfo> updatedPlayerList = new List <PlayerInfo>();
                int listSize = pkg.ReadInt();
                for (int j = 0; j < listSize; j++)
                {
                    updatedPlayerList.Add(new PlayerInfo());
                    updatedPlayerList[j].id         = pkg.ReadInt();
                    updatedPlayerList[j].isLiving   = pkg.ReadBoolean();
                    updatedPlayerList[j].x          = pkg.ReadInt();
                    updatedPlayerList[j].y          = pkg.ReadInt();
                    updatedPlayerList[j].blood      = pkg.ReadInt();
                    updatedPlayerList[j].isNoHole   = pkg.ReadBoolean();
                    updatedPlayerList[j].energy     = pkg.ReadInt();
                    updatedPlayerList[j].dander     = pkg.ReadInt();
                    updatedPlayerList[j].shootCount = pkg.ReadInt();
                }
                int turnIndex = pkg.ReadInt();

                UnityThread.executeInUpdate(() =>
                    {
                        //Call TurnHandler in ConnectorManager
                        connectorManager.TurnHandler(pkg.Parameter1, newBoxesList, updatedPlayerList);
                    });
                // this.m_state = ePlayerState.Shoot;
                // this.m_shootCount++;
                //this.Act(new PlayerExecutable(this.Shoot));
                return;

            case eTankCmdType.CURRENTBALL:
                bool special       = pkg.ReadBoolean();
                int  currentBallId = pkg.ReadInt();
                UnityThread.executeInUpdate(() =>
                    {
                        //Call CurrentBallHandler in ConnectorManager
                        connectorManager.CurrentBallHandler(pId, special, currentBallId);
                    });
                //pkg.WriteByte((byte)player.BallCount);
                return;

            case eTankCmdType.FIRE:
                List <FireInfo> fireInfos = ClientRecvPreparer.MakeFireInfo(ref pkg);
                UnityThread.executeInUpdate(() =>
                    {
                        //Call FireHandler in ConnectorManager
                        connectorManager.FireHandler(pId, fireInfos);
                    });
                return;

            case eTankCmdType.FIRE_TAG:
                bool tag       = pkg.ReadBoolean();
                byte speedTime = pkg.ReadByte();
                UnityThread.executeInUpdate(() =>
                    {
                        //Call FireTagHandler in ConnectorManager
                        connectorManager.FireTagHandler(pId, tag, speedTime);
                    });
                return;

            case eTankCmdType.USING_PROP:
                byte propType   = pkg.ReadByte();
                int  place      = pkg.ReadInt();
                int  templateId = pkg.ReadInt();
                UnityThread.executeInUpdate(() =>
                    {
                        //Call UsingPropHandler in ConnectorManager
                        connectorManager.UsingPropHandler(pId, propType, place, templateId);
                    });
                return;

            case eTankCmdType.MOVESTART:
                byte   moveType = pkg.ReadByte();
                int    tx       = pkg.ReadInt();
                int    ty       = pkg.ReadInt();
                byte   dir      = pkg.ReadByte();
                bool   isLiving = pkg.ReadBoolean();
                string action   = pkg.ReadString();       // is null or empty or "move"
                // Debug.Log("MOVE type: "+moveType.ToString()+" tX: " + tx.ToString()+
                //         " tY:"+ ty.ToString()+ " dir: "+dir.ToString());
                UnityThread.executeInUpdate(() =>
                    {
                        //Call MoveStartHandler in ConnectorManager
                        connectorManager.MoveStartHandler(pId, moveType, tx, ty, dir, isLiving);
                    });

                return;

            case eTankCmdType.DIRECTION:
                int direction = pkg.ReadInt();
                UnityThread.executeInUpdate(() =>
                    {
                        //Call CurrentBallHandler in ConnectorManager
                        connectorManager.DirectionHandler(pId, direction);
                    });
                return;

            case eTankCmdType.DANDER:
                int playerId = pkg.Parameter1;
                int dander   = pkg.ReadInt();
                localPlayerInfo.dander = dander;
                UnityThread.executeInUpdate(() =>
                    {
                        //Call CurrentBallHandler in ConnectorManager
                        connectorManager.DanderHandler(pId, dander);
                    });
                return;

            default:
                Debug.Log("Unhandled GAME_CMD type: " + type.ToString());
                return;
            }
            break;
        }

        // case 0x5e:{
        // //GAME_CREATE_ROOM
        //     Debug.Log("GET GAME_CREATE_ROOM PACKAGE");
        //     if (pkg.ReadInt() != 0)//if (m_isHost == false)
        //     {
        //         this.m_state = ePlayerState.Room;
        //         this.m_isHost = true;
        //         this.m_roomCount++;
        //         this.Act(new PlayerExecutable(this.StartGame));
        //     }
        //         break;
        // }
        case 5:
        {
            //SYS_DATE
            Debug.Log("GET SYS_DATE PACKAGE");
            // this.Act(new PlayerExecutable(this.EnterWaitingRoom));
            // System.Threading.Thread.Sleep(100);
            // this.Act(new PlayerExecutable(this.CreateRoom));
            // //Debug.Log(this.m_State == ePlayerState.Room);
            // //Debug.Log(this.m_isHost);
            // //Debug.Log(this.m_roomCount);
            // System.Threading.Thread.Sleep(100);
            // this.Act(new PlayerExecutable(this.StartGame));
            break;
        }

        case (int)ePackageType.GAME_PAIRUP_START:
            UnityThread.executeInUpdate(() =>
            {
                connectorManager.ConfirmMatching();
            });
            break;

        default:
            Debug.Log("Unhandled Package type: " + ((ePackageType)pkg.Code).ToString());
            break;
        }
    }