Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
syuilo committed Feb 4, 2024
1 parent be3b255 commit 38e3d24
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 89 deletions.
16 changes: 6 additions & 10 deletions packages/backend/src/core/MahjongService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,16 +376,14 @@ export class MahjongService implements OnApplicationShutdown, OnModuleInit {
}

@bindThis
private async dahai(room: Room, engine: Mahjong.MasterGameEngine, house: Mahjong.House, tile: Mahjong.Tile, riichi = false) {
private async dahai(room: Room, engine: Mahjong.MasterGameEngine, house: Mahjong.House, tile: Mahjong.TileId, riichi = false) {
const res = engine.commit_dahai(house, tile, riichi);
room.gameState = engine.state;
await this.saveRoom(room);

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 (res.asking) {
console.log('asking', res);

const answers: CallingAnswers = {
pon: null,
cii: null,
Expand Down Expand Up @@ -478,11 +476,10 @@ export class MahjongService implements OnApplicationShutdown, OnModuleInit {
}

@bindThis
public async commit_dahai(roomId: MiMahjongGame['id'], user: MiUser, tile: string, riichi = false) {
public async commit_dahai(roomId: MiMahjongGame['id'], user: MiUser, tile: Mahjong.TileId, riichi = false) {
const room = await this.getRoom(roomId);
if (room == null) return;
if (room.gameState == null) return;
if (!Mahjong.isTile(tile)) return;

const engine = new Mahjong.MasterGameEngine(room.gameState);
const myHouse = getHouseOfUserId(room, engine, user.id);
Expand All @@ -493,7 +490,7 @@ export class MahjongService implements OnApplicationShutdown, OnModuleInit {
}

@bindThis
public async commit_ankan(roomId: MiMahjongGame['id'], user: MiUser, tile: string) {
public async commit_ankan(roomId: MiMahjongGame['id'], user: MiUser, tile: Mahjong.TileId) {
const room = await this.getRoom(roomId);
if (room == null) return;
if (room.gameState == null) return;
Expand All @@ -511,7 +508,7 @@ export class MahjongService implements OnApplicationShutdown, OnModuleInit {
}

@bindThis
public async commit_kakan(roomId: MiMahjongGame['id'], user: MiUser, tile: string) {
public async commit_kakan(roomId: MiMahjongGame['id'], user: MiUser, tile: Mahjong.TileId) {
const room = await this.getRoom(roomId);
if (room == null) return;
if (room.gameState == null) return;
Expand Down Expand Up @@ -643,11 +640,10 @@ export class MahjongService implements OnApplicationShutdown, OnModuleInit {

if (engine.state.riichis[house]) {
// リーチ時はアガリ牌でない限りツモ切り
const handTiles = engine.state.handTiles[house];
const horaSets = Mahjong.getHoraSets(handTiles);
const horaSets = Mahjong.getHoraSets(engine.handTileTypes[house]);
if (horaSets.length === 0) {
setTimeout(() => {
this.dahai(room, engine, house, handTiles.at(-1));
this.dahai(room, engine, house, engine.state.handTiles[house].at(-1));
}, 500);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class MahjongRoomChannel extends Channel {
}

@bindThis
private async dahai(tile: string, riichi = false) {
private async dahai(tile: number, riichi = false) {
if (this.user == null) return;

this.mahjongService.commit_dahai(this.roomId!, this.user, tile, riichi);
Expand Down Expand Up @@ -136,14 +136,14 @@ class MahjongRoomChannel extends Channel {
}

@bindThis
private async ankan(tile: string) {
private async ankan(tile: number) {
if (this.user == null) return;

this.mahjongService.commit_ankan(this.roomId!, this.user, tile);
}

@bindThis
private async kakan(tile: string) {
private async kakan(tile: number) {
if (this.user == null) return;

this.mahjongService.commit_kakan(this.roomId!, this.user, tile);
Expand Down
Binary file added packages/frontend/assets/mahjong/tiles/m5r.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 added packages/frontend/assets/mahjong/tiles/p5r.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 added packages/frontend/assets/mahjong/tiles/s5r.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 15 additions & 15 deletions packages/frontend/src/pages/mahjong/huro.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ 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"/>
<XTile :tile="Mahjong.findTileByIdOrFail(huro.tiles[0])" :variation="variation" :doras="doras"/>
<XTile :tile="Mahjong.findTileByIdOrFail(huro.tiles[1])" :variation="variation" :doras="doras"/>
<XTile :tile="Mahjong.findTileByIdOrFail(huro.tiles[2])" :variation="variation" :doras="doras"/>
<XTile :tile="Mahjong.findTileByIdOrFail(huro.tiles[3])" :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"/>
<XTile :tile="Mahjong.findTileByIdOrFail(huro.tiles[0])" :variation="variation" :doras="doras"/>
<XTile :tile="Mahjong.findTileByIdOrFail(huro.tiles[1])" :variation="variation" :doras="doras"/>
<XTile :tile="Mahjong.findTileByIdOrFail(huro.tiles[2])" :variation="variation" :doras="doras"/>
<XTile :tile="Mahjong.findTileByIdOrFail(huro.tiles[3])" :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"/>
<XTile :tile="Mahjong.findTileByIdOrFail(huro.tiles[0])" :variation="variation" :doras="doras"/>
<XTile :tile="Mahjong.findTileByIdOrFail(huro.tiles[1])" :variation="variation" :doras="doras"/>
<XTile :tile="Mahjong.findTileByIdOrFail(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"/>
<XTile :tile="Mahjong.findTileByIdOrFail(huro.tiles[0])" :variation="variation" :doras="doras"/>
<XTile :tile="Mahjong.findTileByIdOrFail(huro.tiles[1])" :variation="variation" :doras="doras"/>
<XTile :tile="Mahjong.findTileByIdOrFail(huro.tiles[2])" :variation="variation" :doras="doras"/>
</div>
</template>

Expand All @@ -36,7 +36,7 @@ import XTile from './tile.vue';
const props = defineProps<{
huro: Mahjong.Huro;
variation: string;
doras: Mahjong.Tile[];
doras: Mahjong.TileType[];
}>();
</script>

Expand Down
68 changes: 16 additions & 52 deletions packages/frontend/src/pages/mahjong/room.game.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,28 @@ SPDX-License-Identifier: AGPL-3.0-only
<div :class="$style.hoTilesContainerOfToimen">
<div :class="$style.hoTilesOfToimen">
<div v-for="(tile, i) in engine.state.hoTiles[Mahjong.prevHouse(Mahjong.prevHouse(engine.myHouse))]" :class="$style.hoTile" :style="{ zIndex: engine.state.hoTiles[Mahjong.prevHouse(Mahjong.prevHouse(engine.myHouse))].length - i }">
<XTile :tile="tile" variation="2" :doras="engine.doras"/>
<XTile :tile="Mahjong.findTileByIdOrFail(tile)" variation="2" :doras="engine.doras"/>
</div>
</div>
</div>
<div :class="$style.hoTilesContainerOfKamitya">
<div :class="$style.hoTilesOfKamitya">
<div v-for="tile in engine.state.hoTiles[Mahjong.prevHouse(engine.myHouse)]" :class="$style.hoTile">
<XTile :tile="tile" variation="4" :doras="engine.doras"/>
<XTile :tile="Mahjong.findTileByIdOrFail(tile)" variation="4" :doras="engine.doras"/>
</div>
</div>
</div>
<div :class="$style.hoTilesContainerOfSimotya">
<div :class="$style.hoTilesOfSimotya">
<div v-for="(tile, i) in engine.state.hoTiles[Mahjong.nextHouse(engine.myHouse)]" :class="$style.hoTile" :style="{ zIndex: engine.state.hoTiles[Mahjong.nextHouse(engine.myHouse)].length - i }">
<XTile :tile="tile" variation="5" :doras="engine.doras"/>
<XTile :tile="Mahjong.findTileByIdOrFail(tile)" variation="5" :doras="engine.doras"/>
</div>
</div>
</div>
<div :class="$style.hoTilesContainerOfMe">
<div :class="$style.hoTilesOfMe">
<div v-for="tile in engine.state.hoTiles[engine.myHouse]" :class="$style.hoTile">
<XTile :tile="tile" variation="1" :doras="engine.doras"/>
<XTile :tile="Mahjong.findTileByIdOrFail(tile)" variation="1" :doras="engine.doras"/>
</div>
</div>
</div>
Expand All @@ -106,20 +106,20 @@ SPDX-License-Identifier: AGPL-3.0-only
<div :class="$style.handTilesOfMe">
<div
v-for="tile in Mahjong.sortTiles((isMyTurn && iTsumoed) ? engine.myHandTiles.slice(0, engine.myHandTiles.length - 1) : engine.myHandTiles)"
:class="[$style.myTile, { [$style.myTileNonSelectable]: selectableTiles != null && !selectableTiles.includes(tile), [$style.myTileDora]: engine.doras.includes(tile) }]"
:class="[$style.myTile, { [$style.myTileNonSelectable]: selectableTiles != null && !selectableTiles.includes(Mahjong.findTileByIdOrFail(tile).t), [$style.myTileDora]: engine.doras.includes(Mahjong.findTileByIdOrFail(tile).t) }]"
@click="chooseTile(tile, $event)"
>
<img :src="`/client-assets/mahjong/tile-front.png`" :class="$style.myTileBg"/>
<img :src="`/client-assets/mahjong/tiles/${tile}.png`" :class="$style.myTileFg"/>
<img :src="`/client-assets/mahjong/tiles/${Mahjong.findTileByIdOrFail(tile).red ? Mahjong.findTileByIdOrFail(tile).t + 'r' : Mahjong.findTileByIdOrFail(tile).t}.png`" :class="$style.myTileFg"/>
</div>
<div
v-if="isMyTurn && iTsumoed"
style="display: inline-block; margin-left: 5px;"
:class="[$style.myTile, { [$style.myTileNonSelectable]: selectableTiles != null && !selectableTiles.includes(engine.myHandTiles.at(-1)), [$style.myTileDora]: engine.doras.includes(engine.myHandTiles.at(-1)) }]"
:class="[$style.myTile, { [$style.myTileNonSelectable]: selectableTiles != null && !selectableTiles.includes(Mahjong.findTileByIdOrFail(engine.myHandTiles.at(-1)).t), [$style.myTileDora]: engine.doras.includes(Mahjong.findTileByIdOrFail(engine.myHandTiles.at(-1)).t) }]"
@click="chooseTile(engine.myHandTiles.at(-1), $event)"
>
<img :src="`/client-assets/mahjong/tile-front.png`" :class="$style.myTileBg"/>
<img :src="`/client-assets/mahjong/tiles/${engine.myHandTiles.at(-1)}.png`" :class="$style.myTileFg"/>
<img :src="`/client-assets/mahjong/tiles/${Mahjong.findTileByIdOrFail(engine.myHandTiles.at(-1)).red ? Mahjong.findTileByIdOrFail(engine.myHandTiles.at(-1)).t + 'r' : Mahjong.findTileByIdOrFail(engine.myHandTiles.at(-1)).t}.png`" :class="$style.myTileFg"/>
</div>
</div>

Expand Down Expand Up @@ -259,51 +259,14 @@ const canHora = computed(() => {
return Mahjong.getHoraSets(engine.value.myHandTiles).length > 0;
});
const selectableTiles = ref<Mahjong.Tile[] | null>(null);
const selectableTiles = ref<Mahjong.TileType[] | null>(null);
const ronSerifHouses = reactive<Record<Mahjong.House, boolean>>({ e: false, s: false, w: false, n: false });
const ciiSerifHouses = reactive<Record<Mahjong.House, boolean>>({ e: false, s: false, w: false, n: false });
const ponSerifHouses = reactive<Record<Mahjong.House, boolean>>({ e: false, s: false, w: false, n: false });
const kanSerifHouses = reactive<Record<Mahjong.House, boolean>>({ e: false, s: false, w: false, n: false });
const tsumoSerifHouses = reactive<Record<Mahjong.House, boolean>>({ e: false, s: false, w: false, n: false });
const riichiSerifHouses = reactive<Record<Mahjong.House, boolean>>({ e: false, s: false, w: false, n: false });
/*
console.log(Mahjong.getTilesForRiichi([
'm1',
'm2',
'm2',
'm3',
'm3',
'm5',
'm6',
'p4',
'p5',
'p6',
's6',
's7',
's8',
'm7',
]));
*/
/*
console.log(Mahjong.getHoraSets([
'm3',
'm3',
'm4',
'm4',
'm5',
'm5',
'p4',
'p4',
'p7',
'p8',
'p9',
's4',
's5',
's6',
]));
*/
/*
if (room.value.isStarted && !room.value.isEnded) {
useInterval(() => {
Expand Down Expand Up @@ -354,7 +317,7 @@ let ankanSelect = false;
let kakanSelect = false;
let ciiSelect = false;
function chooseTile(tile: Mahjong.Tile, ev: MouseEvent) {
function chooseTile(tile: Mahjong.TileId, ev: MouseEvent) {
if (!isMyTurn.value) return;
iTsumoed.value = false;
Expand Down Expand Up @@ -390,22 +353,22 @@ function riichi() {
if (!isMyTurn.value) return;
riichiSelect = true;
selectableTiles.value = Mahjong.getTilesForRiichi(engine.value.myHandTiles);
console.log(Mahjong.getTilesForRiichi(engine.value.myHandTiles));
selectableTiles.value = Mahjong.getTilesForRiichi(engine.value.myHandTileTypes);
console.log(Mahjong.getTilesForRiichi(engine.value.myHandTileTypes));
}
function ankan() {
if (!isMyTurn.value) return;
ankanSelect = true;
selectableTiles.value = engine.value.getAnkanableTiles();
selectableTiles.value = engine.value.getAnkanableTiles().map(id => Mahjong.findTileByIdOrFail(id).t);
}
function kakan() {
if (!isMyTurn.value) return;
kakanSelect = true;
selectableTiles.value = engine.value.getKakanableTiles();
selectableTiles.value = engine.value.getKakanableTiles().map(id => Mahjong.findTileByIdOrFail(id).t);
}
function tsumoHora() {
Expand Down Expand Up @@ -957,7 +920,6 @@ onUnmounted(() => {
aspect-ratio: 0.7;
transition: translate 0.1s ease;
cursor: pointer;
overflow: clip;
}
.myTile:hover {
translate: 0 -10px;
Expand All @@ -967,6 +929,7 @@ onUnmounted(() => {
opacity: 0.7;
pointer-events: none;
}
/*
.myTileDora {
&:after {
content: "";
Expand All @@ -982,6 +945,7 @@ onUnmounted(() => {
pointer-events: none;
}
}
*/
.myTileBg {
position: absolute;
top: 0;
Expand Down
12 changes: 6 additions & 6 deletions packages/frontend/src/pages/mahjong/tile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SPDX-License-Identifier: AGPL-3.0-only
<template>
<div :class="[$style.root, { [$style.h]: ['3', '4', '5'].includes(variation), [$style.v]: ['1', '2'].includes(variation), [$style.isDora]: isDora }]">
<img :src="`/client-assets/mahjong/putted-tile-${variation}.png`" :class="$style.bg"/>
<img :src="`/client-assets/mahjong/tiles/${tile}.png`" :class="$style.fg"/>
<img :src="`/client-assets/mahjong/tiles/${tile.red ? tile.t + 'r' : tile.t}.png`" :class="$style.fg"/>
</div>
</template>

Expand All @@ -15,12 +15,12 @@ import { computed } from 'vue';
import * as Mahjong from 'misskey-mahjong';
const props = defineProps<{
tile: Mahjong.Tile;
tile: Mahjong.TileInstance;
variation: string;
doras: Mahjong.Tile[];
doras: Mahjong.TileType[];
}>();
const isDora = computed(() => props.doras.includes(props.tile));
const isDora = computed(() => props.doras.includes(props.tile.t));
</script>

<style lang="scss" module>
Expand Down Expand Up @@ -59,7 +59,7 @@ const isDora = computed(() => props.doras.includes(props.tile));
height: 53%;
object-fit: contain;
}
/*
.isDora {
&:after {
content: "";
Expand All @@ -74,5 +74,5 @@ const isDora = computed(() => props.doras.includes(props.tile));
animation: shine 2s infinite;
pointer-events: none;
}
}
}*/
</style>
4 changes: 2 additions & 2 deletions packages/misskey-mahjong/src/engine.master.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function $(tileId: TileId): Common.TileInstance {
}

function $type(tileId: TileId): TileType {
return Common.findTileByIdOrFail(tileId).t;
return $(tileId).t;
}
//#endregion

Expand Down Expand Up @@ -148,7 +148,7 @@ export class MasterGameEngine {
}

public static createInitialState(): MasterState {
const ikasama: TileId[] = [125, 129, 9, 54, 57, 61, 77, 81, 85, 133, 134, 135, 121, 122];
const ikasama: TileId[] = [125, 129, 9, 56, 57, 61, 77, 81, 85, 133, 134, 135, 121, 122];

const tiles = [...Common.TILE_ID_MAP.keys()];
tiles.sort(() => Math.random() - 0.5);
Expand Down
2 changes: 1 addition & 1 deletion packages/misskey-mahjong/src/engine.player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function $(tileId: TileId): Common.TileInstance {
}

function $type(tileId: TileId): TileType {
return Common.findTileByIdOrFail(tileId).t;
return $(tileId).t;
}
//#endregion

Expand Down

0 comments on commit 38e3d24

Please sign in to comment.