Пример #1
0
        /// <summary>
        ///     Returns the world data for the given Lat Lng rectangle.
        ///     This calls updates the server cache if new playable locations are found.
        /// </summary>
        /// <param name="worldDataRequest">World data request</param>
        /// <param name="onSuccess">The callback invoked when the call returns successfully</param>
        /// <param name="onError">The callback invoked if the call fails</param>
        /// <returns>An enumerator for the coroutine function</returns>
        public IEnumerator PostWorldData(
            WorldDataRequest worldDataRequest,
            Action <WorldData> onSuccess,
            Action <string> onError)
        {
            string json = "";

            if (worldDataRequest != null)
            {
                json = JsonMapper.ToJson(worldDataRequest);
            }

            using (UnityWebRequest webRequest =
                       new UnityWebRequest(SERVER_URL + "/worlds/" + GetUserId(), "POST"))
            {
                byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
                webRequest.uploadHandler   = new UploadHandlerRaw(bodyRaw);
                webRequest.downloadHandler = new DownloadHandlerBuffer();
                webRequest.SetRequestHeader("Content-Type", "application/json");

                yield return(webRequest.SendWebRequest());

                if (webRequest.isNetworkError || webRequest.isHttpError)
                {
                    onError?.Invoke(webRequest.error);
                }
                else
                {
                    onSuccess?.Invoke(
                        JsonMapper.ToObject <WorldData>(webRequest.downloadHandler.text));
                }
            }
        }
Пример #2
0
        /// <summary>
        ///     Loads all playable locations within a round area centered on the player's
        ///     GPS position.
        /// </summary>
        /// <param name="currentPosition">The current lat lng of our avatar</param>
        /// <param name="distance">The radius of the map area around us</param>
        private void UpdateWorldData(LatLng currentPosition, float distance)
        {
            if (!_worldDataIsLoading)
            {
                _worldDataIsLoading = true;

                Vector3 center = MapsService.Coords.FromLatLngToVector3(currentPosition);

                Vector3 NorthEastCorner = center + new Vector3(distance, 0f, distance);
                LatLng  NorthEastLatLng = MapsService.Coords.FromVector3ToLatLng(NorthEastCorner);

                Vector3 SouthWestCorner = center + new Vector3(-distance, 0f, -distance);
                LatLng  SouthWestLatLng = MapsService.Coords.FromVector3ToLatLng(SouthWestCorner);

                WorldDataRequest wdr = new WorldDataRequest();
                wdr.CopyFrom(SouthWestLatLng, NorthEastLatLng);

                StartCoroutine(ServerManager.PostWorldData(wdr, OnWorldDataLoaded, OnError));
            }
        }