Skip to content
Snippets Groups Projects
GameView.js 6.26 KiB
Newer Older
import React from "react";
Joni Laukka's avatar
Joni Laukka committed
import UserMap from "./UserMap";
import TaskListButton from "./TaskListButton";
Joni Laukka's avatar
Joni Laukka committed
import { Link } from "react-router-dom";
import EditGameForm from "./EditGameForm";
import JoinGameForm from "./JoinGameForm";
import PlayerlistView from "./PlayerlistView";
import NotificationView from "./NotificationView";
Joni Laukka's avatar
Joni Laukka committed
import GameStateButtons from "./GameStateButtons";

export default class GameView extends React.Component {
  state = {
Joni Laukka's avatar
Joni Laukka committed
    role: "", //empty, soldier, factionleader, admin
Joni Laukka's avatar
Joni Laukka committed
    lat: 62.2416479,
    lng: 25.7597186,
    zoom: 13,
    mapUrl: "https://tiles.kartat.kapsi.fi/taustakartta/{z}/{x}/{y}.jpg",
    socketSignal: null
  };

  componentDidMount() {
    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 = "/";
      });

    // Get game info
    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
      }
    })
Joni Laukka's avatar
Joni Laukka committed
      .then(res => res.json())
      .then(res => {
Joni Laukka's avatar
Joni Laukka committed
        this.setState({ role: res.role });
Joni Laukka's avatar
Joni Laukka committed
      .catch(error => console.log(error));
Joni Laukka's avatar
Joni Laukka committed
  handleLeaveFaction = e => {
    let token = sessionStorage.getItem("token");
Joni Laukka's avatar
Joni Laukka committed
    fetch(
      `${process.env.REACT_APP_API_URL}/faction/leave/${
        this.state.gameInfo.id
      }`,
      {
        method: "DELETE",
        headers: {
          Authorization: "Bearer " + token
        }
      }
    )
      .then(res => {
Joni Laukka's avatar
Joni Laukka committed
        }
Joni Laukka's avatar
Joni Laukka committed
      })
      .then(res => {
        alert(res.message);
      })
      .catch(error => console.log(error));
  };
Joni Laukka's avatar
Joni Laukka 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
        });
      }
    );
  };

Joni Laukka's avatar
Joni Laukka committed
    const initialPosition = [this.state.lat, this.state.lng];

    return (
      <div>
Joni Laukka's avatar
Joni Laukka committed
        <Link to="/">
          <button>Game selection</button>
        </Link>
            {this.state.gameInfo.id && (
              <ClientSocket
                gameId={this.state.gameInfo.id}
                getSocketSignal={this.getSocketSignal}
              />
            )}
            <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>
            )}
            <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}
              />
            )}
Joni Laukka's avatar
Joni Laukka committed
            {this.state.role !== "admin" && this.state.role !== "" && (
              <button id="leaveFactionButton" onClick={this.handleLeaveFaction}>
                Leave Faction
              </button>
            )}
Joni Laukka's avatar
Joni Laukka committed
            {this.state.role === "admin" && (
              <GameStateButtons
                gameState={this.state.gameInfo.state}
                gameId={this.state.gameInfo.id}
              />
Joni Laukka's avatar
Joni Laukka committed
            )}
            <UserMap
              position={initialPosition}
              zoom={this.state.zoom}
              mapUrl={this.state.mapUrl}
              currentGameId={this.state.gameInfo.id}
            />
            {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}
                role={this.state.role}
                toggleView={() => this.setState({ form: "" })}
              />
            )}
            {this.state.form === "notifications" && (
              <NotificationView
                gameId={this.state.gameInfo.id}
                toggleView={() => this.setState({ form: "" })}
              />
            )}
          </div>
        )}
Joni Laukka's avatar
Joni Laukka committed
      </div>
    );