haxjs
Version:
haxball headless api written in typescript
791 lines • 37.4 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.Room = void 0;
var logger_1 = require("./logger");
var big_browser_1 = require("./big-browser");
var stadium_1 = require("./stadium");
var Room = (function () {
function Room(config) {
this.config = config;
}
Room.prototype.openNewTab = function () {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
logger_1.Logger.begin('opening new tab');
_a = this;
return [4, big_browser_1.BigBrowser.getNewPage()];
case 1:
_a.page = _b.sent();
logger_1.Logger.end();
return [2];
}
});
});
};
Room.prototype.exposeFunctions = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
logger_1.Logger.begin('exposing callback functions');
return [4, this.page.exposeFunction('onPlayerJoin', function (player) {
_this.onPlayerJoin && _this.onPlayerJoin(player);
})];
case 1:
_a.sent();
return [4, this.page.exposeFunction('onPlayerLeave', function (player) {
_this.onPlayerLeave && _this.onPlayerLeave(player);
})];
case 2:
_a.sent();
return [4, this.page.exposeFunction('onTeamVictory', function (scores) {
_this.onTeamVictory && _this.onTeamVictory(scores);
})];
case 3:
_a.sent();
return [4, this.page.exposeFunction('onPlayerChat', function (player, message) {
_this.onPlayerChat && _this.onPlayerChat(player, message);
})];
case 4:
_a.sent();
return [4, this.page.exposeFunction('onPlayerBallKick', function (player) {
_this.onPlayerBallKick && _this.onPlayerBallKick(player);
})];
case 5:
_a.sent();
return [4, this.page.exposeFunction('onTeamGoal', function (team) {
_this.onTeamGoal && _this.onTeamGoal(team);
})];
case 6:
_a.sent();
return [4, this.page.exposeFunction('onGameStart', function (byPlayer) {
_this.onGameStart && _this.onGameStart(byPlayer);
})];
case 7:
_a.sent();
return [4, this.page.exposeFunction('onGameStop', function (byPlayer) {
_this.onGameStop && _this.onGameStop(byPlayer);
})];
case 8:
_a.sent();
return [4, this.page.exposeFunction('onPlayerAdminChange', function (changedPlayer, byPlayer) {
_this.onPlayerAdminChange &&
_this.onPlayerAdminChange(changedPlayer, byPlayer);
})];
case 9:
_a.sent();
return [4, this.page.exposeFunction('onPlayerTeamChange', function (changedPlayer, byPlayer) {
_this.onPlayerTeamChange &&
_this.onPlayerTeamChange(changedPlayer, byPlayer);
})];
case 10:
_a.sent();
return [4, this.page.exposeFunction('onPlayerKicked', function (kickedPlayer, reason, ban, byPlayer) {
_this.onPlayerKicked &&
_this.onPlayerKicked(kickedPlayer, reason, ban, byPlayer);
})];
case 11:
_a.sent();
return [4, this.page.exposeFunction('onGameTick', function () {
_this.onGameTick && _this.onGameTick();
})];
case 12:
_a.sent();
return [4, this.page.exposeFunction('onGamePause', function (byPlayer) {
_this.onGamePause && _this.onGamePause(byPlayer);
})];
case 13:
_a.sent();
return [4, this.page.exposeFunction('onGameUnpause', function (byPlayer) {
_this.onGameUnpause && _this.onGameUnpause(byPlayer);
})];
case 14:
_a.sent();
return [4, this.page.exposeFunction('onPositionsReset', function () {
_this.onPositionsReset && _this.onPositionsReset();
})];
case 15:
_a.sent();
return [4, this.page.exposeFunction('onPlayerActivity', function (player) {
_this.onPlayerActivity && _this.onPlayerActivity(player);
})];
case 16:
_a.sent();
return [4, this.page.exposeFunction('onStadiumChange', function (newStadiumName, byPlayer) {
_this.onStadiumChange &&
_this.onStadiumChange(newStadiumName, byPlayer);
})];
case 17:
_a.sent();
return [4, this.page.exposeFunction('onKickRateLimitSet', function (min, rate, burst, byPlayer) {
_this.onKickRateLimitSet &&
_this.onKickRateLimitSet(min, rate, burst, byPlayer);
})];
case 18:
_a.sent();
logger_1.Logger.end();
return [2];
}
});
});
};
Room.prototype.createRoom = function () {
return __awaiter(this, void 0, void 0, function () {
var _a, iframehandle, iframe, failed, link;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
logger_1.Logger.begin('creating room');
return [4, this.page.goto('https://www.haxball.com/headless', {
waitUntil: 'networkidle2'
})];
case 1:
_b.sent();
return [4, this.page.evaluate(function (serializedConfig) {
window._room = window.HBInit(JSON.parse(serializedConfig));
}, JSON.stringify(this.config))];
case 2:
_b.sent();
return [4, this.page.evaluate(function () {
window._room.onPlayerJoin = function (player) {
window.onPlayerJoin(player);
};
window._room.onPlayerLeave = function (player) {
window.onPlayerLeave(player);
};
window._room.onTeamVictory = function (scores) {
window.onTeamVictory(scores);
};
window._room.onPlayerChat = function (player, message) {
window.onPlayerChat(player, message);
return false;
};
window._room.onPlayerBallKick = function (player) {
window.onPlayerBallKick(player);
};
window._room.onTeamGoal = function (team) {
window.onTeamGoal(team);
};
window._room.onGameStart = function (byPlayer) {
window.onGameStart(byPlayer);
};
window._room.onGameStop = function (byPlayer) {
window.onGameStop(byPlayer);
};
window._room.onPlayerAdminChange = function (changedPlayer, byPlayer) {
window.onPlayerAdminChange(changedPlayer, byPlayer);
};
window._room.onPlayerTeamChange = function (changedPlayer, byPlayer) {
window.onPlayerTeamChange(changedPlayer, byPlayer);
};
window._room.onPlayerKicked = function (kickedPlayer, reason, ban, byPlayer) {
window.onPlayerKicked(kickedPlayer, reason, ban, byPlayer);
};
window._room.onGameTick = function () {
window.onGameTick();
};
window._room.onGamePause = function (byPlayer) {
window.onGamePause(byPlayer);
};
window._room.onGameUnpause = function (byPlayer) {
window.onGameUnpause(byPlayer);
};
window._room.onPositionsReset = function () {
window.onPositionsReset();
};
window._room.onPlayerActivity = function (player) {
window.onPlayerActivity(player);
};
window._room.onStadiumChange = function (newStadiumName, byPlayer) {
window.onStadiumChange(newStadiumName, byPlayer);
};
window._room.onKickRateLimitSet = function (min, rate, burst, byPlayer) {
window.onKickRateLimitSet(min, rate, burst, byPlayer);
};
})];
case 3:
_b.sent();
_a = this;
return [4, this.page.evaluate(function () {
return window._room.CollisionFlags;
})];
case 4:
_a.CollisionFlags = _b.sent();
return [4, this.page.$('iframe')];
case 5:
iframehandle = _b.sent();
return [4, (iframehandle === null || iframehandle === void 0 ? void 0 : iframehandle.contentFrame())];
case 6:
iframe = _b.sent();
return [4, (iframe === null || iframe === void 0 ? void 0 : iframe.waitForFunction(function () {
var recaptcha = document.querySelector('#recaptcha');
var link = document.querySelector('#roomlink');
if (recaptcha == null || link == null)
return false;
return recaptcha.clientHeight + link.clientHeight > 0;
}))];
case 7:
_b.sent();
return [4, (iframe === null || iframe === void 0 ? void 0 : iframe.evaluate(function () {
var recaptcha = document.querySelector('#recaptcha');
if (recaptcha == null)
return true;
return recaptcha.clientHeight > 0;
}))];
case 8:
failed = _b.sent();
if (failed) {
logger_1.Logger.end(true);
process.exit(1);
}
logger_1.Logger.end();
return [4, (iframe === null || iframe === void 0 ? void 0 : iframe.evaluate(function () {
var roomLink = document.querySelector('#roomlink a');
if (roomLink != null)
return roomLink.getAttribute('href');
return '';
}))];
case 9:
link = _b.sent();
this.onRoomLink && this.onRoomLink(link || '');
return [2];
}
});
});
};
Room.prototype.start = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.openNewTab()];
case 1:
_a.sent();
return [4, this.exposeFunctions()];
case 2:
_a.sent();
return [4, this.createRoom()];
case 3:
_a.sent();
return [2];
}
});
});
};
Room.prototype.sendChat = function (message, targetId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (targetId == undefined)
targetId = null;
return [4, this.page.evaluate(function (_a) {
var message = _a.message, targetId = _a.targetId;
window._room.sendChat(message, targetId);
}, { message: message, targetId: targetId })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.setPlayerAdmin = function (playerId, admin) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var playerId = _a.playerId, admin = _a.admin;
window._room.setPlayerAdmin(playerId, admin);
}, { playerId: playerId, admin: admin })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.setPlayerTeam = function (playerId, team) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var playerId = _a.playerId, team = _a.team;
window._room.setPlayerTeam(playerId, team);
}, { playerId: playerId, team: team })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.kickPlayer = function (playerId, reason, ban) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var playerId = _a.playerId, reason = _a.reason, ban = _a.ban;
window._room.kickPlayer(playerId, reason, ban);
}, { playerId: playerId, reason: reason, ban: ban })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.clearBan = function (playerId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var playerId = _a.playerId;
window._room.clearBan(playerId);
}, { playerId: playerId })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.clearBans = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function () {
window._room.clearBans();
})];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.setScoreLimit = function (limit) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var limit = _a.limit;
window._room.setScoreLimit(limit);
}, { limit: limit })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.setTimeLimit = function (limitInMinutes) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var limitInMinutes = _a.limitInMinutes;
window._room.setTimeLimit(limitInMinutes);
}, { limitInMinutes: limitInMinutes })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.setCustomStadium = function (stadiumFileContents) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!stadium_1.Stadium.isValid(stadiumFileContents)) return [3, 2];
return [4, this.page.evaluate(function (_a) {
var stadiumFileContents = _a.stadiumFileContents;
try {
window._room.setCustomStadium(stadiumFileContents);
return true;
}
catch (_) {
return false;
}
}, { stadiumFileContents: stadiumFileContents })];
case 1: return [2, _a.sent()];
case 2: return [2, false];
}
});
});
};
Room.prototype.setDefaultStadium = function (stadiumName) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var stadiumName = _a.stadiumName;
window._room.setDefaultStadium(stadiumName);
}, { stadiumName: stadiumName })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.setTeamsLock = function (locked) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var locked = _a.locked;
window._room.setTeamsLock(locked);
}, { locked: locked })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.setTeamColors = function (team, angle, textColor, colors) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var team = _a.team, angle = _a.angle, textColor = _a.textColor, colors = _a.colors;
window._room.setTeamColors(team, angle, textColor, colors);
}, { team: team, angle: angle, textColor: textColor, colors: colors })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.startGame = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function () {
window._room.startGame();
})];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.stopGame = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function () {
window._room.stopGame();
})];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.pauseGame = function (pauseState) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var pauseState = _a.pauseState;
window._room.pauseGame(pauseState);
}, { pauseState: pauseState })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.getPlayer = function (playerId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var playerId = _a.playerId;
return window._room.getPlayer(playerId);
}, { playerId: playerId })];
case 1: return [2, _a.sent()];
}
});
});
};
Room.prototype.getPlayerList = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function () {
return window._room.getPlayerList();
})];
case 1: return [2, _a.sent()];
}
});
});
};
Room.prototype.getScores = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function () {
return window._room.getScores();
})];
case 1: return [2, _a.sent()];
}
});
});
};
Room.prototype.getBallPosition = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function () {
return window._room.getBallPosition();
})];
case 1: return [2, _a.sent()];
}
});
});
};
Room.prototype.startRecording = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function () {
window._room.startRecording();
})];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.stopRecording = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function () {
return window._room.stopRecording();
})];
case 1: return [2, _a.sent()];
}
});
});
};
Room.prototype.setPassword = function (password) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var password = _a.password;
window._room.setPassword(password);
}, { password: password })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.setRequireRecaptcha = function (required) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var required = _a.required;
window._room.setRequireRecaptcha(required);
}, { required: required })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.reorderPlayers = function (playerIdList, moveToTop) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var playerIdList = _a.playerIdList, moveToTop = _a.moveToTop;
window._room.reorderPlayers(playerIdList, moveToTop);
}, { playerIdList: playerIdList, moveToTop: moveToTop })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.sendAnnouncement = function (msg, targetId, color, style, sound) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (targetId == undefined)
targetId = null;
if (color == undefined)
color = null;
if (style == undefined)
style = null;
if (sound == undefined)
sound = null;
return [4, this.page.evaluate(function (_a) {
var msg = _a.msg, targetId = _a.targetId, color = _a.color, style = _a.style, sound = _a.sound;
window._room.sendAnnouncement(msg, targetId, color, style, sound);
}, { msg: msg, targetId: targetId, color: color, style: style, sound: sound })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.setKickRateLimit = function (min, rate, burst) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var min = _a.min, rate = _a.rate, burst = _a.burst;
window._room.setKickRateLimit(min, rate, burst);
}, { min: min, rate: rate, burst: burst })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.setPlayerAvatar = function (playerId, avatar) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var playerId = _a.playerId, avatar = _a.avatar;
window._room.setPlayerAvatar(playerId, avatar);
}, { playerId: playerId, avatar: avatar })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.setDiscProperties = function (discIndex, properties) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var discIndex = _a.discIndex, properties = _a.properties;
window._room.setDiscProperties(discIndex, properties);
}, { discIndex: discIndex, properties: properties })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.getDiscProperties = function (discIndex) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var discIndex = _a.discIndex;
return window._room.getDiscProperties(discIndex);
}, { discIndex: discIndex })];
case 1: return [2, _a.sent()];
}
});
});
};
Room.prototype.setPlayerDiscProperties = function (playerId, properties) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var playerId = _a.playerId, properties = _a.properties;
window._room.setPlayerDiscProperties(playerId, properties);
}, { playerId: playerId, properties: properties })];
case 1:
_a.sent();
return [2];
}
});
});
};
Room.prototype.getPlayerDiscProperties = function (playerId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function (_a) {
var playerId = _a.playerId;
return window._room.getPlayerDiscProperties(playerId);
}, { playerId: playerId })];
case 1: return [2, _a.sent()];
}
});
});
};
Room.prototype.getDiscCount = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.page.evaluate(function () {
return window._room.getDiscCount();
})];
case 1: return [2, _a.sent()];
}
});
});
};
return Room;
}());
exports.Room = Room;
//# sourceMappingURL=room.js.map