Refactoring, Clean up and bug fixes
This commit is contained in:
@ -1,33 +1,42 @@
|
||||
import $ from 'cafy'; import ID from '../../../../../misc/cafy-id';
|
||||
import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id';
|
||||
import ReversiGame, { pack } from '../../../../../models/games/reversi/game';
|
||||
import { ILocalUser } from '../../../../../models/user';
|
||||
import getParams from '../../../get-params';
|
||||
|
||||
export const meta = {
|
||||
params: {
|
||||
limit: {
|
||||
validator: $.num.optional.range(1, 100),
|
||||
default: 10
|
||||
},
|
||||
|
||||
sinceId: {
|
||||
validator: $.type(ID).optional,
|
||||
transform: transform,
|
||||
},
|
||||
|
||||
untilId: {
|
||||
validator: $.type(ID).optional,
|
||||
transform: transform,
|
||||
},
|
||||
|
||||
my: {
|
||||
validator: $.bool.optional,
|
||||
default: false
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
|
||||
// Get 'my' parameter
|
||||
const [my = false, myErr] = $.bool.optional.get(params.my);
|
||||
if (myErr) return rej('invalid my param');
|
||||
|
||||
// Get 'limit' parameter
|
||||
const [limit = 10, limitErr] = $.num.optional.range(1, 100).get(params.limit);
|
||||
if (limitErr) return rej('invalid limit param');
|
||||
|
||||
// Get 'sinceId' parameter
|
||||
const [sinceId, sinceIdErr] = $.type(ID).optional.get(params.sinceId);
|
||||
if (sinceIdErr) return rej('invalid sinceId param');
|
||||
|
||||
// Get 'untilId' parameter
|
||||
const [untilId, untilIdErr] = $.type(ID).optional.get(params.untilId);
|
||||
if (untilIdErr) return rej('invalid untilId param');
|
||||
const [ps, psErr] = getParams(meta, params);
|
||||
if (psErr) return rej(psErr);
|
||||
|
||||
// Check if both of sinceId and untilId is specified
|
||||
if (sinceId && untilId) {
|
||||
if (ps.sinceId && ps.untilId) {
|
||||
return rej('cannot set sinceId and untilId');
|
||||
}
|
||||
|
||||
const q: any = my ? {
|
||||
const q: any = ps.my ? {
|
||||
isStarted: true,
|
||||
$or: [{
|
||||
user1Id: user._id
|
||||
@ -42,21 +51,21 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
|
||||
_id: -1
|
||||
};
|
||||
|
||||
if (sinceId) {
|
||||
if (ps.sinceId) {
|
||||
sort._id = 1;
|
||||
q._id = {
|
||||
$gt: sinceId
|
||||
$gt: ps.sinceId
|
||||
};
|
||||
} else if (untilId) {
|
||||
} else if (ps.untilId) {
|
||||
q._id = {
|
||||
$lt: untilId
|
||||
$lt: ps.untilId
|
||||
};
|
||||
}
|
||||
|
||||
// Fetch games
|
||||
const games = await ReversiGame.find(q, {
|
||||
sort,
|
||||
limit
|
||||
sort: sort,
|
||||
limit: ps.limit
|
||||
});
|
||||
|
||||
// Reponse
|
||||
|
@ -1,14 +1,23 @@
|
||||
import $ from 'cafy'; import ID from '../../../../../../misc/cafy-id';
|
||||
import $ from 'cafy'; import ID, { transform } from '../../../../../../misc/cafy-id';
|
||||
import ReversiGame, { pack } from '../../../../../../models/games/reversi/game';
|
||||
import Reversi from '../../../../../../games/reversi/core';
|
||||
import { ILocalUser } from '../../../../../../models/user';
|
||||
import getParams from '../../../../get-params';
|
||||
|
||||
export const meta = {
|
||||
params: {
|
||||
gameId: {
|
||||
validator: $.type(ID),
|
||||
transform: transform,
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
|
||||
// Get 'gameId' parameter
|
||||
const [gameId, gameIdErr] = $.type(ID).get(params.gameId);
|
||||
if (gameIdErr) return rej('invalid gameId param');
|
||||
const [ps, psErr] = getParams(meta, params);
|
||||
if (psErr) return rej(psErr);
|
||||
|
||||
const game = await ReversiGame.findOne({ _id: gameId });
|
||||
const game = await ReversiGame.findOne({ _id: ps.gameId });
|
||||
|
||||
if (game == null) {
|
||||
return rej('game not found');
|
||||
|
@ -1,4 +1,4 @@
|
||||
import $ from 'cafy'; import ID from '../../../../../../misc/cafy-id';
|
||||
import $ from 'cafy'; import ID, { transform } from '../../../../../../misc/cafy-id';
|
||||
import ReversiGame, { pack } from '../../../../../../models/games/reversi/game';
|
||||
import { ILocalUser } from '../../../../../../models/user';
|
||||
import getParams from '../../../../get-params';
|
||||
@ -12,11 +12,13 @@ export const meta = {
|
||||
requireCredential: true,
|
||||
|
||||
params: {
|
||||
gameId: $.type(ID).note({
|
||||
gameId: {
|
||||
validator: $.type(ID),
|
||||
transform: transform,
|
||||
desc: {
|
||||
'ja-JP': '投了したい対局'
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,27 +1,34 @@
|
||||
import $ from 'cafy'; import ID from '../../../../../misc/cafy-id';
|
||||
import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id';
|
||||
import Matching, { pack as packMatching } from '../../../../../models/games/reversi/matching';
|
||||
import ReversiGame, { pack as packGame } from '../../../../../models/games/reversi/game';
|
||||
import User, { ILocalUser } from '../../../../../models/user';
|
||||
import { publishMainStream, publishReversiStream } from '../../../../../stream';
|
||||
import { eighteight } from '../../../../../games/reversi/maps';
|
||||
import getParams from '../../../get-params';
|
||||
|
||||
export const meta = {
|
||||
requireCredential: true
|
||||
requireCredential: true,
|
||||
|
||||
params: {
|
||||
userId: {
|
||||
validator: $.type(ID),
|
||||
transform: transform,
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
|
||||
// Get 'userId' parameter
|
||||
const [childId, childIdErr] = $.type(ID).get(params.userId);
|
||||
if (childIdErr) return rej('invalid userId param');
|
||||
const [ps, psErr] = getParams(meta, params);
|
||||
if (psErr) return rej(psErr);
|
||||
|
||||
// Myself
|
||||
if (childId.equals(user._id)) {
|
||||
if (ps.userId.equals(user._id)) {
|
||||
return rej('invalid userId param');
|
||||
}
|
||||
|
||||
// Find session
|
||||
const exist = await Matching.findOne({
|
||||
parentId: childId,
|
||||
parentId: ps.userId,
|
||||
childId: user._id
|
||||
});
|
||||
|
||||
@ -63,7 +70,7 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
|
||||
} else {
|
||||
// Fetch child
|
||||
const child = await User.findOne({
|
||||
_id: childId
|
||||
_id: ps.userId
|
||||
}, {
|
||||
fields: {
|
||||
_id: true
|
||||
|
Reference in New Issue
Block a user