Completed
Pull Request — master (#148)
by De Cramer
02:37
created

PlayerDataProvider::onPlayerConnect()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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