Commit 4ff2f37c authored by Chunchi Che's avatar Chunchi Che

Merge branch 'feat/new_turn' into 'main'

Feat/new turn

See merge request !27
parents f334f373 e7bbc317
Pipeline #18299 passed with stages
in 2 minutes and 12 seconds
Subproject commit cdceb9956b250e56cc59c18936872cdda3f3fb2c Subproject commit f4278cdb78436dd2fd2a521f825c7498eef7b65d
...@@ -3100,7 +3100,7 @@ export namespace ygopro { ...@@ -3100,7 +3100,7 @@ export namespace ygopro {
} }
} }
export class StocGameMessage extends pb_1.Message { export class StocGameMessage extends pb_1.Message {
#one_of_decls: number[][] = [[1, 2]]; #one_of_decls: number[][] = [[1, 2, 3]];
constructor( constructor(
data?: data?:
| any[] | any[]
...@@ -3108,10 +3108,17 @@ export namespace ygopro { ...@@ -3108,10 +3108,17 @@ export namespace ygopro {
| { | {
start?: StocGameMessage.MsgStart; start?: StocGameMessage.MsgStart;
draw?: never; draw?: never;
new_turn?: never;
} }
| { | {
start?: never; start?: never;
draw?: StocGameMessage.MsgDraw; draw?: StocGameMessage.MsgDraw;
new_turn?: never;
}
| {
start?: never;
draw?: never;
new_turn?: StocGameMessage.MsgNewTurn;
} }
)) ))
) { ) {
...@@ -3131,6 +3138,9 @@ export namespace ygopro { ...@@ -3131,6 +3138,9 @@ export namespace ygopro {
if ("draw" in data && data.draw != undefined) { if ("draw" in data && data.draw != undefined) {
this.draw = data.draw; this.draw = data.draw;
} }
if ("new_turn" in data && data.new_turn != undefined) {
this.new_turn = data.new_turn;
}
} }
} }
get start() { get start() {
...@@ -3159,19 +3169,36 @@ export namespace ygopro { ...@@ -3159,19 +3169,36 @@ export namespace ygopro {
get has_draw() { get has_draw() {
return pb_1.Message.getField(this, 2) != null; return pb_1.Message.getField(this, 2) != null;
} }
get new_turn() {
return pb_1.Message.getWrapperField(
this,
StocGameMessage.MsgNewTurn,
3
) as StocGameMessage.MsgNewTurn;
}
set new_turn(value: StocGameMessage.MsgNewTurn) {
pb_1.Message.setOneofWrapperField(this, 3, this.#one_of_decls[0], value);
}
get has_new_turn() {
return pb_1.Message.getField(this, 3) != null;
}
get gameMsg() { get gameMsg() {
const cases: { const cases: {
[index: number]: "none" | "start" | "draw"; [index: number]: "none" | "start" | "draw" | "new_turn";
} = { } = {
0: "none", 0: "none",
1: "start", 1: "start",
2: "draw", 2: "draw",
3: "new_turn",
}; };
return cases[pb_1.Message.computeOneofCase(this, [1, 2])]; return cases[pb_1.Message.computeOneofCase(this, [1, 2, 3])];
} }
static fromObject(data: { static fromObject(data: {
start?: ReturnType<typeof StocGameMessage.MsgStart.prototype.toObject>; start?: ReturnType<typeof StocGameMessage.MsgStart.prototype.toObject>;
draw?: ReturnType<typeof StocGameMessage.MsgDraw.prototype.toObject>; draw?: ReturnType<typeof StocGameMessage.MsgDraw.prototype.toObject>;
new_turn?: ReturnType<
typeof StocGameMessage.MsgNewTurn.prototype.toObject
>;
}): StocGameMessage { }): StocGameMessage {
const message = new StocGameMessage({}); const message = new StocGameMessage({});
if (data.start != null) { if (data.start != null) {
...@@ -3180,12 +3207,18 @@ export namespace ygopro { ...@@ -3180,12 +3207,18 @@ export namespace ygopro {
if (data.draw != null) { if (data.draw != null) {
message.draw = StocGameMessage.MsgDraw.fromObject(data.draw); message.draw = StocGameMessage.MsgDraw.fromObject(data.draw);
} }
if (data.new_turn != null) {
message.new_turn = StocGameMessage.MsgNewTurn.fromObject(data.new_turn);
}
return message; return message;
} }
toObject() { toObject() {
const data: { const data: {
start?: ReturnType<typeof StocGameMessage.MsgStart.prototype.toObject>; start?: ReturnType<typeof StocGameMessage.MsgStart.prototype.toObject>;
draw?: ReturnType<typeof StocGameMessage.MsgDraw.prototype.toObject>; draw?: ReturnType<typeof StocGameMessage.MsgDraw.prototype.toObject>;
new_turn?: ReturnType<
typeof StocGameMessage.MsgNewTurn.prototype.toObject
>;
} = {}; } = {};
if (this.start != null) { if (this.start != null) {
data.start = this.start.toObject(); data.start = this.start.toObject();
...@@ -3193,6 +3226,9 @@ export namespace ygopro { ...@@ -3193,6 +3226,9 @@ export namespace ygopro {
if (this.draw != null) { if (this.draw != null) {
data.draw = this.draw.toObject(); data.draw = this.draw.toObject();
} }
if (this.new_turn != null) {
data.new_turn = this.new_turn.toObject();
}
return data; return data;
} }
serialize(): Uint8Array; serialize(): Uint8Array;
...@@ -3203,6 +3239,10 @@ export namespace ygopro { ...@@ -3203,6 +3239,10 @@ export namespace ygopro {
writer.writeMessage(1, this.start, () => this.start.serialize(writer)); writer.writeMessage(1, this.start, () => this.start.serialize(writer));
if (this.has_draw) if (this.has_draw)
writer.writeMessage(2, this.draw, () => this.draw.serialize(writer)); writer.writeMessage(2, this.draw, () => this.draw.serialize(writer));
if (this.has_new_turn)
writer.writeMessage(3, this.new_turn, () =>
this.new_turn.serialize(writer)
);
if (!w) return writer.getResultBuffer(); if (!w) return writer.getResultBuffer();
} }
static deserialize(bytes: Uint8Array | pb_1.BinaryReader): StocGameMessage { static deserialize(bytes: Uint8Array | pb_1.BinaryReader): StocGameMessage {
...@@ -3227,6 +3267,14 @@ export namespace ygopro { ...@@ -3227,6 +3267,14 @@ export namespace ygopro {
() => (message.draw = StocGameMessage.MsgDraw.deserialize(reader)) () => (message.draw = StocGameMessage.MsgDraw.deserialize(reader))
); );
break; break;
case 3:
reader.readMessage(
message.new_turn,
() =>
(message.new_turn =
StocGameMessage.MsgNewTurn.deserialize(reader))
);
break;
default: default:
reader.skipField(); reader.skipField();
} }
...@@ -3590,5 +3638,83 @@ export namespace ygopro { ...@@ -3590,5 +3638,83 @@ export namespace ygopro {
return MsgDraw.deserialize(bytes); return MsgDraw.deserialize(bytes);
} }
} }
export class MsgNewTurn extends pb_1.Message {
#one_of_decls: number[][] = [];
constructor(
data?:
| any[]
| {
player?: number;
}
) {
super();
pb_1.Message.initialize(
this,
Array.isArray(data) ? data : [],
0,
-1,
[],
this.#one_of_decls
);
if (!Array.isArray(data) && typeof data == "object") {
if ("player" in data && data.player != undefined) {
this.player = data.player;
}
}
}
get player() {
return pb_1.Message.getFieldWithDefault(this, 1, 0) as number;
}
set player(value: number) {
pb_1.Message.setField(this, 1, value);
}
static fromObject(data: { player?: number }): MsgNewTurn {
const message = new MsgNewTurn({});
if (data.player != null) {
message.player = data.player;
}
return message;
}
toObject() {
const data: {
player?: number;
} = {};
if (this.player != null) {
data.player = this.player;
}
return data;
}
serialize(): Uint8Array;
serialize(w: pb_1.BinaryWriter): void;
serialize(w?: pb_1.BinaryWriter): Uint8Array | void {
const writer = w || new pb_1.BinaryWriter();
if (this.player != 0) writer.writeInt32(1, this.player);
if (!w) return writer.getResultBuffer();
}
static deserialize(bytes: Uint8Array | pb_1.BinaryReader): MsgNewTurn {
const reader =
bytes instanceof pb_1.BinaryReader
? bytes
: new pb_1.BinaryReader(bytes),
message = new MsgNewTurn();
while (reader.nextField()) {
if (reader.isEndGroup()) break;
switch (reader.getFieldNumber()) {
case 1:
message.player = reader.readInt32();
break;
default:
reader.skipField();
}
}
return message;
}
serializeBinary(): Uint8Array {
return this.serialize();
}
static deserializeBinary(bytes: Uint8Array): MsgNewTurn {
return MsgNewTurn.deserialize(bytes);
}
}
} }
} }
...@@ -25,3 +25,4 @@ export const STOC_GAME_MSG = 1; ...@@ -25,3 +25,4 @@ export const STOC_GAME_MSG = 1;
export const MSG_START = 4; export const MSG_START = 4;
export const MSG_DRAW = 90; export const MSG_DRAW = 90;
export const MSG_NEW_TURN = 40;
...@@ -8,6 +8,7 @@ import { YgoProPacket, StocAdapter } from "../../packet"; ...@@ -8,6 +8,7 @@ import { YgoProPacket, StocAdapter } from "../../packet";
import * as GAME_MSG from "../../protoDecl"; import * as GAME_MSG from "../../protoDecl";
import MsgStartAdapter from "./start"; import MsgStartAdapter from "./start";
import MsgDrawAdapter from "./draw"; import MsgDrawAdapter from "./draw";
import MsgNewTurnAdapter from "./newTurn";
/* /*
* STOC GameMsg * STOC GameMsg
...@@ -43,6 +44,11 @@ export default class GameMsgAdapter implements StocAdapter { ...@@ -43,6 +44,11 @@ export default class GameMsgAdapter implements StocAdapter {
break; break;
} }
case GAME_MSG.MSG_NEW_TURN: {
gameMsg.new_turn = MsgNewTurnAdapter(gameData);
break;
}
default: { default: {
console.log("Unhandled GameMessage function=", func); console.log("Unhandled GameMessage function=", func);
......
import { ygopro } from "../../../idl/ocgcore";
import { BufferReader } from "../../bufferIO";
const LITTLE_ENDIAN = true;
/*
* MSG New Turn
*
* @param player: char - 下一个操作的玩家编号
*
* @usage - 服务端告诉前端下一个操作的玩家
* */
export default (data: Uint8Array) => {
const reader = new BufferReader(data, LITTLE_ENDIAN);
const player = reader.readUint8();
return new ygopro.StocGameMessage.MsgNewTurn({
player,
});
};
...@@ -11,6 +11,7 @@ import { ...@@ -11,6 +11,7 @@ import {
opAddHandsImpl, opAddHandsImpl,
meHandsCase, meHandsCase,
} from "./handsSlice"; } from "./handsSlice";
import { newTurnImpl } from "./turnSlice";
import { RootState } from "../../store"; import { RootState } from "../../store";
export interface DuelState { export interface DuelState {
...@@ -18,6 +19,7 @@ export interface DuelState { ...@@ -18,6 +19,7 @@ export interface DuelState {
opInitInfo?: InitInfo; // 对手的初始状态 opInitInfo?: InitInfo; // 对手的初始状态
meHands?: Hands; // 自己的手牌 meHands?: Hands; // 自己的手牌
opHands?: Hands; // 对手的手牌 opHands?: Hands; // 对手的手牌
currentPlayer?: number; // 当前的操作方
} }
const initialState: DuelState = {}; const initialState: DuelState = {};
...@@ -30,13 +32,14 @@ const duelSlice = createSlice({ ...@@ -30,13 +32,14 @@ const duelSlice = createSlice({
opInfoInit: opInfoInitImpl, opInfoInit: opInfoInitImpl,
meAddHands: meAddHandsImpl, meAddHands: meAddHandsImpl,
opAddHands: opAddHandsImpl, opAddHands: opAddHandsImpl,
updateTurn: newTurnImpl,
}, },
extraReducers(builder) { extraReducers(builder) {
meHandsCase(builder); meHandsCase(builder);
}, },
}); });
export const { meInfoInit, opInfoInit, meAddHands, opAddHands } = export const { meInfoInit, opInfoInit, meAddHands, opAddHands, updateTurn } =
duelSlice.actions; duelSlice.actions;
export const selectDuelHsStart = (state: RootState) => { export const selectDuelHsStart = (state: RootState) => {
return state.duel.meInitInfo != null; return state.duel.meInitInfo != null;
......
import { PayloadAction, CaseReducer } from "@reduxjs/toolkit";
import { RootState } from "../../store";
import { DuelState } from "./mod";
export const newTurnImpl: CaseReducer<DuelState, PayloadAction<number>> = (
state,
action
) => {
state.currentPlayer = action.payload;
};
export const selectCurrentPlayer = (state: RootState) =>
state.duel.currentPlayer;
...@@ -2,6 +2,7 @@ import { ygopro } from "../../api/ocgcore/idl/ocgcore"; ...@@ -2,6 +2,7 @@ import { ygopro } from "../../api/ocgcore/idl/ocgcore";
import { store } from "../../store"; import { store } from "../../store";
import onMsgStart from "./start"; import onMsgStart from "./start";
import onMsgDraw from "./draw"; import onMsgDraw from "./draw";
import onMsgNewTurn from "./newTurn";
export default function handleGameMsg(pb: ygopro.YgoStocMsg) { export default function handleGameMsg(pb: ygopro.YgoStocMsg) {
const dispatch = store.dispatch; const dispatch = store.dispatch;
...@@ -22,6 +23,13 @@ export default function handleGameMsg(pb: ygopro.YgoStocMsg) { ...@@ -22,6 +23,13 @@ export default function handleGameMsg(pb: ygopro.YgoStocMsg) {
break; break;
} }
case "new_turn": {
const newTurn = msg.new_turn;
onMsgNewTurn(newTurn, dispatch);
break;
}
default: { default: {
console.log("Unhandled GameMsg=" + msg.gameMsg); console.log("Unhandled GameMsg=" + msg.gameMsg);
......
import { ygopro } from "../../api/ocgcore/idl/ocgcore";
import { AppDispatch } from "../../store";
import { updateTurn } from "../../reducers/duel/mod";
export default (
newTurn: ygopro.StocGameMessage.MsgNewTurn,
dispatch: AppDispatch
) => {
const player = newTurn.player;
dispatch(updateTurn(player));
};
...@@ -18,6 +18,7 @@ import renderExclusion from "./exclusion"; ...@@ -18,6 +18,7 @@ import renderExclusion from "./exclusion";
import renderField from "./field"; import renderField from "./field";
import * as CONFIG from "../../../config/ui"; import * as CONFIG from "../../../config/ui";
import { Card } from "../../../api/cards"; import { Card } from "../../../api/cards";
import { selectCurrentPlayer } from "../../../reducers/duel/turnSlice";
// CONFIG // CONFIG
...@@ -34,6 +35,7 @@ export default class SimpleDuelPlateImpl implements IDuelPlate { ...@@ -34,6 +35,7 @@ export default class SimpleDuelPlateImpl implements IDuelPlate {
return new Array(5).fill({ id: 10000, data: {}, text: {} }); return new Array(5).fill({ id: 10000, data: {}, text: {} });
}; };
const hands = useAppSelector(this.handsSelector || defaultHandsSelector); const hands = useAppSelector(this.handsSelector || defaultHandsSelector);
const currentPlayer = useAppSelector(selectCurrentPlayer);
// ----- WebGL渲染 ----- // ----- WebGL渲染 -----
const canvasRef = useRef<HTMLCanvasElement>(null); const canvasRef = useRef<HTMLCanvasElement>(null);
...@@ -101,11 +103,16 @@ export default class SimpleDuelPlateImpl implements IDuelPlate { ...@@ -101,11 +103,16 @@ export default class SimpleDuelPlateImpl implements IDuelPlate {
groundMaterial.diffuseTexture.hasAlpha = true; groundMaterial.diffuseTexture.hasAlpha = true;
ground.material = groundMaterial; ground.material = groundMaterial;
/* 一些未处理的逻辑,在这里用日志打印出来 */
// 当前操作玩家
console.log(`currentPlayer:` + currentPlayer);
// 渲染循环 // 渲染循环
engine.runRenderLoop(() => { engine.runRenderLoop(() => {
scene.render(); scene.render();
}); });
}, [canvasRef, hands]); }, [canvasRef, hands, currentPlayer]);
useEffect(() => { useEffect(() => {
// 监听状态变化,并实现动画 // 监听状态变化,并实现动画
......
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