Completed
Push — master ( 206125...47c10a )
by De Cramer
13s
created

Test::onApplicationReady()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace eXpansion\Bundle\Acme\Plugins;
4
5
use eXpansion\Bundle\Acme\Plugins\Gui\MemoryWidgetFactory;
6
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpApplication;
7
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpTimer;
8
use eXpansion\Framework\Core\Helpers\Time;
9
use eXpansion\Framework\Core\Model\UserGroups\Group;
10
use eXpansion\Framework\Core\Plugins\StatusAwarePluginInterface;
11
use eXpansion\Framework\Core\Services\Console;
12
use eXpansion\Framework\Core\Storage\PlayerStorage;
13
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpLegacyMap;
14
use Maniaplanet\DedicatedServer\Connection;
15
use Maniaplanet\DedicatedServer\Structures\Map;
16
17
class Test implements ListenerInterfaceExpApplication, ListenerInterfaceMpLegacyMap, ListenerInterfaceExpTimer, StatusAwarePluginInterface
18
{
19
20
    /** @var  string */
21
    static public $memoryMsg;
22
23
    /** @var Connection */
24
    protected $connection;
25
26
    /** @var Console */
27
    protected $console;
28
29
    /** @var Time */
30
    protected $time;
31
32
    /** @var float|int */
33
    private $previousMemoryValue = 0;
34
35
    /** @var int */
36
    private $startMemValue = 0;
37
38
    /**
39
     * @var MemoryWidgetFactory
40
     */
41
    private $mlFactory;
42
    /**
43
     * @var PlayerStorage
44
     */
45
    private $players;
46
    /**
47
     * @var Group
48
     */
49
    private $playergroup;
50
51
    /**
52
     * Test constructor.
53
     * @param PlayerStorage $players
54
     * @param Connection $connection
55
     * @param Console $console
56
     * @param Time $time
57
     * @param Group $playergroup
58
     * @param MemoryWidgetFactory $mlFactory
59
     */
60
    function __construct(
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...
61
        PlayerStorage $players,
62
        Connection $connection,
63
        Console $console,
64
        Time $time,
65
        Group $playergroup,
66
        MemoryWidgetFactory $mlFactory
67
    ) {
68
        $this->connection = $connection;
69
        $this->console = $console;
70
        $this->time = $time;
71
        $this->mlFactory = $mlFactory;
72
        $this->players = $players;
73
        $this->playergroup = $playergroup;
74
    }
75
76
    public function onBeginMap(Map $map)
77
    {
78
        $this->console->writeln('$0f0Begin Map: $fff'.$map->name);
79
    }
80
81
    public function onEndMap(Map $map)
82
    {
83
        $this->console->writeln('$0f0End Map: $fff'.$map->name);
84
    }
85
86
    public function onPreLoop()
87
    {
88
        // do nothing
89
    }
90
91
    public function onPostLoop()
92
    {
93
        // do nothing
94
    }
95
96
    public function onEverySecond()
97
    {
98
        $mem = memory_get_usage(false);
99
100
        if ($this->previousMemoryValue != $mem) {
101
            $diff = ($mem - $this->previousMemoryValue);
102
            $msg = 'Memory: $0d0'.round($mem / 1024)."kb ";
103
104
            if ($this->previousMemoryValue < $mem) {
105
                $msg .= ' $f00+'.round($diff / 1024)."kb";
106
            } else {
107
                $msg .= ' $0f0'.round($diff / 1024)."kb";
108
            }
109
110
            $diff = ($mem - $this->startMemValue);
111
            if ($this->startMemValue > $mem) {
112
                $msg .= ' $0f0('.round($diff / 1024)."kb)";
113
            } else {
114
                $msg .= ' $f00('.round($diff / 1024)."kb)";
115
            }
116
117
            self::$memoryMsg = $msg;
118
            $this->mlFactory->update($this->playergroup);
119
            $this->console->writeln($msg);
120
        }
121
122
        $this->previousMemoryValue = $mem;
123
124
    }
125
126
    /**
127
     * @param Map[] $oldMaps
128
     * @param string $currentMapUid
129
     * @param string $nextMapUid
130
     * @param bool $isListModified
131
     * @return mixed
132
     */
133
    public function onMapListModified($oldMaps, $currentMapUid, $nextMapUid, $isListModified)
0 ignored issues
show
Unused Code introduced by
The parameter $oldMaps is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $currentMapUid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $nextMapUid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $isListModified is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
134
    {
135
136
    }
137
138
    /**
139
     * @param $currentMap
140
     * @param $previousMap
141
     */
142
    public function onExpansionMapChange($currentMap, $previousMap)
0 ignored issues
show
Unused Code introduced by
The parameter $currentMap is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $previousMap is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
143
    {
144
145
    }
146
147
    /**
148
     * @param $nextMap
149
     * @param $previousNextMap
150
     */
151
    public function onExpansionNextMapChange($nextMap, $previousNextMap)
0 ignored issues
show
Unused Code introduced by
The parameter $nextMap is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $previousNextMap is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
152
    {
153
154
    }
155
156
    /**
157
     * Set the status of the plugin
158
     *
159
     * @param boolean $status
160
     *
161
     * @return null
162
     */
163
    public function setStatus($status)
164
    {
165
166
    }
167
168
    /**
169
     * called at eXpansion init
170
     *
171
     * @return void
172
     */
173
    public function onApplicationInit()
174
    {
175
        // TODO: Implement onApplicationInit() method.
176
    }
177
178
    /**
179
     * called when init is done and callbacks are enabled
180
     *
181
     * @return void
182
     */
183
    public function onApplicationReady()
184
    {
185
        $this->startMemValue = memory_get_usage(false);
186
        $this->mlFactory->create($this->playergroup);
187
    }
188
189
    /**
190
     * called when requesting application stop
191
     *
192
     * @return void
193
     */
194
    public function onApplicationStop()
195
    {
196
        // TODO: Implement onApplicationStop() method.
197
    }
198
}
199