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

Test::onPlayerCheckpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

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