Commit 6c48f7ee authored by Chunchi Che's avatar Chunchi Che

update WaitRoom.tsx and add WaitRoom.css

parent 7afc3b00
import React, { useState, ChangeEvent } from "react"; import React, { useState, ChangeEvent } from "react";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
import "./css/WaitRoom.css"; import "./css/JoinRoom.css";
export default function JoinRoom() { export default function JoinRoom() {
const [player, setPlayer] = useState(""); const [player, setPlayer] = useState("");
......
...@@ -2,6 +2,12 @@ import React, { useRef, useEffect, useState } from "react"; ...@@ -2,6 +2,12 @@ import React, { useRef, useEffect, useState } from "react";
import { useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
import { ygopro } from "./api/idl/ocgcore"; import { ygopro } from "./api/idl/ocgcore";
import { fetchDeck, IDeck } from "./Card"; import { fetchDeck, IDeck } from "./Card";
import "./css/WaitRoom.css";
type Player = {
name?: string;
state?: string;
};
export default function WaitRoom() { export default function WaitRoom() {
const params = useParams<{ const params = useParams<{
...@@ -13,6 +19,9 @@ export default function WaitRoom() { ...@@ -13,6 +19,9 @@ export default function WaitRoom() {
const [joined, setJoined] = useState<boolean>(false); const [joined, setJoined] = useState<boolean>(false);
const [chat, setChat] = useState<string>(""); const [chat, setChat] = useState<string>("");
const [choseDeck, setChoseDeck] = useState<boolean>(false); const [choseDeck, setChoseDeck] = useState<boolean>(false);
const [observerCount, setObserverCount] = useState<number>(0);
const [player0, setPlayer0] = useState<Player>({});
const [player1, setPlayer1] = useState<Player>({});
const ws = useRef<WebSocket | null>(null); const ws = useRef<WebSocket | null>(null);
...@@ -64,41 +73,73 @@ export default function WaitRoom() { ...@@ -64,41 +73,73 @@ export default function WaitRoom() {
case "stoc_hs_player_change": { case "stoc_hs_player_change": {
const change = pb.stoc_hs_player_change; const change = pb.stoc_hs_player_change;
switch (change.state) { if (change.pos > 1) {
case ygopro.StocHsPlayerChange.State.UNKNOWN: { console.log("Currently only supported 2v2 mode.");
console.log("Unknown HsPlayerChange State"); } else {
switch (change.state) {
break; case ygopro.StocHsPlayerChange.State.UNKNOWN: {
} console.log("Unknown HsPlayerChange State");
case ygopro.StocHsPlayerChange.State.MOVE: {
console.log( break;
"Player " + change.pos + " moved to " + change.moved_pos }
); case ygopro.StocHsPlayerChange.State.MOVE: {
console.log(
break; "Player " + change.pos + " moved to " + change.moved_pos
} );
case ygopro.StocHsPlayerChange.State.READY: {
console.log("Player " + change.pos + " has ready"); let src = change.pos;
let dst = change.moved_pos;
break;
} if (src === 0 && dst === 1) {
case ygopro.StocHsPlayerChange.State.NO_READY: { setPlayer1(player0);
console.log("Player " + change.pos + " not ready"); setPlayer0({});
} else if (src === 1 && dst === 0) {
break; setPlayer0(player1);
} setPlayer1({});
case ygopro.StocHsPlayerChange.State.LEAVE: { }
console.log("Player " + change.pos + " has leave");
break;
break; }
} case ygopro.StocHsPlayerChange.State.READY: {
case ygopro.StocHsPlayerChange.State.TO_OBSERVER: { console.log("Player " + change.pos + " has ready");
console.log("Player " + change.pos + " has moved to observer");
const state = "ready";
break; let player = change.pos == 0 ? player0 : player1;
} player.state = state;
default: {
break; change.pos == 0 ? setPlayer0(player) : setPlayer1(player);
break;
}
case ygopro.StocHsPlayerChange.State.NO_READY: {
console.log("Player " + change.pos + " not ready");
const state = "not ready";
let player = change.pos == 0 ? player0 : player1;
player.state = state;
change.pos == 0 ? setPlayer0(player) : setPlayer1(player);
break;
}
case ygopro.StocHsPlayerChange.State.LEAVE: {
console.log("Player " + change.pos + " has leave");
change.pos == 0 ? setPlayer0({}) : setPlayer1({});
break;
}
case ygopro.StocHsPlayerChange.State.TO_OBSERVER: {
console.log("Player " + change.pos + " has moved to observer");
change.pos == 0 ? setPlayer0({}) : setPlayer1({});
setObserverCount(observerCount + 1);
break;
}
default: {
break;
}
} }
} }
...@@ -107,7 +148,7 @@ export default function WaitRoom() { ...@@ -107,7 +148,7 @@ export default function WaitRoom() {
case "stoc_hs_watch_change": { case "stoc_hs_watch_change": {
const count = pb.stoc_hs_watch_change.count; const count = pb.stoc_hs_watch_change.count;
console.log("Watch change to " + count); setObserverCount(count);
break; break;
} }
default: { default: {
...@@ -142,15 +183,31 @@ export default function WaitRoom() { ...@@ -142,15 +183,31 @@ export default function WaitRoom() {
}; };
return ( return (
<div> <div className="container">
<p>joined: {joined ? "true" : "false"}</p> <div className="playerRegion">
<button disabled={!joined} onClick={handleChoseDeck}> <h2>{joined ? "Room Joined!" : "Room Not Joined."}</h2>
choose hero.ydk <p>
</button> <button disabled={!joined} onClick={handleChoseDeck}>
<button disabled={!choseDeck} onClick={handleChoseReady}> choose hero.ydk
ready </button>
</button> </p>
<p>chat: {chat}</p> <p>
<button disabled={!choseDeck} onClick={handleChoseReady}>
ready
</button>
</p>
</div>
<div className="roomRegion">
<h2>Room PassWd: {passWd}</h2>
<p>
player0: {player0.name} {player0.state}
</p>
<p>
player1: {player1.name} {player1.state}
</p>
<p>observer: {observerCount}</p>
<p>chat: {chat}</p>
</div>
</div> </div>
); );
} }
......
body {
margin: 0;
justify-content: center;
}
div {
margin: auto;
padding: 10%;
}
p {
text-align: center;
}
body { .container {
margin: 0; display: flex;
justify-content: center;
}
div {
margin: auto; margin: auto;
padding: 10%; padding: 0;
} }
p { .playerRegion {
text-align: center; text-align: center;
flex: 1;
}
.roomRegion {
flex: 1;
}
.roomRegion > p {
text-align: start;
} }
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