Sessionのメンバ型エラーに対処

This commit is contained in:
takejohn 2024-03-29 19:36:36 +09:00
parent c485a5b1ed
commit fb0a9b0f62

View file

@ -11,6 +11,7 @@ import * as Reversi from './engine.js';
import config from '@/config.js'; import config from '@/config.js';
import serifs from '@/serifs.js'; import serifs from '@/serifs.js';
import type { User } from '@/misskey/user.js'; import type { User } from '@/misskey/user.js';
import { Note } from '@/misskey/note.js';
function getUserName(user) { function getUserName(user) {
return user.name || user.username; return user.name || user.username;
@ -24,11 +25,35 @@ const titles = [
]; ];
class Session { class Session {
private account: User; private maybeAccount?: User;
private game: any; private game: any;
private form: any; private form: any;
private engine: Reversi.Game; private maybeEngine?: Reversi.Game;
private botColor: Reversi.Color; private maybeBotColor?: Reversi.Color;
private get account(): User {
const maybeAccount = this.maybeAccount;
if (maybeAccount == null) {
throw new Error('Have not received "_init_" message');
}
return maybeAccount;
}
private get engine(): Reversi.Game {
const maybeEngine = this.maybeEngine;
if (maybeEngine == null) {
throw new Error('Have not received "started" message');
}
return maybeEngine;
}
private get botColor(): Reversi.Color {
const maybeBotColor = this.maybeBotColor;
if (maybeBotColor == null) {
throw new Error('Have not received "started" message');
}
return maybeBotColor;
}
private appliedOps: string[] = []; private appliedOps: string[] = [];
@ -100,7 +125,7 @@ class Session {
private onInit = (msg: any) => { private onInit = (msg: any) => {
this.game = msg.game; this.game = msg.game;
this.form = msg.form; this.form = msg.form;
this.account = msg.account; this.maybeAccount = msg.account;
} }
/** /**
@ -121,7 +146,7 @@ class Session {
}); });
// リバーシエンジン初期化 // リバーシエンジン初期化
this.engine = new Reversi.Game(this.game.map, { this.maybeEngine = new Reversi.Game(this.game.map, {
isLlotheo: this.game.isLlotheo, isLlotheo: this.game.isLlotheo,
canPutEverywhere: this.game.canPutEverywhere, canPutEverywhere: this.game.canPutEverywhere,
loopedBoard: this.game.loopedBoard loopedBoard: this.game.loopedBoard
@ -198,7 +223,7 @@ class Session {
//#endregion //#endregion
this.botColor = this.game.user1Id == this.account.id && this.game.black == 1 || this.game.user2Id == this.account.id && this.game.black == 2; this.maybeBotColor = this.game.user1Id == this.account.id && this.game.black == 1 || this.game.user2Id == this.account.id && this.game.black == 2;
if (this.botColor) { if (this.botColor) {
this.think(); this.think();
@ -454,7 +479,7 @@ class Session {
try { try {
const res = await got.post(`${config.host}/api/notes/create`, { const res = await got.post(`${config.host}/api/notes/create`, {
json: body json: body
}).json(); }).json<{ createdNote: Note }>();
return res.createdNote; return res.createdNote;
} catch (e) { } catch (e) {