reversi 💮 💯

This commit is contained in:
syuilo
2018-06-17 08:10:54 +09:00
parent 03f20599ba
commit 1ef66c962a
48 changed files with 197 additions and 197 deletions

View File

@ -245,27 +245,27 @@ const endpoints: Endpoint[] = [
},
{
name: 'othello/match',
name: 'reversi/match',
withCredential: true
},
{
name: 'othello/match/cancel',
name: 'reversi/match/cancel',
withCredential: true
},
{
name: 'othello/invitations',
name: 'reversi/invitations',
withCredential: true
},
{
name: 'othello/games',
name: 'reversi/games',
withCredential: true
},
{
name: 'othello/games/show'
name: 'reversi/games/show'
},
{

View File

@ -1,5 +1,5 @@
import $ from 'cafy'; import ID from '../../../../cafy-id';
import OthelloGame, { pack } from '../../../../models/othello-game';
import ReversiGame, { pack } from '../../../../models/reversi-game';
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'my' parameter
@ -50,7 +50,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
}
// Fetch games
const games = await OthelloGame.find(q, {
const games = await ReversiGame.find(q, {
sort,
limit
});

View File

@ -1,19 +1,19 @@
import $ from 'cafy'; import ID from '../../../../../cafy-id';
import OthelloGame, { pack } from '../../../../../models/othello-game';
import Othello from '../../../../../othello/core';
import ReversiGame, { pack } from '../../../../../models/reversi-game';
import Reversi from '../../../../../reversi/core';
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'gameId' parameter
const [gameId, gameIdErr] = $.type(ID).get(params.gameId);
if (gameIdErr) return rej('invalid gameId param');
const game = await OthelloGame.findOne({ _id: gameId });
const game = await ReversiGame.findOne({ _id: gameId });
if (game == null) {
return rej('game not found');
}
const o = new Othello(game.settings.map, {
const o = new Reversi(game.settings.map, {
isLlotheo: game.settings.isLlotheo,
canPutEverywhere: game.settings.canPutEverywhere,
loopedBoard: game.settings.loopedBoard

View File

@ -1,4 +1,4 @@
import Matching, { pack as packMatching } from '../../../../models/othello-matching';
import Matching, { pack as packMatching } from '../../../../models/reversi-matching';
module.exports = (params, user) => new Promise(async (res, rej) => {
// Find session

View File

@ -1,9 +1,9 @@
import $ from 'cafy'; import ID from '../../../../cafy-id';
import Matching, { pack as packMatching } from '../../../../models/othello-matching';
import OthelloGame, { pack as packGame } from '../../../../models/othello-game';
import Matching, { pack as packMatching } from '../../../../models/reversi-matching';
import ReversiGame, { pack as packGame } from '../../../../models/reversi-game';
import User from '../../../../models/user';
import publishUserStream, { publishOthelloStream } from '../../../../publishers/stream';
import { eighteight } from '../../../../othello/maps';
import publishUserStream, { publishReversiStream } from '../../../../publishers/stream';
import { eighteight } from '../../../../reversi/maps';
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'userId' parameter
@ -28,7 +28,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
});
// Create game
const game = await OthelloGame.insert({
const game = await ReversiGame.insert({
createdAt: new Date(),
user1Id: exist.parentId,
user2Id: user._id,
@ -47,14 +47,14 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Reponse
res(await packGame(game, user));
publishOthelloStream(exist.parentId, 'matched', await packGame(game, exist.parentId));
publishReversiStream(exist.parentId, 'matched', await packGame(game, exist.parentId));
const other = await Matching.count({
childId: user._id
});
if (other == 0) {
publishUserStream(user._id, 'othello_no_invites');
publishUserStream(user._id, 'reversi_no_invites');
}
} else {
// Fetch child
@ -88,8 +88,8 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
const packed = await packMatching(matching, child);
// 招待
publishOthelloStream(child._id, 'invited', packed);
publishReversiStream(child._id, 'invited', packed);
publishUserStream(child._id, 'othello_invited', packed);
publishUserStream(child._id, 'reversi_invited', packed);
}
});

View File

@ -1,4 +1,4 @@
import Matching from '../../../../../models/othello-matching';
import Matching from '../../../../../models/reversi-matching';
module.exports = (params, user) => new Promise(async (res, rej) => {
await Matching.remove({

View File

@ -1,10 +1,10 @@
import * as websocket from 'websocket';
import * as redis from 'redis';
import * as CRC32 from 'crc-32';
import OthelloGame, { pack } from '../../../models/othello-game';
import { publishOthelloGameStream } from '../../../publishers/stream';
import Othello from '../../../othello/core';
import * as maps from '../../../othello/maps';
import ReversiGame, { pack } from '../../../models/reversi-game';
import { publishReversiGameStream } from '../../../publishers/stream';
import Reversi from '../../../reversi/core';
import * as maps from '../../../reversi/maps';
import { ParsedUrlQuery } from 'querystring';
export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user?: any): void {
@ -12,7 +12,7 @@ export default function(request: websocket.request, connection: websocket.connec
const gameId = q.game;
// Subscribe game stream
subscriber.subscribe(`misskey:othello-game-stream:${gameId}`);
subscriber.subscribe(`misskey:reversi-game-stream:${gameId}`);
subscriber.on('message', (_, data) => {
connection.send(data);
});
@ -62,24 +62,24 @@ export default function(request: websocket.request, connection: websocket.connec
});
async function updateSettings(settings) {
const game = await OthelloGame.findOne({ _id: gameId });
const game = await ReversiGame.findOne({ _id: gameId });
if (game.isStarted) return;
if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return;
if (game.user1Id.equals(user._id) && game.user1Accepted) return;
if (game.user2Id.equals(user._id) && game.user2Accepted) return;
await OthelloGame.update({ _id: gameId }, {
await ReversiGame.update({ _id: gameId }, {
$set: {
settings
}
});
publishOthelloGameStream(gameId, 'update-settings', settings);
publishReversiGameStream(gameId, 'update-settings', settings);
}
async function initForm(form) {
const game = await OthelloGame.findOne({ _id: gameId });
const game = await ReversiGame.findOne({ _id: gameId });
if (game.isStarted) return;
if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return;
@ -90,18 +90,18 @@ export default function(request: websocket.request, connection: websocket.connec
form2: form
};
await OthelloGame.update({ _id: gameId }, {
await ReversiGame.update({ _id: gameId }, {
$set: set
});
publishOthelloGameStream(gameId, 'init-form', {
publishReversiGameStream(gameId, 'init-form', {
userId: user._id,
form
});
}
async function updateForm(id, value) {
const game = await OthelloGame.findOne({ _id: gameId });
const game = await ReversiGame.findOne({ _id: gameId });
if (game.isStarted) return;
if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return;
@ -120,11 +120,11 @@ export default function(request: websocket.request, connection: websocket.connec
form1: form
};
await OthelloGame.update({ _id: gameId }, {
await ReversiGame.update({ _id: gameId }, {
$set: set
});
publishOthelloGameStream(gameId, 'update-form', {
publishReversiGameStream(gameId, 'update-form', {
userId: user._id,
id,
value
@ -133,40 +133,40 @@ export default function(request: websocket.request, connection: websocket.connec
async function message(message) {
message.id = Math.random();
publishOthelloGameStream(gameId, 'message', {
publishReversiGameStream(gameId, 'message', {
userId: user._id,
message
});
}
async function accept(accept: boolean) {
const game = await OthelloGame.findOne({ _id: gameId });
const game = await ReversiGame.findOne({ _id: gameId });
if (game.isStarted) return;
let bothAccepted = false;
if (game.user1Id.equals(user._id)) {
await OthelloGame.update({ _id: gameId }, {
await ReversiGame.update({ _id: gameId }, {
$set: {
user1Accepted: accept
}
});
publishOthelloGameStream(gameId, 'change-accepts', {
publishReversiGameStream(gameId, 'change-accepts', {
user1: accept,
user2: game.user2Accepted
});
if (accept && game.user2Accepted) bothAccepted = true;
} else if (game.user2Id.equals(user._id)) {
await OthelloGame.update({ _id: gameId }, {
await ReversiGame.update({ _id: gameId }, {
$set: {
user2Accepted: accept
}
});
publishOthelloGameStream(gameId, 'change-accepts', {
publishReversiGameStream(gameId, 'change-accepts', {
user1: game.user1Accepted,
user2: accept
});
@ -179,7 +179,7 @@ export default function(request: websocket.request, connection: websocket.connec
if (bothAccepted) {
// 3秒後、まだacceptされていたらゲーム開始
setTimeout(async () => {
const freshGame = await OthelloGame.findOne({ _id: gameId });
const freshGame = await ReversiGame.findOne({ _id: gameId });
if (freshGame == null || freshGame.isStarted || freshGame.isEnded) return;
if (!freshGame.user1Accepted || !freshGame.user2Accepted) return;
@ -198,7 +198,7 @@ export default function(request: websocket.request, connection: websocket.connec
const map = freshGame.settings.map != null ? freshGame.settings.map : getRandomMap();
await OthelloGame.update({ _id: gameId }, {
await ReversiGame.update({ _id: gameId }, {
$set: {
startedAt: new Date(),
isStarted: true,
@ -208,7 +208,7 @@ export default function(request: websocket.request, connection: websocket.connec
});
//#region 盤面に最初から石がないなどして始まった瞬間に勝敗が決定する場合があるのでその処理
const o = new Othello(map, {
const o = new Reversi(map, {
isLlotheo: freshGame.settings.isLlotheo,
canPutEverywhere: freshGame.settings.canPutEverywhere,
loopedBoard: freshGame.settings.loopedBoard
@ -224,7 +224,7 @@ export default function(request: websocket.request, connection: websocket.connec
winner = null;
}
await OthelloGame.update({
await ReversiGame.update({
_id: gameId
}, {
$set: {
@ -233,27 +233,27 @@ export default function(request: websocket.request, connection: websocket.connec
}
});
publishOthelloGameStream(gameId, 'ended', {
publishReversiGameStream(gameId, 'ended', {
winnerId: winner,
game: await pack(gameId, user)
});
}
//#endregion
publishOthelloGameStream(gameId, 'started', await pack(gameId, user));
publishReversiGameStream(gameId, 'started', await pack(gameId, user));
}, 3000);
}
}
// 石を打つ
async function set(pos) {
const game = await OthelloGame.findOne({ _id: gameId });
const game = await ReversiGame.findOne({ _id: gameId });
if (!game.isStarted) return;
if (game.isEnded) return;
if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return;
const o = new Othello(game.settings.map, {
const o = new Reversi(game.settings.map, {
isLlotheo: game.settings.isLlotheo,
canPutEverywhere: game.settings.canPutEverywhere,
loopedBoard: game.settings.loopedBoard
@ -290,7 +290,7 @@ export default function(request: websocket.request, connection: websocket.connec
const crc32 = CRC32.str(game.logs.map(x => x.pos.toString()).join('') + pos.toString());
await OthelloGame.update({
await ReversiGame.update({
_id: gameId
}, {
$set: {
@ -303,12 +303,12 @@ export default function(request: websocket.request, connection: websocket.connec
}
});
publishOthelloGameStream(gameId, 'set', Object.assign(log, {
publishReversiGameStream(gameId, 'set', Object.assign(log, {
next: o.turn
}));
if (o.isEnded) {
publishOthelloGameStream(gameId, 'ended', {
publishReversiGameStream(gameId, 'ended', {
winnerId: winner,
game: await pack(gameId, user)
});
@ -316,7 +316,7 @@ export default function(request: websocket.request, connection: websocket.connec
}
async function check(crc32) {
const game = await OthelloGame.findOne({ _id: gameId });
const game = await ReversiGame.findOne({ _id: gameId });
if (!game.isStarted) return;

View File

@ -1,12 +1,12 @@
import * as mongo from 'mongodb';
import * as websocket from 'websocket';
import * as redis from 'redis';
import Matching, { pack } from '../../../models/othello-matching';
import Matching, { pack } from '../../../models/reversi-matching';
import publishUserStream from '../../../publishers/stream';
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}`);
// Subscribe reversi stream
subscriber.subscribe(`misskey:reversi-stream:${user._id}`);
subscriber.on('message', (_, data) => {
connection.send(data);
});
@ -22,7 +22,7 @@ export default function(request: websocket.request, connection: websocket.connec
childId: new mongo.ObjectID(msg.id)
});
if (matching == null) return;
publishUserStream(matching.childId, 'othello_invited', await pack(matching, matching.childId));
publishUserStream(matching.childId, 'reversi_invited', await pack(matching, matching.childId));
break;
}
});

View File

@ -10,8 +10,8 @@ import userListStream from './stream/user-list';
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 reversiGameStream from './stream/reversi-game';
import reversiStream from './stream/reversi';
import serverStatsStream from './stream/server-stats';
import notesStatsStream from './stream/notes-stats';
import requestsStream from './stream/requests';
@ -56,8 +56,8 @@ module.exports = (server: http.Server) => {
const q = request.resourceURL.query as ParsedUrlQuery;
const [user, app] = await authenticate(q.i as string);
if (request.resourceURL.pathname === '/othello-game') {
othelloGameStream(request, connection, subscriber, user);
if (request.resourceURL.pathname === '/reversi-game') {
reversiGameStream(request, connection, subscriber, user);
return;
}
@ -75,7 +75,7 @@ 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' ? othelloStream :
request.resourceURL.pathname === '/reversi' ? reversiStream :
null;
if (channel !== null) {