Exemplo n.º 1
0
        protected override void OnMessage(MessageEventArgs e)
        {
            try
            {
                ServerLog.writeLog(string.Format("Player:{0}, Msg:", ID, e.Data));
                string data = e.Data;

                object jsonObject = new JavaScriptSerializer().DeserializeObject(data);
                IDictionary <string, object> payload = (IDictionary <string, object>)jsonObject;
                string pid = (string)payload["pid"];

                switch (pid)
                {
                case "LoginInfo":
                    LoginInfo loginInfo = new JavaScriptSerializer().ConvertToType <LoginInfo>(jsonObject);
                    ServerLog.writeLog(string.Format("Loginin GamePlayer:{0} DeviceId:{1}", ID, loginInfo.deviceId));
                    LoginInfoRes loginRes = new LoginInfoRes();
                    //Check LoginInfo
                    if (loginInfo.deviceId.Length > 10)
                    {
                        myDeviceID           = loginInfo.deviceId;
                        myNickName           = loginInfo.nickName;
                        loginRes.loginResult = 1;
                    }
                    sendData("LoginInfoRes", loginRes);
                    if (loginRes.loginResult > 0)
                    {
                        GameInfo gameinfo = new GameInfo();
                        sendData("GameInfo", gameinfo);
                    }
                    break;

                case "QuickSeatReq":
                    myGame = ServerApp.getGameTableActor().quickJoin(this);
                    break;

                case "MoveInfoReq":
                    MoveInfoReq moveInfo = new JavaScriptSerializer().ConvertToType <MoveInfoReq>(jsonObject);
                    myGame.moveInfoReq(moveInfo, isBlack);
                    break;

                case "GameResultInfo":
                    GameResultInfo gameResult = new JavaScriptSerializer().ConvertToType <GameResultInfo>(jsonObject);
                    if (gameResult.wiinnerIsme == true)
                    {
                        Task resultTask = new Task(() =>
                        {
                            Task.Delay(5000).Wait();
                            myGame.prePareGame();
                        });
                        resultTask.Start();
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                ServerLog.writeLog(ex.Message);
            }
        }
Exemplo n.º 2
0
    private void ProcessPackets()
    {
        string curWsData = "";

        lock (packetList)
        {
            if (packetList.Count > 0)
            {
                curWsData = packetList.Dequeue();
            }
        }

        if (curWsData.Length < 1)
        {
            return;
        }

        var    jsonObject = JsonUtility.FromJson <WebDataRes>(curWsData);
        string pid        = jsonObject.pid;

        switch (pid)
        {
        case "Connected":
            LoginInfo loginInfo = new LoginInfo();
            loginInfo.deviceId = deviceId;
            loginInfo.nickName = PlayerPrefs.GetString("playerName");
            if (Debug.isDebugBuild)
            {
                System.Random rnd    = new System.Random();
                int           random = rnd.Next(1, 1000);
                loginInfo.deviceId = deviceId + random;
            }
            ws.Send(loginInfo.ToString());
            txtServerState.text = "Conneted Server";
            setOnLine(true);
            break;

        case "Disconnected":
            dols.CleanDols();
            isNetworkPlay       = true;
            txtServerState.text = "Disconneted Server";
            isNetworkPlay       = false;
            setOnLine(false);
            //Application.Quit();
            break;

        case "LoginInfoRes":
            isOffLineMode = false;
            LoginInfoRes loginRes = new LoginInfoRes();
            loginRes.FromJsonOverwrite(jsonObject.data);
            Debug.Log("LoginInfoRes: " + loginRes.ToString());
            if (loginRes.loginResult > 0)
            {
                txtServerState.text = "Wait Opponent Player(You Can run PracticeMode during waiting)";
                QuickSeatReq quickSeat = new QuickSeatReq();
                ws.Send(quickSeat.ToString());
            }
            else
            {
                txtServerState.text = "Login Failed...";
            }
            break;

        case "GameInfo":
            GameInfo gameInfoRes = new GameInfo();
            gameInfoRes.FromJsonOverwrite(jsonObject.data);
            gameInfo       = gameInfoRes;
            globalTimeLeft = (float)gameInfoRes.totalTimeBank;
            break;

        case "DolsInfo":
            setOnLine(true);
            globalTimeLeft = (float)gameInfo.totalTimeBank;
            isOffLineMode  = false;
            isNetworkPlay  = true;
            DolsInfo dolsinfo = new DolsInfo();
            dolsinfo.FromJsonOverwrite(jsonObject.data);
            Debug.Log("DolsInfo: " + dolsinfo.ToString());

            if (dolsinfo.isBlack == false)
            {
                dols.firstWplayDols = dolsinfo;
            }
            else
            {
                dols.firstBplayDols = dolsinfo;
                dols.CleanDols();
                dols.InitDols();
            }

            if (dolsinfo.nickName.Length > 0)
            {
                if (dolsinfo.isMe)
                {
                    txtYour_name.text = dolsinfo.nickName;
                }
                else
                {
                    txtOtherName.text = dolsinfo.nickName;
                }
            }
            onStageInit();
            break;

        case "MoveInfoRes":
            MoveInfoRes moveInfoRes = new MoveInfoRes();
            moveInfoRes.FromJsonOverwrite(jsonObject.data);
            SwapDolPos(moveInfoRes.source, moveInfoRes.target);
            break;

        case "TurnInfo":
            TurnInfo turnInfo = new TurnInfo();
            turnInfo.FromJsonOverwrite(jsonObject.data);
            string txtTurnInfo     = "";
            string currentDolColor = turnInfo.isBlack == true ? "black" : "white";
            if (turnInfo.isMe)
            {
                txtTurnInfo       = string.Format("Your({0}) turn", currentDolColor);
                isMyTurn          = true;
                isMyDolColorBlack = turnInfo.isBlack;
                privateTimeLeft   = gameInfo.privateTimeBank;
            }
            else
            {
                txtTurnInfo       = string.Format("Wait other player({0}) Action", currentDolColor);
                isMyTurn          = false;
                isMyDolColorBlack = !turnInfo.isBlack;
                privateTimeLeft   = gameInfo.privateTimeBank;
            }

            if (isOffLineMode)
            {
                isMyDolColorBlack = turnInfo.isBlack;
                txtTurnInfo       = string.Format("{0} Turn", currentDolColor);

                if (isAIMode)
                {
                    if (turnInfo.isBlack == true)
                    {
                        //dols.doaskAIAction
                        PlayDol sourceDol = null;
                        PlayDol targetDol = null;
                        dols.askAIAction(2, ref sourceDol, ref targetDol);

                        MoveInfoRes moveInfoReq = new MoveInfoRes();
                        moveInfoReq.source.setPos(sourceDol.GetDolPos());
                        moveInfoReq.target.setPos(targetDol.GetDolPos());
                        Debug.Log("AI:" + moveInfoReq.ToString());
                        sendLocalData("MoveInfoRes", moveInfoReq.ToString());

                        TurnInfo turnChange = new TurnInfo();
                        turnChange.isMe    = true;
                        turnChange.isBlack = false;

                        sendLocalData("TurnInfo", turnChange.ToString());
                    }
                }
            }

            txtServerState.text = txtTurnInfo;
            break;

        case "CrashGameInfo":
            dols.CleanDols();
            txtServerState.text = "Other User Leaver, Wait Other Player";
            isNetworkPlay       = false;
            break;

        case "CheckGame":
            if (isOffLineMode == false)
            {
                if (isMyDolColorBlack == true && curPlayDol.GetMyDolType() == 2)
                {
                    myPlyScore += Dols.checkGame(curPlayDol);
                }

                if (isMyDolColorBlack == true && curPlayDol.GetMyDolType() == 1)
                {
                    otherPlyScore += Dols.checkGame(curPlayDol);
                }

                if (isMyDolColorBlack == false && curPlayDol.GetMyDolType() == 1)
                {
                    myPlyScore += Dols.checkGame(curPlayDol);
                }

                if (isMyDolColorBlack == false && curPlayDol.GetMyDolType() == 2)
                {
                    otherPlyScore += Dols.checkGame(curPlayDol);
                }
            }
            else
            {
                if (curPlayDol.GetMyDolType() == 1)
                {
                    myPlyScore += Dols.checkGame(curPlayDol);
                }
                else
                {
                    otherPlyScore += Dols.checkGame(curPlayDol);
                }
            }

            if (isOffLineMode == false)
            {
                txtMyPlyScore.text     = myPlyScore.ToString();
                txtOtherPlayScore.text = otherPlyScore.ToString();
            }
            else
            {
                txtMyPlyScore.text     = myPlyScore.ToString();
                txtOtherPlayScore.text = otherPlyScore.ToString();
                txtYour_name.text      = "White";
                txtOtherName.text      = "Black";
            }

            GameResultInfo gameResultInfo = new GameResultInfo();
            if (myPlyScore > gameInfo.winScore - 1)
            {
                gameResultInfo.winnerColor = curPlayDol.GetMyDolType();
                gameResultInfo.wiinerScore = myPlyScore;
                gameResultInfo.loseScore   = otherPlyScore;
                gameResultInfo.wiinnerIsme = true;

                if (isOffLineMode == false)
                {
                    showResult("You Win", true);
                    ws.Send(gameResultInfo.ToString());
                }
                else
                {
                    showResult("White Win", true);
                }
            }

            if (otherPlyScore > gameInfo.winScore - 1)
            {
                gameResultInfo.winnerColor = curPlayDol.GetMyDolType();
                gameResultInfo.wiinerScore = otherPlyScore;
                gameResultInfo.loseScore   = myPlyScore;
                gameResultInfo.wiinnerIsme = false;

                if (isOffLineMode == false)
                {
                    showResult("You Lose", true);
                    ws.Send(gameResultInfo.ToString());
                }
                else
                {
                    showResult("Black Win", true);
                }
            }
            break;
        }
    }