Newer
Older
import TaskListButton from "./TaskListButton";
Joni Laukka
committed
import EditGameForm from "./EditGameForm";
import JoinGameForm from "./JoinGameForm";
import PlayerlistView from "./PlayerlistView";
import NotificationView from "./NotificationView";

L4929
committed
import ClientSocket from "./Socket";
export default class GameView extends React.Component {
state = {
Joni Laukka
committed
gameInfo: null,
Joni Laukka
committed
form: "",

L4929
committed
mapUrl: "https://tiles.kartat.kapsi.fi/taustakartta/{z}/{x}/{y}.jpg",
socketSignal: null
Joni Laukka
committed
let gameId = new URL(window.location.href).searchParams.get("id");
let token = sessionStorage.getItem("token");
fetch(`${process.env.REACT_APP_API_URL}/game/${gameId}`)
.then(res => {
if (!res.ok) {
throw Error();
}
})
.catch(error => {
alert("Game not found");
window.document.location.href = "/";
});
Joni Laukka
committed
fetch(`${process.env.REACT_APP_API_URL}/game/${gameId}`)
.then(res => res.json())
.then(res => {
this.setState({
gameInfo: res
});
})
.catch(error => console.log(error));
// Get Role
fetch(`${process.env.REACT_APP_API_URL}/faction/check-faction/${gameId}`, {
method: "GET",
headers: {
Authorization: "Bearer " + token
}
})
handleLeaveFaction = e => {
let token = sessionStorage.getItem("token");
let error = false;
fetch(
`${process.env.REACT_APP_API_URL}/faction/leave/${
this.state.gameInfo.id
}`,
{
method: "DELETE",
headers: {
Authorization: "Bearer " + token
}
}
)
.then(res => {
if (!res.ok) {
error = true;
return res.json();
})
.then(res => {
alert(res.message);
})
.catch(error => console.log(error));
};

L4929
committed
// setting the socket signal automatically fires shouldComponentUpdate function where socketSignal prop is present
// setting socketSignal to null immediately after to avoid multiple database fetches
getSocketSignal = type => {
console.log(type);
this.setState(
{
socketSignal: type
},
() => {
this.setState({
socketSignal: null
});
}
);
};
const initialPosition = [this.state.lat, this.state.lng];
return (
<div>
<Link to="/">
<button>Game selection</button>
</Link>
Joni Laukka
committed
{this.state.gameInfo !== null && (
<div>

L4929
committed
{this.state.gameInfo.id && (
<ClientSocket
gameId={this.state.gameInfo.id}
getSocketSignal={this.getSocketSignal}
/>
)}
Joni Laukka
committed
<div>Game Name: {this.state.gameInfo.name}</div>
{this.state.role === "" && (
<div>You don't have a role in this game</div>
)}
{this.state.role !== "" && (
<div>Your role in this game: {this.state.role}</div>
)}
{this.state.role === "admin" && (
<button
id="editGameButton"
onClick={() => this.setState({ form: "edit" })}
>
Edit
</button>
)}
{this.state.role === "" && (
<button
id="joinGameButton"
onClick={() => this.setState({ form: "join" })}
>
Join
</button>
)}
Joni Laukka
committed
<button
id="showPlayersButton"
onClick={() => this.setState({ form: "players" })}
>
Players
</button>
{this.state.role !== "" && (
<button
id="notificationsButton"
onClick={() => this.setState({ form: "notifications" })}
>
Notifications
</button>
)}
{this.state.role !== "" && (
<TaskListButton
gameId={this.state.gameInfo.id}
role={this.state.role}
/>
)}
{this.state.role !== "admin" && this.state.role !== "" && (
<button id="leaveFactionButton" onClick={this.handleLeaveFaction}>
Leave Faction
</button>
)}
<GameStateButtons
gameState={this.state.gameInfo.state}
gameId={this.state.gameInfo.id}
/>
Joni Laukka
committed
<UserMap
position={initialPosition}
zoom={this.state.zoom}
mapUrl={this.state.mapUrl}
currentGameId={this.state.gameInfo.id}

L4929
committed
socketSignal={this.state.socketSignal}
role={this.state.role}
Joni Laukka
committed
/>
{this.state.form === "edit" && (
<EditGameForm
gameId={this.state.gameInfo.id}
toggleView={() => this.setState({ form: "" })}
onEditSave={() => {
this.getGameInfo();
}}
/>
)}
{this.state.form === "join" && (
<JoinGameForm
gameId={this.state.gameInfo.id}
toggleView={() => this.setState({ form: "" })}
onJoin={() => console.log("joinde")}
/>
)}
{this.state.form === "players" && (
<PlayerlistView
gameId={this.state.gameInfo.id}
Joni Laukka
committed
toggleView={() => this.setState({ form: "" })}
/>
)}
{this.state.form === "notifications" && (
<NotificationView
gameId={this.state.gameInfo.id}
toggleView={() => this.setState({ form: "" })}
/>
)}
</div>
)}