コード例 #1
0
    /*private void SendAndCreateToServer()
     * {
     *  switch (sendOptions)
     *  {
     *      case SendOptions.AI_VS_AI:
     *          SendAI_VS_AI_Data();
     *          break;
     *
     *      case SendOptions.AckAI_VS_AI:
     *          SendAck();
     *          break;
     *
     *      case SendOptions.Ack:
     *          SendAck();
     *          break;
     *
     *      default:
     *          break;
     *  }
     * }*/

    private IEnumerator RecieveAndParseServerReply()
    {
        recievingInCorutine = true;

        while (!s.DataAvailable)
        {
            yield return(null);
        }

        string ServerReply = sr.ReadLine();

        Debug.Log("Recvd " + ServerReply);
        loggerText.text = "Recvd " + ServerReply + "\n";

        Dictionary <string, object> ServerReplyDict = JsonConvert.DeserializeObject <Dictionary <string, object> >(ServerReply);

        MsgsEnum ServerReplytype = (MsgsEnum)int.Parse(ServerReplyDict["type"].ToString());

        string serverMsg = ServerReplyDict["msg"].ToString();

        switch (ServerReplytype)
        {
        case MsgsEnum.AI_VS_AI:
            break;

        case MsgsEnum.AckAI_VS_AI:

            AckAI_VS_AI_Data ackAI_VS_AI = JsonConvert.DeserializeObject <AckAI_VS_AI_Data>(serverMsg);

            manager.SetMyTurn(ackAI_VS_AI.myTurn);

            if (ackAI_VS_AI.ourRemainingTime > 0)
            {
                manager.SetOurTimer(ackAI_VS_AI.ourRemainingTime / 1000.0f);
            }

            if (ackAI_VS_AI.theirRemainingTime > 0)
            {
                manager.SetTheirTimer(ackAI_VS_AI.theirRemainingTime / 1000.0f);
            }

            SendAck();

            break;

        case MsgsEnum.moveConfigrations:

            MoveData moveConfigData = JsonConvert.DeserializeObject <MoveData>(serverMsg);
            boardObject.PlaceStone(moveConfigData.y, moveConfigData.x, moveConfigData.color != 'b');

            if (moveConfigData.ourScore > 0 || moveConfigData.theirScore > 0)
            {
                manager.SetScore(moveConfigData.ourScore, moveConfigData.theirScore);
            }

            if (moveConfigData.ourTimer > 0)
            {
                manager.SetOurTimer(moveConfigData.ourTimer / 1000.0f);
            }

            if (moveConfigData.theirTimer > 0)
            {
                manager.SetTheirTimer(moveConfigData.theirTimer / 1000.0f);
            }

            manager.GamePlayedAvA();

            //sendOptions = SendOptions.Ack;
            SendAck();

            break;

        case MsgsEnum.gameEnd:

            GameEndData gameEndData = JsonConvert.DeserializeObject <GameEndData>(serverMsg);
            manager.GameEnd(gameEndData.ourScore, gameEndData.theirScore, gameEndData.win);

            SendAck();

            break;

        case MsgsEnum.gamePaused:

            manager.PauseGame();

            SendAck();

            break;

        case MsgsEnum.exit:
            break;

        case MsgsEnum.ack:

            AckData ackData = JsonConvert.DeserializeObject <AckData>(serverMsg);
            Debug.Log("Ack data " + ackData.reason + " " + ackData.valid + " " + ackData.valid);

            if (ackData.valid)
            {
                manager.PlayerMoveAccepted();

                if (ackData.ourScore > 0 || ackData.theirScore > 0)
                {
                    manager.SetScore(ackData.ourScore, ackData.theirScore);
                }
            }
            else
            {
                manager.PlayerMoveNotAccepted();
            }

            manager.ShowWarning(ackData.reason);

            SendAck();

            break;

        case MsgsEnum.gameStart:

            GameStartData gameStartData = JsonConvert.DeserializeObject <GameStartData>(serverMsg);

            manager.SetMyTurn(gameStartData.myTurn);

            if (gameStartData.ourRemainingTime > 0)
            {
                manager.SetOurTimer(gameStartData.ourRemainingTime / 1000.0f);
            }

            if (gameStartData.theirRemainingTime > 0)
            {
                manager.SetTheirTimer(gameStartData.theirRemainingTime / 1000.0f);
            }

            manager.ResumeGame();

            SendAck();

            break;

        case MsgsEnum.AI_VSHuman:
            break;

        case MsgsEnum.move:

            MoveData moveData = new MoveData();
            moveData.countCaptured = 0;

            moveData = JsonConvert.DeserializeObject <MoveData>(serverMsg);
            boardObject.PlaceStone(moveData.y, moveData.x, moveData.color != 'b');

            manager.AiPlayed();

            if (moveData.ourScore > 0 || moveData.theirScore > 0)
            {
                manager.SetScore(moveData.ourScore, moveData.theirScore);
            }

            //sendOptions = SendOptions.Ack;
            SendAck();

            break;

        case MsgsEnum.forfeit:
            break;

        case MsgsEnum.remove:

            RemoveData removeData = JsonConvert.DeserializeObject <RemoveData>(serverMsg);
            boardObject.RemoveStone(removeData.y, removeData.x);

            //sendOptions = SendOptions.Ack;
            SendAck();

            break;

        default:
            break;
        }

        recievingInCorutine = false;
    }