Completed
Push — odev ( b6090c...84a1fd )
by De Cramer
04:24 queued 01:56
created

PlayerDataProvider::onPlayerDisconnect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 8
cp 0.75
cc 2
eloc 7
nc 2
nop 2
crap 2.0625
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
29
     */
30 7
    public function __construct(PlayerStorage $playerStorage, Connection $connection, Application $application)
31
    {
32 7
        $this->playerStorage = $playerStorage;
33 7
        $this->connection = $connection;
34 7
        $this->application = $application;
35 7
    }
36
37
    /**
38
     * Called when eXpansion is started.
39
     */
40 1
    public function onRun()
41
    {
42 1
        $infos = $this->connection->getPlayerList(-1, 0);
43 1
        foreach ($infos as $info) {
44 1
            $this->onPlayerConnect($info->login);
45 1
        }
46 1
    }
47
48
    /**
49
     * Called when a player connects
50
     *
51
     * @param string $login
52
     * @param bool $isSpectator
53
     */
54 3
    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...
55
    {
56
        try {
57 3
            $playerData = $this->playerStorage->getPlayerInfo($login);
58 3
        } catch (\Exception $e) {
59
            // TODO log that player disconnected very fast.
60 1
            return;
61
        }
62
63 2
        $this->playerStorage->onPlayerConnect($playerData);
64 2
        $this->dispatch(__FUNCTION__, [$playerData]);
65 2
    }
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
            $this->application->stopApplication();
81
            return;
82
        }
83
84 1
        $this->playerStorage->onPlayerDisconnect($playerData, $disconnectionReason);
85 1
        $this->dispatch(__FUNCTION__, [$playerData, $disconnectionReason]);
86 1
    }
87
88
    /**
89
     * When user information changes (changes from spec to player...)
90
     *
91
     * @param PlayerInfo $playerInfo
92
     */
93 1
    public function onPlayerInfoChanged($playerInfo)
94
    {
95 1
        $playerData = $this->playerStorage->getPlayerInfo($playerInfo['Login']);
96
97 1
        $newPlayerData = clone $playerData;
98 1
        $newPlayerData->merge($playerInfo);
99
100 1
        $this->playerStorage->onPlayerInfoChanged($playerData, $newPlayerData);
101 1
        $this->dispatch(__FUNCTION__, [$playerData, $newPlayerData]);
102 1
    }
103
104
    /**
105
     * When player changes allies.
106
     *
107
     */
108 1
    public function onPlayerAlliesChanged($login)
109
    {
110 1
        $playerData = $this->playerStorage->getPlayerInfo($login);
111 1
        $newPlayerData = $this->playerStorage->getPlayerInfo($login, true);
112
113 1
        $this->playerStorage->onPlayerAlliesChanged($playerData, $newPlayerData);
114 1
        $this->dispatch(__FUNCTION__, [$playerData, $newPlayerData]);
115 1
    }
116
}
117