|
| 1 | +var PlayFab = typeof PlayFab != 'undefined' ? PlayFab : {}; |
| 2 | + |
| 3 | +if(!PlayFab.settings) { |
| 4 | + PlayFab.settings = { |
| 5 | + titleID: null, |
| 6 | + developerSecretKey: null // For security reasons you must never expose this value to the client or players |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +if(!PlayFab._internalSettings) { |
| 11 | + PlayFab._internalSettings = { |
| 12 | + sessionTicket: null, |
| 13 | + sdkVersion: "0.0.151019", |
| 14 | + apiVersion: "1.7.20151019", |
| 15 | + productionServerUrl: ".playfabapi.com", |
| 16 | + logicServerUrl: null, |
| 17 | + |
| 18 | + getServerUrl: function () { |
| 19 | + return "https://" + PlayFab.settings.titleId + PlayFab._internalSettings.productionServerUrl; |
| 20 | + }, |
| 21 | + |
| 22 | + getLogicServerUrl: function () { |
| 23 | + return PlayFab._internalSettings.logicServerUrl; |
| 24 | + }, |
| 25 | + |
| 26 | + executeRequest: function (completeUrl, data, authkey, authValue, callback) { |
| 27 | + if (callback != null && typeof (callback) != "function") { |
| 28 | + throw "Callback must be null of a function"; |
| 29 | + } |
| 30 | + |
| 31 | + if (data == null) { |
| 32 | + data = {}; |
| 33 | + } |
| 34 | + |
| 35 | + var startTime = new Date(); |
| 36 | + |
| 37 | + var requestBody = JSON.stringify(data); |
| 38 | + |
| 39 | + var xhr = new XMLHttpRequest();completeUrl |
| 40 | + // window.console.log("URL: " + completeUrl); |
| 41 | + xhr.open("POST", completeUrl, true); |
| 42 | + |
| 43 | + xhr.setRequestHeader('Content-Type', 'application/json'); |
| 44 | + |
| 45 | + if (authkey != null) |
| 46 | + xhr.setRequestHeader(authkey, authValue); |
| 47 | + |
| 48 | + xhr.setRequestHeader('X-PlayFabSDK', "JavaScriptSDK-" + PlayFab._internalSettings.sdkVersion + "-" + PlayFab._internalSettings.apiVersion); |
| 49 | + |
| 50 | + xhr.onloadend = function () { |
| 51 | + if (callback == null) |
| 52 | + return; |
| 53 | + |
| 54 | + var result = null; |
| 55 | + try { |
| 56 | + // window.console.log("parsing json result: " + xhr.responseText); |
| 57 | + result = JSON.parse(xhr.responseText); |
| 58 | + } catch (e) { |
| 59 | + result = { |
| 60 | + code: 503, // Service Unavailable |
| 61 | + status: "Service Unavailable", |
| 62 | + error: "Connection error", |
| 63 | + errorCode: 2, // PlayFabErrorCode.ConnectionError |
| 64 | + errorMessage: xhr.responseText |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + result.CallBackTimeMS = new Date() - startTime; |
| 69 | + |
| 70 | + if (result.code == 200) |
| 71 | + callback(result.data, null); |
| 72 | + else |
| 73 | + callback(null, result); |
| 74 | + } |
| 75 | + |
| 76 | + xhr.onerror = function () { |
| 77 | + if (callback == null) |
| 78 | + return; |
| 79 | + |
| 80 | + var result = null; |
| 81 | + try { |
| 82 | + result = JSON.parse(xhr.responseText); |
| 83 | + } catch (e) { |
| 84 | + result = { |
| 85 | + code: 503, // Service Unavailable |
| 86 | + status: "Service Unavailable", |
| 87 | + error: "Connection error", |
| 88 | + errorCode: 2, // PlayFabErrorCode.ConnectionError |
| 89 | + errorMessage: xhr.responseText |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + result.CallBackTimeMS = new Date() - startTime; |
| 94 | + callback(null, result); |
| 95 | + } |
| 96 | + |
| 97 | + xhr.send(requestBody); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +PlayFab.MatchmakerApi = { |
| 103 | + AuthUser: function (request, callback) { |
| 104 | + if (PlayFab.settings.developerSecretKey == null) throw "Must have PlayFab.settings.developerSecretKey set to call this method"; |
| 105 | + |
| 106 | + PlayFab._internalSettings.executeRequest(PlayFab._internalSettings.getServerUrl() + "/Matchmaker/AuthUser", request, "X-SecretKey", PlayFab.settings.developerSecretKey, callback); |
| 107 | + }, |
| 108 | + |
| 109 | + PlayerJoined: function (request, callback) { |
| 110 | + if (PlayFab.settings.developerSecretKey == null) throw "Must have PlayFab.settings.developerSecretKey set to call this method"; |
| 111 | + |
| 112 | + PlayFab._internalSettings.executeRequest(PlayFab._internalSettings.getServerUrl() + "/Matchmaker/PlayerJoined", request, "X-SecretKey", PlayFab.settings.developerSecretKey, callback); |
| 113 | + }, |
| 114 | + |
| 115 | + PlayerLeft: function (request, callback) { |
| 116 | + if (PlayFab.settings.developerSecretKey == null) throw "Must have PlayFab.settings.developerSecretKey set to call this method"; |
| 117 | + |
| 118 | + PlayFab._internalSettings.executeRequest(PlayFab._internalSettings.getServerUrl() + "/Matchmaker/PlayerLeft", request, "X-SecretKey", PlayFab.settings.developerSecretKey, callback); |
| 119 | + }, |
| 120 | + |
| 121 | + StartGame: function (request, callback) { |
| 122 | + if (PlayFab.settings.developerSecretKey == null) throw "Must have PlayFab.settings.developerSecretKey set to call this method"; |
| 123 | + |
| 124 | + PlayFab._internalSettings.executeRequest(PlayFab._internalSettings.getServerUrl() + "/Matchmaker/StartGame", request, "X-SecretKey", PlayFab.settings.developerSecretKey, callback); |
| 125 | + }, |
| 126 | + |
| 127 | + UserInfo: function (request, callback) { |
| 128 | + if (PlayFab.settings.developerSecretKey == null) throw "Must have PlayFab.settings.developerSecretKey set to call this method"; |
| 129 | + |
| 130 | + PlayFab._internalSettings.executeRequest(PlayFab._internalSettings.getServerUrl() + "/Matchmaker/UserInfo", request, "X-SecretKey", PlayFab.settings.developerSecretKey, callback); |
| 131 | + }, |
| 132 | + |
| 133 | +}; |
| 134 | + |
| 135 | +var PlayFabMatchmakerSDK = PlayFab.MatchmakerApi; |
0 commit comments