Completed
Push — master ( a13e43...d36035 )
by De Cramer
15s
created

MapRatingsService::save()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 0
cts 10
cp 0
rs 9.4285
cc 3
eloc 9
nc 3
nop 0
crap 12
1
<?php
2
3
namespace eXpansion\Bundle\LocalMapRatings\Services;
4
5
use eXpansion\Bundle\LocalMapRatings\Model\Map\MapratingTableMap;
6
use eXpansion\Bundle\LocalMapRatings\Model\Maprating;
7
use eXpansion\Bundle\LocalMapRatings\Model\MapratingQueryBuilder;
8
use eXpansion\Framework\Core\Services\Application\Dispatcher;
9
use eXpansion\Framework\Core\Storage\MapStorage;
10
use eXpansion\Framework\PlayersBundle\Model\PlayerQueryBuilder;
11
use Maniaplanet\DedicatedServer\Structures\Map;
12
use Propel\Runtime\Propel;
13
14
class MapRatingsService
15
{
16
    protected $dispatcher;
17
18
    /** @var Maprating[] */
19
    protected $changedRatings;
20
21
    /** @var Maprating[] */
22
    protected $ratingsPerPlayer = [];
23
    /**
24
     * @var MapratingQueryBuilder
25
     */
26
    private $mapratingQueryBuilder;
27
    /**
28
     * @var PlayerQueryBuilder
29
     */
30
    private $playerQueryBuilder;
31
    /**
32
     * @var MapStorage
33
     */
34
    private $mapStorage;
35
36
    /**
37
     * MapRatingsService constructor.
38
     * @param MapratingQueryBuilder $mapratingQueryBuilder
39
     * @param PlayerQueryBuilder    $playerQueryBuilder
40
     * @param Dispatcher            $dispatcher
41
     * @param MapStorage            $mapStorage
42
     */
43
    public function __construct(
44
        MapratingQueryBuilder $mapratingQueryBuilder,
45
        PlayerQueryBuilder $playerQueryBuilder,
46
        Dispatcher $dispatcher,
47
        MapStorage $mapStorage
48
    ) {
49
50
        $this->mapratingQueryBuilder = $mapratingQueryBuilder;
51
        $this->playerQueryBuilder = $playerQueryBuilder;
52
        $this->dispatcher = $dispatcher;
53
        $this->mapStorage = $mapStorage;
54
    }
55
56
57
    /**
58
     * @param Map $map
59
     * @throws \Propel\Runtime\Exception\PropelException
60
     *
61
     * @return void
62
     */
63
    public function load(Map $map)
64
    {
65
        $this->changedRatings = [];
66
        $this->ratingsPerPlayer = [];
67
        MapratingTableMap::clearInstancePool();
68
69
        /** @var Maprating[] $ratings */
70
        $ratings = $this->mapratingQueryBuilder->getRatingsForMap($map);
71
72
        foreach ($ratings as $rating) {
73
            $this->ratingsPerPlayer[$rating->getLogin()] = $rating;
74
        }
75
76
        $this->dispatcher->dispatch("expansion.mapratings.loaded", [$this->ratingsPerPlayer]);
77
    }
78
79
    /**
80
     * @throws \Propel\Runtime\Exception\PropelException
81
     */
82
    public function save()
83
    {
84
        if (count($this->changedRatings) > 0) {
85
            $con = Propel::getWriteConnection(MapratingTableMap::DATABASE_NAME);
86
            $con->beginTransaction();
87
88
            foreach ($this->changedRatings as $rating) {
89
                $rating->save();
90
            }
91
            $con->commit();
92
            $this->changedRatings = [];
93
94
            MapratingTableMap::clearInstancePool();
95
        }
96
    }
97
98
    /**
99
     * @param string $login
100
     * @param int    $score
101
     */
102
    public function changeRating($login, $score)
103
    {
104
        $rating = $this->getRating($login);
105
        $rating->setScore($score);
106
107
        $this->changedRatings[$login] = $rating;
108
        $this->ratingsPerPlayer[$login] = $rating;
109
110
        $param = [
111
            "login" => $login,
112
            "score" => $score,
113
            "ratings" => $this->ratingsPerPlayer,
114
        ];
115
        $this->dispatcher->dispatch("expansion.mapratings.changed", $param);
116
    }
117
118
    /**
119
     * @param $login
120
     * @return Maprating
121
     */
122
    public function getRating($login)
123
    {
124
        if (array_key_exists($login, $this->changedRatings)) {
125
            $rating = $this->changedRatings[$login];
126
        } else {
127
            if (array_key_exists($login, $this->ratingsPerPlayer)) {
128
                $rating = $this->ratingsPerPlayer[$login];
129
            } else {
130
                $rating = new Maprating();
131
                $rating->setLogin($login);
132
                $rating->setMapuid($this->mapStorage->getCurrentMap()->uId);
133
            }
134
        }
135
136
        return $rating;
137
    }
138
139
    /**
140
     * @return Maprating[]
141
     */
142
    public function getRatingsPerPlayer(): array
143
    {
144
        return $this->ratingsPerPlayer;
145
    }
146
147
}
148