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

Test::onPlayerFinish()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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