-
Notifications
You must be signed in to change notification settings - Fork 3
/
ChallSolveEngine.php
163 lines (143 loc) · 4.38 KB
/
ChallSolveEngine.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace GDO\TBS;
use GDO\Core\GDT_Error;
use GDO\Core\GDT_Success;
use GDO\User\GDO_User;
use GDO\Util\Process;
use GDO\Util\Strings;
use GDO\Python\Module_Python;
use GDO\TBS\Method\Ranking;
/**
* Logic for solution submission.
* Also calls solutionChecker.py
* @author gizmore
* @version 6.10
* @since 6.10
*/
final class ChallSolveEngine
{
private $challenge;
public function __construct(GDO_TBS_Challenge $challenge)
{
$this->challenge = $challenge;
}
public function onSolve($answer)
{
if (!$this->checkSolveAttempt())
{
return;
}
/** @var $bcrypt \GDO\Util\BCrypt **/
$bcrypt = $this->challenge->getValue('chall_solution');
if ($bcrypt === null)
{
if ($this->challenge->getStatus() === GDT_TBS_ChallengeStatus::NOT_TRIED)
{
$this->onSolveSolutionChecker($answer);
}
}
elseif ($bcrypt->validate($answer))
{
$this->solved(GDO_User::current());
}
else
{
$this->wrong(GDO_User::current());
}
}
/**
* Check if a user needs to wait to submit another solution.
* @return boolean
*/
private function checkSolveAttempt()
{
$user = GDO_User::current();
if ($time = GDO_TBS_ChallengeSolveAttempt::getTimeout($user))
{
echo GDT_Error::responseWith('err_tbs_solve_timeout', [$time])->render();
return false;
}
return true;
}
private function getURL()
{
return $this->challenge->getURL()->raw;
}
private function onSolveSolutionChecker($answer)
{
$module = Module_TBS::instance();
$python = Module_Python::instance()->cfgPythonPath();
$script = GDO_PATH . 'GDO/TBS/bin/solutionChecker.py';
$script = Process::osPath($script);
$tbsURL = Strings::substrFrom($this->getURL(), '/challenges/');
$user = $module->cfgSolveUser();
$pass = $module->cfgSolvePass();
$params = sprintf('"%s" "%s" "%s" "%s"',
$tbsURL, $answer, $user, $pass);
$output = null; # returned via reference
$return = null; # returned via reference
$command = "$python $script $params";
exec($command, $output, $return);
var_dump($output);
if ($return === 0) #&& $output)
{
if ($output[0] === 'OK')
{
$this->solved(GDO_User::current());
$this->onRegisterSolution($answer);
}
elseif ($output[0] === 'FAILED')
{
$this->wrong(GDO_User::current());
}
else
{
echo "Checker output is " . implode('<br/>', $output);
}
}
else
{
echo "Checker returned $return: " . implode('<br/>', $output) . ".";
}
}
private function onRegisterSolution($answer)
{
$this->challenge->saveVar('chall_solution', $answer);
}
private function wrong(GDO_User $user)
{
echo GDT_Error::responseWith('err_tbs_solve_wrong')->render();
GDO_TBS_ChallengeSolveAttempt::tried(GDO_User::current(), $this->challenge);
return false;
}
public function solved(GDO_User $user=null)
{
$user = $user === null ? GDO_User::current() : $user;
if (!$this->challenge->isPersisted())
{
return $this->response('msg_tbs_solved_alpha');
}
if ($this->challenge->hasSolved($user))
{
return $this->response('msg_tbs_solved_already');
}
return $this->onSolved($user);
}
private function onSolved(GDO_User $user)
{
# Mark solved
GDO_TBS_ChallengeSolved::challengeSolved($this->challenge, $user);
# Calculate category stats
list($before, $after) = GDO_TBS_ChallengeSolvedCategory::updateUser($user);
# Flush Ranking cache
Ranking::make()->fileUncache();
# Response with gain.
$gain = $after - $before;
return $this->response('msg_tbs_solved', [$gain, $after]);
}
private function response($key, array $args=null)
{
echo GDT_Success::make()->text($key, $args)->render();
return true;
}
}