@aqul/akinator-api
Version:
An Simple API for Akinator
79 lines (78 loc) • 2.85 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Akinator = void 0;
const Config_1 = require("./constants/Config");
const Request_1 = require("./functions/Request");
class Akinator {
constructor({ region = "en", childMode = false, config = {} }) {
this.step = 0;
this.progress = 0.0;
this.question = "";
this.isWin = false;
this.sugestion_name = "";
this.sugestion_desc = "";
this.sugestion_photo = "";
this.session = "";
this.signature = "";
this.baseUrl = "";
this.sid = 0;
this.config = {};
if (!Config_1.regions.includes(region)) {
throw new Error("Please insert a correct region!");
}
this.region = region;
this.childMode = childMode;
this.config = config;
}
async start() {
const { session, signature, question, baseUrl, sid } = await (0, Request_1.setupAki)(this.region, this.childMode, this.config);
if (!session || !signature || !question) {
throw new Error("Failed to get session and signature");
}
this.session = session;
this.signature = signature;
this.baseUrl = baseUrl;
this.sid = sid;
this.question = question;
}
async answer(answ) {
const response = await (0, Request_1.requestAki)(`${this.baseUrl}/answer`, {
step: this.step,
progression: this.progress,
sid: this.sid,
cm: this.childMode,
answer: answ,
step_last_proposition: this.step_last ?? "",
session: this.session,
signature: this.signature
}, this.config);
if (response.completion !== "OK") {
throw new Error("Failed making request, completion: " + response.completion);
}
if (response.id_proposition) {
this.sugestion_name = response.name_proposition;
this.sugestion_desc = response.description_proposition;
this.sugestion_photo = response.photo;
this.isWin = true;
}
else {
this.step = parseInt(response.step);
this.progress = parseFloat(response.progression);
this.question = response.question;
}
}
async cancelAnswer() {
const response = await (0, Request_1.requestAki)(`${this.baseUrl}/cancel_answer`, {
step: this.step,
progression: this.progress,
sid: this.sid,
cm: this.childMode,
session: this.session,
signature: this.signature
}, this.config);
this.step = parseInt(response.step);
this.progress = parseFloat(response.progression);
this.question = response.question;
}
}
exports.Akinator = Akinator;