Skip to content

Commit 8a4c151

Browse files
committed
Added api_game_result.php. Updated worker.
1 parent 04c6415 commit 8a4c151

13 files changed

+633
-371
lines changed

website/api_game_result.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
// ini_set('error_reporting', E_ALL);
3+
// ini_set('display_errors', true);
4+
5+
require_once('api_functions.php');
6+
7+
header("Content-type: application/json");
8+
9+
$json_string = file_get_contents('php://input');
10+
$json_hash = md5($json_string);
11+
$gamedata = json_decode($json_string);
12+
13+
if ($gamedata == null) {
14+
api_log("Did not recieve post data for game result.");
15+
} else {
16+
if (array_key_exists('error', $gamedata)) {
17+
// set to non-existant worker and email admin
18+
if (mysql_query($sql['update_matchup_failed'])) {
19+
echo json_encode(array( "hash" => $json_hash ));
20+
} else {
21+
api_log(sprintf("Error updating failed matchup %s",
22+
$gamedata->matchup_id));
23+
}
24+
} else {
25+
// move matchup data to game table
26+
mysql_query("SET AUTOCOMMIT=0");
27+
mysql_query("START TRANSACTION");
28+
$stmt = sprintf($sql["insert_game_data"], $gamedata->matchup_id);
29+
if (mysql_query($stmt)) {
30+
$game_id = mysql_insert_id();
31+
for ($i = 0, $size = sizeof($gamedata->rank); $i < $size; ++$i) {
32+
$stmt = sprintf($sql["insert_game_player"],
33+
$game_id,
34+
$gamedata->rank[$i],
35+
$gamedata->score[$i],
36+
$gamedata->matchup_id,
37+
$i);
38+
if (!mysql_query($stmt)) {
39+
api_log(mysql_error());
40+
mysql_query("ROLLBACK");
41+
api_log(sprintf("Error updating game players for matchup %s: %s",
42+
$gamedata->matchup_id,
43+
$stmt));
44+
die();
45+
}
46+
}
47+
if (mysql_query(sprintf($sql["delete_matchup_player"], $gamedata->matchup_id))) {
48+
if (mysql_query(sprintf($sql["delete_matchup"], $gamedata->matchup_id))) {
49+
mysql_query("COMMIT");
50+
echo json_encode(array( "hash" => $json_hash ));
51+
} else {
52+
mysql_query("ROLLBACK");
53+
api_log(sprintf("Error deleting matchup %s",
54+
$gamedata->matchup_id));
55+
}
56+
} else {
57+
mysql_query("ROLLBACK");
58+
api_log(sprintf("Error deleting players for matchup %s",
59+
$gamedata->matchup_id));
60+
}
61+
} else {
62+
mysql_query("ROLLBACK");
63+
api_log(sprintf("Error updating game table for matchup %s: %s",
64+
$gamedata->matchup_id,
65+
$stmt));
66+
}
67+
}
68+
}
69+
70+
?>

website/api_get_languages.php

-18
This file was deleted.

website/api_get_task.php

+13-10
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,31 @@
2626
}
2727

2828
// look for match
29-
$match_result = mysql_query(sprintf($sql["select_next_match"],
29+
$match_result = mysql_query(sprintf($sql["select_next_matchup"],
3030
$worker["worker_id"]));
3131
if ($match_result) {
3232
while ($match_row = mysql_fetch_assoc($match_result)) {
3333
$json = array( "task" => "game",
34-
"match_id" => $match_row["match_id"],
35-
"map_url" => $match_row["filename"],
34+
"matchup_id" => $match_row["matchup_id"],
35+
"map_filename" => $match_row["filename"],
3636
"players" => array());
3737

38-
$player_result = mysql_query(sprintf($sql["select_match_players"],
39-
$row["match_id"]));
40-
while ($player_row = mysql_fetch_assoc($player_result)) {
41-
$json["players"][] = $player_row["submission_id"];
38+
$player_result = mysql_query(sprintf($sql["select_matchup_players"],
39+
$match_row["matchup_id"]));
40+
if ($player_result) {
41+
while ($player_row = mysql_fetch_assoc($player_result)) {
42+
$json["players"][] = $player_row["submission_id"];
43+
}
44+
echo json_encode($json);
45+
die();
46+
} else {
47+
api_log(sprintf("Error selecting matchup players: %s", mysql_error()));
4248
}
43-
echo json_encode($json);
44-
die();
4549
}
4650
} else {
4751
api_log(sprintf("Error selected next match: %s", mysql_error()));
4852
}
4953

5054
// nothing to do
5155
echo json_encode(array( "task" => "expand lolcode specification" ));
52-
5356
?>

website/sql.php

+35-16
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,52 @@
66
where status = 20
77
or (status = 24 and worker_id = %s)
88
order by submission_id asc
9-
limit 1",
9+
limit 1;",
1010
"update_submission_compiling" => "update submission
1111
set status = 24,
1212
worker_id = %s
13-
where submission_id = %s",
13+
where submission_id = %s;",
1414
"update_submission_success" => "update submission
1515
set status = 40,
1616
latest = 1
1717
where worker_id = %s
18-
and submission_id = %s",
18+
and submission_id = %s;",
1919
"update_submission_failure" => "update submission
2020
set status = %s
2121
where worker_id = %s
22-
and submission_id = %s",
23-
"select_next_match" => "select `match`.*, map.filename
24-
from `match`
25-
left join map on `match`.map_id = map.map_id
26-
where worker is null
27-
or worker = %s
28-
order by match_id asc
29-
limit 1",
30-
"select_match_players" => "select *
31-
from match_player
32-
where match_id = %s
33-
order by player_id",
22+
and submission_id = %s;",
23+
"select_next_matchup" => "select matchup.*, map.filename
24+
from matchup
25+
left join map on matchup.map_id = map.map_id
26+
where worker_id is null
27+
or worker_id = %s
28+
order by matchup_id asc
29+
limit 1;",
30+
"select_matchup_players" => "select *
31+
from matchup_player
32+
where matchup_id = %s
33+
order by player_id;",
3434
"select_languages" => "select *
35-
from language"
35+
from language;",
36+
"select_player_skills" => "select player_id, sigma, mu
37+
from matchup_player p
38+
inner join submission s on p.submission_id = s.submission_id
39+
where matchup_id = %s
40+
order by player_id;",
41+
"insert_game_data" => "insert into game
42+
select null, seed_id, map_id, current_timestamp, worker_id, null
43+
from matchup
44+
where matchup_id = %s;",
45+
"insert_game_player" => "insert into game_player
46+
select %s, p.user_id, p.submission_id, player_id,
47+
null, null, %s, %s,
48+
s.sigma, null, s.mu, null, 1
49+
from matchup_player p
50+
inner join submission s on p.submission_id = s.submission_id
51+
where matchup_id = %s
52+
and player_id = %s;",
53+
"delete_matchup" => "delete from matchup where matchup_id = %s;",
54+
"delete_matchup_player" => "delete from matchup_player where matchup_id = %s;"
3655

3756
);
3857

-1.05 MB
Binary file not shown.
Binary file not shown.
-1.05 MB
Binary file not shown.
Binary file not shown.

worker/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
server_info.py
22
submissions*
3+
games*
4+
maps*
35
*.log

0 commit comments

Comments
 (0)