Use PostgreSQL instead of MongoDB (#4572)

* wip

* Update note.ts

* Update timeline.ts

* Update core.ts

* wip

* Update generate-visibility-query.ts

* wip

* wip

* wip

* wip

* wip

* Update global-timeline.ts

* wip

* wip

* wip

* Update vote.ts

* wip

* wip

* Update create.ts

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Update files.ts

* wip

* wip

* Update CONTRIBUTING.md

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Update read-notification.ts

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Update cancel.ts

* wip

* wip

* wip

* Update show.ts

* wip

* wip

* Update gen-id.ts

* Update create.ts

* Update id.ts

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Docker: Update files about Docker (#4599)

* Docker: Use cache if files used by `yarn install` was not updated

This patch reduces the number of times to installing node_modules.
For example, `yarn install` step will be skipped when only ".config/default.yml" is updated.

* Docker: Migrate MongoDB to Postgresql

Misskey uses Postgresql as a database instead of Mongodb since version 11.

* Docker: Uncomment about data persistence

This patch will save a lot of databases.

* wip

* wip

* wip

* Update activitypub.ts

* wip

* wip

* wip

* Update logs.ts

* wip

* Update drive-file.ts

* Update register.ts

* wip

* wip

* Update mentions.ts

* wip

* wip

* wip

* Update recommendation.ts

* wip

* Update index.ts

* wip

* Update recommendation.ts

* Doc: Update docker.ja.md and docker.en.md (#1) (#4608)

Update how to set up misskey.

* wip

* ✌️

* wip

* Update note.ts

* Update postgre.ts

* wip

* wip

* wip

* wip

* Update add-file.ts

* wip

* wip

* wip

* Clean up

* Update logs.ts

* wip

* 🍕

* wip

* Ad notes

* wip

* Update api-visibility.ts

* Update note.ts

* Update add-file.ts

* tests

* tests

* Update postgre.ts

* Update utils.ts

* wip

* wip

* Refactor

* wip

* Refactor

* wip

* wip

* Update show-users.ts

* Update update-instance.ts

* wip

* Update feed.ts

* Update outbox.ts

* Update outbox.ts

* Update user.ts

* wip

* Update list.ts

* Update update-hashtag.ts

* wip

* Update update-hashtag.ts

* Refactor

* Update update.ts

* wip

* wip

* ✌️

* clean up

* docs

* Update push.ts

* wip

* Update api.ts

* wip

* ✌️

* Update make-pagination-query.ts

* ✌️

* Delete hashtags.ts

* Update instances.ts

* Update instances.ts

* Update create.ts

* Update search.ts

* Update reversi-game.ts

* Update signup.ts

* Update user.ts

* id

* Update example.yml

* 🎨

* objectid

* fix

* reversi

* reversi

* Fix bug of chart engine

* Add test of chart engine

* Improve test

* Better testing

* Improve chart engine

* Refactor

* Add test of chart engine

* Refactor

* Add chart test

* Fix bug

* コミットし忘れ

* Refactoring

* ✌️

* Add tests

* Add test

* Extarct note tests

* Refactor

* 存在しないユーザーにメンションできなくなっていた問題を修正

* Fix bug

* Update update-meta.ts

* Fix bug

* Update mention.vue

* Fix bug

* Update meta.ts

* Update CONTRIBUTING.md

* Fix bug

* Fix bug

* Fix bug

* Clean up

* Clean up

* Update notification.ts

* Clean up

* Add mute tests

* Add test

* Refactor

* Add test

* Fix test

* Refactor

* Refactor

* Add tests

* Update utils.ts

* Update utils.ts

* Fix test

* Update package.json

* Update update.ts

* Update manifest.ts

* Fix bug

* Fix bug

* Add test

* 🎨

* Update endpoint permissions

* Updaye permisison

* Update person.ts

#4299

* データベースと同期しないように

* Fix bug

* Fix bug

* Update reversi-game.ts

* Use a feature of Node v11.7.0 to extract a public key (#4644)

* wip

* wip

* ✌️

* Refactoring

#1540

* test

* test

* test

* test

* test

* test

* test

* Fix bug

* Fix test

* 🍣

* wip

* #4471

* Add test for #4335

* Refactor

* Fix test

* Add tests

* 🕓

* Fix bug

* Add test

* Add test

* rename

* Fix bug
This commit is contained in:
syuilo 2019-04-07 21:50:36 +09:00 committed by GitHub
parent 13caf37991
commit f0a29721c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
592 changed files with 13463 additions and 14147 deletions

View file

@ -1,22 +1,22 @@
import autobind from 'autobind-decorator';
import * as CRC32 from 'crc-32';
import * as mongo from 'mongodb';
import ReversiGame, { pack } from '../../../../../models/games/reversi/game';
import { publishReversiGameStream } from '../../../../../services/stream';
import Reversi from '../../../../../games/reversi/core';
import * as maps from '../../../../../games/reversi/maps';
import Channel from '../../channel';
import { ReversiGame } from '../../../../../models/entities/games/reversi/game';
import { ReversiGames } from '../../../../../models';
export default class extends Channel {
public readonly chName = 'gamesReversiGame';
public static shouldShare = false;
public static requireCredential = false;
private gameId: mongo.ObjectID;
private gameId: ReversiGame['id'];
@autobind
public async init(params: any) {
this.gameId = new mongo.ObjectID(params.gameId as string);
this.gameId = params.gameId;
// Subscribe game stream
this.subscriber.on(`reversiGameStream:${this.gameId}`, data => {
@ -29,7 +29,7 @@ export default class extends Channel {
switch (type) {
case 'accept': this.accept(true); break;
case 'cancelAccept': this.accept(false); break;
case 'updateSettings': this.updateSettings(body.settings); break;
case 'updateSettings': this.updateSettings(body.key, body.value); break;
case 'initForm': this.initForm(body); break;
case 'updateForm': this.updateForm(body.id, body.value); break;
case 'message': this.message(body); break;
@ -39,54 +39,55 @@ export default class extends Channel {
}
@autobind
private async updateSettings(settings: any) {
const game = await ReversiGame.findOne({ _id: this.gameId });
private async updateSettings(key: string, value: any) {
const game = await ReversiGames.findOne(this.gameId);
if (game.isStarted) return;
if (!game.user1Id.equals(this.user._id) && !game.user2Id.equals(this.user._id)) return;
if (game.user1Id.equals(this.user._id) && game.user1Accepted) return;
if (game.user2Id.equals(this.user._id) && game.user2Accepted) return;
if ((game.user1Id !== this.user.id) && (game.user2Id !== this.user.id)) return;
if ((game.user1Id === this.user.id) && game.user1Accepted) return;
if ((game.user2Id === this.user.id) && game.user2Accepted) return;
await ReversiGame.update({ _id: this.gameId }, {
$set: {
settings
}
if (!['map', 'bw', 'isLlotheo', 'canPutEverywhere', 'loopedBoard'].includes(key)) return;
await ReversiGames.update({ id: this.gameId }, {
[key]: value
});
publishReversiGameStream(this.gameId, 'updateSettings', settings);
publishReversiGameStream(this.gameId, 'updateSettings', {
key: key,
value: value
});
}
@autobind
private async initForm(form: any) {
const game = await ReversiGame.findOne({ _id: this.gameId });
const game = await ReversiGames.findOne(this.gameId);
if (game.isStarted) return;
if (!game.user1Id.equals(this.user._id) && !game.user2Id.equals(this.user._id)) return;
if ((game.user1Id !== this.user.id) && (game.user2Id !== this.user.id)) return;
const set = game.user1Id.equals(this.user._id) ? {
const set = game.user1Id === this.user.id ? {
form1: form
} : {
form2: form
};
form2: form
};
await ReversiGame.update({ _id: this.gameId }, {
$set: set
});
await ReversiGames.update({ id: this.gameId }, set);
publishReversiGameStream(this.gameId, 'initForm', {
userId: this.user._id,
userId: this.user.id,
form
});
}
@autobind
private async updateForm(id: string, value: any) {
const game = await ReversiGame.findOne({ _id: this.gameId });
const game = await ReversiGames.findOne({ id: this.gameId });
if (game.isStarted) return;
if (!game.user1Id.equals(this.user._id) && !game.user2Id.equals(this.user._id)) return;
if ((game.user1Id !== this.user.id) && (game.user2Id !== this.user.id)) return;
const form = game.user1Id.equals(this.user._id) ? game.form2 : game.form1;
const form = game.user1Id === this.user.id ? game.form2 : game.form1;
const item = form.find((i: any) => i.id == id);
@ -94,18 +95,16 @@ export default class extends Channel {
item.value = value;
const set = game.user1Id.equals(this.user._id) ? {
const set = game.user1Id === this.user.id ? {
form2: form
} : {
form1: form
};
await ReversiGame.update({ _id: this.gameId }, {
$set: set
});
await ReversiGames.update({ id: this.gameId }, set);
publishReversiGameStream(this.gameId, 'updateForm', {
userId: this.user._id,
userId: this.user.id,
id,
value
});
@ -115,24 +114,22 @@ export default class extends Channel {
private async message(message: any) {
message.id = Math.random();
publishReversiGameStream(this.gameId, 'message', {
userId: this.user._id,
userId: this.user.id,
message
});
}
@autobind
private async accept(accept: boolean) {
const game = await ReversiGame.findOne({ _id: this.gameId });
const game = await ReversiGames.findOne(this.gameId);
if (game.isStarted) return;
let bothAccepted = false;
if (game.user1Id.equals(this.user._id)) {
await ReversiGame.update({ _id: this.gameId }, {
$set: {
user1Accepted: accept
}
if (game.user1Id === this.user.id) {
await ReversiGames.update({ id: this.gameId }, {
user1Accepted: accept
});
publishReversiGameStream(this.gameId, 'changeAccepts', {
@ -141,11 +138,9 @@ export default class extends Channel {
});
if (accept && game.user2Accepted) bothAccepted = true;
} else if (game.user2Id.equals(this.user._id)) {
await ReversiGame.update({ _id: this.gameId }, {
$set: {
user2Accepted: accept
}
} else if (game.user2Id === this.user.id) {
await ReversiGames.update({ id: this.gameId }, {
user2Accepted: accept
});
publishReversiGameStream(this.gameId, 'changeAccepts', {
@ -161,15 +156,15 @@ export default class extends Channel {
if (bothAccepted) {
// 3秒後、まだacceptされていたらゲーム開始
setTimeout(async () => {
const freshGame = await ReversiGame.findOne({ _id: this.gameId });
const freshGame = await ReversiGames.findOne(this.gameId);
if (freshGame == null || freshGame.isStarted || freshGame.isEnded) return;
if (!freshGame.user1Accepted || !freshGame.user2Accepted) return;
let bw: number;
if (freshGame.settings.bw == 'random') {
if (freshGame.bw == 'random') {
bw = Math.random() > 0.5 ? 1 : 2;
} else {
bw = freshGame.settings.bw as number;
bw = parseInt(freshGame.bw, 10);
}
function getRandomMap() {
@ -178,22 +173,20 @@ export default class extends Channel {
return Object.values(maps)[rnd].data;
}
const map = freshGame.settings.map != null ? freshGame.settings.map : getRandomMap();
const map = freshGame.map != null ? freshGame.map : getRandomMap();
await ReversiGame.update({ _id: this.gameId }, {
$set: {
startedAt: new Date(),
isStarted: true,
black: bw,
'settings.map': map
}
await ReversiGames.update({ id: this.gameId }, {
startedAt: new Date(),
isStarted: true,
black: bw,
map: map
});
//#region 盤面に最初から石がないなどして始まった瞬間に勝敗が決定する場合があるのでその処理
const o = new Reversi(map, {
isLlotheo: freshGame.settings.isLlotheo,
canPutEverywhere: freshGame.settings.canPutEverywhere,
loopedBoard: freshGame.settings.loopedBoard
isLlotheo: freshGame.isLlotheo,
canPutEverywhere: freshGame.canPutEverywhere,
loopedBoard: freshGame.loopedBoard
});
if (o.isEnded) {
@ -206,23 +199,22 @@ export default class extends Channel {
winner = null;
}
await ReversiGame.update({
_id: this.gameId
await ReversiGames.update({
id: this.gameId
}, {
$set: {
isEnded: true,
winnerId: winner
}
});
isEnded: true,
winnerId: winner
});
publishReversiGameStream(this.gameId, 'ended', {
winnerId: winner,
game: await pack(this.gameId, this.user)
game: await ReversiGames.pack(this.gameId, this.user)
});
}
//#endregion
publishReversiGameStream(this.gameId, 'started', await pack(this.gameId, this.user));
publishReversiGameStream(this.gameId, 'started',
await ReversiGames.pack(this.gameId, this.user));
}, 3000);
}
}
@ -230,16 +222,16 @@ export default class extends Channel {
// 石を打つ
@autobind
private async set(pos: number) {
const game = await ReversiGame.findOne({ _id: this.gameId });
const game = await ReversiGames.findOne(this.gameId);
if (!game.isStarted) return;
if (game.isEnded) return;
if (!game.user1Id.equals(this.user._id) && !game.user2Id.equals(this.user._id)) return;
if ((game.user1Id !== this.user.id) && (game.user2Id !== this.user.id)) return;
const o = new Reversi(game.settings.map, {
isLlotheo: game.settings.isLlotheo,
canPutEverywhere: game.settings.canPutEverywhere,
loopedBoard: game.settings.loopedBoard
const o = new Reversi(game.map, {
isLlotheo: game.isLlotheo,
canPutEverywhere: game.canPutEverywhere,
loopedBoard: game.loopedBoard
});
for (const log of game.logs) {
@ -247,7 +239,7 @@ export default class extends Channel {
}
const myColor =
(game.user1Id.equals(this.user._id) && game.black == 1) || (game.user2Id.equals(this.user._id) && game.black == 2)
((game.user1Id === this.user.id) && game.black == 1) || ((game.user2Id === this.user.id) && game.black == 2)
? true
: false;
@ -271,20 +263,18 @@ export default class extends Channel {
pos
};
const crc32 = CRC32.str(game.logs.map(x => x.pos.toString()).join('') + pos.toString());
const crc32 = CRC32.str(game.logs.map(x => x.pos.toString()).join('') + pos.toString()).toString();
await ReversiGame.update({
_id: this.gameId
game.logs.push(log);
await ReversiGames.update({
id: this.gameId
}, {
$set: {
crc32,
isEnded: o.isEnded,
winnerId: winner
},
$push: {
logs: log
}
});
crc32,
isEnded: o.isEnded,
winnerId: winner,
logs: game.logs
});
publishReversiGameStream(this.gameId, 'set', Object.assign(log, {
next: o.turn
@ -293,14 +283,14 @@ export default class extends Channel {
if (o.isEnded) {
publishReversiGameStream(this.gameId, 'ended', {
winnerId: winner,
game: await pack(this.gameId, this.user)
game: await ReversiGames.pack(this.gameId, this.user)
});
}
}
@autobind
private async check(crc32: string) {
const game = await ReversiGame.findOne({ _id: this.gameId });
const game = await ReversiGames.findOne(this.gameId);
if (!game.isStarted) return;
@ -308,7 +298,7 @@ export default class extends Channel {
if (game.crc32 == null) return;
if (crc32 !== game.crc32) {
this.send('rescue', await pack(game, this.user));
this.send('rescue', await ReversiGames.pack(game, this.user));
}
}
}

View file

@ -1,8 +1,7 @@
import autobind from 'autobind-decorator';
import * as mongo from 'mongodb';
import Matching, { pack } from '../../../../../models/games/reversi/matching';
import { publishMainStream } from '../../../../../services/stream';
import Channel from '../../channel';
import { ReversiMatchings } from '../../../../../models';
export default class extends Channel {
public readonly chName = 'gamesReversi';
@ -12,7 +11,7 @@ export default class extends Channel {
@autobind
public async init(params: any) {
// Subscribe reversi stream
this.subscriber.on(`reversiStream:${this.user._id}`, data => {
this.subscriber.on(`reversiStream:${this.user.id}`, data => {
this.send(data);
});
}
@ -22,12 +21,12 @@ export default class extends Channel {
switch (type) {
case 'ping':
if (body.id == null) return;
const matching = await Matching.findOne({
parentId: this.user._id,
childId: new mongo.ObjectID(body.id)
const matching = await ReversiMatchings.findOne({
parentId: this.user.id,
childId: body.id
});
if (matching == null) return;
publishMainStream(matching.childId, 'reversiInvited', await pack(matching, matching.childId));
publishMainStream(matching.childId, 'reversiInvited', await ReversiMatchings.pack(matching, matching.childId));
break;
}
}