Commit 9b927ea0 authored by Chunchi Che's avatar Chunchi Che

Merge branch 'feat/migrate/service' into 'main'

Feat/migrate/service

See merge request !5
parents 39268956 76900b5b
Pipeline #17260 passed with stage
in 55 seconds
import { ygopro } from "./idl/ocgcore";
import socketMiddleWare, { socketCmd } from "../middleware/socket";
import { IDeck } from "./Card";
export function sendUpdateDeck(deck: IDeck) {
const updateDeck = new ygopro.YgoCtosMsg({
ctos_update_deck: new ygopro.CtosUpdateDeck({
main: deck.main,
extra: deck.extra,
side: deck.side,
}),
});
socketMiddleWare({ cmd: socketCmd.SEND, payload: updateDeck });
}
export function sendHsReady() {
const hasReady = new ygopro.YgoCtosMsg({
ctos_hs_ready: new ygopro.CtosHsReady({}),
});
socketMiddleWare({ cmd: socketCmd.SEND, payload: hasReady });
}
export function sendHsStart() {
const hasStart = new ygopro.YgoCtosMsg({
ctos_hs_start: new ygopro.CtosHsStart({}),
});
socketMiddleWare({ cmd: socketCmd.SEND, payload: hasStart });
}
export function sendPlayerInfo(ws: WebSocket, player: string) {
const playerInfo = new ygopro.YgoCtosMsg({
ctos_player_info: new ygopro.CtosPlayerInfo({
name: player,
}),
});
ws.send(playerInfo.serialize());
}
export function sendJoinGame(ws: WebSocket, version: number, passWd: string) {
const joinGame = new ygopro.YgoCtosMsg({
ctos_join_game: new ygopro.CtosJoinGame({
version, // todo: use config
gameid: 0,
passwd: passWd,
}),
});
ws.send(joinGame.serialize());
}
import { ygopro } from "../api/idl/ocgcore";
import { setJoined } from "../reducers/joinSlice";
import { postChat } from "../reducers/chatSlice";
import { store } from "../store";
import {
player0Enter,
player1Enter,
player0Update,
player1Update,
player0Leave,
player1Leave,
hostChange,
observerIncrement,
observerChange,
updateIsHost,
} from "../reducers/playerSlice";
import handleSocketOpen from "../service/onSocketOpen";
import handleSocketMessage from "../service/onSocketMessage";
export enum socketCmd {
CONNECT,
......@@ -23,189 +10,40 @@ export enum socketCmd {
export interface socketAction {
cmd: socketCmd;
ip?: string;
player?: string;
version?: number;
passWd?: string;
initInfo?: {
ip: string;
player: string;
passWd: string;
};
payload?: ygopro.YgoCtosMsg;
}
let ws: WebSocket | null = null;
const READY_STATE = "ready";
const NO_READY_STATE = "not ready";
export default function (action: socketAction) {
switch (action.cmd) {
case socketCmd.CONNECT: {
const ip = action.ip;
const player = action.player;
const version = action.version;
const passWd = action.passWd;
if (ip && player && version && passWd) {
ws = new WebSocket("ws://" + ip);
const info = action.initInfo;
if (info) {
ws = new WebSocket("ws://" + info.ip);
ws.onopen = () => {
console.log("WebSocket open.");
if (ws && ws.readyState == 1) {
ws.binaryType = "arraybuffer";
sendPlayerInfo(ws, player);
sendJoinGame(ws, version, passWd);
}
handleSocketOpen(ws, info.ip, info.player, info.passWd);
};
ws.onclose = () => {
console.log("WebSocket closed.");
ws = null;
};
ws.onmessage = (e) => {
const pb = ygopro.YgoStocMsg.deserializeBinary(e.data);
const dispatch = store.dispatch;
switch (pb.msg) {
case "stoc_join_game": {
const msg = pb.stoc_join_game;
// todo
dispatch(setJoined());
break;
}
case "stoc_chat": {
const chat = pb.stoc_chat;
dispatch(postChat(chat.msg));
break;
}
case "stoc_hs_player_change": {
const change = pb.stoc_hs_player_change;
if (change.pos > 1) {
console.log("Currently only supported 2v2 mode.");
} else {
switch (change.state) {
case ygopro.StocHsPlayerChange.State.UNKNOWN: {
console.log("Unknown HsPlayerChange State");
break;
}
case ygopro.StocHsPlayerChange.State.MOVE: {
console.log(
"Player " + change.pos + " moved to " + change.moved_pos
);
let src = change.pos;
let dst = change.moved_pos;
// todo
// if (src === 0 && dst === 1) {
// setPlayer1(player0);
// setPlayer0({});
// } else if (src === 1 && dst === 0) {
// setPlayer0(player1);
// setPlayer1({});
// }
break;
}
case ygopro.StocHsPlayerChange.State.READY: {
change.pos == 0
? dispatch(player0Update(READY_STATE))
: dispatch(player1Update(READY_STATE));
break;
}
case ygopro.StocHsPlayerChange.State.NO_READY: {
change.pos == 0
? dispatch(player0Update(NO_READY_STATE))
: dispatch(player1Update(NO_READY_STATE));
break;
}
case ygopro.StocHsPlayerChange.State.LEAVE: {
change.pos == 0
? dispatch(player0Leave)
: dispatch(player1Leave);
break;
}
case ygopro.StocHsPlayerChange.State.TO_OBSERVER: {
change.pos == 0
? dispatch(player0Leave)
: dispatch(player1Leave);
dispatch(observerIncrement());
break;
}
default: {
break;
}
}
}
break;
}
case "stoc_hs_watch_change": {
const count = pb.stoc_hs_watch_change.count;
dispatch(observerChange(count));
break;
}
case "stoc_hs_player_enter": {
const name = pb.stoc_hs_player_enter.name;
const pos = pb.stoc_hs_player_enter.pos;
if (pos > 1) {
console.log("Currently only supported 2v2 mode.");
} else {
pos == 0
? dispatch(player0Enter(name))
: dispatch(player1Enter(name));
}
break;
}
case "stoc_type_change": {
const selfType = pb.stoc_type_change.self_type;
const assertHost = pb.stoc_type_change.is_host;
dispatch(updateIsHost(assertHost));
if (assertHost) {
switch (selfType) {
case ygopro.StocTypeChange.SelfType.PLAYER1: {
dispatch(hostChange(0));
dispatch(player0Update(NO_READY_STATE));
break;
}
case ygopro.StocTypeChange.SelfType.PLAYER2: {
dispatch(hostChange(0));
dispatch(player1Update(NO_READY_STATE));
break;
}
default: {
break;
}
}
}
break;
}
default: {
break;
}
}
};
ws.onmessage = handleSocketMessage;
}
break;
}
case socketCmd.DISCONNECT: {
if (ws) {
ws.close();
}
break;
}
case socketCmd.SEND: {
......@@ -213,34 +51,13 @@ export default function (action: socketAction) {
if (ws && pb) {
ws.send(pb.serialize());
}
break;
}
default: {
console.log("Unhandled socket command: " + action.cmd);
break;
}
}
}
// todo: move to api/*
function sendPlayerInfo(ws: WebSocket, player: string) {
const playerInfo = new ygopro.YgoCtosMsg({
ctos_player_info: new ygopro.CtosPlayerInfo({
name: player,
}),
});
ws.send(playerInfo.serialize());
}
function sendJoinGame(ws: WebSocket, version: number, passWd: string) {
const joinGame = new ygopro.YgoCtosMsg({
ctos_join_game: new ygopro.CtosJoinGame({
version, // todo: use config
gameid: 0,
passwd: passWd,
}),
});
ws.send(joinGame.serialize());
}
import { ygopro } from "../api/idl/ocgcore";
import { store } from "../store";
import { setJoined } from "../reducers/joinSlice";
import { postChat } from "../reducers/chatSlice";
import {
player0Enter,
player1Enter,
player0Update,
player1Update,
player0Leave,
player1Leave,
hostChange,
observerIncrement,
observerChange,
updateIsHost,
} from "../reducers/playerSlice";
const READY_STATE = "ready";
const NO_READY_STATE = "not ready";
export default function handleSocketMessage(e: MessageEvent) {
const pb = ygopro.YgoStocMsg.deserializeBinary(e.data);
const dispatch = store.dispatch;
switch (pb.msg) {
case "stoc_join_game": {
const msg = pb.stoc_join_game;
// todo
dispatch(setJoined());
break;
}
case "stoc_chat": {
const chat = pb.stoc_chat;
dispatch(postChat(chat.msg));
break;
}
case "stoc_hs_player_change": {
const change = pb.stoc_hs_player_change;
if (change.pos > 1) {
console.log("Currently only supported 2v2 mode.");
} else {
switch (change.state) {
case ygopro.StocHsPlayerChange.State.UNKNOWN: {
console.log("Unknown HsPlayerChange State");
break;
}
case ygopro.StocHsPlayerChange.State.MOVE: {
console.log(
"Player " + change.pos + " moved to " + change.moved_pos
);
let src = change.pos;
let dst = change.moved_pos;
// todo
// if (src === 0 && dst === 1) {
// setPlayer1(player0);
// setPlayer0({});
// } else if (src === 1 && dst === 0) {
// setPlayer0(player1);
// setPlayer1({});
// }
break;
}
case ygopro.StocHsPlayerChange.State.READY: {
change.pos == 0
? dispatch(player0Update(READY_STATE))
: dispatch(player1Update(READY_STATE));
break;
}
case ygopro.StocHsPlayerChange.State.NO_READY: {
change.pos == 0
? dispatch(player0Update(NO_READY_STATE))
: dispatch(player1Update(NO_READY_STATE));
break;
}
case ygopro.StocHsPlayerChange.State.LEAVE: {
change.pos == 0 ? dispatch(player0Leave) : dispatch(player1Leave);
break;
}
case ygopro.StocHsPlayerChange.State.TO_OBSERVER: {
change.pos == 0 ? dispatch(player0Leave) : dispatch(player1Leave);
dispatch(observerIncrement());
break;
}
default: {
break;
}
}
}
break;
}
case "stoc_hs_watch_change": {
const count = pb.stoc_hs_watch_change.count;
dispatch(observerChange(count));
break;
}
case "stoc_hs_player_enter": {
const name = pb.stoc_hs_player_enter.name;
const pos = pb.stoc_hs_player_enter.pos;
if (pos > 1) {
console.log("Currently only supported 2v2 mode.");
} else {
pos == 0 ? dispatch(player0Enter(name)) : dispatch(player1Enter(name));
}
break;
}
case "stoc_type_change": {
const selfType = pb.stoc_type_change.self_type;
const assertHost = pb.stoc_type_change.is_host;
dispatch(updateIsHost(assertHost));
if (assertHost) {
switch (selfType) {
case ygopro.StocTypeChange.SelfType.PLAYER1: {
dispatch(hostChange(0));
dispatch(player0Update(NO_READY_STATE));
break;
}
case ygopro.StocTypeChange.SelfType.PLAYER2: {
dispatch(hostChange(0));
dispatch(player1Update(NO_READY_STATE));
break;
}
default: {
break;
}
}
}
break;
}
default: {
break;
}
}
}
import { sendJoinGame, sendPlayerInfo } from "../api/helper";
export default function handleSocketOpen(
ws: WebSocket | null,
ip: string,
player: string,
passWd: string
) {
console.log("WebSocket opened.");
if (ws && ws.readyState == 1) {
ws.binaryType = "arraybuffer";
sendPlayerInfo(ws, player);
sendJoinGame(ws, 4947, passWd); // todo: version use config
}
}
......@@ -12,7 +12,7 @@ import {
selectPlayer1,
selectObserverCount,
} from "../reducers/playerSlice";
import { sendUpdateDeck, sendHsReady, sendHsStart } from "../api/helper";
import socketMiddleWare, { socketCmd } from "../middleware/socket";
const READY_STATE = "ready";
......@@ -28,18 +28,14 @@ export default function WaitRoom() {
const { player, passWd, ip } = params;
useEffect(() => {
if (
player != null &&
player.length != 0 &&
passWd != null &&
passWd.length != 0
) {
if (ip && player && player.length != 0 && passWd && passWd.length != 0) {
socketMiddleWare({
cmd: socketCmd.CONNECT,
ip,
player,
version: 4947,
passWd,
initInfo: {
ip,
player,
passWd,
},
});
}
}, []);
......@@ -114,32 +110,3 @@ export default function WaitRoom() {
</div>
);
}
// todo: move to api/*
function sendUpdateDeck(deck: IDeck) {
const updateDeck = new ygopro.YgoCtosMsg({
ctos_update_deck: new ygopro.CtosUpdateDeck({
main: deck.main,
extra: deck.extra,
side: deck.side,
}),
});
socketMiddleWare({ cmd: socketCmd.SEND, payload: updateDeck });
}
function sendHsReady() {
const hasReady = new ygopro.YgoCtosMsg({
ctos_hs_ready: new ygopro.CtosHsReady({}),
});
socketMiddleWare({ cmd: socketCmd.SEND, payload: hasReady });
}
function sendHsStart() {
const hasStart = new ygopro.YgoCtosMsg({
ctos_hs_start: new ygopro.CtosHsStart({}),
});
socketMiddleWare({ cmd: socketCmd.SEND, payload: hasStart });
}
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