2017-05-02 22:49:35 +00:00
|
|
|
/// <reference path="../common/UIComponent.ts"/>
|
|
|
|
|
2017-09-24 22:23:22 +00:00
|
|
|
module TK.SpaceTac.UI {
|
2017-05-02 22:49:35 +00:00
|
|
|
/**
|
|
|
|
* Dialog to load a saved game, or join an online one
|
|
|
|
*/
|
|
|
|
export class LoadDialog extends UIComponent {
|
2017-05-04 23:19:28 +00:00
|
|
|
saves: [string, string][] = []
|
|
|
|
save_selected = 0
|
|
|
|
save_name: UILabel
|
|
|
|
token_input: UITextInput
|
|
|
|
|
2017-05-02 22:49:35 +00:00
|
|
|
constructor(parent: MainMenu) {
|
|
|
|
super(parent, 1344, 566, "menu-load-bg");
|
|
|
|
|
2017-08-21 22:01:11 +00:00
|
|
|
let button = this.addButton(600, 115, () => this.paginateSave(-1), "common-arrow", 0, 0, "Scroll to newer saves");
|
|
|
|
button.angle = 180;
|
|
|
|
this.addButton(1038, 115, () => this.paginateSave(1), "common-arrow", 0, 0, "Scroll to older saves");
|
2017-05-04 23:19:28 +00:00
|
|
|
this.addButton(1224, 115, () => this.load(), "common-button-ok");
|
|
|
|
this.addButton(1224, 341, () => this.join(), "common-button-ok");
|
|
|
|
|
|
|
|
this.save_name = new UILabel(this, 351, 185, "", 32, "#000000");
|
|
|
|
this.save_name.setPosition(645, 28);
|
|
|
|
|
|
|
|
this.token_input = new UITextInput(this, 468, 68, 10, "#000000");
|
|
|
|
this.token_input.setPosition(585, 304);
|
|
|
|
|
|
|
|
this.refreshSaves();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refresh available save games
|
|
|
|
*/
|
2017-06-08 21:58:23 +00:00
|
|
|
refreshSaves(): void {
|
2017-05-04 23:19:28 +00:00
|
|
|
let connection = this.view.getConnection();
|
|
|
|
|
|
|
|
// TODO include local save
|
|
|
|
// TODO Disable interaction, with loading icon
|
|
|
|
|
|
|
|
connection.listSaves().then(results => {
|
|
|
|
this.saves = items(results).sort(([id1, info1], [id2, info2]) => cmp(info2, info1));
|
|
|
|
this.setCurrentSave(0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current selected save game
|
|
|
|
*/
|
2017-06-08 21:58:23 +00:00
|
|
|
setCurrentSave(position: number): void {
|
2017-05-04 23:19:28 +00:00
|
|
|
if (this.saves.length == 0) {
|
|
|
|
this.save_name.setContent("No save game found");
|
|
|
|
} else {
|
|
|
|
this.save_selected = clamp(position, 0, this.saves.length - 1);
|
|
|
|
|
|
|
|
let [saveid, saveinfo] = this.saves[this.save_selected];
|
|
|
|
this.save_name.setContent(saveinfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the selected save
|
|
|
|
*/
|
2017-06-08 21:58:23 +00:00
|
|
|
paginateSave(offset: number) {
|
2017-05-04 23:19:28 +00:00
|
|
|
this.setCurrentSave(this.save_selected + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Join an online game
|
|
|
|
*/
|
2017-06-08 21:58:23 +00:00
|
|
|
join(): void {
|
2017-05-04 23:19:28 +00:00
|
|
|
let token = this.token_input.getContent();
|
|
|
|
let connection = this.view.getConnection();
|
|
|
|
|
|
|
|
connection.loadByToken(token).then(session => {
|
|
|
|
if (session) {
|
2017-06-08 21:58:23 +00:00
|
|
|
// For now, we will only spectate
|
|
|
|
session.primary = false;
|
|
|
|
session.spectator = true;
|
|
|
|
|
2017-07-24 18:12:41 +00:00
|
|
|
this.view.gameui.setSession(session, token);
|
2017-05-04 23:19:28 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-05-03 18:12:13 +00:00
|
|
|
|
2017-05-04 23:19:28 +00:00
|
|
|
/**
|
|
|
|
* Load selected save game
|
|
|
|
*/
|
2017-06-08 21:58:23 +00:00
|
|
|
load(): void {
|
2017-05-04 23:19:28 +00:00
|
|
|
if (this.save_selected >= 0 && this.saves.length > this.save_selected) {
|
|
|
|
let connection = this.view.getConnection();
|
|
|
|
let [saveid, saveinfo] = this.saves[this.save_selected];
|
2017-07-20 15:32:00 +00:00
|
|
|
|
|
|
|
let dialog = new UIWaitingDialog(this.view, "Loading game from server...");
|
2017-05-04 23:19:28 +00:00
|
|
|
connection.loadById(saveid).then(session => {
|
|
|
|
if (session) {
|
|
|
|
this.view.gameui.setSession(session);
|
2017-07-20 15:32:00 +00:00
|
|
|
dialog.close();
|
|
|
|
} else {
|
|
|
|
dialog.displayError("No suitable data found in save game (saved with older version ?)");
|
2017-05-04 23:19:28 +00:00
|
|
|
}
|
2017-07-20 15:32:00 +00:00
|
|
|
}).catch(() => {
|
|
|
|
dialog.displayError("Error while loading game from server");
|
2017-05-04 23:19:28 +00:00
|
|
|
});
|
|
|
|
}
|
2017-05-02 22:49:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|