Skip to content

Commit

Permalink
Setting up changing states
Browse files Browse the repository at this point in the history
  • Loading branch information
winnet101 committed Sep 4, 2024
1 parent 3e6de50 commit 7164dfd
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from "react";
import Host from "./Host";
import Client from "./Client";
import Host from "./host/Join";
import Client from "./client/Join";

export default function App() {
const [isHost, setIsHost] = useState(false);
Expand Down
13 changes: 5 additions & 8 deletions src/Client.tsx → src/client/Join.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "react";
import StringInput from "./lib/StringInput";
import { State } from "./types/gameTypes";
import { useClientState } from "./react-use-peer-state";
import StringInput from "../lib/StringInput";
import { State } from "../types/gameTypes";
import { useClientState } from "../hooks/react-use-peer-state";

export default function Client() {
const [host, setHost] = useState<string>();
Expand All @@ -16,16 +16,13 @@ export default function Client() {
<h3>CLIENT</h3>
<p>
{name ? `Current name: ${name}` : "Choose a name"}{" "}
<StringInput
onSubmit={(name) => {
name.trim() && setName(name.trim());
}}
/>
<StringInput onSubmit={(name) => setName(name)} />
</p>

{connected ? (
<>
<p>Connected</p>
<code>{state?.kind === "join" && state.players}</code>
</>
) : (
<>
Expand Down
File renamed without changes.
42 changes: 32 additions & 10 deletions src/Host.tsx → src/host/Join.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useEffect, useState } from "react";
import StringInput from "./lib/StringInput";
import { GameState, State } from "./types/gameTypes";
import { useHostState } from "./react-use-peer-state";
import { toRemoved } from "./utils";
import StringInput from "../lib/StringInput";
import { State } from "../types/gameTypes";
import { useHostState } from "../hooks/react-use-peer-state";
import { toRemoved } from "../utils";

export default function Host() {
const [id, setId] = useState<string>();
Expand All @@ -25,8 +25,11 @@ export default function Host() {

conn.on("close", () => {
if (state.kind !== "join") return;
setState({...state, players: toRemoved(state.players, conn.label)})
})
setState({
...state,
players: [name, ...toRemoved(state.players, conn.label)],
});
});
});

return () => {
Expand All @@ -36,7 +39,7 @@ export default function Host() {
connection.off("error");
});
};
});
}, [connections]);

return (
<>
Expand All @@ -45,16 +48,35 @@ export default function Host() {
<StringInput onSubmit={setId} />
{connections ? (
<>
<p>{connections.length} connected</p>
<code> [{connections.map((con) => con.label)}]</code> <br></br>
<code> [{state.kind === "join" && state.players.join(", ")}]</code>{" "}
<br></br>
</>
) : (
<p>None are connected</p>
)}
<p>
Current name: {name}{" "}
<StringInput onSubmit={(name) => setName(name.trim())} />
<StringInput
onSubmit={(newName) => {
if (state.kind !== "join") return;
setName(newName);
setState({
...state,
players: toRemoved([...state.players, newName], name),
});
// toRemoved([...], name): removes old name
// [...state.players, newName]: adds new name
}}
/>
</p>
<button
onClick={() => {
if (state.kind !== "join") return;
setState({ kind: "assign", players: state.players.map((p) => ({name: p, partyLeader: ""})) });
}}
>
Start Game
</button>
</>
);
}
3 changes: 3 additions & 0 deletions src/lib/StringInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ export default function StringInput({
onSubmit,
clearOnSubmit = true,
enterSubmit = true,
acceptEmpty = false
}: {
onSubmit: (value: string) => void;
clearOnSubmit?: boolean;
enterSubmit?: boolean;
acceptEmpty?: boolean;
}) {
const [value, setValue] = useState("");

Expand All @@ -22,6 +24,7 @@ export default function StringInput({
}

function handleSubmit() {
if (!acceptEmpty && !value.trim()) return;
if (clearOnSubmit) setValue("");
onSubmit(value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/gameTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ type State =
| ModifierState
| ChallengeState;

export type { GameState, State, Player };
export type { JoinState, GameState, State, Player };

0 comments on commit 7164dfd

Please sign in to comment.