diff --git a/src/api/bot/core.ts b/src/api/bot/core.ts index 0a073a312..75564b81e 100644 --- a/src/api/bot/core.ts +++ b/src/api/bot/core.ts @@ -475,8 +475,8 @@ class OthelloContext extends Context { othelloAi('white', this.othello); if (this.othello.getPattern('black').length === 0) { this.bot.clearContext(); - const blackCount = this.othello.board.map(row => row.filter(s => s == 'black').length).reduce((a, b) => a + b); - const whiteCount = this.othello.board.map(row => row.filter(s => s == 'white').length).reduce((a, b) => a + b); + const blackCount = this.othello.board.filter(s => s == 'black').length; + const whiteCount = this.othello.board.filter(s => s == 'white').length; const winner = blackCount == whiteCount ? '引き分け' : blackCount > whiteCount ? '黒の勝ち' : '白の勝ち'; return this.othello.toString() + `\n\n~終了~\n\n黒${blackCount}、白${whiteCount}で${winner}です。`; } else { diff --git a/src/api/endpoints.ts b/src/api/endpoints.ts index cbc016f20..fad666711 100644 --- a/src/api/endpoints.ts +++ b/src/api/endpoints.ts @@ -233,6 +233,26 @@ const endpoints: Endpoint[] = [ kind: 'notification-read' }, + { + name: 'othello/match', + withCredential: true + }, + + { + name: 'othello/match/cancel', + withCredential: true + }, + + { + name: 'othello/invitations', + withCredential: true + }, + + { + name: 'othello/games', + withCredential: true + }, + { name: 'mute/create', withCredential: true, diff --git a/src/api/endpoints/othello/games.ts b/src/api/endpoints/othello/games.ts new file mode 100644 index 000000000..39963fcd2 --- /dev/null +++ b/src/api/endpoints/othello/games.ts @@ -0,0 +1,26 @@ +import $ from 'cafy'; +import Game, { pack } from '../../models/othello-game'; + +module.exports = (params, user) => new Promise(async (res, rej) => { + // Get 'my' parameter + const [my = false, myErr] = $(params.my).optional.boolean().$; + if (myErr) return rej('invalid my param'); + + const q = my ? { + $or: [{ + black_user_id: user._id + }, { + white_user_id: user._id + }] + } : {}; + + // Fetch games + const games = await Game.find(q, { + sort: { + _id: -1 + } + }); + + // Reponse + res(Promise.all(games.map(async (g) => await pack(g, user)))); +}); diff --git a/src/api/endpoints/othello/invitations.ts b/src/api/endpoints/othello/invitations.ts new file mode 100644 index 000000000..02fb421fb --- /dev/null +++ b/src/api/endpoints/othello/invitations.ts @@ -0,0 +1,15 @@ +import Matching, { pack as packMatching } from '../../models/othello-matching'; + +module.exports = (params, user) => new Promise(async (res, rej) => { + // Find session + const invitations = await Matching.find({ + child_id: user._id + }, { + sort: { + _id: -1 + } + }); + + // Reponse + res(Promise.all(invitations.map(async (i) => await packMatching(i, user)))); +}); diff --git a/src/api/endpoints/othello/match.ts b/src/api/endpoints/othello/match.ts new file mode 100644 index 000000000..cb094bbc6 --- /dev/null +++ b/src/api/endpoints/othello/match.ts @@ -0,0 +1,77 @@ +import $ from 'cafy'; +import Matching, { pack as packMatching } from '../../models/othello-matching'; +import Game, { pack as packGame } from '../../models/othello-game'; +import User from '../../models/user'; +import { publishOthelloStream } from '../../event'; + +module.exports = (params, user) => new Promise(async (res, rej) => { + // Get 'user_id' parameter + const [childId, childIdErr] = $(params.user_id).id().$; + if (childIdErr) return rej('invalid user_id param'); + + // Myself + if (childId.equals(user._id)) { + return rej('invalid user_id param'); + } + + // Find session + const exist = await Matching.findOne({ + parent_id: childId, + child_id: user._id + }); + + if (exist) { + // Destroy session + Matching.remove({ + _id: exist._id + }); + + const parentIsBlack = Math.random() > 0.5; + + // Start game + const game = await Game.insert({ + created_at: new Date(), + black_user_id: parentIsBlack ? exist.parent_id : user._id, + white_user_id: parentIsBlack ? user._id : exist.parent_id, + turn_user_id: parentIsBlack ? exist.parent_id : user._id, + is_ended: false, + logs: [] + }); + + // Reponse + res(await packGame(game, user)); + + publishOthelloStream(exist.parent_id, 'matched', await packGame(game, exist.parent_id)); + } else { + // Fetch child + const child = await User.findOne({ + _id: childId + }, { + fields: { + _id: true + } + }); + + if (child === null) { + return rej('user not found'); + } + + // 以前のセッションはすべて削除しておく + await Matching.remove({ + parent_id: user._id + }); + + // セッションを作成 + const matching = await Matching.insert({ + created_at: new Date(), + parent_id: user._id, + child_id: child._id + }); + + // Reponse + res(); + + // 招待 + publishOthelloStream(child._id, 'invited', await packMatching(matching, child)); + } +}); diff --git a/src/api/endpoints/othello/match/cancel.ts b/src/api/endpoints/othello/match/cancel.ts new file mode 100644 index 000000000..6f751ef83 --- /dev/null +++ b/src/api/endpoints/othello/match/cancel.ts @@ -0,0 +1,9 @@ +import Matching from '../../../models/othello-matching'; + +module.exports = (params, user) => new Promise(async (res, rej) => { + await Matching.remove({ + parent_id: user._id + }); + + res(); +}); diff --git a/src/api/event.ts b/src/api/event.ts index 4a2e4e453..4c9cc18e4 100644 --- a/src/api/event.ts +++ b/src/api/event.ts @@ -38,6 +38,14 @@ class MisskeyEvent { this.publish(`messaging-index-stream:${userId}`, type, typeof value === 'undefined' ? null : value); } + public publishOthelloStream(userId: ID, type: string, value?: any): void { + this.publish(`othello-stream:${userId}`, type, typeof value === 'undefined' ? null : value); + } + + public publishOthelloGameStream(gameId: ID, type: string, value?: any): void { + this.publish(`othello-game-stream:${gameId}`, type, typeof value === 'undefined' ? null : value); + } + public publishChannelStream(channelId: ID, type: string, value?: any): void { this.publish(`channel-stream:${channelId}`, type, typeof value === 'undefined' ? null : value); } @@ -65,4 +73,8 @@ export const publishMessagingStream = ev.publishMessagingStream.bind(ev); export const publishMessagingIndexStream = ev.publishMessagingIndexStream.bind(ev); +export const publishOthelloStream = ev.publishOthelloStream.bind(ev); + +export const publishOthelloGameStream = ev.publishOthelloGameStream.bind(ev); + export const publishChannelStream = ev.publishChannelStream.bind(ev); diff --git a/src/api/models/othello-game.ts b/src/api/models/othello-game.ts new file mode 100644 index 000000000..73a5c94b2 --- /dev/null +++ b/src/api/models/othello-game.ts @@ -0,0 +1,53 @@ +import * as mongo from 'mongodb'; +import deepcopy = require('deepcopy'); +import db from '../../db/mongodb'; +import { IUser, pack as packUser } from './user'; + +const Game = db.get('othello_games'); +export default Game; + +export interface IGame { + _id: mongo.ObjectID; + created_at: Date; + black_user_id: mongo.ObjectID; + white_user_id: mongo.ObjectID; + turn_user_id: mongo.ObjectID; + is_ended: boolean; + winner_id: mongo.ObjectID; + logs: any[]; +} + +/** + * Pack an othello game for API response + */ +export const pack = ( + game: any, + me?: string | mongo.ObjectID | IUser +) => new Promise(async (resolve, reject) => { + + // Me + const meId: mongo.ObjectID = me + ? mongo.ObjectID.prototype.isPrototypeOf(me) + ? me as mongo.ObjectID + : typeof me === 'string' + ? new mongo.ObjectID(me) + : (me as IUser)._id + : null; + + const _game = deepcopy(game); + + // Rename _id to id + _game.id = _game._id; + delete _game._id; + + // Populate user + _game.black_user = await packUser(_game.black_user_id, meId); + _game.white_user = await packUser(_game.white_user_id, meId); + if (_game.winner_id) { + _game.winner = await packUser(_game.winner_id, meId); + } else { + _game.winner = null; + } + + resolve(_game); +}); diff --git a/src/api/models/othello-matching.ts b/src/api/models/othello-matching.ts new file mode 100644 index 000000000..89fcd6df6 --- /dev/null +++ b/src/api/models/othello-matching.ts @@ -0,0 +1,42 @@ +import * as mongo from 'mongodb'; +import deepcopy = require('deepcopy'); +import db from '../../db/mongodb'; +import { IUser, pack as packUser } from './user'; + +const Matching = db.get('othello_matchings'); +export default Matching; + +export interface IMatching { + _id: mongo.ObjectID; + created_at: Date; + parent_id: mongo.ObjectID; + child_id: mongo.ObjectID; +} + +/** + * Pack an othello matching for API response + */ +export const pack = ( + matching: any, + me?: string | mongo.ObjectID | IUser +) => new Promise(async (resolve, reject) => { + + // Me + const meId: mongo.ObjectID = me + ? mongo.ObjectID.prototype.isPrototypeOf(me) + ? me as mongo.ObjectID + : typeof me === 'string' + ? new mongo.ObjectID(me) + : (me as IUser)._id + : null; + + const _matching = deepcopy(matching); + + delete _matching._id; + + // Populate user + _matching.parent = await packUser(_matching.parent_id, meId); + _matching.child = await packUser(_matching.child_id, meId); + + resolve(_matching); +}); diff --git a/src/api/stream/messaging.ts b/src/api/stream/messaging.ts index 3f505cfaf..a4a12426a 100644 --- a/src/api/stream/messaging.ts +++ b/src/api/stream/messaging.ts @@ -2,7 +2,7 @@ import * as websocket from 'websocket'; import * as redis from 'redis'; import read from '../common/read-messaging-message'; -export default function messagingStream(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void { +export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void { const otherparty = request.resourceURL.query.otherparty; // Subscribe messaging stream diff --git a/src/api/stream/othello-game.ts b/src/api/stream/othello-game.ts new file mode 100644 index 000000000..59d964777 --- /dev/null +++ b/src/api/stream/othello-game.ts @@ -0,0 +1,86 @@ +import * as websocket from 'websocket'; +import * as redis from 'redis'; +import Game from '../models/othello-game'; +import { publishOthelloGameStream } from '../event'; +import Othello from '../../common/othello'; + +export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void { + const gameId = request.resourceURL.query.game; + + // Subscribe game stream + subscriber.subscribe(`misskey:othello-game-stream:${gameId}`); + subscriber.on('message', (_, data) => { + connection.send(data); + }); + + connection.on('message', async (data) => { + const msg = JSON.parse(data.utf8Data); + + switch (msg.type) { + case 'set': + if (msg.pos == null) return; + set(msg.pos); + break; + } + }); + + async function set(pos) { + const game = await Game.findOne({ _id: gameId }); + + if (game.is_ended) return; + + const o = new Othello(); + + game.logs.forEach(log => { + o.set(log.color, log.pos); + }); + + const myColor = game.black_user_id.equals(user._id) ? 'black' : 'white'; + const opColor = myColor == 'black' ? 'white' : 'black'; + + if (!o.canReverse(myColor, pos)) return; + o.set(myColor, pos); + + let turn; + if (o.getPattern(opColor).length > 0) { + turn = myColor == 'black' ? game.white_user_id : game.black_user_id; + } else if (o.getPattern(myColor).length > 0) { + turn = myColor == 'black' ? game.black_user_id : game.white_user_id; + } else { + turn = null; + } + + const isEnded = turn === null; + + let winner; + if (isEnded) { + const blackCount = o.board.filter(s => s == 'black').length; + const whiteCount = o.board.filter(s => s == 'white').length; + winner = blackCount == whiteCount ? null : blackCount > whiteCount ? game.black_user_id : game.white_user_id; + } + + const log = { + at: new Date(), + color: myColor, + pos + }; + + await Game.update({ + _id: gameId + }, { + $set: { + turn_user_id: turn, + is_ended: isEnded, + winner_id: winner + }, + $push: { + logs: log + } + }); + + publishOthelloGameStream(gameId, 'set', { + color: myColor, + pos + }); + } +} diff --git a/src/api/stream/othello.ts b/src/api/stream/othello.ts new file mode 100644 index 000000000..5056eb535 --- /dev/null +++ b/src/api/stream/othello.ts @@ -0,0 +1,10 @@ +import * as websocket from 'websocket'; +import * as redis from 'redis'; + +export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void { + // Subscribe othello stream + subscriber.subscribe(`misskey:othello-stream:${user._id}`); + subscriber.on('message', (_, data) => { + connection.send(data); + }); +} diff --git a/src/api/stream/requests.ts b/src/api/stream/requests.ts index 2c36e58b6..d7bb5e6c5 100644 --- a/src/api/stream/requests.ts +++ b/src/api/stream/requests.ts @@ -3,7 +3,7 @@ import Xev from 'xev'; const ev = new Xev(); -export default function homeStream(request: websocket.request, connection: websocket.connection): void { +export default function(request: websocket.request, connection: websocket.connection): void { const onRequest = request => { connection.send(JSON.stringify({ type: 'request', diff --git a/src/api/stream/server.ts b/src/api/stream/server.ts index 0db6643d4..4ca2ad1b1 100644 --- a/src/api/stream/server.ts +++ b/src/api/stream/server.ts @@ -3,7 +3,7 @@ import Xev from 'xev'; const ev = new Xev(); -export default function homeStream(request: websocket.request, connection: websocket.connection): void { +export default function(request: websocket.request, connection: websocket.connection): void { const onStats = stats => { connection.send(JSON.stringify({ type: 'stats', diff --git a/src/api/streaming.ts b/src/api/streaming.ts index c06d64c24..7d67ba957 100644 --- a/src/api/streaming.ts +++ b/src/api/streaming.ts @@ -10,6 +10,8 @@ import homeStream from './stream/home'; import driveStream from './stream/drive'; import messagingStream from './stream/messaging'; import messagingIndexStream from './stream/messaging-index'; +import othelloGameStream from './stream/othello-game'; +import othelloStream from './stream/othello'; import serverStream from './stream/server'; import requestsStream from './stream/requests'; import channelStream from './stream/channel'; @@ -62,6 +64,8 @@ module.exports = (server: http.Server) => { request.resourceURL.pathname === '/drive' ? driveStream : request.resourceURL.pathname === '/messaging' ? messagingStream : request.resourceURL.pathname === '/messaging-index' ? messagingIndexStream : + request.resourceURL.pathname === '/othello-game' ? othelloGameStream : + request.resourceURL.pathname === '/othello' ? othelloStream : null; if (channel !== null) { diff --git a/src/common/othello.ts b/src/common/othello.ts index 858fc3315..fc27d72dc 100644 --- a/src/common/othello.ts +++ b/src/common/othello.ts @@ -1,37 +1,38 @@ const BOARD_SIZE = 8; export default class Othello { - public board: Array>; + public board: Array<'black' | 'white'>; /** * ゲームを初期化します */ constructor() { this.board = [ - [null, null, null, null, null, null, null, null], - [null, null, null, null, null, null, null, null], - [null, null, null, null, null, null, null, null], - [null, null, null, 'black', 'white', null, null, null], - [null, null, null, 'white', 'black', null, null, null], - [null, null, null, null, null, null, null, null], - [null, null, null, null, null, null, null, null], - [null, null, null, null, null, null, null, null] + null, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null, + null, null, null, 'white', 'black', null, null, null, + null, null, null, 'black', 'white', null, null, null, + null, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null ]; } public setByNumber(color, n) { const ps = this.getPattern(color); - this.set(color, ps[n][0], ps[n][1]); + this.set2(color, ps[n][0], ps[n][1]); } private write(color, x, y) { - this.board[y][x] = color; + const pos = x + (y * 8); + this.board[pos] = color; } /** * 石を配置します */ - public set(color, x, y) { + public set2(color, x, y) { this.write(color, x, y); const reverses = this.getReverse(color, x, y); @@ -89,24 +90,42 @@ export default class Othello { }); } + public set(color, pos) { + const x = pos % BOARD_SIZE; + const y = Math.floor(pos / BOARD_SIZE); + this.set2(color, x, y); + } + + public get(x, y) { + const pos = x + (y * 8); + return this.board[pos]; + } + /** * 打つことができる場所を取得します */ public getPattern(myColor): number[][] { const result = []; - this.board.forEach((stones, y) => stones.forEach((stone, x) => { + this.board.forEach((stone, i) => { if (stone != null) return; - if (this.canReverse(myColor, x, y)) result.push([x, y]); - })); + const x = i % BOARD_SIZE; + const y = Math.floor(i / BOARD_SIZE); + if (this.canReverse2(myColor, x, y)) result.push([x, y]); + }); return result; } /** * 指定の位置に石を打つことができるかどうか(相手の石を1つでも反転させられるか)を取得します */ - public canReverse(myColor, targetx, targety): boolean { + public canReverse2(myColor, targetx, targety): boolean { return this.getReverse(myColor, targetx, targety) !== null; } + public canReverse(myColor, pos): boolean { + const x = pos % BOARD_SIZE; + const y = Math.floor(pos / BOARD_SIZE); + return this.getReverse(myColor, x, y) !== null; + } private getReverse(myColor, targetx, targety): number[] { const opponentColor = myColor == 'black' ? 'white' : 'black'; @@ -117,11 +136,11 @@ export default class Othello { return (x, y): any => { if (breaked) { return; - } else if (this.board[y][x] == myColor && opponentStoneFound) { + } else if (this.get(x, y) == myColor && opponentStoneFound) { return true; - } else if (this.board[y][x] == myColor && !opponentStoneFound) { + } else if (this.get(x, y) == myColor && !opponentStoneFound) { breaked = true; - } else if (this.board[y][x] == opponentColor) { + } else if (this.get(x, y) == opponentColor) { opponentStoneFound = true; } else { breaked = true; @@ -210,12 +229,13 @@ export default class Othello { public toString(): string { //return this.board.map(row => row.map(state => state === 'black' ? '●' : state === 'white' ? '○' : '┼').join('')).join('\n'); - return this.board.map(row => row.map(state => state === 'black' ? '⚫️' : state === 'white' ? '⚪️' : '🔹').join('')).join('\n'); + //return this.board.map(row => row.map(state => state === 'black' ? '⚫️' : state === 'white' ? '⚪️' : '🔹').join('')).join('\n'); + return 'wip'; } public toPatternString(color): string { //const num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; - const num = ['0️⃣', '1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣', '🔟', '🍏', '🍎', '🍐', '🍊', '🍋', '🍌', '🍉', '🍇', '🍓', '🍈', '🍒', '🍑', '🍍']; + /*const num = ['0️⃣', '1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣', '🔟', '🍏', '🍎', '🍐', '🍊', '🍋', '🍌', '🍉', '🍇', '🍓', '🍈', '🍒', '🍑', '🍍']; const pattern = this.getPattern(color); @@ -223,7 +243,9 @@ export default class Othello { const i = pattern.findIndex(p => p[0] == x && p[1] == y); //return state === 'black' ? '●' : state === 'white' ? '○' : i != -1 ? num[i] : '┼'; return state === 'black' ? '⚫️' : state === 'white' ? '⚪️' : i != -1 ? num[i] : '🔹'; - }).join('')).join('\n'); + }).join('')).join('\n');*/ + + return 'wip'; } } diff --git a/src/web/app/common/mios.ts b/src/web/app/common/mios.ts index cbb4400a7..3690d3171 100644 --- a/src/web/app/common/mios.ts +++ b/src/web/app/common/mios.ts @@ -4,11 +4,12 @@ import * as merge from 'object-assign-deep'; import { host, apiUrl, swPublickey, version, lang, googleMapsApiKey } from '../config'; import Progress from './scripts/loading'; -import HomeStreamManager from './scripts/streaming/home-stream-manager'; -import DriveStreamManager from './scripts/streaming/drive-stream-manager'; -import ServerStreamManager from './scripts/streaming/server-stream-manager'; -import RequestsStreamManager from './scripts/streaming/requests-stream-manager'; -import MessagingIndexStreamManager from './scripts/streaming/messaging-index-stream-manager'; +import { HomeStreamManager } from './scripts/streaming/home'; +import { DriveStreamManager } from './scripts/streaming/drive'; +import { ServerStreamManager } from './scripts/streaming/server'; +import { RequestsStreamManager } from './scripts/streaming/requests'; +import { MessagingIndexStreamManager } from './scripts/streaming/messaging-index'; +import { OthelloStreamManager } from './scripts/streaming/othello'; import Err from '../common/views/components/connect-failed.vue'; @@ -117,11 +118,13 @@ export default class MiOS extends EventEmitter { serverStream: ServerStreamManager; requestsStream: RequestsStreamManager; messagingIndexStream: MessagingIndexStreamManager; + othelloStream: OthelloStreamManager; } = { driveStream: null, serverStream: null, requestsStream: null, - messagingIndexStream: null + messagingIndexStream: null, + othelloStream: null }; /** @@ -169,6 +172,7 @@ export default class MiOS extends EventEmitter { // Init other stream manager this.streams.driveStream = new DriveStreamManager(this.i); this.streams.messagingIndexStream = new MessagingIndexStreamManager(this.i); + this.streams.othelloStream = new OthelloStreamManager(this.i); }); if (this.debug) { diff --git a/src/web/app/common/scripts/streaming/channel-stream.ts b/src/web/app/common/scripts/streaming/channel.ts similarity index 100% rename from src/web/app/common/scripts/streaming/channel-stream.ts rename to src/web/app/common/scripts/streaming/channel.ts diff --git a/src/web/app/common/scripts/streaming/drive-stream-manager.ts b/src/web/app/common/scripts/streaming/drive-stream-manager.ts deleted file mode 100644 index 8acdd7cbb..000000000 --- a/src/web/app/common/scripts/streaming/drive-stream-manager.ts +++ /dev/null @@ -1,20 +0,0 @@ -import StreamManager from './stream-manager'; -import Connection from './drive-stream'; - -export default class DriveStreamManager extends StreamManager { - private me; - - constructor(me) { - super(); - - this.me = me; - } - - public getConnection() { - if (this.connection == null) { - this.connection = new Connection(this.me); - } - - return this.connection; - } -} diff --git a/src/web/app/common/scripts/streaming/drive-stream.ts b/src/web/app/common/scripts/streaming/drive-stream.ts deleted file mode 100644 index 0da3f1255..000000000 --- a/src/web/app/common/scripts/streaming/drive-stream.ts +++ /dev/null @@ -1,12 +0,0 @@ -import Stream from './stream'; - -/** - * Drive stream connection - */ -export default class Connection extends Stream { - constructor(me) { - super('drive', { - i: me.token - }); - } -} diff --git a/src/web/app/common/scripts/streaming/drive.ts b/src/web/app/common/scripts/streaming/drive.ts new file mode 100644 index 000000000..5805e5803 --- /dev/null +++ b/src/web/app/common/scripts/streaming/drive.ts @@ -0,0 +1,31 @@ +import Stream from './stream'; +import StreamManager from './stream-manager'; + +/** + * Drive stream connection + */ +export class DriveStream extends Stream { + constructor(me) { + super('drive', { + i: me.token + }); + } +} + +export class DriveStreamManager extends StreamManager { + private me; + + constructor(me) { + super(); + + this.me = me; + } + + public getConnection() { + if (this.connection == null) { + this.connection = new DriveStream(this.me); + } + + return this.connection; + } +} diff --git a/src/web/app/common/scripts/streaming/home-stream-manager.ts b/src/web/app/common/scripts/streaming/home-stream-manager.ts deleted file mode 100644 index ab56d5a73..000000000 --- a/src/web/app/common/scripts/streaming/home-stream-manager.ts +++ /dev/null @@ -1,23 +0,0 @@ -import StreamManager from './stream-manager'; -import Connection from './home-stream'; -import MiOS from '../../mios'; - -export default class HomeStreamManager extends StreamManager { - private me; - private os: MiOS; - - constructor(os: MiOS, me) { - super(); - - this.me = me; - this.os = os; - } - - public getConnection() { - if (this.connection == null) { - this.connection = new Connection(this.os, this.me); - } - - return this.connection; - } -} diff --git a/src/web/app/common/scripts/streaming/home-stream.ts b/src/web/app/common/scripts/streaming/home.ts similarity index 66% rename from src/web/app/common/scripts/streaming/home-stream.ts rename to src/web/app/common/scripts/streaming/home.ts index 3516705e2..1f110bfd3 100644 --- a/src/web/app/common/scripts/streaming/home-stream.ts +++ b/src/web/app/common/scripts/streaming/home.ts @@ -1,12 +1,13 @@ import * as merge from 'object-assign-deep'; import Stream from './stream'; +import StreamManager from './stream-manager'; import MiOS from '../../mios'; /** * Home stream connection */ -export default class Connection extends Stream { +export class HomeStream extends Stream { constructor(os: MiOS, me) { super('', { i: me.token @@ -34,3 +35,23 @@ export default class Connection extends Stream { }); } } + +export class HomeStreamManager extends StreamManager { + private me; + private os: MiOS; + + constructor(os: MiOS, me) { + super(); + + this.me = me; + this.os = os; + } + + public getConnection() { + if (this.connection == null) { + this.connection = new HomeStream(this.os, this.me); + } + + return this.connection; + } +} diff --git a/src/web/app/common/scripts/streaming/messaging-index-stream-manager.ts b/src/web/app/common/scripts/streaming/messaging-index-stream-manager.ts deleted file mode 100644 index 0f08b0148..000000000 --- a/src/web/app/common/scripts/streaming/messaging-index-stream-manager.ts +++ /dev/null @@ -1,20 +0,0 @@ -import StreamManager from './stream-manager'; -import Connection from './messaging-index-stream'; - -export default class MessagingIndexStreamManager extends StreamManager { - private me; - - constructor(me) { - super(); - - this.me = me; - } - - public getConnection() { - if (this.connection == null) { - this.connection = new Connection(this.me); - } - - return this.connection; - } -} diff --git a/src/web/app/common/scripts/streaming/messaging-index-stream.ts b/src/web/app/common/scripts/streaming/messaging-index-stream.ts deleted file mode 100644 index 8015c840b..000000000 --- a/src/web/app/common/scripts/streaming/messaging-index-stream.ts +++ /dev/null @@ -1,12 +0,0 @@ -import Stream from './stream'; - -/** - * Messaging index stream connection - */ -export default class Connection extends Stream { - constructor(me) { - super('messaging-index', { - i: me.token - }); - } -} diff --git a/src/web/app/common/scripts/streaming/messaging-index.ts b/src/web/app/common/scripts/streaming/messaging-index.ts new file mode 100644 index 000000000..69758416d --- /dev/null +++ b/src/web/app/common/scripts/streaming/messaging-index.ts @@ -0,0 +1,31 @@ +import Stream from './stream'; +import StreamManager from './stream-manager'; + +/** + * Messaging index stream connection + */ +export class MessagingIndexStream extends Stream { + constructor(me) { + super('messaging-index', { + i: me.token + }); + } +} + +export class MessagingIndexStreamManager extends StreamManager { + private me; + + constructor(me) { + super(); + + this.me = me; + } + + public getConnection() { + if (this.connection == null) { + this.connection = new MessagingIndexStream(this.me); + } + + return this.connection; + } +} diff --git a/src/web/app/common/scripts/streaming/messaging-stream.ts b/src/web/app/common/scripts/streaming/messaging.ts similarity index 83% rename from src/web/app/common/scripts/streaming/messaging-stream.ts rename to src/web/app/common/scripts/streaming/messaging.ts index 68dfc5ec0..1fff2286b 100644 --- a/src/web/app/common/scripts/streaming/messaging-stream.ts +++ b/src/web/app/common/scripts/streaming/messaging.ts @@ -3,7 +3,7 @@ import Stream from './stream'; /** * Messaging stream connection */ -export default class Connection extends Stream { +export class MessagingStream extends Stream { constructor(me, otherparty) { super('messaging', { i: me.token, diff --git a/src/web/app/common/scripts/streaming/othello-game.ts b/src/web/app/common/scripts/streaming/othello-game.ts new file mode 100644 index 000000000..51a435541 --- /dev/null +++ b/src/web/app/common/scripts/streaming/othello-game.ts @@ -0,0 +1,10 @@ +import Stream from './stream'; + +export class OthelloGameStream extends Stream { + constructor(me, game) { + super('othello-game', { + i: me.token, + game: game.id + }); + } +} diff --git a/src/web/app/common/scripts/streaming/othello.ts b/src/web/app/common/scripts/streaming/othello.ts new file mode 100644 index 000000000..febc5d498 --- /dev/null +++ b/src/web/app/common/scripts/streaming/othello.ts @@ -0,0 +1,28 @@ +import StreamManager from './stream-manager'; +import Stream from './stream'; + +export class OthelloStream extends Stream { + constructor(me) { + super('othello', { + i: me.token + }); + } +} + +export class OthelloStreamManager extends StreamManager { + private me; + + constructor(me) { + super(); + + this.me = me; + } + + public getConnection() { + if (this.connection == null) { + this.connection = new OthelloStream(this.me); + } + + return this.connection; + } +} diff --git a/src/web/app/common/scripts/streaming/requests-stream-manager.ts b/src/web/app/common/scripts/streaming/requests-stream-manager.ts deleted file mode 100644 index 44db913e7..000000000 --- a/src/web/app/common/scripts/streaming/requests-stream-manager.ts +++ /dev/null @@ -1,12 +0,0 @@ -import StreamManager from './stream-manager'; -import Connection from './requests-stream'; - -export default class RequestsStreamManager extends StreamManager { - public getConnection() { - if (this.connection == null) { - this.connection = new Connection(); - } - - return this.connection; - } -} diff --git a/src/web/app/common/scripts/streaming/requests-stream.ts b/src/web/app/common/scripts/streaming/requests-stream.ts deleted file mode 100644 index 22ecea6c0..000000000 --- a/src/web/app/common/scripts/streaming/requests-stream.ts +++ /dev/null @@ -1,10 +0,0 @@ -import Stream from './stream'; - -/** - * Requests stream connection - */ -export default class Connection extends Stream { - constructor() { - super('requests'); - } -} diff --git a/src/web/app/common/scripts/streaming/requests.ts b/src/web/app/common/scripts/streaming/requests.ts new file mode 100644 index 000000000..5d199a074 --- /dev/null +++ b/src/web/app/common/scripts/streaming/requests.ts @@ -0,0 +1,21 @@ +import Stream from './stream'; +import StreamManager from './stream-manager'; + +/** + * Requests stream connection + */ +export class RequestsStream extends Stream { + constructor() { + super('requests'); + } +} + +export class RequestsStreamManager extends StreamManager { + public getConnection() { + if (this.connection == null) { + this.connection = new RequestsStream(); + } + + return this.connection; + } +} diff --git a/src/web/app/common/scripts/streaming/server-stream-manager.ts b/src/web/app/common/scripts/streaming/server-stream-manager.ts deleted file mode 100644 index a170daebb..000000000 --- a/src/web/app/common/scripts/streaming/server-stream-manager.ts +++ /dev/null @@ -1,12 +0,0 @@ -import StreamManager from './stream-manager'; -import Connection from './server-stream'; - -export default class ServerStreamManager extends StreamManager { - public getConnection() { - if (this.connection == null) { - this.connection = new Connection(); - } - - return this.connection; - } -} diff --git a/src/web/app/common/scripts/streaming/server-stream.ts b/src/web/app/common/scripts/streaming/server-stream.ts deleted file mode 100644 index b9e068446..000000000 --- a/src/web/app/common/scripts/streaming/server-stream.ts +++ /dev/null @@ -1,10 +0,0 @@ -import Stream from './stream'; - -/** - * Server stream connection - */ -export default class Connection extends Stream { - constructor() { - super('server'); - } -} diff --git a/src/web/app/common/scripts/streaming/server.ts b/src/web/app/common/scripts/streaming/server.ts new file mode 100644 index 000000000..b12198d2f --- /dev/null +++ b/src/web/app/common/scripts/streaming/server.ts @@ -0,0 +1,21 @@ +import Stream from './stream'; +import StreamManager from './stream-manager'; + +/** + * Server stream connection + */ +export class ServerStream extends Stream { + constructor() { + super('server'); + } +} + +export class ServerStreamManager extends StreamManager { + public getConnection() { + if (this.connection == null) { + this.connection = new ServerStream(); + } + + return this.connection; + } +} diff --git a/src/web/app/common/views/components/index.ts b/src/web/app/common/views/components/index.ts index 527492022..98fc2352f 100644 --- a/src/web/app/common/views/components/index.ts +++ b/src/web/app/common/views/components/index.ts @@ -21,6 +21,7 @@ import urlPreview from './url-preview.vue'; import twitterSetting from './twitter-setting.vue'; import fileTypeIcon from './file-type-icon.vue'; import Switch from './switch.vue'; +import Othello from './othello.vue'; Vue.component('mk-signin', signin); Vue.component('mk-signup', signup); @@ -43,3 +44,4 @@ Vue.component('mk-url-preview', urlPreview); Vue.component('mk-twitter-setting', twitterSetting); Vue.component('mk-file-type-icon', fileTypeIcon); Vue.component('mk-switch', Switch); +Vue.component('mk-othello', Othello); diff --git a/src/web/app/common/views/components/messaging-room.vue b/src/web/app/common/views/components/messaging-room.vue index 637fa9cd6..547e9494e 100644 --- a/src/web/app/common/views/components/messaging-room.vue +++ b/src/web/app/common/views/components/messaging-room.vue @@ -26,7 +26,7 @@ + + diff --git a/src/web/app/common/views/components/othello.vue b/src/web/app/common/views/components/othello.vue new file mode 100644 index 000000000..326a8d80c --- /dev/null +++ b/src/web/app/common/views/components/othello.vue @@ -0,0 +1,275 @@ + + + + + diff --git a/src/web/app/desktop/views/components/game-window.vue b/src/web/app/desktop/views/components/game-window.vue new file mode 100644 index 000000000..bf339092a --- /dev/null +++ b/src/web/app/desktop/views/components/game-window.vue @@ -0,0 +1,24 @@ + + + + + diff --git a/src/web/app/desktop/views/components/ui.header.nav.vue b/src/web/app/desktop/views/components/ui.header.nav.vue index a5b6ecd6f..54045db8d 100644 --- a/src/web/app/desktop/views/components/ui.header.nav.vue +++ b/src/web/app/desktop/views/components/ui.header.nav.vue @@ -15,6 +15,13 @@ +
  • + + %fa:gamepad% +

    ゲーム

    + +
    +
  • @@ -22,12 +29,6 @@

    %i18n:desktop.tags.mk-ui-header-nav.ch%

  • -
  • - - %fa:info% -

    %i18n:desktop.tags.mk-ui-header-nav.info%

    -
    -
  • @@ -36,11 +37,13 @@ import Vue from 'vue'; import { chUrl } from '../../../config'; import MkMessagingWindow from './messaging-window.vue'; +import MkGameWindow from './game-window.vue'; export default Vue.extend({ data() { return { hasUnreadMessagingMessages: false, + hasGameInvitations: false, connection: null, connectionId: null, chUrl @@ -80,6 +83,10 @@ export default Vue.extend({ messaging() { (this as any).os.new(MkMessagingWindow); + }, + + game() { + (this as any).os.new(MkGameWindow); } } }); diff --git a/src/web/app/desktop/views/widgets/channel.channel.vue b/src/web/app/desktop/views/widgets/channel.channel.vue index 70dac316c..02cdf6de1 100644 --- a/src/web/app/desktop/views/widgets/channel.channel.vue +++ b/src/web/app/desktop/views/widgets/channel.channel.vue @@ -11,7 +11,7 @@