Completed
Pull Request — master (#22)
by De Cramer
02:13
created

Test::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 5
cp 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
namespace eXpansion\Core\Plugins;
4
5
use eXpansion\Core\DataProviders\Listener\MatchDataListenerInterface;
6
use eXpansion\Core\DataProviders\Listener\TimerDataListenerInterface;
7
use eXpansion\Core\Helpers\Time;
8
use eXpansion\Core\Services\Console;
9
use eXpansion\Core\Storage\Data\Player;
10
use Maniaplanet\DedicatedServer\Connection;
11
use Maniaplanet\DedicatedServer\Structures\Map;
12
13
class Test implements MatchDataListenerInterface, TimerDataListenerInterface
14
{
15
    /** @var Connection */
16
    protected $connection;
17
    /** @var Console */
18
    protected $console;
19
20
    /**
21
     * @var int
22
     */
23
    private $time = 0;
0 ignored issues
show
Unused Code introduced by
The property $time is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
24
25
    /**
26
     * @var float|int
27
     */
28
    private $previousMemoryValue = 0;
29
30
    function __construct(Connection $connection, Console $console)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
31
    {
32
        $this->connection = $connection;
33
        $this->console = $console;
34
    }
35
36
    public function onBeginMatch()
37
    {
38
        $this->console->writeln('$0f0Begin Match');
39
    }
40
41
    public function onEndMatch()
42
    {
43
        $this->console->writeln('$0f0End Match');
44
    }
45
46
    public function onBeginMap(Map $map)
47
    {
48
        $this->console->writeln('$0f0Begin Map: $fff' . $map->name);
49
    }
50
51
    public function onEndMap(Map $map)
52
    {
53
        $this->console->writeln('$0f0End Map: $fff' . $map->name);
54
    }
55
56
    /**
57
     * Callback when player passes checkpoint.
58
     *
59
     * @param Player $player
60
     * @param $time
61
     * @param $lap
62
     * @param $index
63
     * @return mixed
64
     */
65
    public function onPlayerCheckpoint(Player $player, $time, $lap, $index)
66
    {
67
        $this->console->writeln('$0f0Checkpoint $ff0' . $index . ': $fff' . Time::TMtoMS($time, true) . ' $777' . $player->getNickName());
68
69
    }
70
71
    /**
72
     * Callback when player retire or finish
73
     * @param Player $player
74
     * @param $time 0 if retire, > 0 if finish
75
     * @return mixed
76
     */
77
    public function onPlayerFinish(Player $player, $time)
78
    {
79
        if ($time > 0) {
80
            $this->console->writeln('$777' . $player->getNickName() . ' $0f0Finished with time: $fff' . Time::TMtoMS($time, true));
81
        } else {
82
            $this->console->writeln('$777' . $player->getNickName() . ' $f00Retired');
83
        }
84
    }
85
86
    public function onBeginRound()
87
    {
88
        $this->console->writeln('$0f0 Begin Round');
89
    }
90
91
    public function onEndRound()
92
    {
93
        $this->console->writeln('$0f0 End Round');
94
    }
95
96
    public function onPreLoop()
97
    {
98
        // TODO: Implement onPreLoop() method.
99
    }
100
101
    public function onPostLoop()
102
    {
103
        // TODO: Implement onPostLoop() method.
104
    }
105
106
    public function onEverySecond()
107
    {
108
        $mem = memory_get_usage(true) / 1024;
109
        if ($this->previousMemoryValue != $mem) {
110
111
            $diff = ($mem - $this->previousMemoryValue);
112
            $msg = '$fff> Memory: $ff0' . $mem . "kb ";
113
114
            if ($this->previousMemoryValue < $mem) {
115
                $msg .= ' $f00+' . $diff . "kb";
116
            } else {
117
                $msg .= ' $0f0-' . $diff . "kb";
118
            }
119
            $this->console->writeln($msg);
120
121
            $this->previousMemoryValue = $mem;
122
123
        }
124
125
    }
126
}
127