Commit 3fc9bbbf authored by nanahira's avatar nanahira

fix

parent 65f39ef4
import { YGOProMsgWin } from 'ygopro-msg-encode';
import { Room } from '../room'; import { Room } from '../room';
import { RoomEvent } from './room-event'; import { RoomEvent } from './room-event';
export class OnRoomWin extends RoomEvent { export class OnRoomWin extends RoomEvent {
constructor( constructor(
room: Room, room: Room,
public winPos: number, public winMsg: YGOProMsgWin,
public winMatch = false, public winMatch = false,
) { ) {
super(room); super(room);
......
...@@ -18,6 +18,7 @@ import { ...@@ -18,6 +18,7 @@ import {
YGOProStocSelectHand, YGOProStocSelectHand,
ChatColor, ChatColor,
YGOProCtosChat, YGOProCtosChat,
YGOProMsgWin,
} from 'ygopro-msg-encode'; } from 'ygopro-msg-encode';
import { DefaultHostInfoProvider } from './default-hostinfo-provder'; import { DefaultHostInfoProvider } from './default-hostinfo-provder';
import { CardReaderFinalized } from 'koishipro-core.js'; import { CardReaderFinalized } from 'koishipro-core.js';
...@@ -56,10 +57,10 @@ export class Room { ...@@ -56,10 +57,10 @@ export class Room {
return this.hostinfo.mode === 2; return this.hostinfo.mode === 2;
} }
players = new Array<Client>(this.hostinfo.mode === 2 ? 4 : 2); players = new Array<Client | undefined>(this.hostinfo.mode === 2 ? 4 : 2);
watchers = new Set<Client>(); watchers = new Set<Client>();
get playingPlayers() { get playingPlayers() {
return this.players.filter((p) => p); return this.players.filter((p) => p) as Client[];
} }
get allPlayers() { get allPlayers() {
return [...this.playingPlayers, ...this.watchers]; return [...this.playingPlayers, ...this.watchers];
...@@ -192,7 +193,19 @@ export class Room { ...@@ -192,7 +193,19 @@ export class Room {
return pos ^ (0x1 << this.teamOffsetBit); return pos ^ (0x1 << this.teamOffsetBit);
} }
getPosPlayers(duelPos: number) { getSwappedDuelPosByDuelPos(duelPos: number) {
if ([0, 1].includes(duelPos) && this.isPosSwapped) {
return 1 - duelPos;
}
return duelPos;
}
getSwappedDuelPos(clientOrPos: Client | number) {
const duelPos = this.getDuelPos(clientOrPos);
return this.getSwappedDuelPosByDuelPos(duelPos);
}
getDuelPosPlayers(duelPos: number) {
if (duelPos === NetPlayerType.OBSERVER) { if (duelPos === NetPlayerType.OBSERVER) {
return [...this.watchers]; return [...this.watchers];
} }
...@@ -248,17 +261,22 @@ export class Room { ...@@ -248,17 +261,22 @@ export class Room {
duelStage = DuelStage.Begin; duelStage = DuelStage.Begin;
score = [0, 0]; score = [0, 0];
async win(duelPos: number, winMatch = false) { async win(winMsg: Partial<YGOProMsgWin>, winMatch = false) {
if (this.duelStage === DuelStage.Siding) { if (this.duelStage === DuelStage.Siding) {
this.playingPlayers this.playingPlayers
.filter((p) => p.deck) .filter((p) => p.deck)
.forEach((p) => p.send(new YGOProStocDuelStart())); .forEach((p) => p.send(new YGOProStocDuelStart()));
} }
++this.score[duelPos]; const duelPos = this.getSwappedDuelPosByDuelPos(winMsg.player!);
const exactWinMsg = new YGOProMsgWin().fromPartial({
...winMsg,
player: duelPos,
});
++this.score[this.getSwappedPos(exactWinMsg.player)];
// TODO: next game or finalize // TODO: next game or finalize
await this.ctx.dispatch( await this.ctx.dispatch(
new OnRoomWin(this, duelPos, winMatch), new OnRoomWin(this, exactWinMsg, winMatch),
this.getPosPlayers(duelPos)[0], this.getDuelPosPlayers(duelPos)[0],
); );
} }
...@@ -279,7 +297,10 @@ export class Room { ...@@ -279,7 +297,10 @@ export class Room {
}); });
} else { } else {
this.score[this.getDuelPos(client)] = -9; this.score[this.getDuelPos(client)] = -9;
await this.win(this.getDuelPos(client), true); await this.win(
{ player: this.getSwappedDuelPos(client), type: 0x4 },
true,
);
} }
if (client.isHost) { if (client.isHost) {
const nextHost = this.allPlayers.find((p) => p !== client); const nextHost = this.allPlayers.find((p) => p !== client);
...@@ -467,14 +488,14 @@ export class Room { ...@@ -467,14 +488,14 @@ export class Room {
firstgoPlayer?: Client; firstgoPlayer?: Client;
private async toFirstGo(firstgoPos: number) { private async toFirstGo(firstgoPos: number) {
this.firstgoPlayer = this.getPosPlayers(firstgoPos)[0]; this.firstgoPlayer = this.getDuelPosPlayers(firstgoPos)[0];
this.duelStage = DuelStage.FirstGo; this.duelStage = DuelStage.FirstGo;
this.firstgoPlayer.send(new YGOProStocSelectTp()); this.firstgoPlayer.send(new YGOProStocSelectTp());
} }
private async toFinger() { private async toFinger() {
this.duelStage = DuelStage.Finger; this.duelStage = DuelStage.Finger;
const fingerPlayers = [0, 1].map((p) => this.getPosPlayers(p)[0]); const fingerPlayers = [0, 1].map((p) => this.getDuelPosPlayers(p)[0]);
fingerPlayers.forEach((p) => { fingerPlayers.forEach((p) => {
p.send(new YGOProStocSelectHand()); p.send(new YGOProStocSelectHand());
}); });
...@@ -485,10 +506,10 @@ export class Room { ...@@ -485,10 +506,10 @@ export class Room {
return false; return false;
} }
++this.duelCount; ++this.duelCount;
this.allPlayers.forEach((p) => p.send(new YGOProStocDuelStart()));
if (this.duelCount === 1) { if (this.duelCount === 1) {
this.allPlayers.forEach((p) => p.send(new YGOProStocDuelStart()));
const displayCountDecks = [0, 1].map( const displayCountDecks = [0, 1].map(
(p) => this.getPosPlayers(p)[0].deck, (p) => this.getDuelPosPlayers(p)[0].deck!,
); );
const toDeckCount = (d: YGOProDeck) => { const toDeckCount = (d: YGOProDeck) => {
const res = new YGOProStocDeckCount_DeckInfo(); const res = new YGOProStocDeckCount_DeckInfo();
...@@ -500,7 +521,7 @@ export class Room { ...@@ -500,7 +521,7 @@ export class Room {
[0, 1].forEach((p) => { [0, 1].forEach((p) => {
const selfDeck = displayCountDecks[p]; const selfDeck = displayCountDecks[p];
const otherDeck = displayCountDecks[1 - p]; const otherDeck = displayCountDecks[1 - p];
this.getPosPlayers(p).forEach((c) => { this.getDuelPosPlayers(p).forEach((c) => {
c.send( c.send(
new YGOProStocDeckCount().fromPartial({ new YGOProStocDeckCount().fromPartial({
player0DeckCount: toDeckCount(selfDeck), player0DeckCount: toDeckCount(selfDeck),
......
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