-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit.php
148 lines (123 loc) · 3.74 KB
/
submit.php
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
<?php
include_once 'util.php';
include_once 'config.php';
session_start();
// Redirect if not logged in
if (!isset($_SESSION['UserId'])) {
header('Location: login.php');
die();
}
// Start connection
// TODO Validate connection maybe
mysql_connect(DB_HOST, DB_USER, DB_PASS);
mysql_select_db(DB_NAME);
// Get info
$userId = (int)$_SESSION['UserId'];
$roundId = (int)$_POST['roundid'];
// Get round info
$result = mysql_query(
"select RoundId, CodingStart, BurningStart, RoundEnd, RedUserId, BlueUserId " .
"from `Round` natural join `Match` " .
"where RoundId = $roundId and (RedUserId = $userId or BlueUserId = $userId) " .
"limit 1;"
);
// User not matched in this round or round doesn't exist
if (mysql_num_rows($result) == 0) {
die('Round doesnt exist'); // TODO
}
$roundInfo = mysql_fetch_array($result);
$stInfo = StageInfo(
strtotime($roundInfo['CodingStart']), strtotime($roundInfo['BurningStart']), strtotime($roundInfo['RoundEnd'])
);
// Valid languages extensions
$VALID_LANG = Array('c', 'cpp', 'java', 'py');
$type = $_POST['type'];
switch ($type) {
case 'code':
// Check if we're in coding stage
if ($stInfo['StageName'] == WAIT_STAGE) {
die('Has not started'); // TODO
} else if ($stInfo['StageName'] != CODE_STAGE) {
die('Ended stage'); // TODO
}
// Check if language is valid
$lang = $_POST['lang'];
if (!in_array($lang, $VALID_LANG)) {
die('ERROR'); // TODO
}
// Set file route
$dir = 'codes';
$filename = "{$roundId}_{$userId}.{$lang}";
// Save or update code in DB
$result = mysql_query(
"select 1 as res from `Code` " .
"where UserId = $userId and RoundId = $roundId limit 1;"
);
if (mysql_num_rows($result) == 0) {
mysql_query(
"insert into `Code`(UserId, RoundId, Submission, CodeLang) " .
"values ($userId, $roundId, now(), '$lang');"
);
// TODO Inserted code
} else {
mysql_query(
"update `Code` set Submission = now(), CodeLang = '$lang' " .
"where UserId = $userId and RoundId = $roundId;"
);
// TODO New code
}
break;
case 'burn':
// Check if we're in coding stage
if ($stInfo['StageName'] == CLOS_STAGE) {
die('Ended stage'); // TODO
} else if ($stInfo['StageName'] != BURN_STAGE) {
die('Has not started'); // TODO
}
// Check if user uploaded a code to proceed
$result = mysql_query(
"select 1 as res from `Code` " .
"where UserId = $userId and RoundId = $roundId limit 1;"
);
if (mysql_num_rows($result) == 0) {
die('Not uploaded code'); // TODO
}
// Set file route
$dir = 'burns';
$burnedid = ($roundInfo['RedUserId'] == $userId) ? $roundInfo['BlueUserId'] : $roundInfo['RedUserId'];
$filename = "{$roundId}_{$burnedid}.txt";
// Save or update burn in DB
$result = mysql_query(
"select 1 as res from `Burn` " .
"where UserId = $burnedid and RoundId = $roundId limit 1;"
);
if (mysql_num_rows($result) == 0) {
mysql_query(
"insert into `Burn`(UserId, RoundId, Submission) " .
"values ($burnedid, $roundId, now());"
);
// TODO Inserted burn
} else {
mysql_query(
"update `Burn` set Submission = now() " .
"where UserId = $burnedid and RoundId = $roundId;"
);
// TODO New burn
}
break;
default:
die('Invalid type'); // TODO
}
// Create file
$subdir = $roundId;
$dirroute = "$dir/$subdir";
if (!file_exists($dirroute))
mkdir($dirroute, 0777, true);
$route = "$dirroute/$filename";
// Use uploaded file; if not possible, use input
if ($_FILES['file']['error'] == 0)
move_uploaded_file($_FILES['file']['tmp_name'], $route);
else
file_put_contents($route, $_POST['dinput']);
header("Location: arena.php?round=$roundId");
?>