aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/routes/index.svelte
blob: fd1f505414f492f8451a8f56c7d6bd344a9c434b (plain)
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
<script lang="ts" context="module">
  import { ApiClient } from "$lib/api_client";

  const NUM_MATCHES = "25";

  export async function load({ fetch }) {
    try {
      const apiClient = new ApiClient(fetch);

      let { matches, has_next } = await apiClient.get("/api/matches", {
        count: NUM_MATCHES,
      });

      return {
        props: {
          matches,
          hasNext: has_next,
        },
      };
    } catch (error) {
      return {
        status: error.status,
        error: new Error("failed to load matches"),
      };
    }
  }
</script>

<script lang="ts">
  import LinkButton from "$lib/components/LinkButton.svelte";
  import MatchList from "$lib/components/matches/MatchList.svelte";

  export let matches;
  export let hasNext;

  $: viewMoreUrl = olderMatchesLink(matches);

  // TODO: deduplicate.
  // Maybe move to ApiClient logic?
  function olderMatchesLink(matches: object[]): string {
    if (matches.length == 0 || !hasNext) {
      return null;
    }
    const lastTimestamp = matches[matches.length - 1]["timestamp"];
    return `/matches?before=${lastTimestamp}`;
  }
</script>

<div class="container">
  <div class="introduction">
    <h2>Welcome to PlanetWars!</h2>
    <p>
      Planetwars is a game of galactic conquest for busy people. Your goal is to program a bot that
      will conquer the galaxy for you, while you take care of more important stuff.
    </p>
    <p>
      Feel free to watch some games below to see what it's all about. When you are ready to try
      writing your own bot, head over to
      <a href="/docs">How to play</a> for instructions. You can program your bot in the browser
      using the <a href="/editor">Editor</a>.
    </p>
  </div>
  <h2>Recent matches</h2>
  <MatchList {matches} />
  <div class="see-more-container">
    <LinkButton href={viewMoreUrl}>View more</LinkButton>
  </div>
</div>

<style scoped lang="scss">
  .container {
    max-width: 800px;
    margin: 0 auto;
  }

  .introduction {
    padding-top: 16px;
    a {
      color: rgb(9, 105, 218);
      text-decoration: none;
    }

    a:hover {
      text-decoration: underline;
    }
  }

  .see-more-container {
    padding: 24px;
    text-align: center;
  }
</style>