Пример #1
0
    public void RequestRoomData(RoomKey roomKey, GameEvent.OnEventCompleteDelegate onComplete)
    {
        if (!IsRoomDataRequestPending)
        {
            // Cached room data is available
            if (SessionData.GetInstance().CurrentGameData.HasRoomData(roomKey))
            {
                // Update which room we're currently in
                SessionData.GetInstance().CurrentGameData.CurrentRoomKey = roomKey;

                // Notify the controller
                Debug.Log("Using cached room data");
                m_gameWorldController.OnRoomLoaded(roomKey);

                // Notify the caller
                if (onComplete != null)
                {
                    onComplete();
                }
            }
            // Have to request room data from the server
            else
            {
                AsyncJSONRequest            roomDataRequest = AsyncJSONRequest.Create(m_gameWorldController.gameObject);
                Dictionary <string, object> request         = new Dictionary <string, object>();

                request["game_id"] = SessionData.GetInstance().GameID;
                request["room_x"]  = roomKey.x;
                request["room_y"]  = roomKey.y;
                request["room_z"]  = roomKey.z;

                IsRoomDataRequestPending = true;

                roomDataRequest.POST(
                    ServerConstants.roomDataRequestURL,
                    request,
                    (AsyncJSONRequest asyncRequest) =>
                {
                    if (asyncRequest.GetRequestState() == AsyncJSONRequest.eRequestState.succeded)
                    {
                        JsonData response     = asyncRequest.GetResult();
                        string responseResult = (string)response["result"];

                        if (responseResult == "Success")
                        {
                            SessionData sessionData = SessionData.GetInstance();
                            GameData currentGame    = sessionData.CurrentGameData;
                            RoomData roomData       = RoomData.FromObject(response);

                            // Add the room data to the room cache
                            currentGame.SetCachedRoomData(roomData.RoomKey, roomData);

                            // Update which room we're currently in
                            currentGame.CurrentRoomKey = roomKey;

                            // Notify the controller
                            Debug.Log("Room Loaded");
                            m_gameWorldController.OnRoomLoaded(roomData.RoomKey);
                        }
                        else
                        {
                            Debug.Log("Room Data Request Failed: " + responseResult);
                            m_gameWorldController.OnRequestFailed(responseResult);
                        }
                    }
                    else
                    {
                        Debug.Log("Room Data Request Failed: " + asyncRequest.GetFailureReason());
                        m_gameWorldController.OnRequestFailed("Connection Failed!");
                    }

                    // Notify the caller
                    if (onComplete != null)
                    {
                        onComplete();
                    }

                    IsRoomDataRequestPending = false;
                });
            }
        }
    }