summaryrefslogtreecommitdiff
path: root/Game/Lobby/Server/index.php
blob: 16be3ef825628ebfb0dfc64b52466bc88bb883e8 (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
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
<?php

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

$dbname = $_ENV['DB_NAME'] ?? 'db';
$timeoutMax = intval($_ENV['TIMEOUT_MAX'] ?? (1 * 60));

global $db;
$db = new PDO('sqlite:./' . $dbname, 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,
        creation_time integer,
        ping_time integer,
        name text
      );
      SQL)
      ->execute();
  }

  return;
}

$inputPost = json_decode(file_get_contents('php://input'), true);
if ($inputPost) {
  $_POST = $inputPost + $_POST;
}

$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'];
    $id = md5($name . time());

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


    if ($success) {
      $response = [
        'success' => $success,
        'data' => $id,
      ];
    } else {
      $response = [
        'success' => $success,
        'error' => $db->errorInfo(),
      ];
    }
  }
}

else if (isset($_GET['get-list'])) {
  $statement = $db->prepare(<<<SQL
    select id, name from games
    where ping_time > :comparison_time
    SQL);
  $statement->execute([
    'comparison_time' => time() - $timeoutMax,
  ]);
  $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 if (isset($_POST['keep-alive'])) {
  $id = $_POST['keep-alive'];

  $success = $db->prepare(<<<SQL
    update games set ping_time = :ping_time where id = :id
    SQL)
    ->execute([
      'id' => $id,
      'ping_time' => time(),
    ]);

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

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

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