///
declare var apiHost: string;
import * as ko from "knockout";
import { App } from "../app";
import { appConfig } from "../appconfig";
import { _ } from "../languagemanager";
import { AccountManager } from "../services/accountManager";
import { TableView } from "../table/tableview";
import { SimplePopup } from "./simplepopup";
declare var app: App;
export class JoinTablePopup implements KnockoutValidationGroup {
public buyin: KnockoutObservable;
public ticketCode: KnockoutObservable;
public minBuyin: KnockoutObservable;
public maxBuyin: KnockoutObservable;
public minBet: KnockoutObservable;
public maxBet: KnockoutObservable;
public accountTotal: KnockoutObservable;
public tableName: KnockoutObservable;
public errors: KnockoutValidationErrors;
public isValid: () => boolean;
public errorMessage: KnockoutObservable;
public seatNumber: KnockoutObservable;
public tableView: KnockoutObservable;
public loading: KnockoutObservable;
public allowUsePersonalAccount: KnockoutObservable;
public allowTickets: KnockoutObservable;
public constructor() {
this.buyin = ko.observable()
.extend({ required: appConfig.joinTable.allowUsePersonalAccount, validatable: true });
this.ticketCode = ko.observable()
.extend({ required: appConfig.joinTable.allowTickets, validatable: true });
this.tableView = ko.observable();
this.seatNumber = ko.observable(0);
this.accountTotal = ko.observable(0);
this.loading = ko.observable(false);
this.tableName = ko.observable();
this.minBuyin = ko.observable(0);
this.maxBuyin = ko.observable(0);
this.minBet = ko.observable(0);
this.maxBet = ko.observable(0);
this.errors = ko.validation.group(this);
this.errorMessage = ko.observable();
this.allowUsePersonalAccount = ko.observable(appConfig.joinTable.allowUsePersonalAccount);
this.allowTickets = ko.observable(appConfig.joinTable.allowTickets);
}
public async shown() {
const self = this;
const manager = new AccountManager();
self.loading(true);
if (appConfig.joinTable.allowUsePersonalAccount) {
try {
const data = await manager.getAccount();
self.loading(false);
if (data.Status === "Ok") {
const personalAccountData = data.Data;
const tableData = self.tableView().model;
let balance = 0;
const currencyId = tableData.CurrencyId;
if (currencyId === 1) {
balance = personalAccountData.RealMoney;
} else {
balance = personalAccountData.GameMoney;
}
self.accountTotal(balance);
self.tableName(tableData.TableName);
self.minBet(tableData.SmallBlind);
self.maxBet(tableData.BigBlind);
self.updateEntries();
if (appConfig.game.seatMode) {
app.executeCommand("page.seats");
}
} else {
SimplePopup.display(_("joinTable.caption"), _("errors." + data.Status));
}
} catch (e) {
self.loading(false);
SimplePopup.display(_("joinTable.caption"), _("joinTable.joinError"));
}
} else {
self.loading(false);
}
}
public async confirm() {
const self = this;
const isValid = this.isValid();
if (!isValid) {
this.errors.showAllMessages(true);
return;
}
if (appConfig.joinTable.allowUsePersonalAccount) {
if (this.buyin() < this.minBuyin()) {
this.buyin.setError(_("joinTable.putMoreMoney"));
return;
}
if (this.buyin() > this.maxBuyin()) {
this.buyin.setError(_("joinTable.putLessMoney"));
return;
}
if (this.accountTotal() < this.minBuyin()) {
this.buyin.setError(_("errors.NotSufficiendFunds"));
return;
}
if (this.accountTotal() < this.buyin()) {
this.buyin.setError(_("errors.NotSufficiendFunds"));
return;
}
}
const seat = this.seatNumber();
const amount = this.buyin();
const ticketCode = this.ticketCode();
this.loading(true);
const result = await this.tableView().sit(seat, amount, ticketCode);
if (result.success) {
self.loading(false);
app.closePopup();
self.ticketCode(null);
return;
}
const { status, minimalAmount } = result;
self.loading(false);
if (status === "AmountTooLow") {
self.tableView().minimalPlayerBuyIn(minimalAmount);
self.updateEntries();
self.buyin.setError(_("errors." + status));
} else {
SimplePopup.display(_("joinTable.caption"), _("errors." + status));
}
self.ticketCode(null);
}
private updateEntries() {
const self = this;
const tableView = self.tableView();
const tableData = self.tableView().model;
const baseMinimalBuyIn = tableView.minimalBuyIn() * tableData.BigBlind;
self.minBuyin(Math.max(baseMinimalBuyIn, tableView.minimalPlayerBuyIn()));
self.maxBuyin(20 * baseMinimalBuyIn);
self.buyin(Math.max(2 * baseMinimalBuyIn, tableView.minimalPlayerBuyIn()));
if (self.maxBuyin() < tableView.minimalPlayerBuyIn()) {
self.maxBuyin(tableView.minimalPlayerBuyIn());
self.buyin(tableView.minimalPlayerBuyIn());
}
}
}