summaryrefslogtreecommitdiff
path: root/Matchmaking/Server/index.php
blob: de7d5c0b7cd0fbba6b2b236b46347ba376eb7d3d (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
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
<?php

/*
 * Routes:
 * /host      ?host&port=$port&name=$name
 * /get-list  ?get-list
 * /join      ?join=$id
 */

global $db;
$db = new PDO('sqlite:./db', options: [
  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);

if (php_sapi_name() == 'cli') {
  if ($argv[$argc - 1] == 'init-db') {
    $db->query(<<<SQL
      create table if not exists games (
        id text primary key,
        ip text unique,
        port integer,
        timestamp integer,
        name text
      );
      SQL)
      ->execute();
  }

  return;
}

$response = [];

if (isset($_POST['host'])) {
  $ip = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP);

  if ($ip === false) {
    $response = [
      'success' => false,
      'error' => 'Invalid IP.',
    ];
  }

  else {
    $port = intval($_POST['port']);
    $name = $_POST['name'];

    $success = $db->prepare(<<<SQL
      insert into games (id, ip, port, timestamp, name) values (:id, :ip, :port, :timestamp, :name)
      SQL)
      ->execute([
        'id' => md5($name . time()),
        'ip' => $ip,
        'port' => $port,
        'timestamp' => time(),
        'name' => $name,
      ]);

    $response = [
      'success' => $success,
    ];
  }
}

else if (isset($_GET['get-list'])) {
  $statement = $db->prepare(<<<SQL
    select id, name from games
    SQL);
  $statement->execute();
  $results = $statement->fetchAll();

  $list = [];
  foreach ($results as $row) {
    $list[] = [
      'id' => $row['id'],
      'name' => $row['name'],
    ];
  }

  $response = [
    'success' => true,
    'data' => $list,
  ];
}

else if (isset($_GET['join'])) {
  $id = $_GET['join'];

  $statement = $db->prepare(<<<SQL
    select ip, port from games
    where id = :id
    SQL);
  $statement->execute(['id' => $id]);
  $result = $statement->fetch();

  if ($result === false) {
    $response = [
      'success' => false,
      'error' => 'Game not found.'
    ];
    http_response_code(404);
  }

  else {
    $response = [
      'success' => true,
      'data' => [
        'ip' => $result['ip'],
        'port' => $result['port'],
      ],
    ];
  }
}

else if (isset($_POST['close'])) {
  $id = $_POST['close'];

  $success = $db->prepare(<<<SQL
    delete from games where id = :id
    SQL)
    ->execute([
      'id' => $id,
    ]);

  $response = [
    'success' => $success,
  ];
}

else {
  $response = [
    'success' => false,
    'error' => 'Route not found.'
  ];
  http_response_code(404);
}

header('Content-Type: application/json');
echo json_encode($response);