2023-05-11 14:32:48 +00:00
|
|
|
import { GoBoard } from "./components/Board";
|
2023-05-16 03:59:41 +00:00
|
|
|
import { CoreRequest, CoreResponse } from "core-types";
|
2023-05-11 13:39:31 +00:00
|
|
|
import { CoreApi, initCore } from "./coreApi";
|
2023-05-16 14:21:57 +00:00
|
|
|
// import { assertNever } from "./assertNever";
|
2023-05-16 03:59:41 +00:00
|
|
|
|
|
|
|
class UIState {
|
|
|
|
private currentView: GoBoard | null;
|
|
|
|
private rootElement: HTMLElement;
|
|
|
|
coreApi: CoreApi;
|
|
|
|
|
|
|
|
constructor(coreApi: CoreApi, root: HTMLElement) {
|
|
|
|
this.currentView = null;
|
|
|
|
this.rootElement = root;
|
|
|
|
this.coreApi = coreApi;
|
|
|
|
|
|
|
|
if (!root) {
|
|
|
|
console.log("root element not found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
processResponse(response: CoreResponse) {
|
|
|
|
switch (response.type) {
|
|
|
|
case "PlayingFieldView":
|
|
|
|
if (this.currentView) {
|
|
|
|
this.currentView.setBoard(response.content.board);
|
|
|
|
} else {
|
|
|
|
this.currentView = new GoBoard({
|
|
|
|
board: response.content.board,
|
|
|
|
onClick: async (request: CoreRequest) => {
|
|
|
|
const response = await this.coreApi.dispatch(request);
|
|
|
|
this.processResponse(response);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
this.rootElement?.appendChild(this.currentView.canvas);
|
|
|
|
this.currentView.renderBoard();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log("impossible branch: ", response);
|
|
|
|
alert("impossible branch");
|
|
|
|
// assertNever(response);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-10 13:42:02 +00:00
|
|
|
|
|
|
|
const main = async () => {
|
2023-05-11 13:39:31 +00:00
|
|
|
let coreApi = await initCore();
|
2023-05-16 03:59:41 +00:00
|
|
|
let response = await coreApi.dispatch({ type: "PlayingField" });
|
|
|
|
const root = document.getElementById("root");
|
|
|
|
|
|
|
|
if (!root) {
|
|
|
|
console.log("root element not present");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uiState = new UIState(coreApi, root);
|
|
|
|
uiState.processResponse(response);
|
2023-05-10 13:42:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
main();
|