UNPKG

@jovo-community/plugin-playfab

Version:

A Jovo Framework plugin for PlayFab LiveOps game backend.

493 lines 26.5 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; import { PlayFab, PlayFabAdmin, PlayFabServer } from 'playfab-sdk'; import { promisify } from 'util'; export var LoginStatus; (function (LoginStatus) { LoginStatus["NewUser"] = "newUser"; LoginStatus["ExistingUser"] = "existingUser"; LoginStatus["Error"] = "error"; })(LoginStatus || (LoginStatus = {})); export var UserDataPermission; (function (UserDataPermission) { UserDataPermission["Private"] = "Private"; UserDataPermission["Public"] = "Public"; })(UserDataPermission || (UserDataPermission = {})); var ApiStatus; (function (ApiStatus) { ApiStatus["OK"] = "OK"; })(ApiStatus || (ApiStatus = {})); var JovoPlayFab = /** @class */ (function () { function JovoPlayFab(config, jovo) { this.config = config; this.jovo = jovo; } Object.defineProperty(JovoPlayFab.prototype, "PlayFabServer", { get: function () { return PlayFabServer; }, enumerable: false, configurable: true }); Object.defineProperty(JovoPlayFab.prototype, "PlayFabAdmin", { get: function () { return PlayFabAdmin; }, enumerable: false, configurable: true }); Object.defineProperty(JovoPlayFab.prototype, "PlayFabId", { get: function () { var _a, _b, _c, _d; return (_d = (_c = (_b = (_a = this.jovo.$session.data) === null || _a === void 0 ? void 0 : _a.playfab) === null || _b === void 0 ? void 0 : _b.loginInfo) === null || _c === void 0 ? void 0 : _c.PlayFabId) !== null && _d !== void 0 ? _d : ''; }, enumerable: false, configurable: true }); JovoPlayFab.prototype.init = function () { PlayFab.settings.titleId = this.config.titleId; PlayFab.settings.developerSecretKey = this.config.developerSecretKey; if (!this.jovo.$session.data.playfab) { this.jovo.$session.data.playfab = {}; } if (!this.jovo.$session.data.playfab.profile) { this.jovo.$session.data.playfab.profile = {}; } }; JovoPlayFab.prototype.login = function () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; return __awaiter(this, void 0, void 0, function () { var loginRequest, loginWithCustomID, result, profile, count, profile_1, profile, key, data, error_1; return __generator(this, function (_l) { switch (_l.label) { case 0: console.debug('JovoPlayFab.login'); if (!!((_c = (_b = (_a = this.jovo.$session.data) === null || _a === void 0 ? void 0 : _a.playfab) === null || _b === void 0 ? void 0 : _b.loginInfo) === null || _c === void 0 ? void 0 : _c.SessionTicket)) return [3 /*break*/, 17]; loginRequest = { ServerCustomId: this.jovo.$user.id, CreateAccount: true, CustomTags: { sessionId: this.jovo.$request.getSessionId() }, InfoRequestParameters: this.config.login.infoRequestParameters, }; loginWithCustomID = promisify(PlayFabServer.LoginWithServerCustomId); _l.label = 1; case 1: _l.trys.push([1, 16, , 17]); return [4 /*yield*/, loginWithCustomID(loginRequest)]; case 2: result = _l.sent(); if (!(result.status === ApiStatus.OK)) return [3 /*break*/, 15]; this.jovo.$session.data.playfab.loginInfo = result.data; if (!result.data.NewlyCreated) return [3 /*break*/, 10]; // new player this.jovo.$session.data.playfab.loginStatus = LoginStatus.NewUser; if (!this.config.login.onNewProfile) return [3 /*break*/, 9]; return [4 /*yield*/, this.config.login.onNewProfile(this.jovo)]; case 3: profile = _l.sent(); return [4 /*yield*/, this.updateProfile(profile)]; case 4: _l.sent(); if (!!this.jovo.$session.data.playfab.isDisplayNameUpdated) return [3 /*break*/, 9]; count = 0; _l.label = 5; case 5: count++; return [4 /*yield*/, this.config.login.onNewProfile(this.jovo)]; case 6: profile_1 = _l.sent(); return [4 /*yield*/, this.updateProfile({ displayName: profile_1.displayName })]; case 7: _l.sent(); _l.label = 8; case 8: if (count < this.config.login.maxNewProfileRetries && !this.jovo.$session.data.playfab.isDisplayNameUpdated) return [3 /*break*/, 5]; _l.label = 9; case 9: return [3 /*break*/, 15]; case 10: // existing player this.jovo.$session.data.playfab.loginStatus = LoginStatus.ExistingUser; profile = { displayName: (_e = (_d = result.data.InfoResultPayload) === null || _d === void 0 ? void 0 : _d.PlayerProfile) === null || _e === void 0 ? void 0 : _e.DisplayName, avatarUrl: (_g = (_f = result.data.InfoResultPayload) === null || _f === void 0 ? void 0 : _f.PlayerProfile) === null || _g === void 0 ? void 0 : _g.AvatarUrl, }; key = this.config.login.extendedProfileKey; if (!key) return [3 /*break*/, 14]; data = void 0; if (!((_j = (_h = this.jovo.$session.data.playfab.loginInfo.InfoResultPayload) === null || _h === void 0 ? void 0 : _h.UserData) === null || _j === void 0 ? void 0 : _j[key])) return [3 /*break*/, 11]; data = this.parseUserDataRecords((_k = this.jovo.$session.data.playfab.loginInfo.InfoResultPayload) === null || _k === void 0 ? void 0 : _k.UserData); return [3 /*break*/, 13]; case 11: return [4 /*yield*/, this.getUserData(key)]; case 12: data = _l.sent(); _l.label = 13; case 13: if (data && data[key]) { profile.extendedProfile = data[key]; } _l.label = 14; case 14: this.jovo.$session.data.playfab.profile = profile; _l.label = 15; case 15: return [3 /*break*/, 17]; case 16: error_1 = _l.sent(); console.error('PlayFabServer.LoginWithCustomID', error_1); this.jovo.$session.data.playfab.loginStatus = LoginStatus.Error; return [3 /*break*/, 17]; case 17: return [2 /*return*/]; } }); }); }; JovoPlayFab.prototype.updateProfile = function (profile) { return __awaiter(this, void 0, void 0, function () { var result, updateUserTitleDisplayName, updateAvatarUrl, promises, updateUserTitleDisplayNamePromise, updateAvatarUrlPromise, results, successCount, _i, results_1, result_1, data; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: console.debug('JovoPlayFab.updateProfile'); result = false; updateUserTitleDisplayName = promisify(PlayFabAdmin.UpdateUserTitleDisplayName); updateAvatarUrl = promisify(PlayFabServer.UpdateAvatarUrl); promises = []; if (!profile.displayName && !profile.avatarUrl) { return [2 /*return*/, result]; } if (profile.displayName) { updateUserTitleDisplayNamePromise = updateUserTitleDisplayName({ PlayFabId: this.PlayFabId, DisplayName: profile.displayName, }); promises.push(updateUserTitleDisplayNamePromise); } if (profile.avatarUrl) { updateAvatarUrlPromise = updateAvatarUrl({ PlayFabId: this.PlayFabId, ImageUrl: profile.avatarUrl, }); promises.push(updateAvatarUrlPromise); } return [4 /*yield*/, Promise.allSettled(promises)]; case 1: results = _b.sent(); successCount = 0; for (_i = 0, results_1 = results; _i < results_1.length; _i++) { result_1 = results_1[_i]; if (result_1.status === 'fulfilled') { if (result_1.value.status === ApiStatus.OK) { successCount++; if (result_1.value.data.DisplayName) { profile.displayName = result_1.value.data.DisplayName; this.jovo.$session.data.playfab.isDisplayNameUpdated = true; } } } else { // rejected if (result_1.reason.error === 'NameNotAvailable') { this.jovo.$session.data.playfab.isDisplayNameUpdated = false; } } } if (!(successCount > 0)) return [3 /*break*/, 3]; this.jovo.$session.data.playfab.profile = profile; result = true; if (!(profile.extendedProfile && this.config.login.extendedProfileKey)) return [3 /*break*/, 3]; data = (_a = {}, _a[this.config.login.extendedProfileKey] = profile.extendedProfile, _a); return [4 /*yield*/, this.updateUserData(data)]; case 2: _b.sent(); _b.label = 3; case 3: return [2 /*return*/, result]; } }); }); }; JovoPlayFab.prototype.updateStat = function (statName, value) { return __awaiter(this, void 0, void 0, function () { var updatePlayerStatisticsRequest, updatePlayerStatistics, result, error_2; return __generator(this, function (_a) { switch (_a.label) { case 0: console.debug('JovoPlayFab.updateStat'); updatePlayerStatisticsRequest = { PlayFabId: this.PlayFabId, Statistics: [ { StatisticName: statName, Value: value, CustomTags: { sessionId: this.jovo.$request.getSessionId() }, }, ], }; updatePlayerStatistics = promisify(PlayFabServer.UpdatePlayerStatistics); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, updatePlayerStatistics(updatePlayerStatisticsRequest)]; case 2: result = _a.sent(); console.debug('PlayFabServer.UpdatePlayerStatistics', JSON.stringify(result, null, 2)); return [2 /*return*/, true]; case 3: error_2 = _a.sent(); console.error('PlayFabServer.UpdatePlayerStatistics', error_2); return [3 /*break*/, 4]; case 4: return [2 /*return*/, false]; } }); }); }; JovoPlayFab.prototype.getStat = function (statName) { return __awaiter(this, void 0, void 0, function () { var value, getPlayerStatisticsRequest, getPlayerStatistics, result, error_3; return __generator(this, function (_a) { switch (_a.label) { case 0: value = null; getPlayerStatisticsRequest = { PlayFabId: this.PlayFabId, StatisticNames: [statName], }; getPlayerStatistics = promisify(PlayFabServer.GetPlayerStatistics); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, getPlayerStatistics(getPlayerStatisticsRequest)]; case 2: result = _a.sent(); console.debug('PlayFabServer.GetPlayerStatistics', JSON.stringify(result, null, 2)); if (result.status === ApiStatus.OK && result.data.Statistics && result.data.Statistics.length > 0) { value = result.data.Statistics[0].Value; } return [3 /*break*/, 4]; case 3: error_3 = _a.sent(); console.error('PlayFabServer.GetPlayerStatistics', error_3); return [3 /*break*/, 4]; case 4: return [2 /*return*/, value]; } }); }); }; JovoPlayFab.prototype.getLeaderboard = function (statName) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function () { var promises, getLeaderboard, getLeaderboardAroundPlayer, results, allEntries, mappedEntries_1, combinedLeaderboard, _i, _d, entry, userData, error_4; var _this = this; return __generator(this, function (_e) { switch (_e.label) { case 0: console.debug('JovoPlayFab.getLeaderboard'); promises = []; if (this.config.leaderboard.topMax > 0) { getLeaderboard = promisify(PlayFabServer.GetLeaderboard); promises.push(getLeaderboard({ MaxResultsCount: this.config.leaderboard.topMax, ProfileConstraints: this.config.leaderboard.profileConstraints, StatisticName: statName, StartPosition: 0, })); } if (this.config.leaderboard.neighborMax > 0) { getLeaderboardAroundPlayer = promisify(PlayFabServer.GetLeaderboardAroundUser); promises.push(getLeaderboardAroundPlayer({ PlayFabId: this.PlayFabId, MaxResultsCount: this.config.leaderboard.neighborMax, ProfileConstraints: this.config.leaderboard.profileConstraints, StatisticName: statName, })); } _e.label = 1; case 1: _e.trys.push([1, 7, , 8]); return [4 /*yield*/, Promise.all(promises)]; case 2: results = _e.sent(); allEntries = []; if (results.length === 2) { allEntries = __spreadArray(__spreadArray([], ((_a = results[0].data.Leaderboard) !== null && _a !== void 0 ? _a : []), true), ((_b = results[1].data.Leaderboard) !== null && _b !== void 0 ? _b : []), true); } else { allEntries = (_c = results[0].data.Leaderboard) !== null && _c !== void 0 ? _c : []; } mappedEntries_1 = allEntries.map(function (item) { var currentPlayerPlayFabId = _this.jovo.$session.data.playfab.loginInfo.PlayFabId; item.IsCurrentPlayer = item.PlayFabId === currentPlayerPlayFabId; return item; }); combinedLeaderboard = mappedEntries_1.filter(function (item, index) { return mappedEntries_1.findIndex(function (i) { return i.PlayFabId === item.PlayFabId; }) === index; }); if (!(this.config.leaderboard.userDataKeys.length > 0)) return [3 /*break*/, 6]; _i = 0, _d = Object.values(combinedLeaderboard); _e.label = 3; case 3: if (!(_i < _d.length)) return [3 /*break*/, 6]; entry = _d[_i]; return [4 /*yield*/, this.getUserData(this.config.leaderboard.userDataKeys, entry.PlayFabId)]; case 4: userData = _e.sent(); entry.UserData = userData; _e.label = 5; case 5: _i++; return [3 /*break*/, 3]; case 6: return [2 /*return*/, combinedLeaderboard]; case 7: error_4 = _e.sent(); console.error('PlayFabServer Leaderboards', error_4); return [3 /*break*/, 8]; case 8: return [2 /*return*/, null]; } }); }); }; JovoPlayFab.prototype.updateUserData = function (data, permission) { if (permission === void 0) { permission = UserDataPermission.Public; } return __awaiter(this, void 0, void 0, function () { var updateUserDataRequest, updateUserData, result, error_5; return __generator(this, function (_a) { switch (_a.label) { case 0: console.debug('JovoPlayFab.updateUserData'); updateUserDataRequest = { PlayFabId: this.PlayFabId, Data: this.stringifyValues(data), Permission: permission, }; updateUserData = promisify(PlayFabServer.UpdateUserData); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, updateUserData(updateUserDataRequest)]; case 2: result = _a.sent(); console.debug('PlayFabServer.UpdateUserData', JSON.stringify(result, null, 2)); return [2 /*return*/, true]; case 3: error_5 = _a.sent(); console.error('PlayFabServer.UpdateUserData', error_5); return [3 /*break*/, 4]; case 4: return [2 /*return*/, false]; } }); }); }; JovoPlayFab.prototype.getUserData = function (keys, playFabId) { return __awaiter(this, void 0, void 0, function () { var value, getUserDataRequest, getUserData, result, error_6; return __generator(this, function (_a) { switch (_a.label) { case 0: console.debug('JovoPlayFab.getUserData'); if (!playFabId) { playFabId = this.PlayFabId; } getUserDataRequest = { PlayFabId: playFabId, Keys: Array.isArray(keys) ? keys : [keys], }; getUserData = promisify(PlayFabServer.GetUserData); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, getUserData(getUserDataRequest)]; case 2: result = _a.sent(); console.debug('PlayFabServer.GetUserData', JSON.stringify(result, null, 2)); if (result.status === ApiStatus.OK) { value = this.parseUserDataRecords(result.data.Data); } return [3 /*break*/, 4]; case 3: error_6 = _a.sent(); console.error('PlayFabServer.GetUserData', error_6); return [3 /*break*/, 4]; case 4: return [2 /*return*/, value]; } }); }); }; JovoPlayFab.prototype.stringifyValues = function (data) { var newData = {}; for (var _i = 0, _a = Object.entries(data); _i < _a.length; _i++) { var _b = _a[_i], k = _b[0], v = _b[1]; newData[k] = JSON.stringify(v); } return newData; }; JovoPlayFab.prototype.parseUserDataRecords = function (data) { var _a; var newData = {}; if (!data) { return newData; } for (var _i = 0, _b = Object.entries(data); _i < _b.length; _i++) { var _c = _b[_i], k = _c[0], v = _c[1]; var value = (_a = v === null || v === void 0 ? void 0 : v.Value) !== null && _a !== void 0 ? _a : ''; newData[k] = this.isJson(value) ? JSON.parse(value) : value.toString(); } return newData; }; JovoPlayFab.prototype.isJson = function (value) { try { JSON.parse(value); } catch (e) { return false; } return true; }; return JovoPlayFab; }()); export { JovoPlayFab }; //# sourceMappingURL=JovoPlayFab.js.map