UNPKG

ogame-bot-api

Version:
220 lines 9.69 kB
"use strict"; 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 }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LobbyApi = void 0; var axios_1 = require("axios"); var GameApi_1 = require("../game/GameApi"); var axios = axios_1.default.create({ withCredentials: true, baseURL: 'https://lobby.ogame.gameforge.com/api', }); var sortAccountByLastLogin = function (accounts) { return accounts.sort(function (a, b) { var da = new Date(a.lastLogin); var db = new Date(b.lastLogin); return db.getTime() - da.getTime(); }); }; /** * The LobbyApi is used to login, get servers informations and load the GameApi */ var LobbyApi = /** @class */ (function () { function LobbyApi() { } /** * * @param email Email of the ogame account * @param password Password of the ogame account */ LobbyApi.prototype.login = function (email, password) { return __awaiter(this, void 0, void 0, function () { var response, token_1, e_1; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); return [4 /*yield*/, axios.post('https://gameforge.com/api/v1/auth/thin/sessions', { autoGameAccountCreation: false, gameEnvironmentId: "0a31d605-ffaf-43e7-aa02-d06df7116fc8", gfLang: "en", identity: email, locale: "en_GB", password: password, platformGameId: "1dfd8e7e-6e1a-4eb1-8c64-03c3b62efd2f", })]; case 1: response = _a.sent(); token_1 = response.data.token; this.cookie = response.headers['set-cookie'][0]; axios.interceptors.request.use(function (config) { config.headers.cookie = _this.cookie; config.headers.authorization = "Bearer " + token_1; return config; }); return [4 /*yield*/, this.refreshUser()]; case 2: _a.sent(); return [3 /*break*/, 4]; case 3: e_1 = _a.sent(); throw new Error('Failed to login'); case 4: return [2 /*return*/]; } }); }); }; /** * @returns Returns the gloabl user informations */ LobbyApi.prototype.me = function () { return __awaiter(this, void 0, void 0, function () { var data; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, axios.get('/users/me')]; case 1: data = (_a.sent()).data; return [2 /*return*/, data]; } }); }); }; /** * @returns Returns the user accounts. The user accounts are the * differents server which the user is already connected at least one time */ LobbyApi.prototype.getAccounts = function () { return __awaiter(this, void 0, void 0, function () { var data; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, axios.get('/users/me/accounts')]; case 1: data = (_a.sent()).data; return [2 /*return*/, sortAccountByLastLogin(data)]; } }); }); }; /** * @returns Returns the list of all ogame servers. */ LobbyApi.prototype.listServers = function () { return __awaiter(this, void 0, void 0, function () { var data; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, axios.get('/servers')]; case 1: data = (_a.sent()).data; return [2 /*return*/, data]; } }); }); }; /** * @hidden */ LobbyApi.prototype.refreshUser = function () { return __awaiter(this, void 0, void 0, function () { var _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: _a = this; return [4 /*yield*/, this.me()]; case 1: _a.user = _c.sent(); console.log("Connected with " + this.user.email); _b = this.user; return [4 /*yield*/, this.getAccounts()]; case 2: _b.accounts = _c.sent(); return [2 /*return*/]; } }); }); }; /** * Select last account played by the user */ LobbyApi.prototype.selectLastPlayedAccount = function () { return __awaiter(this, void 0, void 0, function () { var accounts, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = sortAccountByLastLogin; return [4 /*yield*/, this.getAccounts()]; case 1: accounts = _a.apply(void 0, [_b.sent()]); if (accounts.length === 0) throw new Error("User don't have accounts"); return [2 /*return*/, accounts[0]]; } }); }); }; /** * Load the GameApi * @param account The selected account to load the game */ LobbyApi.prototype.loadGame = function (account) { return __awaiter(this, void 0, void 0, function () { var login, api; return __generator(this, function (_a) { switch (_a.label) { case 0: console.log("Loading game api with server s" + account.server.number + "-" + account.server.language); return [4 /*yield*/, axios.get("/users/me/loginLink?id=" + account.id + "&server[language]=" + account.server.language + "&server[number]=" + account.server.number + "&clickedButton=account_list")]; case 1: login = (_a.sent()).data; api = new GameApi_1.GameApi(this.cookie, account, login.url); return [4 /*yield*/, api.start()]; case 2: _a.sent(); return [2 /*return*/, api]; } }); }); }; return LobbyApi; }()); exports.LobbyApi = LobbyApi; //# sourceMappingURL=LobbyApi.js.map