-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks.php
167 lines (167 loc) · 8.49 KB
/
tasks.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
164
165
166
167
<?php
define('HEADER_TEXT', 'Tasks');
require_once __DIR__ . '/includes/globals.php';
if(array_key_exists('id', $_GET)) {
$_GET['id'] = isset($_GET['id']) && ctype_digit($_GET['id']) ? $_GET['id'] : null;
if(empty($_GET['id']))
$mtg->error('You didn\'t specify a valid task');
$_GET['code'] = isset($_GET['code']) && ctype_alnum($_GET['code']) ? $_GET['code'] : null;
if(empty($_GET['code']))
$mtg->error('You didn\'t specify a valid code');
if($_GET['code'] != $_SESSION['code'])
$mtg->error('You supplied an invalid code');
$_SESSION['code'] = md5(microtime(true));
$db->query('SELECT * FROM `tasks` WHERE `id` = ?');
$db->execute([$_GET['id']]);
if(!$db->num_rows())
$mtg->error('That task doesn\'t exist');
$task = $db->fetch_row(true);
if($task['upgraded_only'] && time() > strtotime($my['upgraded']))
$mtg->error('That task can be completed only by those whom have an upgraded account.');
if($task['nerve'] > $my['nerve'])
$mtg->error('You don\'t have enough nerve to complete this task');
if($task['courses_required']) {
$required = explode(',', trim($task['courses_required']));
if(count($required)) {
foreach($required as $req) {
$db->query('SELECT `id` FROM `users_courses_complete` WHERE `course` = ? AND `user` = ?');
$db->execute([$req, $my['id']]);
if(!$db->num_rows())
$mtg->error('You haven\'t completed all the courses required for this task');
}
}
}
$db->query('SELECT `enabled` FROM `tasks_groups` WHERE `id` = ?');
$db->execute([$task['group_id']]);
if($db->fetch_single() != 1)
$mtg->error('That task is currently disabled');
require_once __DIR__ . '/includes/class/jbbcode/Parser.php';
$parser = new jBBCode\Parser();
$parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());
$find = ['[TOTAL_STATS]', '[STRENGTH]', '[AGILITY]', '[GUARD]', '[LABOUR]', '[IQ]', '[MONEY]', '[POINTS]', '[POWER]', '[ENERGY]', '[NERVE]', '[LIFE]', '[EXP]', '[EXP_GIVEN]', '[MONEY_GIVEN]', '[POINTS_GIVEN]', '[ITEM_GIVEN]', '[HOSPITAL_TIME]', '[JAIL_TIME]'];
if($task['awarded_money_min'] && $task['awarded_money_max'])
$task['money'] = mt_rand($task['awarded_money_min'], $task['awarded_money_max']);
else if(!$task['awarded_money_min'] && $task['awarded_money_max'])
$task['money'] = mt_rand(0, $task['awarded_money_max']);
else if($task['awarded_money_min'] && !$task['awarded_money_max'])
$task['money'] = $task['awarded_money_min'];
else
$task['money'] = 0;
if($task['awarded_points_min'] && $task['awarded_points_max'])
$task['points'] = mt_rand($task['awarded_points_min'], $task['awarded_points_max']);
else if(!$task['awarded_points_min'] && $task['awarded_points_max'])
$task['points'] = mt_rand(0, $task['awarded_points_max']);
else if($task['awarded_points_min'] && !$task['awarded_points_max'])
$task['points'] = $task['awarded_points_min'];
else
$task['points'] = 0;
if($task['awarded_xp_min'] && $task['awarded_xp_max'])
$task['xp_awarded'] = mt_rand($task['awarded_xp_min'] * 100, $task['awarded_xp_max'] * 100) / 100;
else if(!$task['awarded_xp_min'] && $task['awarded_xp_max'])
$task['xp_awarded'] = mt_rand(0, $task['awarded_xp_max'] * 100) / 100;
else if($task['awarded_xp_min'] && !$task['awarded_xp_max'])
$task['xp_awarded'] = $task['awarded_points_min'];
else
$task['xp_awarded'] = 0;
if($task['awarded_item']) {
$db->query('SELECT `name` FROM `items` WHERE `id` = ?');
$db->execute([$task['awarded_item']]);
$task['item'] = $db->num_rows() ? $items->name($task['awarded_item']).' x'.$mtg->format($task['awarded_item_qty']) : 'None';
} else {
$task['awarded_item'] = 0;
$task['item'] = 'None';
}
$repl = [$my['total_stats'], $my['strength'], $my['agility'], $my['guard'], $my['labour'], $my['iq'], $my['money'], $my['points'], $my['power'], $my['energy'], $my['nerve'], $my['health'], $my['exp'], $task['xp_awarded'], $set['main_currency_symbol'].$mtg->format($task['money']), $mtg->format($task['points']).' point'.$mtg->s($task['points']), $task['item'], $mtg->time_format($task['time_hospital'] * 60), $mtg->time_format($task['time_jail'] * 60)];
$strs = ['text_start', 'text_success', 'text_failure', 'text_jail', 'text_reason_jail', 'text_hospital', 'text_reason_jail'];
foreach($strs as $str)
if(array_key_exists($str, $task))
$task[$str] = str_replace($find, $repl, $task[$str]);
$parser->parse(nl2br($mtg->format($task['text_start'])));
echo '<p>',$parser->getAsHTML(),'</p>';
$process = '$formula = '.str_replace($find, $repl, $task['formula']).';';
eval($process);
$formula += mt_rand(0, 50);
if($formula >=0 && $formula <= 10 && ($task['time_jail'] || $task['time_hospital'])) {
if($task['time_jail'] && $task['time_hospital']) {
$rand = mt_rand(0, 1);
$which = $rand == 1 ? 'hospital' : 'jail';
} else
$which = $task['jail_time'] ? 'jail' : 'hospital';
$db->startTrans();
$db->query('UPDATE `users` SET `'.$which.'` = ?, `'.$which.'_reason` = ? WHERE id = ?');
$db->execute([$task['time_'.$which], $task['text_reason_'.$which], $id]);
$col = $which == 'jail' ? 'jailed' : 'hospitalised';
$db->query('UPDATE `users_stats` SET `nerve` = `nerve` = ?, `tasks_'.$col.'` = `tasks_'.$col.'` + 1 WHERE `id` = ?');
$db->execute([$task['nerve'], $my['id']]);
$db->endTrans();
$parser->parse(nl2br($mtg->format($task['text_'.$which])));
echo '<p class="green">',$parser->getAsHTML(),'</p>';
} else if($formula >= 11 && $formula <= 40 || (!$task['time_jail'] && !$task['time_hospital'])) {
$db->query('UPDATE `users_stats` SET `nerve` = `nerve` - ?, `tasks_failed` = `tasks_failed` + 1 WHERE `id` = ?');
$db->execute([$task['nerve'], $my['id']]);
$parser->parse(nl2br($mtg->format($task['text_failure'])));
echo '<p class="orange">',$parser->getAsHTML(),'</p>';
} else {
$db->startTrans();
if($task['money'] > 0) {
$db->query('UPDATE `users_finances` SET `money` = `money` + ? WHERE `id` = ?');
$db->execute([$task['money'], $my['id']]);
}
if($task['points'] > 0) {
$db->query('UPDATE `users_finances` SET `points` = `points` + ? WHERE `id` = ?');
$db->execute([$task['points'], $my['id']]);
}
if($task['xp_awarded'] > 0) {
$db->query('UPDATE `users` SET `exp` = `exp` + ? WHERE `id` = ?');
$db->execute([$task['xp_awarded'], $my['id']]);
}
if($task['awarded_item'])
$users->giveItem($task['awarded_item'], $my['id'], $task['awarded_item_qty']);
$db->query('UPDATE `users_stats` SET `nerve` = `nerve` - ?, `tasks_complete` = `tasks_complete` + 1 WHERE `id` = ?');
$db->execute([$task['nerve'], $my['id']]);
$db->endTrans();
$parser->parse(nl2br($mtg->format($task['text_success'])));
echo '<p class="green">',$parser->getAsHTML(),'</p>';
}
}
$db->query('SELECT `id`, `name` FROM `tasks_groups` WHERE `enabled` = 1 ORDER BY `ordering` ASC');
$db->execute();
if(!$db->num_rows())
$mtg->info('There are currently no tasks available', true);
$groups = $db->fetch_row();
?><table class="pure-table" width="100%"><?php
foreach($groups as $group) {
?><tr>
<thead><th colspan="3" class="center"><?php echo $mtg->format($group['name']);?></th></thead>
</tr><?php
$extra = !$my['upgraded'] ? ' AND `upgraded_only` = 0' : '';
$db->query('SELECT `id`, `name`, `nerve`, `courses_required`, `upgraded_only` FROM `tasks` WHERE `group_id` = ?'.$extra.' ORDER BY `nerve` ASC');
$db->execute([$group['id']]);
if(!$db->num_rows())
echo '<tr><td colspan="3" class="center">There are no '.$mtg->format($group['name']).' tasks available</td></tr>';
else {
$tasks = $db->fetch_row();
$n = 0;
$_SESSION['code'] = md5(microtime(true));
foreach($tasks as $task) {
$coursesInfo = '';
if($task['courses_required']) {
$db->query('SELECT `id`, `name` FROM `courses` WHERE `enabled` = 1 AND `id` IN('.$task['courses_required'].')');
$db->execute();
$courses = $db->fetch_row();
foreach($courses as $course) {
$db->query('SELECT `id` FROM `courses_complete` WHERE `course` = ? AND `user` = ?');
$db->execute([$course['id'], $my['id']]);
$coursesInfo .= '<br /><span class="small '.($db->num_rows() ? 'green' : 'red').'">'.$mtg->format($course['name']).'</span>';
}
}
$upgrade = $task['upgraded_only'] ? '<img src="images/silk/joystick_add.png" title="Upgraded only!" alt="[Upgraded only]" /> ' : '';
?><tr>
<td<?php echo !$n ? ' width="33%"' : '';?>><?php echo $upgrade.$mtg->format($task['name']).$coursesInfo;?></td>
<td<?php echo !$n ? ' width="34%"' : '';?>><?php echo $mtg->format($task['nerve']);?></td>
<td<?php echo !$n ? ' width="33%"' : '';?>><a href="tasks.php?id=<?php echo $task['id'];?>&code=<?php echo $_SESSION['code'];?>">Attempt</a></td>
</tr><?php
}
}
}
?></table>