summaryrefslogtreecommitdiff
path: root/src/web/app/common/views/components/othello.vue
blob: f5abcfb103fc377a29fd543d1f5968cb2745533d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<template>
<div>
	<div v-if="game">
		<x-game :game="game"/>
	</div>
	<div v-if="matching">
		<h1><b>{{ matching.name }}</b>を待っています<mk-ellipsis/></h1>
	</div>
	<div v-else>
		<h1>Misskey Othello</h1>
		<p>他のMisskeyユーザーとオセロで対戦しよう。</p>
		<button>フリーマッチ(準備中)</button>
		<button @click="match">指名</button>
		<section>
			<h2>対局の招待があります:</h2>
		</section>
		<section>
			<h2>過去の対局</h2>
		</section>
	</div>
</div>
</template>

<script lang="ts">
import Vue from 'vue';
import XGame from './othello.game.vue';

export default Vue.extend({
	components: {
		XGame
	},
	data() {
		return {
			game: null,
			games: [],
			gamesFetching: true,
			gamesMoreFetching: false,
			matching: false,
			invitations: [],
			connection: null,
			connectionId: null
		};
	},
	mounted() {
		this.connection = (this as any).os.streams.othelloStream.getConnection();
		this.connectionId = (this as any).os.streams.othelloStream.use();

		this.connection.on('macthed', this.onMatched);
		this.connection.on('invited', this.onInvited);

		(this as any).api('othello/games').then(games => {
			this.games = games;
			this.gamesFetching = false;
		});

		(this as any).api('othello/invitations').then(invitations => {
			this.invitations = this.invitations.concat(invitations);
		});
	},
	beforeDestroy() {
		this.connection.off('macthed', this.onMatched);
		this.connection.off('invited', this.onInvited);
		(this as any).streams.othelloStream.dispose(this.connectionId);
	},
	methods: {
		match() {
			(this as any).apis.input({
				title: 'ユーザー名を入力してください'
			}).then(username => {
				(this as any).api('users/show', {
					username
				}).then(user => {
					(this as any).api('othello/match', {
						user_id: user.id
					}).then(res => {
						if (res == null) {
							this.matching = user;
						} else {
							this.game = res;
						}
					});
				});
			});
		},
		onMatched(game) {
			this.game = game;
		},
		onInvited(invite) {
			this.invitations.unshift(invite);
		}
	}
});
</script>