summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/endpoints/bubble-game/register.ts
blob: 8eb90fdbf9cc43f44eb5a00bdbda6496ba28b0c5 (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
/*
 * 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);
			}

			// シードが古すぎる(5時間以上前)のも弾く
			if (seedDate.getTime() < now.getTime() - 1000 * 60 * 60 * 5) {
				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,
			});
		});
	}
}