-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feat-12909
- Loading branch information
Showing
22 changed files
with
969 additions
and
421 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/backend/migration/1704959805077-bubble-game-record.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* SPDX-FileCopyrightText: syuilo and other misskey contributors | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
export class BubbleGameRecord1704959805077 { | ||
name = 'BubbleGameRecord1704959805077' | ||
|
||
async up(queryRunner) { | ||
await queryRunner.query(`CREATE TABLE "bubble_game_record" ("id" character varying(32) NOT NULL, "userId" character varying(32) NOT NULL, "seededAt" TIMESTAMP WITH TIME ZONE NOT NULL, "seed" character varying(1024) NOT NULL, "gameVersion" integer NOT NULL, "gameMode" character varying(128) NOT NULL, "score" integer NOT NULL, "logs" jsonb NOT NULL DEFAULT '[]', "isVerified" boolean NOT NULL DEFAULT false, CONSTRAINT "PK_a75395fe404b392e2893b50d7ea" PRIMARY KEY ("id"))`); | ||
await queryRunner.query(`CREATE INDEX "IDX_75276757070d21fdfaf4c05290" ON "bubble_game_record" ("userId") `); | ||
await queryRunner.query(`CREATE INDEX "IDX_4ae7053179014915d1432d3f40" ON "bubble_game_record" ("seededAt") `); | ||
await queryRunner.query(`CREATE INDEX "IDX_26d4ee490b5a487142d35466ee" ON "bubble_game_record" ("score") `); | ||
await queryRunner.query(`ALTER TABLE "bubble_game_record" ADD CONSTRAINT "FK_75276757070d21fdfaf4c052909" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); | ||
} | ||
|
||
async down(queryRunner) { | ||
await queryRunner.query(`ALTER TABLE "bubble_game_record" DROP CONSTRAINT "FK_75276757070d21fdfaf4c052909"`); | ||
await queryRunner.query(`DROP INDEX "public"."IDX_26d4ee490b5a487142d35466ee"`); | ||
await queryRunner.query(`DROP INDEX "public"."IDX_4ae7053179014915d1432d3f40"`); | ||
await queryRunner.query(`DROP INDEX "public"."IDX_75276757070d21fdfaf4c05290"`); | ||
await queryRunner.query(`DROP TABLE "bubble_game_record"`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* SPDX-FileCopyrightText: syuilo and other misskey contributors | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import { PrimaryColumn, Entity, Index, JoinColumn, Column, ManyToOne } from 'typeorm'; | ||
import { id } from './util/id.js'; | ||
import { MiUser } from './User.js'; | ||
|
||
@Entity('bubble_game_record') | ||
export class MiBubbleGameRecord { | ||
@PrimaryColumn(id()) | ||
public id: string; | ||
|
||
@Index() | ||
@Column({ | ||
...id(), | ||
}) | ||
public userId: MiUser['id']; | ||
|
||
@ManyToOne(type => MiUser, { | ||
onDelete: 'CASCADE', | ||
}) | ||
@JoinColumn() | ||
public user: MiUser | null; | ||
|
||
@Index() | ||
@Column('timestamp with time zone') | ||
public seededAt: Date; | ||
|
||
@Column('varchar', { | ||
length: 1024, | ||
}) | ||
public seed: string; | ||
|
||
@Column('integer') | ||
public gameVersion: number; | ||
|
||
@Column('varchar', { | ||
length: 128, | ||
}) | ||
public gameMode: string; | ||
|
||
@Index() | ||
@Column('integer') | ||
public score: number; | ||
|
||
@Column('jsonb', { | ||
default: [], | ||
}) | ||
public logs: any[]; | ||
|
||
@Column('boolean', { | ||
default: false, | ||
}) | ||
public isVerified: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/backend/src/server/api/endpoints/bubble-game/ranking.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* SPDX-FileCopyrightText: syuilo and other misskey contributors | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { MoreThan } from 'typeorm'; | ||
import { Endpoint } from '@/server/api/endpoint-base.js'; | ||
import type { BubbleGameRecordsRepository } from '@/models/_.js'; | ||
import { DI } from '@/di-symbols.js'; | ||
import { UserEntityService } from '@/core/entities/UserEntityService.js'; | ||
|
||
export const meta = { | ||
allowGet: true, | ||
cacheSec: 60, | ||
|
||
errors: { | ||
}, | ||
|
||
res: { | ||
type: 'array', | ||
optional: false, nullable: false, | ||
items: { | ||
type: 'object', | ||
optional: false, nullable: false, | ||
properties: { | ||
id: { type: 'string', format: 'misskey:id' }, | ||
score: { type: 'integer' }, | ||
user: { ref: 'UserLite' }, | ||
}, | ||
}, | ||
}, | ||
} as const; | ||
|
||
export const paramDef = { | ||
type: 'object', | ||
properties: { | ||
gameMode: { type: 'string' }, | ||
}, | ||
required: ['gameMode'], | ||
} as const; | ||
|
||
@Injectable() | ||
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export | ||
constructor( | ||
@Inject(DI.bubbleGameRecordsRepository) | ||
private bubbleGameRecordsRepository: BubbleGameRecordsRepository, | ||
|
||
private userEntityService: UserEntityService, | ||
) { | ||
super(meta, paramDef, async (ps) => { | ||
const records = await this.bubbleGameRecordsRepository.find({ | ||
where: { | ||
gameMode: ps.gameMode, | ||
seededAt: MoreThan(new Date(Date.now() - 1000 * 60 * 60 * 24 * 7)), | ||
}, | ||
order: { | ||
score: 'DESC', | ||
}, | ||
take: 10, | ||
relations: ['user'], | ||
}); | ||
|
||
const users = await this.userEntityService.packMany(records.map(r => r.user!), null, { detail: false }); | ||
|
||
return records.map(r => ({ | ||
id: r.id, | ||
score: r.score, | ||
user: users.find(u => u.id === r.user!.id), | ||
})); | ||
}); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
packages/backend/src/server/api/endpoints/bubble-game/register.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* SPDX-FileCopyrightText: syuilo and other misskey contributors | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import { Inject, Injectable } from '@nestjs/common'; | ||
import ms from 'ms'; | ||
import { Endpoint } from '@/server/api/endpoint-base.js'; | ||
import { IdService } from '@/core/IdService.js'; | ||
import type { BubbleGameRecordsRepository } from '@/models/_.js'; | ||
import { DI } from '@/di-symbols.js'; | ||
import { ApiError } from '../../error.js'; | ||
|
||
export const meta = { | ||
requireCredential: true, | ||
|
||
kind: 'write:account', | ||
|
||
limit: { | ||
duration: ms('1hour'), | ||
max: 120, | ||
minInterval: ms('30sec'), | ||
}, | ||
|
||
errors: { | ||
invalidSeed: { | ||
message: 'Provided seed is invalid.', | ||
code: 'INVALID_SEED', | ||
id: 'eb627bc7-574b-4a52-a860-3c3eae772b88', | ||
}, | ||
}, | ||
|
||
res: { | ||
}, | ||
} as const; | ||
|
||
export const paramDef = { | ||
type: 'object', | ||
properties: { | ||
score: { type: 'integer', minimum: 0 }, | ||
seed: { type: 'string', minLength: 1, maxLength: 1024 }, | ||
logs: { type: 'array' }, | ||
gameMode: { type: 'string' }, | ||
gameVersion: { type: 'integer' }, | ||
}, | ||
required: ['score', 'seed', 'logs', 'gameMode', 'gameVersion'], | ||
} as const; | ||
|
||
@Injectable() | ||
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export | ||
constructor( | ||
@Inject(DI.bubbleGameRecordsRepository) | ||
private bubbleGameRecordsRepository: BubbleGameRecordsRepository, | ||
|
||
private idService: IdService, | ||
) { | ||
super(meta, paramDef, async (ps, me) => { | ||
const seedDate = new Date(parseInt(ps.seed, 10)); | ||
const now = new Date(); | ||
|
||
// シードが未来なのは通常のプレイではありえないので弾く | ||
if (seedDate.getTime() > now.getTime()) { | ||
throw new ApiError(meta.errors.invalidSeed); | ||
} | ||
|
||
// シードが古すぎる(1時間以上前)のも弾く | ||
if (seedDate.getTime() < now.getTime() - 1000 * 60 * 60) { | ||
throw new ApiError(meta.errors.invalidSeed); | ||
} | ||
|
||
await this.bubbleGameRecordsRepository.insert({ | ||
id: this.idService.gen(now.getTime()), | ||
seed: ps.seed, | ||
seededAt: seedDate, | ||
userId: me.id, | ||
score: ps.score, | ||
logs: ps.logs, | ||
gameMode: ps.gameMode, | ||
gameVersion: ps.gameVersion, | ||
isVerified: false, | ||
}); | ||
}); | ||
} | ||
} |
Oops, something went wrong.