Commit f8330b5a authored by Chunchi Che's avatar Chunchi Che Committed by WANG HE

add CTOS HandResult service

parent 6659219b
import { ygopro } from "../../idl/ocgcore";
import { ygoProPacket } from "../packet";
import { CTOS_HAND_RESULT } from "../protoDecl";
/*
* CTOS HandResult
*
* @param res: unsigned char - 玩家的猜拳选择
*
* @usage - 告知服务端当前玩家的猜拳选择
* */
export default class CtosHandResultPacket extends ygoProPacket {
constructor(pb: ygopro.YgoCtosMsg) {
const handResult = pb.ctos_hand_result;
const hand = handResult.hand;
const exData = new Uint8Array(1);
const dataView = new DataView(exData.buffer);
switch (hand) {
case ygopro.CtosHandResult.HandType.SCISSORS: {
dataView.setUint8(0, 1);
break;
}
case ygopro.CtosHandResult.HandType.ROCK: {
dataView.setUint8(0, 2);
break;
}
case ygopro.CtosHandResult.HandType.PAPER: {
dataView.setUint8(0, 3);
break;
}
default: {
console.log("Unknown HandResult type" + hand);
}
}
super(exData.length + 1, CTOS_HAND_RESULT, exData);
}
}
...@@ -7,6 +7,7 @@ export const CTOS_JOIN_GAME = 18; ...@@ -7,6 +7,7 @@ export const CTOS_JOIN_GAME = 18;
export const CTOS_UPDATE_DECK = 2; export const CTOS_UPDATE_DECK = 2;
export const CTOS_HS_READY = 34; export const CTOS_HS_READY = 34;
export const CTOS_HS_START = 37; export const CTOS_HS_START = 37;
export const CTOS_HAND_RESULT = 3;
export const STOC_JOIN_GAME = 18; export const STOC_JOIN_GAME = 18;
export const STOC_CHAT = 25; export const STOC_CHAT = 25;
......
...@@ -10,6 +10,7 @@ import JoinGameAdapter from "./ocgAdapter/ctos/ctosJoinGame"; ...@@ -10,6 +10,7 @@ import JoinGameAdapter from "./ocgAdapter/ctos/ctosJoinGame";
import UpdateDeckAdapter from "./ocgAdapter/ctos/ctosUpdateDeck"; import UpdateDeckAdapter from "./ocgAdapter/ctos/ctosUpdateDeck";
import HsReadyAdapter from "./ocgAdapter/ctos/ctosHsReady"; import HsReadyAdapter from "./ocgAdapter/ctos/ctosHsReady";
import HsStartAdapter from "./ocgAdapter/ctos/ctosHsStart"; import HsStartAdapter from "./ocgAdapter/ctos/ctosHsStart";
import HandResult from "./ocgAdapter/ctos/ctosHandResult";
export function sendUpdateDeck(deck: IDeck) { export function sendUpdateDeck(deck: IDeck) {
const updateDeck = new ygopro.YgoCtosMsg({ const updateDeck = new ygopro.YgoCtosMsg({
...@@ -67,3 +68,23 @@ export function sendJoinGame(ws: WebSocket, version: number, passWd: string) { ...@@ -67,3 +68,23 @@ export function sendJoinGame(ws: WebSocket, version: number, passWd: string) {
ws.send(packet.serialize()); ws.send(packet.serialize());
} }
export function sendHandResult(result: string) {
let hand = ygopro.CtosHandResult.HandType.UNKNOWN;
if (result === "scissors") {
hand = ygopro.CtosHandResult.HandType.SCISSORS;
} else if (result === "rock") {
hand = ygopro.CtosHandResult.HandType.ROCK;
} else if (result === "paper") {
hand = ygopro.CtosHandResult.HandType.PAPER;
}
const handResult = new ygopro.YgoCtosMsg({
ctos_hand_result: new ygopro.CtosHandResult({
hand,
}),
});
const payload = new HandResult(handResult).serialize();
socketMiddleWare({ cmd: socketCmd.SEND, payload });
}
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
* */ * */
import React from "react"; import React from "react";
import { sendHandResult } from "../api/ocgcore/ocgHelper";
import "../css/Mora.css"; import "../css/Mora.css";
import { useAppSelector } from "../hook"; import { useAppSelector } from "../hook";
import { selectMoraSelectAble } from "../reducers/moraSlice"; import { selectMoraSelectAble } from "../reducers/moraSlice";
...@@ -11,16 +12,33 @@ import { selectMoraSelectAble } from "../reducers/moraSlice"; ...@@ -11,16 +12,33 @@ import { selectMoraSelectAble } from "../reducers/moraSlice";
// TODO: 应该展示对手卡组信息和聊天信息 // TODO: 应该展示对手卡组信息和聊天信息
export default function Mora() { export default function Mora() {
const selectAble = useAppSelector(selectMoraSelectAble); const selectAble = useAppSelector(selectMoraSelectAble);
const handleSelectScissors = () => {
sendHandResult("scissors");
};
const handleSelectRock = () => {
sendHandResult("rock");
};
const handleSelectPaper = () => {
sendHandResult("paper");
};
return ( return (
<div className="container"> <div className="container">
<div className="item"> <div className="item">
<button disabled={!selectAble}>rock</button> <button disabled={!selectAble} onClick={handleSelectScissors}>
scissors
</button>
</div> </div>
<div className="item"> <div className="item">
<button disabled={!selectAble}>scissors</button> <button disabled={!selectAble} onClick={handleSelectRock}>
rock
</button>
</div> </div>
<div className="item"> <div className="item">
<button disabled={!selectAble}>paper</button> <button disabled={!selectAble} onClick={handleSelectPaper}>
paper
</button>
</div> </div>
</div> </div>
); );
......
...@@ -88,13 +88,9 @@ export default function WaitRoom() { ...@@ -88,13 +88,9 @@ export default function WaitRoom() {
<p> <p>
<button <button
disabled={ disabled={
!( !isHost ||
isHost && player0.state != READY_STATE ||
player0.state != undefined && player1.state != READY_STATE
player0.state === READY_STATE &&
player1.state != undefined &&
player1.state === READY_STATE
)
} }
onClick={handleChoseStart} onClick={handleChoseStart}
> >
......
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