Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
syuilo committed Feb 3, 2024
1 parent 7cdaa10 commit 2dd886e
Show file tree
Hide file tree
Showing 10 changed files with 351 additions and 133 deletions.
2 changes: 2 additions & 0 deletions packages/backend/src/core/GlobalEventService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,13 @@ export interface MahjongRoomEventTypes {
dahai: {
house: Mahjong.Common.House;
tile: Mahjong.Common.Tile;
riichi: boolean;
};
dahaiAndTsumo: {
dahaiHouse: Mahjong.Common.House;
dahaiTile: Mahjong.Common.Tile;
tsumoTile: Mahjong.Common.Tile;
riichi: boolean;
};
ponned: {
caller: Mahjong.Common.House;
Expand Down
38 changes: 15 additions & 23 deletions packages/backend/src/core/MahjongService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { ReversiGameEntityService } from './entities/ReversiGameEntityService.js
import type { OnApplicationShutdown, OnModuleInit } from '@nestjs/common';

const INVITATION_TIMEOUT_MS = 1000 * 20; // 20sec
const CALL_AND_RON_ASKING_TIMEOUT_MS = 1000 * 5; // 5sec
const CALL_AND_RON_ASKING_TIMEOUT_MS = 1000 * 7; // 7sec
const TURN_TIMEOUT_MS = 1000 * 30; // 30sec
const NEXT_KYOKU_CONFIRMATION_TIMEOUT_MS = 1000 * 15; // 15sec

Expand Down Expand Up @@ -318,7 +318,7 @@ export class MahjongService implements OnApplicationShutdown, OnModuleInit {
switch (res.type) {
case 'tsumo':
this.globalEventService.publishMahjongRoomStream(room.id, 'tsumo', { house: res.house, tile: res.tile });
this.next(room, engine);
this.waitForTurn(room, res.turn, engine);
break;
case 'ponned':
this.globalEventService.publishMahjongRoomStream(room.id, 'ponned', { caller: res.caller, callee: res.callee, tile: res.tile });
Expand Down Expand Up @@ -349,23 +349,6 @@ export class MahjongService implements OnApplicationShutdown, OnModuleInit {
}
}

@bindThis
private async next(room: Room, engine: Mahjong.MasterGameEngine) {
const turn = engine.state.turn;
if (turn == null) throw new Error('turn is null');

const aiHouses = [[1, room.user1Ai], [2, room.user2Ai], [3, room.user3Ai], [4, room.user4Ai]].filter(([id, ai]) => ai).map(([id, ai]) => engine.getHouse(id));

if (aiHouses.includes(turn)) {
// TODO: ちゃんと思考するようにする
setTimeout(() => {
this.dahai(room, engine, turn, engine.state.handTiles[turn].at(-1));
}, 500);
} else {
this.waitForTurn(room, turn, engine);
}
}

@bindThis
private async endKyoku(room: Room, engine: Mahjong.MasterGameEngine) {
const confirmation: NextKyokuConfirmation = {
Expand Down Expand Up @@ -469,11 +452,11 @@ export class MahjongService implements OnApplicationShutdown, OnModuleInit {
}
}, 1000);

this.globalEventService.publishMahjongRoomStream(room.id, 'dahai', { house: house, tile });
this.globalEventService.publishMahjongRoomStream(room.id, 'dahai', { house: house, tile, riichi });
} else {
this.globalEventService.publishMahjongRoomStream(room.id, 'dahaiAndTsumo', { dahaiHouse: house, dahaiTile: tile, tsumoTile: res.tsumoTile });
this.globalEventService.publishMahjongRoomStream(room.id, 'dahaiAndTsumo', { dahaiHouse: house, dahaiTile: tile, tsumoTile: res.tsumoTile, riichi });

this.next(room, engine);
this.waitForTurn(room, res.next, engine);
}
}

Expand Down Expand Up @@ -556,7 +539,7 @@ export class MahjongService implements OnApplicationShutdown, OnModuleInit {

const res = engine.commit_tsumoHora(myHouse);

this.globalEventService.publishMahjongRoomStream(room.id, 'tsumoHora', { });
this.globalEventService.publishMahjongRoomStream(room.id, 'tsumoHora', { house: myHouse, handTiles: res.handTiles, tsumoTile: res.tsumoTile });
}

@bindThis
Expand Down Expand Up @@ -624,6 +607,8 @@ export class MahjongService implements OnApplicationShutdown, OnModuleInit {
*/
@bindThis
private async waitForTurn(room: Room, house: Mahjong.Common.House, engine: Mahjong.MasterGameEngine) {
const aiHouses = [[1, room.user1Ai], [2, room.user2Ai], [3, room.user3Ai], [4, room.user4Ai]].filter(([id, ai]) => ai).map(([id, ai]) => engine.getHouse(id));

if (engine.state.riichis[house]) {
// リーチ時はアガリ牌でない限りツモ切り
const handTiles = engine.state.handTiles[house];
Expand All @@ -636,6 +621,13 @@ export class MahjongService implements OnApplicationShutdown, OnModuleInit {
}
}

if (aiHouses.includes(house)) {
setTimeout(() => {
this.dahai(room, engine, house, engine.state.handTiles[house].at(-1));
}, 500);
return;
}

const id = Math.random().toString(36).slice(2);
console.log('waitForTurn', house, id);
this.redisClient.sadd(`mahjong:gameTurnWaiting:${room.id}`, id);
Expand Down
Binary file added packages/frontend/assets/mahjong/riichi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/frontend/assets/mahjong/ron.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/frontend/assets/mahjong/tsumo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions packages/frontend/src/pages/mahjong/huro.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!--
SPDX-FileCopyrightText: syuilo and other misskey contributors
SPDX-License-Identifier: AGPL-3.0-only
-->

<template>
<div v-if="huro.type === 'ankan'" :class="[$style.root]">
<XTile :tile="huro.tile" :variation="variation" :doras="doras"/>
<XTile :tile="huro.tile" :variation="variation" :doras="doras"/>
<XTile :tile="huro.tile" :variation="variation" :doras="doras"/>
<XTile :tile="huro.tile" :variation="variation" :doras="doras"/>
</div>
<div v-else-if="huro.type === 'minkan'" :class="[$style.root]">
<XTile :tile="huro.tile" :variation="variation" :doras="doras"/>
<XTile :tile="huro.tile" :variation="variation" :doras="doras"/>
<XTile :tile="huro.tile" :variation="variation" :doras="doras"/>
<XTile :tile="huro.tile" :variation="variation" :doras="doras"/>
</div>
<div v-else-if="huro.type === 'cii'" :class="[$style.root]">
<XTile :tile="huro.tiles[0]" :variation="variation" :doras="doras"/>
<XTile :tile="huro.tiles[1]" :variation="variation" :doras="doras"/>
<XTile :tile="huro.tiles[2]" :variation="variation" :doras="doras"/>
</div>
<div v-else :class="[$style.root]">
<XTile :tile="huro.tile" :variation="variation" :doras="doras"/>
<XTile :tile="huro.tile" :variation="variation" :doras="doras"/>
<XTile :tile="huro.tile" :variation="variation" :doras="doras"/>
</div>
</template>

<script lang="ts" setup>
import { computed } from 'vue';
import * as Mahjong from 'misskey-mahjong';
import XTile from './tile.vue';
const props = defineProps<{
huro: Mahjong.Common.Huro;
variation: string;
doras: Mahjong.Common.Tile[];
}>();
</script>

<style lang="scss" module>
.root {
}
</style>
Loading

0 comments on commit 2dd886e

Please sign in to comment.