Passed
Push — master ( 5f0e7b...abd304 )
by Pedro
02:30
created

ImageGenerator::getQuestMap()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 25
rs 9.7666
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
namespace Application\Service;
4
5
use Intervention\Image\ImageManager;
6
use Intervention\Image\Image;
7
8
class ImageGenerator
9
{
10
    const ONLINE_COLOR = '#0080e1';
11
    const OFFLINE_COLOR = '#d30000';
12
    const ITEM_COLOR = '#ff8000';
13
    const UNIQUE_ITEM_COLOR = '#ffc000';
14
    const CROSSHAIR_SIZE = 5;
15
16
    private $config;
17
    private $cache;
0 ignored issues
show
introduced by
The private property $cache is not used, and could be removed.
Loading history...
18
    private $parser;
19
    private $imageManager;
20
21
    public function __construct(array $config, BotParser $parser, ImageManager $imageManager)
22
    {
23
        $this->config = $config;
24
        $this->parser = $parser;
25
        $this->imageManager = $imageManager;
26
    }
27
28
    /**
29
     * Draws a crosshair on $image, using coordinates $x and $y and color $color
30
     * Also writes text next to the crosshair if $text isn't null
31
     * @param Image $image
32
     * @param int $x
33
     * @param int $y
34
     * @param string $color
35
     * @param string|null $text
36
     * @return Image
37
     */
38
    private function drawCrosshair(Image $image, int $x, int $y, string $color, string $text = null)
39
    {
40
        // Bottom top
41
        $image->line($x - self::CROSSHAIR_SIZE, $y, $x + self::CROSSHAIR_SIZE, $y, function ($draw) use ($color) {
42
            $draw->color($color);
43
        });
44
45
        // Left right
46
        $image->line($x, $y - self::CROSSHAIR_SIZE, $x, $y + self::CROSSHAIR_SIZE, function ($draw) use ($color) {
47
            $draw->color($color);
48
        });
49
50
        if ($text) {
51
            $text_x = $x + self::CROSSHAIR_SIZE + 4;
52
            $text_y = $y + self::CROSSHAIR_SIZE;
53
54
            // Draw a "shadow" 1 pixel ahead
55
            $image->text($text, $text_x + 1, $text_y + 1, function ($font) {
56
                $font->file(4);
57
                $font->color("#000");
58
            });
59
60
            // Text
61
            $image->text($text, $text_x, $text_y, function ($font) use ($color) {
62
                $font->file(4);
63
                $font->color($color);
64
            });
65
        }
66
67
        return $image;
68
    }
69
70
    /**
71
     * Returns an array with the dimensions of the map image
72
     * @return array|mixed
73
     */
74
    public function getMapDimensions()
75
    {
76
        $image = $this->imageManager->make($this->config['map_image']);
77
78
        $dimensions = [
79
            'height' => $image->height(),
80
            'width' => $image->width(),
81
        ];
82
83
        return $dimensions;
84
    }
85
86
    /**
87
     * Returns a generated image with the Player position and name
88
     * @param string $nick
89
     * @return \Intervention\Image\Image|mixed
90
     */
91
    public function getPlayerMap(string $nick)
92
    {
93
        $player_info = $this->parser->getDatabase($nick);
94
95
        $image = $this->imageManager->make($this->config['map_image']);
96
97
        if ($player_info != 0) {
98
            $image = $this->drawCrosshair(
99
                $image,
100
                $player_info['x_pos'],
101
                $player_info['y_pos'],
102
                ($player_info['online'] == 'Yes' ? self::ONLINE_COLOR : self::OFFLINE_COLOR),
103
                $nick
104
            );
105
        }
106
107
        return $image->encode('png');
108
    }
109
110
    /**
111
     * Returns a generated image with all the Players positions
112
     * @return \Intervention\Image\Image|mixed
113
     */
114
    public function getWorldMap()
115
    {
116
        $database = $this->parser->getDatabase();
117
        $items = $this->parser->getItems();
118
119
        $image = $this->imageManager->make($this->config['map_image']);
120
121
        foreach ($database as $player) {
122
            $image = $this->drawCrosshair(
123
                $image,
124
                $player['x_pos'],
125
                $player['y_pos'],
126
                ($player['online'] == 'Yes' ? self::ONLINE_COLOR : self::OFFLINE_COLOR)
127
            );
128
        }
129
130
        foreach ($items as $item) {
131
            $image = $this->drawCrosshair(
132
                $image,
133
                $item['x_pos'],
134
                $item['y_pos'],
135
                (is_numeric($item['level']) ? self::ITEM_COLOR : self::UNIQUE_ITEM_COLOR)
136
            );
137
        }
138
139
        return $image->encode('png');
140
    }
141
142
    /**
143
     * Returns a generated image with all the current Players on a quest
144
     * @return \Intervention\Image\Image|mixed
145
     */
146
    public function getQuestMap()
147
    {
148
        $quest = $this->parser->getQuestData();
149
150
        $image = $this->imageManager->make($this->config['map_image']);
151
152
        foreach ($quest['players'] as $player) {
153
            $image = $this->drawCrosshair(
154
                $image,
155
                $player['x_pos'],
156
                $player['y_pos'],
157
                self::ONLINE_COLOR
158
            );
159
        }
160
161
        if ($quest['type'] == 2) {
162
            $image = $this->drawCrosshair(
163
                $image,
164
                $quest['stages'][$quest['objective']]['x_pos'],
165
                $quest['stages'][$quest['objective']]['y_pos'],
166
                self::OFFLINE_COLOR
167
            );
168
        }
169
170
        return $image->encode('png');
171
    }
172
}
173