Completed
Pull Request — master (#104)
by
unknown
04:57 queued 02:15
created

PlayerDataProvider::onPlayerDisconnect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 2
eloc 7
nc 2
nop 2
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Core\DataProviders;
4
5
use eXpansion\Framework\Core\Services\Application;
6
use eXpansion\Framework\Core\Storage\PlayerStorage;
7
use Maniaplanet\DedicatedServer\Connection;
8
use Maniaplanet\DedicatedServer\Structures\PlayerInfo;
9
10
/**
11
 * Class PlayerDataProvider provides information to plugins about what is going on with players.
12
 *
13
 * @package eXpansion\Framework\Core\DataProviders
14
 */
15
class PlayerDataProvider extends AbstractDataProvider
16
{
17
    /** @var PlayerStorage */
18
    protected $playerStorage;
19
20
    /** @var Connection */
21
    protected $connection;
22
23
    /** @var Application */
24
    protected $application;
25
26
    /**
27
     * PlayerDataProvider constructor.
28
     * @param PlayerStorage $playerStorage
29
     * @param Connection $connection
30
     * @param Application $application
31
     */
32 7
    public function __construct(PlayerStorage $playerStorage, Connection $connection, Application $application)
33
    {
34 7
        $this->playerStorage = $playerStorage;
35 7
        $this->connection = $connection;
36 7
        $this->application = $application;
37
38
        // Initialize data with existing players.
39 7
        $infos = $this->connection->getPlayerList(-1, 0);
40 7
        foreach ($infos as $info) {
41 7
            $this->onPlayerConnect($info->login, false, false);
42
        }
43 7
    }
44
45
    /**
46
     * Called when a player connects
47
     *
48
     * @param string $login
49
     * @param bool $isSpectator
50
     */
51 7
    public function onPlayerConnect($login, $isSpectator = false, $dispatch = true)
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...
52
    {
53
        try {
54 7
            $playerData = $this->playerStorage->getPlayerInfo($login);
55 1
        } catch (\Exception $e) {
56
            // TODO log that player disconnected very fast.
57 1
            return;
58
        }
59
60 6
        $this->playerStorage->onPlayerConnect($playerData);
61
62 6
        if ($dispatch) {
63 1
            $this->dispatch(__FUNCTION__, [$playerData]);
64
        }
65 6
    }
66
67
    /**
68
     * Called when a player disconnects
69
     *
70
     * @param $login
71
     * @param $disconnectionReason
72
     */
73 1
    public function onPlayerDisconnect($login, $disconnectionReason)
74
    {
75 1
        $playerData = $this->playerStorage->getPlayerInfo($login);
76
77
        // dedicated server sends disconnect for server itself when it's closed...
78
        // so it's time to stop application gracefully.
79 1
        if ($playerData->getPlayerId() == 0) {
80
            // emit event to plugins
81
            $this->application->stopApplication();
82
            return;
83
        }
84
85 1
        $this->playerStorage->onPlayerDisconnect($playerData, $disconnectionReason);
86 1
        $this->dispatch(__FUNCTION__, [$playerData, $disconnectionReason]);
87 1
    }
88
89
    /**
90
     * When user information changes (changes from spec to player...)
91
     *
92
     * @param PlayerInfo|array $playerInfo
93
     */
94 1
    public function onPlayerInfoChanged($playerInfo)
95
    {
96 1
        $playerData = $this->playerStorage->getPlayerInfo($playerInfo['Login']);
97
98 1
        $newPlayerData = clone $playerData;
99 1
        $newPlayerData->merge($playerInfo);
100
101 1
        $this->playerStorage->onPlayerInfoChanged($playerData, $newPlayerData);
102 1
        $this->dispatch(__FUNCTION__, [$playerData, $newPlayerData]);
103 1
    }
104
105
    /**
106
     * When player changes allies.
107
     *
108
     */
109 1
    public function onPlayerAlliesChanged($login)
110
    {
111 1
        $playerData = $this->playerStorage->getPlayerInfo($login);
112 1
        $newPlayerData = $this->playerStorage->getPlayerInfo($login, true);
113
114 1
        $this->playerStorage->onPlayerAlliesChanged($playerData, $newPlayerData);
115 1
        $this->dispatch(__FUNCTION__, [$playerData, $newPlayerData]);
116 1
    }
117
}
118