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 {
}
}
export class StocGameMessage extends pb_1.Message {
#one_of_decls: number[][] = [[1, 2]];
#one_of_decls: number[][] = [[1, 2, 3]];
constructor(
data?:
| any[]
......@@ -3108,10 +3108,17 @@ export namespace ygopro {
| {
start?: StocGameMessage.MsgStart;
draw?: never;
new_turn?: never;
}
| {
start?: never;
draw?: StocGameMessage.MsgDraw;
new_turn?: never;
}
| {
start?: never;
draw?: never;
new_turn?: StocGameMessage.MsgNewTurn;
}
))
) {
......@@ -3131,6 +3138,9 @@ export namespace ygopro {
if ("draw" in data && data.draw != undefined) {
this.draw = data.draw;
}
if ("new_turn" in data && data.new_turn != undefined) {
this.new_turn = data.new_turn;
}
}
}
get start() {
......@@ -3159,19 +3169,36 @@ export namespace ygopro {
get has_draw() {
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() {
const cases: {
[index: number]: "none" | "start" | "draw";
[index: number]: "none" | "start" | "draw" | "new_turn";
} = {
0: "none",
1: "start",
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: {
start?: ReturnType<typeof StocGameMessage.MsgStart.prototype.toObject>;
draw?: ReturnType<typeof StocGameMessage.MsgDraw.prototype.toObject>;
new_turn?: ReturnType<
typeof StocGameMessage.MsgNewTurn.prototype.toObject
>;
}): StocGameMessage {
const message = new StocGameMessage({});
if (data.start != null) {
......@@ -3180,12 +3207,18 @@ export namespace ygopro {
if (data.draw != null) {
message.draw = StocGameMessage.MsgDraw.fromObject(data.draw);
}
if (data.new_turn != null) {
message.new_turn = StocGameMessage.MsgNewTurn.fromObject(data.new_turn);
}
return message;
}
toObject() {
const data: {
start?: ReturnType<typeof StocGameMessage.MsgStart.prototype.toObject>;
draw?: ReturnType<typeof StocGameMessage.MsgDraw.prototype.toObject>;
new_turn?: ReturnType<
typeof StocGameMessage.MsgNewTurn.prototype.toObject
>;
} = {};
if (this.start != null) {
data.start = this.start.toObject();
......@@ -3193,6 +3226,9 @@ export namespace ygopro {
if (this.draw != null) {
data.draw = this.draw.toObject();
}
if (this.new_turn != null) {
data.new_turn = this.new_turn.toObject();
}
return data;
}
serialize(): Uint8Array;
......@@ -3203,6 +3239,10 @@ export namespace ygopro {
writer.writeMessage(1, this.start, () => this.start.serialize(writer));
if (this.has_draw)
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();
}
static deserialize(bytes: Uint8Array | pb_1.BinaryReader): StocGameMessage {
......@@ -3227,6 +3267,14 @@ export namespace ygopro {
() => (message.draw = StocGameMessage.MsgDraw.deserialize(reader))
);
break;
case 3:
reader.readMessage(
message.new_turn,
() =>
(message.new_turn =
StocGameMessage.MsgNewTurn.deserialize(reader))
);
break;
default:
reader.skipField();
}
......@@ -3590,5 +3638,83 @@ export namespace ygopro {
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;
export const MSG_START = 4;
export const MSG_DRAW = 90;
export const MSG_NEW_TURN = 40;
......@@ -8,6 +8,7 @@ import { YgoProPacket, StocAdapter } from "../../packet";
import * as GAME_MSG from "../../protoDecl";
import MsgStartAdapter from "./start";
import MsgDrawAdapter from "./draw";
import MsgNewTurnAdapter from "./newTurn";
/*
* STOC GameMsg
......@@ -43,6 +44,11 @@ export default class GameMsgAdapter implements StocAdapter {
break;
}
case GAME_MSG.MSG_NEW_TURN: {
gameMsg.new_turn = MsgNewTurnAdapter(gameData);
break;
}
default: {
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 {
opAddHandsImpl,
meHandsCase,
} from "./handsSlice";
import { newTurnImpl } from "./turnSlice";
import { RootState } from "../../store";
export interface DuelState {
......@@ -18,6 +19,7 @@ export interface DuelState {
opInitInfo?: InitInfo; // 对手的初始状态
meHands?: Hands; // 自己的手牌
opHands?: Hands; // 对手的手牌
currentPlayer?: number; // 当前的操作方
}
const initialState: DuelState = {};
......@@ -30,13 +32,14 @@ const duelSlice = createSlice({
opInfoInit: opInfoInitImpl,
meAddHands: meAddHandsImpl,
opAddHands: opAddHandsImpl,
updateTurn: newTurnImpl,
},
extraReducers(builder) {
meHandsCase(builder);
},
});
export const { meInfoInit, opInfoInit, meAddHands, opAddHands } =
export const { meInfoInit, opInfoInit, meAddHands, opAddHands, updateTurn } =
duelSlice.actions;
export const selectDuelHsStart = (state: RootState) => {
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";
import { store } from "../../store";
import onMsgStart from "./start";
import onMsgDraw from "./draw";
import onMsgNewTurn from "./newTurn";
export default function handleGameMsg(pb: ygopro.YgoStocMsg) {
const dispatch = store.dispatch;
......@@ -22,6 +23,13 @@ export default function handleGameMsg(pb: ygopro.YgoStocMsg) {
break;
}
case "new_turn": {
const newTurn = msg.new_turn;
onMsgNewTurn(newTurn, dispatch);
break;
}
default: {
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";
import renderField from "./field";
import * as CONFIG from "../../../config/ui";
import { Card } from "../../../api/cards";
import { selectCurrentPlayer } from "../../../reducers/duel/turnSlice";
// CONFIG
......@@ -34,6 +35,7 @@ export default class SimpleDuelPlateImpl implements IDuelPlate {
return new Array(5).fill({ id: 10000, data: {}, text: {} });
};
const hands = useAppSelector(this.handsSelector || defaultHandsSelector);
const currentPlayer = useAppSelector(selectCurrentPlayer);
// ----- WebGL渲染 -----
const canvasRef = useRef<HTMLCanvasElement>(null);
......@@ -101,11 +103,16 @@ export default class SimpleDuelPlateImpl implements IDuelPlate {
groundMaterial.diffuseTexture.hasAlpha = true;
ground.material = groundMaterial;
/* 一些未处理的逻辑,在这里用日志打印出来 */
// 当前操作玩家
console.log(`currentPlayer:` + currentPlayer);
// 渲染循环
engine.runRenderLoop(() => {
scene.render();
});
}, [canvasRef, hands]);
}, [canvasRef, hands, currentPlayer]);
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