shardy
Version:
Framework for online games and applications on Node.js
124 lines • 4.75 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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Bot = void 0;
const ip_1 = __importDefault(require("ip"));
const net_1 = __importDefault(require("net"));
const ws_1 = require("ws");
const Logger_1 = require("./Logger");
const Client_1 = require("./Client");
const Transport_1 = require("./Transport");
const Commander_1 = require("./Commander");
const Connection_1 = require("./Connection");
const Tools_1 = require("./Tools");
const ID_LENGTH = 10;
class Bot {
constructor(host, port, transport, options, handshakeBody) {
this.host = host;
this.port = port;
this.transport = transport;
this.options = options;
this.handshakeBody = handshakeBody;
this.onDisconnect = () => { };
this.onConnect = () => { };
this.onReady = () => { };
this.log = new Logger_1.Logger([]);
this.isConnected = false;
}
start() {
return __awaiter(this, void 0, void 0, function* () {
const socket = this.transport === Transport_1.TransportType.TCP ? net_1.default.connect(this.port, this.host, () => this.onClientConnect()) : new ws_1.WebSocket(`ws://${this.host}:${this.port}`);
this.connection = new Connection_1.Connection(socket, this.transport);
this.connection.onConnect = () => this.onClientConnect();
});
}
command(command, data) {
return __awaiter(this, void 0, void 0, function* () {
return this.client.command(command, data);
});
}
fetch(request, data) {
return __awaiter(this, void 0, void 0, function* () {
return this.client.fetch(request, data);
});
}
request(request, callback, data) {
return __awaiter(this, void 0, void 0, function* () {
return this.client.request(request, callback, data);
});
}
response(request, data) {
return __awaiter(this, void 0, void 0, function* () {
this.client.response(request, data);
});
}
on(command, callback) {
return __awaiter(this, void 0, void 0, function* () {
this.client.on(command, callback);
});
}
off(command, callback) {
return __awaiter(this, void 0, void 0, function* () {
this.client.off(command, callback);
});
}
cancel(id) {
return __awaiter(this, void 0, void 0, function* () {
this.client.cancel(id);
});
}
onRequest(request, callback) {
return __awaiter(this, void 0, void 0, function* () {
this.client.onRequest(request, callback);
});
}
offRequest(request) {
return __awaiter(this, void 0, void 0, function* () {
this.client.offRequest(request);
});
}
handshake(body) {
return __awaiter(this, void 0, void 0, function* () {
this.client.handshake(body);
});
}
disconnect() {
return __awaiter(this, void 0, void 0, function* () {
this.client.disconnect();
});
}
destroy() {
return __awaiter(this, void 0, void 0, function* () {
this.client.destroy();
});
}
onClientConnect() {
const id = Tools_1.Tools.generateId(ID_LENGTH);
this.log.setLabel([id, ip_1.default.address()]);
this.isConnected = true;
this.client = new Client_1.Client(this.connection, id, this.log, {}, this.options, Commander_1.CommanderMode.Bot);
this.client.onDisconnect = (id, reason) => this.onClientDisconnect(reason);
this.client.onReady = () => this.onReady();
this.onConnect();
if (this.handshakeBody) {
this.handshake(this.handshakeBody);
}
}
onClientDisconnect(reason) {
this.isConnected = false;
this.onDisconnect(reason);
}
}
exports.Bot = Bot;
//# sourceMappingURL=Bot.js.map