Skip to content

Commit 5bcca60

Browse files
authored
Merge pull request #22 from JABirchall/Timers
Timers Implitation
2 parents 0d6a800 + 0ad5512 commit 5bcca60

File tree

114 files changed

+3624
-1380
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+3624
-1380
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ To install this bot all you need to do is download or clone the repository to yo
99
First run the command to setup the autoloader
1010

1111
```
12-
php composer.phar dump-autoload
12+
php composer.phar dump-autoload -o
1313
```
1414

1515
Edit the Teamspeak and database configs in side the config folder then launch the bot with

app/Plugin.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,6 @@ public function __construct($config, $teamSpeak3Bot)
9999
$this->version = $value;
100100
break;
101101

102-
case "level":
103-
$this->level = $value;
104-
break;
105-
106102
case "triggers":
107103
$this->triggers = $this->sortByLengthDESC($value);
108104
//$this->originalTriggers = $this->triggers;

app/TeamSpeak3Bot.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class TeamSpeak3Bot
7474
*/
7575
private $plugins;
7676

77+
private $timers;
78+
7779
public static $config;
7880

7981
private $timer;
@@ -137,7 +139,7 @@ public function __construct($username, $password, $host = "127.0.0.1", $port = 1
137139
$this->carbon = new Carbon;
138140

139141
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' && posix_getuid() === 0 && !Self::$config['ignoreWarnings']) {
140-
$this->printOutput("[WARNING] Running Nimda as root is bad!");
142+
$this->printOutput("[WARNING] Running Nimda as root is dangerous!");
141143
$this->printOutput("Start anyway? Y/N:", false);
142144
$response = rtrim(fgets(STDIN));
143145
if (strcasecmp($response,'y')) {
@@ -147,7 +149,7 @@ public function __construct($username, $password, $host = "127.0.0.1", $port = 1
147149
}
148150

149151
if($username === "serveradmin" && !Self::$config['ignoreWarnings']) {
150-
$this->printOutput("[WARNING] Running Nimda logged in as serveradmin is bad!");
152+
$this->printOutput("[WARNING] Running Nimda logged in as serveradmin is dangerous!");
151153
$this->printOutput("Start anyway? Y/N:", false);
152154
$response = rtrim(fgets(STDIN));
153155
if (strcasecmp($response,'y') ) {
@@ -186,6 +188,7 @@ public function run()
186188
$this->database = new Database;
187189
$this->setup();
188190
$this->initializePlugins();
191+
$this->initializeTimers();
189192
$this->register();
190193
$this->timer->stop();
191194

@@ -320,6 +323,13 @@ private function initializePlugins()
320323
}
321324
}
322325

326+
private function initializeTimers()
327+
{
328+
foreach (glob('./config/timers/*.json') as $file) {
329+
$this->loadTimer($file);
330+
}
331+
}
332+
323333
/**
324334
* @param $configFile
325335
*
@@ -388,6 +398,41 @@ private function loadPlugin($configFile)
388398
return true;
389399
}
390400

401+
private function loadTimer($configFile)
402+
{
403+
$config = $this->parseConfigFile($configFile);
404+
$config['configFile'] = $configFile;
405+
406+
if (!$config) {
407+
$this->printOutput("Timer with config file {$configFile} has not been loaded because it doesn't exist.");
408+
return false;
409+
} elseif (!isset($config['name'])) {
410+
$this->printOutput("Timer with config file {$configFile} has not been loaded because it has no name.");
411+
412+
return false;
413+
}
414+
415+
$this->printOutput(sprintf("%- 80s %s", "Loading Timer [{$config['name']}] by {$config['author']} ", "::"), false, false);
416+
417+
$config['class'] = \Timer::class . '\\' . $config['name'];
418+
419+
if (!class_exists($config['class'])) {
420+
$this->printOutput("Loading failed because class {$config['class']} doesn't exist.");
421+
422+
return false;
423+
} elseif (!is_a($config['class'], \Timer\TimerContract::class, true)) {
424+
$this->printOutput("Loading failed because class {$config['class']} does not implement [TimerContract].");
425+
426+
return false;
427+
}
428+
429+
$this->timers[$config['name']] = new $config['class']($config, $this);
430+
431+
$this->printOutput("Success.");
432+
433+
return true;
434+
}
435+
391436
/**
392437
* @param $file
393438
*
@@ -510,6 +555,9 @@ public function onTimeout($time, AbstractAdapter $adapter)
510555
$adapter->request("clientupdate");
511556
$this->updateList();
512557
}
558+
foreach($this->timers as $timer){
559+
$timer->handle();
560+
}
513561

514562
if(Self::$config['debug'] === true) {
515563
$this->printOutput("Nimda runtime: " . Convert::seconds($this->timer->getRuntime()) . ", Using " . Convert::bytes($this->timer->getMemUsage()) . " memory.");

app/Timer.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Jake
5+
* Date: 14/02/2017
6+
* Time: 14:21
7+
*/
8+
9+
namespace App;
10+
11+
use \Carbon\Carbon;
12+
13+
14+
class Timer
15+
{
16+
protected $time;
17+
protected $nextRunTime;
18+
public $teamSpeak3Bot;
19+
20+
public function __construct($config, $teamSpeak3Bot)
21+
{
22+
$this->time = Carbon::now();
23+
$this->nextRunTime = Carbon::now();
24+
$this->teamSpeak3Bot = $teamSpeak3Bot;
25+
foreach ($config as $name => $value) {
26+
switch ($name) {
27+
case "name":
28+
$this->name = $value;
29+
break;
30+
31+
case "author":
32+
$this->author = $value;
33+
break;
34+
35+
case "description":
36+
$this->description = $value;
37+
break;
38+
39+
case "version":
40+
$this->version = $value;
41+
break;
42+
43+
case "interval":
44+
$this->interval = $value;
45+
$this->setNextRunTime();
46+
break;
47+
48+
case "lastRunTime":
49+
$this->lastRunTime = $this->time->timestamp;
50+
break;
51+
52+
case "configFile":
53+
$this->configFile = $value;
54+
break;
55+
56+
default:
57+
$this->CONFIG[$name] = $value;
58+
break;
59+
}
60+
}
61+
}
62+
63+
public function handle()
64+
{
65+
if($this->nextRunTime->timestamp <= $this->time->now()->timestamp) {
66+
$this->isTriggered();
67+
$this->setNextRunTime();
68+
}
69+
}
70+
71+
public function setNextRunTime()
72+
{
73+
$this->nextRunTime->addHours($this->interval['hours'])
74+
->addMinutes($this->interval['minutes'])
75+
->addSeconds($this->interval['seconds']);
76+
}
77+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"psr-4": {
1212
"App\\": "app/",
1313
"Plugin\\": "plugins/",
14-
"Config\\": "config/"
14+
"Config\\": "config/",
15+
"Timer\\": "timers/"
1516
}
1617
},
1718
"authors": [

composer.phar

108 KB
Binary file not shown.

config/TeamSpeak.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class TeamSpeak
1212

1313
public static $TS3config = [
1414
'username' => 'serveradmin',
15-
'password' => 'password',
15+
'password' => 'QPOcI2i3',
1616
'host' => '127.0.0.1',
1717
'port' => 10011,
1818
'name' => 'Nimda',

config/plugins/BadClass.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

config/plugins/NoInterface.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

config/timers/announcement.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "Announcement",
3+
"author": "JABirchall",
4+
"description": "Sends a server announcement after X time has past",
5+
"version": "1.0",
6+
"interval": {
7+
"hours": 0,
8+
"minutes": 1,
9+
"seconds": 5
10+
},
11+
12+
"message": "Test"
13+
}

database.sqlite

20 KB
Binary file not shown.

plugins/NoInterface.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

timers/Announcement.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Jake
5+
* Date: 14/02/2017
6+
* Time: 15:24
7+
*/
8+
9+
namespace Timer;
10+
11+
use App\Timer;
12+
13+
class Announcement extends Timer implements TimerContract
14+
{
15+
public function isTriggered()
16+
{
17+
$this->teamSpeak3Bot->node->message('Current Server time is '.$this->time->now()->toTimeString());
18+
}
19+
}

timers/TimerContract.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Jake
5+
* Date: 14/02/2017
6+
* Time: 15:07
7+
*/
8+
9+
namespace Timer;
10+
11+
12+
interface TimerContract
13+
{
14+
public function isTriggered();
15+
}

vendor/composer/ClassLoader.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class ClassLoader
5555
private $classMap = array();
5656
private $classMapAuthoritative = false;
5757
private $missingClasses = array();
58+
private $apcuPrefix;
5859

5960
public function getPrefixes()
6061
{
@@ -271,6 +272,26 @@ public function isClassMapAuthoritative()
271272
return $this->classMapAuthoritative;
272273
}
273274

275+
/**
276+
* APCu prefix to use to cache found/not-found classes, if the extension is enabled.
277+
*
278+
* @param string|null $apcuPrefix
279+
*/
280+
public function setApcuPrefix($apcuPrefix)
281+
{
282+
$this->apcuPrefix = function_exists('apcu_fetch') && ini_get('apc.enabled') ? $apcuPrefix : null;
283+
}
284+
285+
/**
286+
* The APCu prefix in use, or null if APCu caching is not enabled.
287+
*
288+
* @return string|null
289+
*/
290+
public function getApcuPrefix()
291+
{
292+
return $this->apcuPrefix;
293+
}
294+
274295
/**
275296
* Registers this instance as an autoloader.
276297
*
@@ -313,18 +334,19 @@ public function loadClass($class)
313334
*/
314335
public function findFile($class)
315336
{
316-
// work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
317-
if ('\\' == $class[0]) {
318-
$class = substr($class, 1);
319-
}
320-
321337
// class map lookup
322338
if (isset($this->classMap[$class])) {
323339
return $this->classMap[$class];
324340
}
325341
if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
326342
return false;
327343
}
344+
if (null !== $this->apcuPrefix) {
345+
$file = apcu_fetch($this->apcuPrefix.$class, $hit);
346+
if ($hit) {
347+
return $file;
348+
}
349+
}
328350

329351
$file = $this->findFileWithExtension($class, '.php');
330352

@@ -333,6 +355,10 @@ public function findFile($class)
333355
$file = $this->findFileWithExtension($class, '.hh');
334356
}
335357

358+
if (null !== $this->apcuPrefix) {
359+
apcu_add($this->apcuPrefix.$class, $file);
360+
}
361+
336362
if (false === $file) {
337363
// Remember that this class does not exist.
338364
$this->missingClasses[$class] = true;

0 commit comments

Comments
 (0)