This commit is contained in:
syuilo
2024-01-18 15:28:39 +09:00
parent 8ef70283fa
commit 1a25401452
12 changed files with 494 additions and 62 deletions

View File

@@ -134,6 +134,7 @@
"microformats-parser": "2.0.2",
"mime-types": "2.1.35",
"misskey-js": "workspace:*",
"misskey-reversi": "workspace:*",
"ms": "3.0.0-canary.1",
"nanoid": "5.0.4",
"nested-property": "4.0.0",

View File

@@ -7,32 +7,26 @@ import { Inject, Injectable } from '@nestjs/common';
import * as Redis from 'ioredis';
import { In } from 'typeorm';
import { ModuleRef } from '@nestjs/core';
import * as Reversi from 'misskey-reversi';
import type {
MiReversiGame,
MiRole,
MiRoleAssignment,
ReversiGamesRepository,
ReversiMatchingsRepository,
RoleAssignmentsRepository,
RolesRepository,
UsersRepository,
} from '@/models/_.js';
import { MemoryKVCache, MemorySingleCache } from '@/misc/cache.js';
import type { MiUser } from '@/models/User.js';
import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js';
import { MetaService } from '@/core/MetaService.js';
import { CacheService } from '@/core/CacheService.js';
import type { RoleCondFormulaValue } from '@/models/Role.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import type { GlobalEvents } from '@/core/GlobalEventService.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { IdService } from '@/core/IdService.js';
import { ModerationLogService } from '@/core/ModerationLogService.js';
import type { Packed } from '@/misc/json-schema.js';
import { FanoutTimelineService } from '@/core/FanoutTimelineService.js';
import { NotificationService } from '@/core/NotificationService.js';
import { ReversiMatchingEntityService } from '@/core/entities/ReversiMatchingEntityService.js';
import { ReversiGameEntityService } from './entities/ReversiGameEntityService.js';
import type { OnApplicationShutdown, OnModuleInit } from '@nestjs/common';
@Injectable()
@@ -64,6 +58,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
private cacheService: CacheService,
private userEntityService: UserEntityService,
private globalEventService: GlobalEventService,
private reversiGameEntityService: ReversiGameEntityService,
private reversiMatchingsEntityService: ReversiMatchingEntityService,
private idService: IdService,
) {
@@ -96,18 +91,18 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
isStarted: false,
isEnded: false,
logs: [],
map: eighteight.data,
map: Reversi.maps.eighteight.data,
bw: 'random',
isLlotheo: false,
}).then(x => this.reversiGamesRepository.findOneByOrFail(x.identifiers[0]));
publishReversiStream(exist.parentId, 'matched', await ReversiGames.pack(game, { id: exist.parentId }));
publishReversiStream(exist.parentId, 'matched', await this.reversiGameEntityService.pack(game, { id: exist.parentId }));
const other = await this.reversiMatchingsRepository.countBy({
childId: me.id,
});
if (other == 0) {
if (other === 0) {
publishMainStream(me.id, 'reversiNoInvites');
}
@@ -133,6 +128,18 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
}
}
@bindThis
public async matchCancel(user: MiUser) {
await this.reversiMatchingsRepository.delete({
parentId: user.id,
});
}
@bindThis
public async get(id: MiReversiGame['id']) {
return this.reversiGamesRepository.findOneBy({ id });
}
@bindThis
public dispose(): void {
}

View File

@@ -0,0 +1,38 @@
/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { ReversiService } from '@/core/ReversiService.js';
export const meta = {
requireCredential: true,
kind: 'write:account',
errors: {
},
res: {
},
} as const;
export const paramDef = {
type: 'object',
properties: {
},
required: [],
} as const;
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
constructor(
private reversiService: ReversiService,
) {
super(meta, paramDef, async (ps, me) => {
await this.reversiService.matchCancel(me);
});
}
}

View File

@@ -0,0 +1,52 @@
/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { ReversiService } from '@/core/ReversiService.js';
import { ReversiGameEntityService } from '@/core/entities/ReversiGameEntityService.js';
import { ApiError } from '../../error.js';
export const meta = {
requireCredential: false,
errors: {
noSuchGame: {
message: 'No such game.',
code: 'NO_SUCH_GAME',
id: 'f13a03db-fae1-46c9-87f3-43c8165419e1',
},
},
res: {
ref: 'ReversiGame',
},
} as const;
export const paramDef = {
type: 'object',
properties: {
gameId: { type: 'string', format: 'misskey:id' },
},
required: ['gameId'],
} as const;
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
constructor(
private reversiService: ReversiService,
private reversiGameEntityService: ReversiGameEntityService,
) {
super(meta, paramDef, async (ps, me) => {
const game = await this.reversiService.get(ps.gameId);
if (game == null) {
throw new ApiError(meta.errors.noSuchGame);
}
return await this.reversiGameEntityService.pack(game, me);
});
}
}

