Skip to content

Commit e0775e9

Browse files
committed
Update Sun Jul 19 17:31:35 CEST 2015
1 parent b9e6d9f commit e0775e9

File tree

4 files changed

+254
-0
lines changed

4 files changed

+254
-0
lines changed

lib/Nitrapi/Services/Gameservers/Gameserver.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Nitrapi\Services\Gameservers\MariaDBs\MariaDBFactory;
1010
use Nitrapi\Services\Gameservers\MariaDBs\MariaDB;
1111
use Nitrapi\Services\Gameservers\PluginSystem\PluginSystem;
12+
use Nitrapi\Services\Gameservers\TaskManager\TaskManager;
1213
use Nitrapi\Services\Service;
1314

1415
class Gameserver extends Service
@@ -292,6 +293,15 @@ public function getDDoSHistory() {
292293
return $this->getApi()->dataGet($url);
293294
}
294295

296+
/**
297+
* Returns the task manager
298+
*
299+
* @return TaskManager
300+
*/
301+
public function getTaskManager() {
302+
return new TaskManager($this);
303+
}
304+
295305
/**
296306
* Returns the admin logs
297307
*
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
namespace Nitrapi\Services\Gameservers\TaskManager;
4+
5+
use Nitrapi\Services\Gameservers\Gameserver;
6+
use Nitrapi\Services\ServiceItem;
7+
8+
class Task extends ServiceItem
9+
{
10+
/**
11+
* @var Gameserver $service
12+
*/
13+
protected $service;
14+
/**
15+
* @var TaskManager
16+
*/
17+
protected $taskManager;
18+
19+
protected $id = null;
20+
protected $service_id;
21+
protected $minute;
22+
protected $hour;
23+
protected $day;
24+
protected $month;
25+
protected $weekday;
26+
protected $next_run;
27+
protected $last_run;
28+
protected $action_method;
29+
protected $action_data = null;
30+
31+
public function __construct() {}
32+
33+
public function setTaskManager(TaskManager &$taskManager, array &$data = []) {
34+
$this->taskManager = $taskManager;
35+
parent::__construct($taskManager->getService(), $data);
36+
$this->setService($taskManager->getService());
37+
}
38+
39+
public function getId() {
40+
return (int)$this->id;
41+
}
42+
43+
public function getMinute() {
44+
return $this->minute;
45+
}
46+
47+
public function setMinute($minute) {
48+
$this->minute = $minute;
49+
50+
return $this;
51+
}
52+
53+
public function getHour() {
54+
return $this->hour;
55+
}
56+
57+
public function setHour($hour) {
58+
$this->hour = $hour;
59+
60+
return $this;
61+
}
62+
63+
public function getDay() {
64+
return $this->day;
65+
}
66+
67+
public function setDay($day) {
68+
$this->day = $day;
69+
70+
return $this;
71+
}
72+
73+
public function getMonth() {
74+
return $this->month;
75+
}
76+
77+
public function setMonth($month) {
78+
$this->month = $month;
79+
80+
return $this;
81+
}
82+
83+
public function getWeekDay() {
84+
return $this->weekday;
85+
}
86+
87+
public function setWeekday($weekday) {
88+
$this->weekday = $weekday;
89+
90+
return $this;
91+
}
92+
93+
public function getActionMethod() {
94+
return $this->action_method;
95+
}
96+
97+
public function setActionMethod($actionMethod) {
98+
$this->action_method = $actionMethod;
99+
100+
return $this;
101+
}
102+
103+
public function getActionData() {
104+
return $this->action_data;
105+
}
106+
107+
public function setActionData($actionData) {
108+
$this->action_data = $actionData;
109+
110+
return $this;
111+
}
112+
113+
public function getNextRun() {
114+
if (empty($this->next_run))
115+
return null;
116+
117+
return (new \DateTime())->setTimestamp(strtotime($this->next_run));
118+
}
119+
120+
public function getLastRun() {
121+
if (empty($this->last_run))
122+
return null;
123+
124+
return (new \DateTime())->setTimestamp(strtotime($this->last_run));
125+
}
126+
127+
public function reloadData() {
128+
if (!empty($this->id)) {
129+
$url = "services/" . $this->getService()->getId() . "/gameservers/tasks";
130+
$_tasks = $this->getService()->getApi()->dataGet($url);
131+
132+
foreach ($_tasks['tasks'] as $task) {
133+
if ($task['id'] == $this->getId()) {
134+
$this->loadData($task);
135+
break;
136+
}
137+
}
138+
}
139+
}
140+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Nitrapi\Services\Gameservers\TaskManager;
4+
5+
use Nitrapi\Services\Gameservers\Gameserver;
6+
7+
class TaskException extends \Exception
8+
{
9+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Nitrapi\Services\Gameservers\TaskManager;
4+
5+
use Nitrapi\Services\Gameservers\Gameserver;
6+
7+
class TaskManager
8+
{
9+
protected $service;
10+
protected $tasks = [];
11+
12+
public function __construct(Gameserver &$service) {
13+
$this->service = $service;
14+
$this->reloadTasks();
15+
}
16+
17+
public function getService() {
18+
return $this->service;
19+
}
20+
21+
/**
22+
* Returns all available tasks
23+
*
24+
* @return array
25+
*/
26+
public function getTasks() {
27+
return $this->tasks;
28+
}
29+
30+
/**
31+
* Persists a task
32+
*
33+
* @param Task $task
34+
* @return Task
35+
*/
36+
public function persistTask(Task $task) {
37+
$id = $task->getId();
38+
39+
if (empty($id)) {
40+
//create new task
41+
$url = "services/" . $this->getService()->getId() . "/gameservers/tasks";
42+
} else {
43+
//save task
44+
$url = "services/" . $this->getService()->getId() . "/gameservers/tasks/" . $id;
45+
}
46+
47+
$this->getService()->getApi()->dataPost($url, [
48+
'minute' => $task->getMinute(),
49+
'hour' => $task->getHour(),
50+
'day' => $task->getDay(),
51+
'month' => $task->getMonth(),
52+
'weekday' => $task->getWeekDay(),
53+
'action_method' => $task->getActionMethod(),
54+
'action_data' => $task->getActionData(),
55+
]);
56+
57+
$task->reloadData();
58+
59+
return $task;
60+
}
61+
62+
/**
63+
* Deletes a task
64+
*
65+
* @param Task $task
66+
* @return bool
67+
*/
68+
public function deleteTask(Task $task) {
69+
$id = $task->getId();
70+
71+
if (empty($id)) {
72+
throw new TaskException("This is not a persistent task, no delete needed");
73+
}
74+
75+
$url = "services/" . $this->getService()->getId() . "/gameservers/tasks/" . $id;
76+
$this->getService()->getApi()->dataDelete($url);
77+
78+
unset($this->tasks[$id]);
79+
80+
return true;
81+
}
82+
83+
84+
public function reloadTasks() {
85+
$url = "services/" . $this->getService()->getId() . "/gameservers/tasks";
86+
$_tasks = $this->getService()->getApi()->dataGet($url);
87+
88+
$this->tasks = [];
89+
foreach ($_tasks['tasks'] as $task) {
90+
$_ = new Task();
91+
$_->setTaskManager($this, $task);
92+
$this->tasks[$task['id']] = $_;
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)