Commit 19dbe06b authored by nanahira's avatar nanahira

add more room events

parent 1f674096
import { RoomEvent } from './room-event';
export class OnRoomDuelStart extends RoomEvent {}
import { RoomEvent } from './room-event';
export class OnRoomGameStart extends RoomEvent {}
import { RoomEvent } from './room-event';
export class OnRoomJoinObserver extends RoomEvent {}
import { RoomEvent } from './room-event';
export class OnRoomJoinPlayer extends RoomEvent {}
import { RoomEvent } from './room-event';
export class OnRoomLeaveObserver extends RoomEvent {}
import { Room } from '../room';
import { RoomEvent } from './room-event';
export class OnRoomLeavePlayer extends RoomEvent {
constructor(
room: Room,
public oldPos: number,
) {
super(room);
}
}
import { RoomEvent } from './room-event';
export class OnRoomMatchStart extends RoomEvent {}
...@@ -28,6 +28,13 @@ import { DuelStage } from './duel-stage'; ...@@ -28,6 +28,13 @@ import { DuelStage } from './duel-stage';
import { OnRoomJoin } from './room-event/on-room-join'; import { OnRoomJoin } from './room-event/on-room-join';
import { OnRoomLeave } from './room-event/on-room-leave'; import { OnRoomLeave } from './room-event/on-room-leave';
import { OnRoomWin } from './room-event/on-room-win'; import { OnRoomWin } from './room-event/on-room-win';
import { OnRoomJoinPlayer } from './room-event/on-room-join-player';
import { OnRoomJoinObserver } from './room-event/on-room-join-observer';
import { OnRoomLeavePlayer } from './room-event/on-room-leave-player';
import { OnRoomLeaveObserver } from './room-event/on-room-leave-observer';
import { OnRoomMatchStart } from './room-event/on-room-match-start';
import { OnRoomGameStart } from './room-event/on-room-game-start';
// import { OnRoomDuelStart } from './room-event/on-room-duel-start'; // 备用事件,暂未使用
import YGOProDeck from 'ygopro-deck-encode'; import YGOProDeck from 'ygopro-deck-encode';
export type RoomFinalizor = (self: Room) => Awaitable<any>; export type RoomFinalizor = (self: Room) => Awaitable<any>;
...@@ -175,7 +182,9 @@ export class Room { ...@@ -175,7 +182,9 @@ export class Room {
client.roomName = this.name; client.roomName = this.name;
client.isHost = !this.allPlayers.length; client.isHost = !this.allPlayers.length;
const firstEmptyPlayerSlot = this.players.findIndex((p) => !p); const firstEmptyPlayerSlot = this.players.findIndex((p) => !p);
if (firstEmptyPlayerSlot >= 0) { const isPlayer = firstEmptyPlayerSlot >= 0;
if (isPlayer) {
this.players[firstEmptyPlayerSlot] = client; this.players[firstEmptyPlayerSlot] = client;
client.pos = firstEmptyPlayerSlot; client.pos = firstEmptyPlayerSlot;
} else { } else {
...@@ -205,6 +214,13 @@ export class Room { ...@@ -205,6 +214,13 @@ export class Room {
}); });
await this.ctx.dispatch(new OnRoomJoin(this), client); await this.ctx.dispatch(new OnRoomJoin(this), client);
// 触发具体的加入事件
if (isPlayer) {
await this.ctx.dispatch(new OnRoomJoinPlayer(this), client);
} else {
await this.ctx.dispatch(new OnRoomJoinObserver(this), client);
}
} }
duelStage = DuelStage.Begin; duelStage = DuelStage.Begin;
...@@ -226,7 +242,10 @@ export class Room { ...@@ -226,7 +242,10 @@ export class Room {
@RoomMethod() @RoomMethod()
private async onDisconnect(client: Client, _msg: YGOProCtosDisconnect) { private async onDisconnect(client: Client, _msg: YGOProCtosDisconnect) {
if (client.pos === NetPlayerType.OBSERVER) { const wasObserver = client.pos === NetPlayerType.OBSERVER;
const oldPos = client.pos;
if (wasObserver) {
this.watchers.delete(client); this.watchers.delete(client);
for (const p of this.allPlayers) { for (const p of this.allPlayers) {
p.send(this.watcherSizeMessage); p.send(this.watcherSizeMessage);
...@@ -249,6 +268,14 @@ export class Room { ...@@ -249,6 +268,14 @@ export class Room {
} }
await this.ctx.dispatch(new OnRoomLeave(this), client); await this.ctx.dispatch(new OnRoomLeave(this), client);
// 触发具体的离开事件
if (wasObserver) {
await this.ctx.dispatch(new OnRoomLeaveObserver(this), client);
} else {
await this.ctx.dispatch(new OnRoomLeavePlayer(this, oldPos), client);
}
client.roomName = undefined; client.roomName = undefined;
} }
...@@ -286,6 +313,10 @@ export class Room { ...@@ -286,6 +313,10 @@ export class Room {
// 发送观战者数量更新 // 发送观战者数量更新
this.allPlayers.forEach((p) => p.send(this.watcherSizeMessage)); this.allPlayers.forEach((p) => p.send(this.watcherSizeMessage));
// 触发事件
await this.ctx.dispatch(new OnRoomLeavePlayer(this, oldPos), client);
await this.ctx.dispatch(new OnRoomJoinObserver(this), client);
} }
@RoomMethod() @RoomMethod()
...@@ -319,6 +350,10 @@ export class Room { ...@@ -319,6 +350,10 @@ export class Room {
// 发送观战者数量更新 // 发送观战者数量更新
this.allPlayers.forEach((p) => p.send(this.watcherSizeMessage)); this.allPlayers.forEach((p) => p.send(this.watcherSizeMessage));
// 触发事件
await this.ctx.dispatch(new OnRoomLeaveObserver(this), client);
await this.ctx.dispatch(new OnRoomJoinPlayer(this), client);
} else if (this.isTag) { } else if (this.isTag) {
// TAG 模式下,已经是玩家,切换到另一个空位 // TAG 模式下,已经是玩家,切换到另一个空位
// 如果已经 ready,不允许切换 // 如果已经 ready,不允许切换
...@@ -342,6 +377,10 @@ export class Room { ...@@ -342,6 +377,10 @@ export class Room {
// 发送 TypeChange 给客户端 // 发送 TypeChange 给客户端
await client.sendTypeChange(); await client.sendTypeChange();
// 触发事件 (玩家切换位置)
await this.ctx.dispatch(new OnRoomLeavePlayer(this, oldPos), client);
await this.ctx.dispatch(new OnRoomJoinPlayer(this), client);
} }
} }
...@@ -448,11 +487,25 @@ export class Room { ...@@ -448,11 +487,25 @@ export class Room {
); );
}); });
} }
if (firstgoPos != null) { if (firstgoPos != null) {
await this.toFirstGo(firstgoPos); await this.toFirstGo(firstgoPos);
} else { } else {
this.duelStage = DuelStage.Finger; await this.toFinger();
} }
// 触发事件
if (this.duelCount === 1) {
// 触发比赛开始事件(第一局)
await this.ctx.dispatch(
new OnRoomMatchStart(this),
this.playingPlayers[0],
);
}
// 触发游戏开始事件(每局游戏)
await this.ctx.dispatch(new OnRoomGameStart(this), this.playingPlayers[0]);
return true; return true;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment