-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.php
More file actions
49 lines (45 loc) · 1.06 KB
/
Copy pathrobot.php
File metadata and controls
49 lines (45 loc) · 1.06 KB
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
<?php
/**
*
*/
class Robot extends Entity {
function __construct($space, $type, $group = null, $name = null) {
parent::__construct($space, $type, $group, $name);
$this->MODE = Entity::IDLE;
}
public function addTask(Action $action, $priority = 10){
array_push($this->tasks, array(
'action' => $action,
'priority' => $priority,
'retry' => 0
));
$this->MODE = Entity::WORKING;
}
public function removeTask($action){
foreach ($this->tasks as $index => $task) {
if ($task['action'] == $action) {
unset($this->tasks[$index]);
}
}
}
public function work(){
if ($this->MODE == Entity::IDLE) {
return true;
}
if (!$this->tasks) {
$this->MODE = Entity::IDLE;
return true;
}
usort($this->tasks, function($a, $b){
return ($a['priority'] <= $b['priority']) ? -1 : 1;
});
$task = array_shift($this->tasks);
$task['action']->start($this);
if ($task['action']->state() == Action::FAILURE) {
$task['retry'] += 1;
array_push($this->tasks, $task);
}
return true;
}
}
?>