Completed
Push — master ( b3bf60...ff8f47 )
by De Cramer
9s
created

PlayerDataProvider::onPlayerInfoChanged()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace eXpansion\Core\DataProviders;
4
5
use eXpansion\Core\Storage\PlayerStorage;
6
use Maniaplanet\DedicatedServer\Connection;
7
use Maniaplanet\DedicatedServer\Structures\PlayerInfo;
8
9
/**
10
 * Class PlayerDataProvider provides information to plugins about what is going on with players.
11
 *
12
 * @package eXpansion\Core\DataProviders
13
 */
14
class PlayerDataProvider extends AbstractDataProvider
15
{
16
    /**
17
     * @var PlayerStorage
18
     */
19
    protected $playerStorage;
20
21
    /**
22
     * @var Connection
23
     */
24
    protected $connection;
25
26
    /**
27
     * PlayerDataProvider constructor.
28
     * @param $playerStorage
29
     */
30
    public function __construct(PlayerStorage $playerStorage, Connection $connection)
31
    {
32
        $this->playerStorage = $playerStorage;
33
        $this->connection = $connection;
34
    }
35
36
    /**
37
     * Called when eXpansion is started.
38
     */
39
    public function onRun()
40
    {
41
        $infos = $this->connection->getPlayerList(-1, 0);
42
        foreach ($infos as $info) {
43
            $this->onPlayerConnect($info->login);
44
        }
45
    }
46
47
    /**
48
     * Called when a player connects
49
     *
50
     * @param string $login
51
     * @param bool $isSpectator
52
     */
53
    public function onPlayerConnect($login, $isSpectator = false)
0 ignored issues
show
Unused Code introduced by
The parameter $isSpectator 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...
54
    {
55
        try {
56
            $playerData = $this->playerStorage->getPlayerInfo($login);
57
            $this->dispatch(__FUNCTION__, [$playerData]);
58
        } catch (\Exception $e) {
59
            // TODO log that player disconnected very fast.
60
        }
61
    }
62
63
    /**
64
     * Called when a player disconnects
65
     *
66
     * @param $login
67
     * @param $disconnectionReason
68
     */
69
    public function onPlayerDisconnect($login, $disconnectionReason)
70
    {
71
        $playerData = $this->playerStorage->getPlayerInfo($login);
72
        $this->dispatch(__FUNCTION__, [$playerData, $disconnectionReason]);
73
    }
74
75
    /**
76
     * When user information changes (changes from spec to player...)
77
     *
78
     * @param PlayerInfo $playerInfo
79
     */
80
    public function onPlayerInfoChanged($playerInfo)
81
    {
82
        $playerData = $this->playerStorage->getPlayerInfo($playerInfo['Login']);
83
84
        $newPlayerData = clone $playerData;
85
        $newPlayerData->merge($playerInfo);
86
87
        $this->dispatch(__FUNCTION__, [$playerData, $newPlayerData]);
88
    }
89
90
    /**
91
     * When player changes allies.
92
     *
93
     * @param PlayerInfo $playerInfo
0 ignored issues
show
Bug introduced by
There is no parameter named $playerInfo. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
94
     */
95
    public function onPlayerAlliesChanged($login)
96
    {
97
        $newPlayerData = $this->playerStorage->getPlayerInfo($login, true);
98
        $playerData = $this->playerStorage->getPlayerInfo($login);
99
        $this->dispatch(__FUNCTION__, [$playerData, $newPlayerData]);
100
    }
101
}