Completed
Pull Request — master (#304)
by
unknown
06:29 queued 02:43
created

TotoWindowFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 8
dl 0
loc 150
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
C updateContent() 0 72 7
A callbackOk() 0 19 2
1
<?php
2
3
namespace eXpansion\Bundle\Acme\Plugins\Gui;
4
5
use eXpansion\Bundle\Maps\Services\JukeboxService;
6
use eXpansion\Framework\Core\Helpers\Time;
7
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
8
use eXpansion\Framework\Core\Model\Gui\Widget;
9
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
10
use eXpansion\Framework\Core\Plugins\Gui\WindowFactory as BaseWindowFactory;
11
use eXpansion\Framework\Core\Storage\MapStorage;
12
use eXpansion\Framework\Gui\Builders\uiBuilder;
13
14
class TotoWindowFactory extends BaseWindowFactory
15
{
16
    /**
17
     * @var MapStorage
18
     */
19
    private $mapStorage;
20
    /**
21
     * @var Time
22
     */
23
    private $time;
24
    /**
25
     * @var JukeboxService
26
     */
27
    private $jukeboxService;
28
29
    /**
30
     * TotoWindowFactory constructor.
31
     * @param                      $name
32
     * @param                      $sizeX
33
     * @param                      $sizeY
34
     * @param null                 $posX
35
     * @param null                 $posY
36
     * @param WindowFactoryContext $context
37
     * @param MapStorage           $mapStorage
38
     * @param Time                 $time
39
     * @param JukeboxService       $jukeboxService
40
     */
41
    public function __construct(
42
        $name,
43
        $sizeX,
44
        $sizeY,
45
        $posX = null,
46
        $posY = null,
47
        WindowFactoryContext $context,
48
        MapStorage $mapStorage,
49
        Time $time,
50
        JukeboxService $jukeboxService
51
    ) {
52
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
53
        $this->mapStorage = $mapStorage;
54
        $this->time = $time;
55
        $this->jukeboxService = $jukeboxService;
56
    }
57
58
    /**
59
     * @param ManialinkInterface|Widget $manialink
60
     */
61
    protected function updateContent(ManialinkInterface $manialink)
62
    {
63
        parent::updateContent($manialink);
64
65
        $manialink->getContentFrame()->removeAllChildren();
66
67
        $builder = new uiBuilder($this->uiFactory, $this->actionFactory, $manialink, $this);
68
69
        $maps = $this->mapStorage->getMaps();
70
71
        $current = $this->mapStorage->getCurrentMap();
72
73
        $queue = $this->jukeboxService->getMapQueue();
74
75
76
        $x = 0;
77
        $inc = '<uiLayoutLine margin="1">';
78
        foreach ($maps as $map) {
79
            if ($x % 6 == 0) {
80
                if ($x != 0) {
81
                    $inc .= '</uiLayoutRow>';
82
                }
83
                $inc .= '<uiLayoutRow margin="1">';
84
            }
85
86
            if ($map->uId == $current->uId) {
87
                $color = "3af3";
88
            } else {
89
                $color = "fff3";
90
            }
91
92
93
            $gtime = $this->time->timeToText($map->goldTime);
94
            $n = $x + 1;
95
96
            $juke = "";
97
            $idx = 1;
98
            foreach ($queue as $qmap) {
99
                if ($map->uId == $qmap->getMap()->uId) {
100
                    $nick = $qmap->getPlayer()->getNickName();
101
                    $color = "0f03";
102
                    $juke = "<uiLabel pos='1 -10' width='30' textColor='fff' textSize='1'>{$idx} queue by $nick </uiLabel>";
103
                }
104
                $idx += 1;
105
            }
106
107
            $inc .= <<<EOL
108
                <frame size="40 14">          
109
                 <quad actionCallback="callbackOk" actionParam="{$map->uId}" backgroundColor="$color" size="40 14"/>         
110
                                       
111
                     <uiLabel pos="1 -1" width="30" textColor="fff" textSize="1.5">$n. {$map->name}</uiLabel>
112
                     <uiLabel pos="1 -4" width="30" textColor="fff" textSize="1">{$map->author}</uiLabel> 
113
                     <uiLabel pos="1 -7" width="30" textColor="fff" textSize="1">{$map->environnement}  / {$gtime}   </uiLabel> 
114
                     $juke
115
                   
116
                </frame>
117
EOL;
118
            $x++;
119
        }
120
121
        $inc .= '</uiLayoutRow>';
122
        $inc .= '</uiLayoutLine>';
123
124
        $manialink->getContentFrame()->addChild($builder->build(/** @lang text */
125
            <<<EOL
126
<window id="main">
127
           $inc
128
</window>
129
EOL
130
        ));
131
132
    }
133
134
    /*
135
     * <uiLayoutRow margin="2.">
136
            <uiLayoutLine margin="2">
137
                <uiButton actionCallback="callbackOk" type="decorated" >Ok</uiButton>
138
                <uiButton>Cancel</uiButton>
139
            </uiLayoutLine>
140
        </uiLayoutRow>
141
     */
142
143
    /** @var ManialinkInterface $manialink */
144
    public function callbackOk(
145
        $manialink,
146
        $login,
147
        $entries,
148
        $args
149
    ) {
150
151
        $map = $this->mapStorage->getMap($args['id']);
152
        if ($this->jukeboxService->checkMap($map)) {
153
            $this->jukeboxService->removeMap($map, $login, false);
154
155
        } else {
156
            $this->jukeboxService->addMapLast($map, $login, false);
157
158
        }
159
160
161
        $this->update($manialink->getUserGroup());
162
    }
163
}
164