Merge branch 'develop' of https://github.com/syuilo/misskey into develop

This commit is contained in:
syuilo 2018-12-17 20:18:09 +09:00
commit 0556a2a2da
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
5 changed files with 44 additions and 64 deletions

View file

@ -76,27 +76,14 @@ export default class Reversi {
this.mapHeight = map.length; this.mapHeight = map.length;
const mapData = map.join(''); const mapData = map.join('');
this.board = mapData.split('').map(d => { this.board = mapData.split('').map(d => d === '-' ? null : d === 'b' ? BLACK : d === 'w' ? WHITE : undefined);
if (d == '-') return null;
if (d == 'b') return BLACK;
if (d == 'w') return WHITE;
return undefined;
});
this.map = mapData.split('').map(d => { this.map = mapData.split('').map(d => d === '-' || d === 'b' || d === 'w' ? 'empty' : 'null');
if (d == '-' || d == 'b' || d == 'w') return 'empty';
return 'null';
});
//#endregion //#endregion
// ゲームが始まった時点で片方の色の石しかないか、始まった時点で勝敗が決定するようなマップの場合がある // ゲームが始まった時点で片方の色の石しかないか、始まった時点で勝敗が決定するようなマップの場合がある
if (!this.canPutSomewhere(BLACK)) { if (!this.canPutSomewhere(BLACK))
if (!this.canPutSomewhere(WHITE)) { this.turn = this.canPutSomewhere(WHITE) ? WHITE : null;
this.turn = null;
} else {
this.turn = WHITE;
}
}
} }
/** /**
@ -117,16 +104,14 @@ export default class Reversi {
* *
*/ */
public get blackP() { public get blackP() {
if (this.blackCount == 0 && this.whiteCount == 0) return 0; return this.blackCount == 0 && this.whiteCount == 0 ? 0 : this.blackCount / (this.blackCount + this.whiteCount);
return this.blackCount / (this.blackCount + this.whiteCount);
} }
/** /**
* *
*/ */
public get whiteP() { public get whiteP() {
if (this.blackCount == 0 && this.whiteCount == 0) return 0; return this.blackCount == 0 && this.whiteCount == 0 ? 0 : this.whiteCount / (this.blackCount + this.whiteCount);
return this.whiteCount / (this.blackCount + this.whiteCount);
} }
public transformPosToXy(pos: number): number[] { public transformPosToXy(pos: number): number[] {
@ -172,13 +157,10 @@ export default class Reversi {
private calcTurn() { private calcTurn() {
// ターン計算 // ターン計算
if (this.canPutSomewhere(!this.prevColor)) { this.turn =
this.turn = !this.prevColor; this.canPutSomewhere(!this.prevColor) ? !this.prevColor :
} else if (this.canPutSomewhere(this.prevColor)) { this.canPutSomewhere(this.prevColor) ? this.prevColor :
this.turn = this.prevColor; null;
} else {
this.turn = null;
}
} }
public undo() { public undo() {
@ -199,8 +181,7 @@ export default class Reversi {
*/ */
public mapDataGet(pos: number): MapPixel { public mapDataGet(pos: number): MapPixel {
const [x, y] = this.transformPosToXy(pos); const [x, y] = this.transformPosToXy(pos);
if (x < 0 || y < 0 || x >= this.mapWidth || y >= this.mapHeight) return 'null'; return x < 0 || y < 0 || x >= this.mapWidth || y >= this.mapHeight ? 'null' : this.map[pos];
return this.map[pos];
} }
/** /**
@ -223,16 +204,10 @@ export default class Reversi {
* @param pos * @param pos
*/ */
public canPut(color: Color, pos: number): boolean { public canPut(color: Color, pos: number): boolean {
// 既に石が置いてある場所には打てない return (
if (this.board[pos] !== null) return false; this.board[pos] !== null ? false : // 既に石が置いてある場所には打てない
this.opts.canPutEverywhere ? this.mapDataGet(pos) == 'empty' : // 挟んでなくても置けるモード
if (this.opts.canPutEverywhere) { this.effects(color, pos).length !== 0); // 相手の石を1つでも反転させられるか
// 挟んでなくても置けるモード
return this.mapDataGet(pos) == 'empty';
} else {
// 相手の石を1つでも反転させられるか
return this.effects(color, pos).length !== 0;
}
} }
/** /**
@ -263,19 +238,13 @@ export default class Reversi {
[x, y] = nextPos(x, y); [x, y] = nextPos(x, y);
// 座標が指し示す位置がボード外に出たとき // 座標が指し示す位置がボード外に出たとき
if (this.opts.loopedBoard) { if (this.opts.loopedBoard && this.transformXyToPos(
x = ((x % this.mapWidth) + this.mapWidth) % this.mapWidth; (x = ((x % this.mapWidth) + this.mapWidth) % this.mapWidth),
y = ((y % this.mapHeight) + this.mapHeight) % this.mapHeight; (y = ((y % this.mapHeight) + this.mapHeight) % this.mapHeight)) == initPos)
if (this.transformXyToPos(x, y) == initPos) {
// 盤面の境界でループし、自分が石を置く位置に戻ってきたとき、挟めるようにしている (ref: Test4のマップ) // 盤面の境界でループし、自分が石を置く位置に戻ってきたとき、挟めるようにしている (ref: Test4のマップ)
return found; return found;
} else if (x == -1 || y == -1 || x == this.mapWidth || y == this.mapHeight)
} else {
if (x == -1 || y == -1 || x == this.mapWidth || y == this.mapHeight) {
return []; // 挟めないことが確定 (盤面外に到達) return []; // 挟めないことが確定 (盤面外に到達)
}
}
const pos = this.transformXyToPos(x, y); const pos = this.transformXyToPos(x, y);
if (this.mapDataGet(pos) === 'null') return []; // 挟めないことが確定 (配置不可能なマスに到達) if (this.mapDataGet(pos) === 'null') return []; // 挟めないことが確定 (配置不可能なマスに到達)
@ -300,14 +269,9 @@ export default class Reversi {
* (null = ) * (null = )
*/ */
public get winner(): Color { public get winner(): Color {
if (!this.isEnded) return undefined; return this.isEnded ?
this.blackCount == this.whiteCount ? null :
if (this.blackCount == this.whiteCount) return null; this.opts.isLlotheo === this.blackCount > this.whiteCount ? WHITE : BLACK :
undefined;
if (this.opts.isLlotheo) {
return this.blackCount > this.whiteCount ? WHITE : BLACK;
} else {
return this.blackCount > this.whiteCount ? BLACK : WHITE;
}
} }
} }

View file

@ -275,7 +275,7 @@ const mfm = P.createLanguage({
mention: r => mention: r =>
P((input, i) => { P((input, i) => {
const text = input.substr(i); const text = input.substr(i);
const match = text.match(/^@[a-z0-9_]+(?:@[a-z0-9\.\-]+[a-z0-9])?/i); const match = text.match(/^@\w([\w-]*\w)?(?:@[\w\.\-]+\w)?/);
if (!match) return P.makeFailure(i, 'not a mention'); if (!match) return P.makeFailure(i, 'not a mention');
if (input[i - 1] != null && input[i - 1].match(/[a-z0-9]/i)) return P.makeFailure(i, 'not a mention'); if (input[i - 1] != null && input[i - 1].match(/[a-z0-9]/i)) return P.makeFailure(i, 'not a mention');
return P.makeSuccess(i + match[0].length, match[0]); return P.makeSuccess(i + match[0].length, match[0]);

View file

@ -155,7 +155,7 @@ export const isRemoteUser = (user: any): user is IRemoteUser =>
//#region Validators //#region Validators
export function validateUsername(username: string, remote?: boolean): boolean { export function validateUsername(username: string, remote?: boolean): boolean {
return typeof username == 'string' && (remote ? /^\w+([\w\.-]+\w+)?$/ : /^[a-zA-Z0-9_]{1,20}$/).test(username); return typeof username == 'string' && (remote ? /^\w([\w-]*\w)?$/ : /^\w{1,20}$/).test(username);
} }
export function validatePassword(password: string): boolean { export function validatePassword(password: string): boolean {

View file

@ -53,7 +53,7 @@ export const meta = {
}; };
export default define(meta, (ps, me) => new Promise(async (res, rej) => { export default define(meta, (ps, me) => new Promise(async (res, rej) => {
const isUsername = validateUsername(ps.query.replace('@', ''), true); const isUsername = validateUsername(ps.query.replace('@', ''), !ps.localOnly);
let users: IUser[] = []; let users: IUser[] = [];

View file

@ -168,6 +168,22 @@ describe('Text', () => {
]), ]),
node('mention', { acct: '@a', canonical: '@a', username: 'a', host: null }) node('mention', { acct: '@a', canonical: '@a', username: 'a', host: null })
], tokens3); ], tokens3);
const tokens4 = analyze('@\n@v\n@veryverylongusername' /* \n@toolongtobeasamention */ );
assert.deepEqual([
text('@\n'),
node('mention', { acct: '@v', canonical: '@v', username: 'v', host: null }),
text('\n'),
node('mention', { acct: '@veryverylongusername', canonical: '@veryverylongusername', username: 'veryverylongusername', host: null }),
// text('\n@toolongtobeasamention')
], tokens4);
/*
const tokens5 = analyze('@domain_is@valid.example.com\n@domain_is@.invalid\n@domain_is@invali.d\n@domain_is@invali.d\n@domain_is@-invalid.com\n@domain_is@invalid-.com');
assert.deepEqual([
node('mention', { acct: '@domain_is@valid.example.com', canonical: '@domain_is@valid.example.com', username: 'domain_is', host: 'valid.example.com' }),
text('\n@domain_is@.invalid\n@domain_is@invali.d\n@domain_is@invali.d\n@domain_is@-invalid.com\n@domain_is@invalid-.com')
], tokens5);
*/
}); });
}); });