|
| 1 | +/// <reference path="../typings/PlayFab/PlayFabLocalizationApi.d.ts" /> |
| 2 | + |
| 3 | +var PlayFab = typeof PlayFab != "undefined" ? PlayFab : {}; |
| 4 | + |
| 5 | +if(!PlayFab.settings) { |
| 6 | + PlayFab.settings = { |
| 7 | + titleId: null, // You must set this value for PlayFabSdk to work properly (Found in the Game Manager for your title, at the PlayFab Website) |
| 8 | + developerSecretKey: null, // For security reasons you must never expose this value to the client or players - You must set this value for Server-APIs to work properly (Found in the Game Manager for your title, at the PlayFab Website) |
| 9 | + advertisingIdType: null, |
| 10 | + advertisingIdValue: null, |
| 11 | + GlobalHeaderInjection: null, |
| 12 | + |
| 13 | + // disableAdvertising is provided for completeness, but changing it is not suggested |
| 14 | + // Disabling this may prevent your advertising-related PlayFab marketplace partners from working correctly |
| 15 | + disableAdvertising: false, |
| 16 | + AD_TYPE_IDFA: "Idfa", |
| 17 | + AD_TYPE_ANDROID_ID: "Adid" |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +if(!PlayFab._internalSettings) { |
| 22 | + PlayFab._internalSettings = { |
| 23 | + entityToken: null, |
| 24 | + sdkVersion: "1.30.180906", |
| 25 | + requestGetParams: { |
| 26 | + sdk: "JavaScriptSDK-1.30.180906" |
| 27 | + }, |
| 28 | + sessionTicket: null, |
| 29 | + productionServerUrl: ".playfabapi.com", |
| 30 | + errorTitleId: "Must be have PlayFab.settings.titleId set to call this method", |
| 31 | + errorLoggedIn: "Must be logged in to call this method", |
| 32 | + errorEntityToken: "You must successfully call GetEntityToken before calling this", |
| 33 | + errorSecretKey: "Must have PlayFab.settings.developerSecretKey set to call this method", |
| 34 | + |
| 35 | + GetServerUrl: function () { |
| 36 | + return "https://" + PlayFab.settings.titleId + PlayFab._internalSettings.productionServerUrl; |
| 37 | + }, |
| 38 | + |
| 39 | + InjectHeaders: function (xhr, headersObj) { |
| 40 | + if (!headersObj) |
| 41 | + return; |
| 42 | + |
| 43 | + for (var headerKey in headersObj) |
| 44 | + { |
| 45 | + try { |
| 46 | + xhr.setRequestHeader(gHeaderKey, headersObj[headerKey]); |
| 47 | + } catch (e) { |
| 48 | + console.log("Failed to append header: " + headerKey + " = " + headersObj[headerKey] + "Error: " + e); |
| 49 | + } |
| 50 | + } |
| 51 | + }, |
| 52 | + |
| 53 | + ExecuteRequest: function (url, request, authkey, authValue, callback, customData, extraHeaders) { |
| 54 | + if (callback != null && typeof (callback) != "function") |
| 55 | + throw "Callback must be null of a function"; |
| 56 | + |
| 57 | + if (request == null) |
| 58 | + request = {}; |
| 59 | + |
| 60 | + var startTime = new Date(); |
| 61 | + var requestBody = JSON.stringify(request); |
| 62 | + |
| 63 | + var urlArr = [url]; |
| 64 | + var getParams = PlayFab._internalSettings.requestGetParams; |
| 65 | + if (getParams != null) { |
| 66 | + var firstParam = true; |
| 67 | + for (var key in getParams) { |
| 68 | + if (firstParam) { |
| 69 | + urlArr.push("?"); |
| 70 | + firstParam = false; |
| 71 | + } else { |
| 72 | + urlArr.push("&"); |
| 73 | + } |
| 74 | + urlArr.push(key); |
| 75 | + urlArr.push("="); |
| 76 | + urlArr.push(getParams[key]); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + var completeUrl = urlArr.join(""); |
| 81 | + |
| 82 | + var xhr = new XMLHttpRequest(); |
| 83 | + // window.console.log("URL: " + completeUrl); |
| 84 | + xhr.open("POST", completeUrl, true); |
| 85 | + |
| 86 | + xhr.setRequestHeader("Content-Type", "application/json"); |
| 87 | + xhr.setRequestHeader("X-PlayFabSDK", "JavaScriptSDK-" + PlayFab._internalSettings.sdkVersion); |
| 88 | + if (authkey != null) |
| 89 | + xhr.setRequestHeader(authkey, authValue); |
| 90 | + PlayFab._internalSettings.InjectHeaders(xhr, PlayFab.settings.GlobalHeaderInjection); |
| 91 | + PlayFab._internalSettings.InjectHeaders(xhr, extraHeaders); |
| 92 | + |
| 93 | + xhr.onloadend = function () { |
| 94 | + if (callback == null) |
| 95 | + return; |
| 96 | + |
| 97 | + var result; |
| 98 | + try { |
| 99 | + // window.console.log("parsing json result: " + xhr.responseText); |
| 100 | + result = JSON.parse(xhr.responseText); |
| 101 | + } catch (e) { |
| 102 | + result = { |
| 103 | + code: 503, // Service Unavailable |
| 104 | + status: "Service Unavailable", |
| 105 | + error: "Connection error", |
| 106 | + errorCode: 2, // PlayFabErrorCode.ConnectionError |
| 107 | + errorMessage: xhr.responseText |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + result.CallBackTimeMS = new Date() - startTime; |
| 112 | + result.Request = request; |
| 113 | + result.CustomData = customData; |
| 114 | + |
| 115 | + if (result.code === 200) |
| 116 | + callback(result, null); |
| 117 | + else |
| 118 | + callback(null, result); |
| 119 | + } |
| 120 | + |
| 121 | + xhr.onerror = function () { |
| 122 | + if (callback == null) |
| 123 | + return; |
| 124 | + |
| 125 | + var result; |
| 126 | + try { |
| 127 | + result = JSON.parse(xhr.responseText); |
| 128 | + } catch (e) { |
| 129 | + result = { |
| 130 | + code: 503, // Service Unavailable |
| 131 | + status: "Service Unavailable", |
| 132 | + error: "Connection error", |
| 133 | + errorCode: 2, // PlayFabErrorCode.ConnectionError |
| 134 | + errorMessage: xhr.responseText |
| 135 | + }; |
| 136 | + } |
| 137 | + |
| 138 | + result.CallBackTimeMS = new Date() - startTime; |
| 139 | + result.Request = request; |
| 140 | + result.CustomData = customData; |
| 141 | + |
| 142 | + callback(null, result); |
| 143 | + } |
| 144 | + |
| 145 | + xhr.send(requestBody); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +PlayFab.buildIdentifier = "jbuild_javascriptsdk__sdk-slave2016-3_1"; |
| 151 | +PlayFab.sdkVersion = "1.30.180906"; |
| 152 | +PlayFab.GenerateErrorReport = function (error) { |
| 153 | + if (error == null) |
| 154 | + return ""; |
| 155 | + var fullErrors = error.errorMessage; |
| 156 | + for (var paramName in error.errorDetails) |
| 157 | + for (var msgIdx in error.errorDetails[paramName]) |
| 158 | + fullErrors += "\n" + paramName + ": " + error.errorDetails[paramName][msgIdx]; |
| 159 | + return fullErrors; |
| 160 | +}; |
| 161 | + |
| 162 | +PlayFab.LocalizationApi = { |
| 163 | + ForgetAllCredentials: function () { |
| 164 | + PlayFab._internalSettings.sessionTicket = null; |
| 165 | + PlayFab._internalSettings.entityToken = null; |
| 166 | + }, |
| 167 | + |
| 168 | + GetLanguageList: function (request, callback, customData, extraHeaders) { |
| 169 | + if (!PlayFab._internalSettings.entityToken) throw PlayFab._internalSettings.errorEntityToken; |
| 170 | + PlayFab._internalSettings.ExecuteRequest(PlayFab._internalSettings.GetServerUrl() + "/Locale/GetLanguageList", request, "X-EntityToken", PlayFab._internalSettings.entityToken, callback, customData, extraHeaders); |
| 171 | + }, |
| 172 | +}; |
| 173 | + |
| 174 | +var PlayFabLocalizationSDK = PlayFab.LocalizationApi; |
| 175 | + |
0 commit comments