View File

@@ -53,6 +53,7 @@
"matter-js": "0.19.0",
"mfm-js": "0.24.0",
"misskey-js": "workspace:*",
"misskey-reversi": "workspace:*",
"photoswipe": "5.4.3",
"punycode": "2.3.1",
"rollup": "4.9.1",

View File

@@ -1,5 +1,5 @@
{
"name": "reversi",
"name": "misskey-reversi",
"version": "0.0.1",
"main": "./built/index.js",
"types": "./built/index.d.ts",
@@ -11,10 +11,10 @@
"lint": "pnpm typecheck && pnpm eslint"
},
"devDependencies": {
"@misskey-dev/eslint-plugin": "^1.0.0",
"@types/node": "20.10.5",
"@typescript-eslint/eslint-plugin": "6.14.0",
"@typescript-eslint/parser": "6.14.0",
"@misskey-dev/eslint-plugin": "1.0.0",
"@types/node": "20.11.5",
"@typescript-eslint/eslint-plugin": "6.19.0",
"@typescript-eslint/parser": "6.19.0",
"eslint": "8.56.0",
"typescript": "5.3.3"
},

View File

@@ -31,7 +31,7 @@ export type Undo = {
turn: Color | null;
};
export class ReversiGame {
export class Game {
public map: MapCell[];
public mapWidth: number;
public mapHeight: number;
@@ -79,13 +79,13 @@ export class ReversiGame {
return this.board.filter(x => x === WHITE).length;
}
public transformPosToXy(pos: number): number[] {
public posToXy(pos: number): number[] {
const x = pos % this.mapWidth;
const y = Math.floor(pos / this.mapWidth);
return [x, y];
}
public transformXyToPos(x: number, y: number): number {
public xyToPos(x: number, y: number): number {
return x + (y * this.mapWidth);
}
@@ -136,7 +136,7 @@ export class ReversiGame {
}
public mapDataGet(pos: number): MapCell {
const [x, y] = this.transformPosToXy(pos);
const [x, y] = this.posToXy(pos);
return x < 0 || y < 0 || x >= this.mapWidth || y >= this.mapHeight ? 'null' : this.map[pos];
}
@@ -164,26 +164,26 @@ export class ReversiGame {
const enemyColor = !color;
const diffVectors: [number, number][] = [
[ 0, -1], // 上
[ +1, -1], // 右上
[ +1, 0], // 右
[ +1, +1], // 右下
[ 0, +1], // 下
[ -1, +1], // 左下
[ -1, 0], // 左
[ -1, -1] // 左上
[ 0, -1], // 上
[+1, -1], // 右上
[+1, 0], // 右
[+1, +1], // 右下
[ 0, +1], // 下
[-1, +1], // 左下
[-1, 0], // 左
[-1, -1] // 左上
];
const effectsInLine = ([dx, dy]: [number, number]): number[] => {
const nextPos = (x: number, y: number): [number, number] => [x + dx, y + dy];
const found: number[] = []; // 挟めるかもしれない相手の石を入れておく配列
let [x, y] = this.transformPosToXy(initPos);
let [x, y] = this.posToXy(initPos);
while (true) {
[x, y] = nextPos(x, y);
// 座標が指し示す位置がボード外に出たとき
if (this.opts.loopedBoard && this.transformXyToPos(
if (this.opts.loopedBoard && this.xyToPos(
(x = ((x % this.mapWidth) + this.mapWidth) % this.mapWidth),
(y = ((y % this.mapHeight) + this.mapHeight) % this.mapHeight)) === initPos)
// 盤面の境界でループし、自分が石を置く位置に戻ってきたとき、挟めるようにしている (ref: Test4のマップ)
@@ -191,7 +191,7 @@ export class ReversiGame {
else if (x === -1 || y === -1 || x === this.mapWidth || y === this.mapHeight)
return []; // 挟めないことが確定 (盤面外に到達)
const pos = this.transformXyToPos(x, y);
const pos = this.xyToPos(x, y);
if (this.mapDataGet(pos) === 'null') return []; // 挟めないことが確定 (配置不可能なマスに到達)
const stone = this.board[pos];
if (stone === null) return []; // 挟めないことが確定 (石が置かれていないマスに到達)

View File

@@ -0,0 +1,7 @@
import { Game } from './game.js';
export {
Game,
};
export * as maps from './maps.js';