Completed
Pull Request — master (#300)
by De Cramer
04:22
created

PlayerDataProvider::onPlayerInfoChanged()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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