ogame-bot-api
Version:
Api to create a bot for ogame
361 lines • 15 kB
JavaScript
"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.GameApi = void 0;
var axios_1 = require("axios");
var puppeteer = require("puppeteer");
var resources_1 = require("./parsing/resources");
var facilities_1 = require("./parsing/facilities");
var research_1 = require("./parsing/research");
var ship_1 = require("./parsing/ship");
var defenses_1 = require("./parsing/defenses");
var navigation_1 = require("./navigation");
/**
* The GameApi is used to interact with the game itself, create and list ressources/ship/research...
* You should use LobbyApi to load it, don't create it yourself
*/
var GameApi = /** @class */ (function () {
/**
* @hidden
*/
function GameApi(cookie, account, loginUrl) {
this.cookie = cookie;
this.loginUrl = loginUrl;
this.account = account;
this.axios = axios_1.default.create({
withCredentials: true,
baseURL: this.__getServerUrl(),
});
this.axios.interceptors.request.use(function (config) {
if (!config.headers.cookie) {
config.headers.cookie = '';
}
config.headers.cookie += cookie;
return config;
});
this.navigation = new navigation_1.Navigation(this.__getServerUrl());
}
/**
* Start the game api
*/
GameApi.prototype.start = function () {
return __awaiter(this, void 0, void 0, function () {
var _a, _b, _c, name, other, value;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
_a = this;
return [4 /*yield*/, puppeteer.launch()];
case 1:
_a.browser = _d.sent();
_b = this;
return [4 /*yield*/, this.browser.newPage()];
case 2:
_b.page = _d.sent();
_c = this.cookie.split('='), name = _c[0], other = _c[1];
value = other.split(';')[0];
return [4 /*yield*/, this.page.setCookie({
name: name,
value: value,
url: this.__getServerUrl(),
})];
case 3:
_d.sent();
return [4 /*yield*/, this.page.setViewport({
width: 1280,
height: 960,
})];
case 4:
_d.sent();
return [4 /*yield*/, this.page.goto(this.loginUrl)];
case 5:
_d.sent();
return [2 /*return*/];
}
});
});
};
GameApi.prototype.__getServerUrl = function () {
return "https://s" + this.account.server.number + "-" + this.account.server.language + ".ogame.gameforge.com";
};
/**
* Stop the game api. You should called this before exiting your program.
*/
GameApi.prototype.stop = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.browser.close()];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
/**
* List your ressources
*/
GameApi.prototype.resourcesList = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.navigation.goToHomePage(this.page)];
case 1:
_a.sent();
return [2 /*return*/, resources_1.loadResources(this.page)];
}
});
});
};
/**
* List your mines and storage
*/
GameApi.prototype.resourceFactoryList = function () {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.navigation.goToResourcePage(this.page)];
case 1:
_a.sent();
return [4 /*yield*/, resources_1.loadMinesAndStorage(this.page)];
case 2:
res = _a.sent();
return [2 /*return*/, res];
}
});
});
};
/**
* List your facilities
*/
GameApi.prototype.facilitiesList = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.navigation.goToFacilitiesPage(this.page)];
case 1:
_a.sent();
return [2 /*return*/, facilities_1.loadFacilities(this.page)];
}
});
});
};
/**
* List you research
*/
GameApi.prototype.researchList = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.navigation.goToResearchPage(this.page)];
case 1:
_a.sent();
return [2 /*return*/, research_1.loadResearch(this.page)];
}
});
});
};
/**
* List your ships
*/
GameApi.prototype.shipList = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.navigation.goToShipPage(this.page)];
case 1:
_a.sent();
return [2 /*return*/, ship_1.loadShips(this.page)];
}
});
});
};
/**
* List your defenses
*/
GameApi.prototype.defenseList = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.navigation.goToDefensePage(this.page)];
case 1:
_a.sent();
return [2 /*return*/, defenses_1.loadDefenses(this.page)];
}
});
});
};
/**
* Upgrade or create a building. Don't use it for ships/defenses
* @param building The building to upgrade
*/
GameApi.prototype.makeUpgrade = function (building) {
return __awaiter(this, void 0, void 0, function () {
var resources;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.resourcesList()];
case 1:
resources = _a.sent();
if (!this.canUpgrade(building, resources)) {
throw new Error("Can't upgrade");
}
return [4 /*yield*/, this.page.goto(building.upgrade.url)];
case 2:
_a.sent();
return [2 /*return*/];
}
});
});
};
GameApi.prototype.haveEnoughResource = function (upgrade, resources, count) {
if (count === void 0) { count = 1; }
return !Object.keys(upgrade.costs).some(function (resource) {
if (resource === 'energy')
return false;
if (resources[resource] < upgrade.costs[resource] * count) {
return true;
}
else {
return false;
}
});
};
/**
* Check if you can upgrade a building
* @param building the building to upgrade
* @param resources Optional parameter, if provided it will speed up the fonction
*/
GameApi.prototype.canUpgrade = function (building, resources) {
return __awaiter(this, void 0, void 0, function () {
var upgrade;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!!resources) return [3 /*break*/, 2];
return [4 /*yield*/, this.resourcesList()];
case 1:
resources = _a.sent();
_a.label = 2;
case 2:
upgrade = building.upgrade;
return [2 /*return*/, upgrade.url && this.haveEnoughResource(upgrade, resources)];
}
});
});
};
/**
* Check if you can create a certain amount of ship/defense
* @param item Ship or Defense
* @param count The number of items you wants to create
* @param resources Optional parameter, if provided it will speed up the fonction
*/
GameApi.prototype.canCreate = function (item, count, resources) {
if (count === void 0) { count = 1; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!!resources) return [3 /*break*/, 2];
return [4 /*yield*/, this.resourcesList()];
case 1:
resources = _a.sent();
_a.label = 2;
case 2: return [2 /*return*/, this.haveEnoughResource(item.upgrade, resources, count) && (item.status === 'on' || item.status === 'active')];
}
});
});
};
/**
* Create a ship
* @param ship ship to create
* @param count number of ship to create
*/
GameApi.prototype.createShip = function (ship, count) {
if (count === void 0) { count = 1; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.canCreate(ship, count)];
case 1:
if (!(_a.sent())) {
throw new Error("Not enough resources");
}
return [4 /*yield*/, this.navigation.goToShipPage(this.page)];
case 2:
_a.sent();
return [4 /*yield*/, ship_1.createShipFromPannel(this.page, ship, count)];
case 3:
_a.sent();
return [2 /*return*/];
}
});
});
};
/**
* Create a defense
* @param defense defense to create
* @param count number of defense to create
*/
GameApi.prototype.createDefense = function (defense, count) {
if (count === void 0) { count = 1; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.canCreate(defense, count)];
case 1:
if (!(_a.sent())) {
throw new Error("Not enough resources");
}
return [4 /*yield*/, this.navigation.goToDefensePage(this.page)];
case 2:
_a.sent();
return [4 /*yield*/, defenses_1.createDefenseFromPannel(this.page, defense, count)];
case 3:
_a.sent();
return [2 /*return*/];
}
});
});
};
return GameApi;
}());
exports.GameApi = GameApi;
//# sourceMappingURL=GameApi.js.map