aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/pages/Maplist.tsx
blob: b1b166467f3248f07973c28f72aef21f8053eb48 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import React, { useEffect } from "react";
import { Link, useLocation, useParams } from "react-router-dom";

import "../css/Maplist.css";
import { API } from "../api/Api";
import { Game, GameChapters } from "../types/Game";
import { GameChapter, GamesChapters } from "../types/Chapters";
import { Map } from "../types/Map";

const Maplist: React.FC = () => {
  const [game, setGame] = React.useState<Game | null>(null);
  const [catNum, setCatNum] = React.useState(0);
  const [id, setId] = React.useState(0);
  const [category, setCategory] = React.useState(0);
  const [load, setLoad] = React.useState(false);
  const [currentlySelected, setCurrentlySelected] = React.useState<number>(0);
  const [hasClicked, setHasClicked] = React.useState(false);
  const [gameChapters, setGameChapters] = React.useState<GamesChapters>();
  const [curChapter, setCurChapter] = React.useState<GameChapter>();
  const [numChapters, setNumChapters] = React.useState<number>(0);

  const [dropdownActive, setDropdownActive] = React.useState("none");

  const params = useParams<{ id: string }>();
  const location = useLocation();

  function _update_currently_selected(catNum2: number) {
      setCurrentlySelected(catNum2);
      setHasClicked(true);
  }

  const _fetch_chapters = async (chapter_id: string) => {
    const chapters = await API.get_chapters(chapter_id);
    setCurChapter(chapters);
  }

  const _handle_dropdown_click = () => {
    if (dropdownActive == "none") {
      setDropdownActive("block");
    } else {
      setDropdownActive("none");
    }
  }

  // im sorry but im too lazy to fix this right now
  useEffect(() => {
    // gameID
    const gameId = parseFloat(params.id || "");
    setId(gameId);

    // location query params
    const queryParams = new URLSearchParams(location.search);
    if (queryParams.get("cat")) {
        const cat = parseFloat(queryParams.get("cat") || "");
        setCategory(cat);
        setCatNum(cat - 1);
    }

    const _fetch_game = async () => {
      const games = await API.get_games();
      const foundGame = games.find((game) => game.id === gameId);
      // console.log(foundGame)
      if (foundGame) {
        setGame(foundGame);
      }
    };
    
    const _fetch_game_chapters = async () => {
      const games_chapters = await API.get_games_chapters(gameId.toString());
      setGameChapters(games_chapters);
      setNumChapters(games_chapters.chapters.length);
    }

    const _fetch_maps = async () => {
      const maps = await API.get_games_maps(gameId.toString());
      setLoad(true);
    }

    _fetch_game();
    _fetch_game_chapters();
    _fetch_maps();
  }, []);

  useEffect(() => {
    if (gameChapters != undefined) {
      _fetch_chapters(gameChapters!.chapters[0].id.toString());
    }
  }, [gameChapters])



  return (
    <main>
      <section style={{ marginTop: "20px" }}>
        <Link to="/games">
          <button className="nav-button" style={{ borderRadius: "20px" }}>
            <i className="triangle"></i>
            <span>Games list</span>
          </button>
        </Link>
      </section>
      {!load ? (
        <div></div>
      ) : (
        <section>
          <h1>{game?.name}</h1>
          <div
            style={{ backgroundImage: `url(${game?.image})` }}
            className="game-header"
          >
            <div className="blur">
              <div className="game-header-portal-count">
                <h2>
                  {
                    game?.category_portals.find(
                      (obj) => obj.category.id === catNum + 1
                    )?.portal_count
                  }
                </h2>
                <h3>portals</h3>
              </div>
              <div className="game-header-categories">
                {game?.category_portals.map((cat, index) => (
                  <button key={index} className={currentlySelected == cat.category.id || cat.category.id - 1 == catNum && !hasClicked ? "game-cat-button selected" : "game-cat-button"} onClick={() => {setCatNum(cat.category.id - 1); _update_currently_selected(cat.category.id)}}>
                    <span>{cat.category.name}</span>
                  </button>
                ))}
              </div>
            </div>
          </div>

          <div>
            <section className="chapter-select-container">
              <div>
                <span style={{fontSize: "18px", transform: "translateY(5px)", display: "block", marginTop: "10px"}}>{curChapter?.chapter.name.split(" - ")[0]}</span>
              </div>
              <div onClick={_handle_dropdown_click} className="dropdown">
                <span>{curChapter?.chapter.name.split(" - ")[1]}</span>
                <i className="triangle"></i>
              </div>
              <div className="dropdown-elements" style={{display: dropdownActive}}>
                {gameChapters?.chapters.map((chapter, i) => {
                  return <div className="dropdown-element" onClick={() => {_fetch_chapters(chapter.id.toString()); _handle_dropdown_click()}}>{chapter.name}</div>
                })

                }
              </div>
            </section>
            <section className="maplist">
                {curChapter?.maps.map((map, i) => {
                  return <div className="maplist-entry">
                    <Link to={`/maps/${map.id}`}>
                    <span>{map.name}</span>
                    <div className="map-entry-image" style={{backgroundImage: `url(${map.image})`}}>
                      <div className="blur map">
                        <span>{map.is_disabled ? map.category_portals[0].portal_count : map.category_portals.find(
                          (obj) => obj.category.id === catNum + 1
                        )?.portal_count}</span>
                        <span>portals</span>
                      </div>
                    </div>
                    <div className="difficulty-bar">
                      <span>Difficulty:</span>
                      <div className={map.difficulty == 0 ? "one" : map.difficulty == 1 ? "two" : map.difficulty == 2 ? "three" : map.difficulty == 3 ? "four" : map.difficulty == 4 ? "five" : "one"}>
                        <div className="difficulty-point"></div>
                        <div className="difficulty-point"></div>
                        <div className="difficulty-point"></div>
                        <div className="difficulty-point"></div>
                        <div className="difficulty-point"></div>
                      </div>
                    </div>
                    </Link>
                  </div>
                })}
            </section>
          </div>
        </section>
      )}
    </main>
  );
};

export default Maplist;