Passed
Push — main ( 7b37bf...2735bf )
by N.
03:10
created

Graf::rendera_linjer()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 38
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 8
nop 5
dl 0
loc 38
ccs 0
cts 11
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Klass Graf.
5
 * @author Niklas Dougherty
6
 */
7
8
declare(strict_types=1);
9
10
namespace Tips\Moduler\DistributionGenerera;
11
12
/**
13
 * Klass Graf.
14
 * Rita graf över distribution.
15
 * 25 % av alla utfall finns inom 1 % distribution.
16
 * 50 % inom 5 %.
17
 * 65 % inom 10 %.
18
 * Jättevinsterna faller utanför detta intervall.
19
 */
20
class Graf extends Rendera {
21
	/**
22
	 * @var int[] $distribution
23
	 */
24
	protected array $distribution = [];
25
26
	/**
27
	 * Uppdatera graf.
28
	 */
29
	protected function uppdatera_graf(): void {
30
		if (count($this->distribution) === 0) {
31
			return;
32
		}
33
34
		/**
35
		 * Oddssummor.
36
		 */
37
		[$oddssumma_min, $oddssumma_max, $oddssumma_utfall] = $this->oddssummor();
38
		$distmax_antal_rader = max($this->distribution);
39
40
		/**
41
		 * Deltan i korordinater.
42
		 */
43
		[$dx, $dy] = [
44
			$this->dist->graf->bredd / ($oddssumma_max - $oddssumma_min),
45
			$this->dist->graf->höjd / $distmax_antal_rader
46
		];
47
48
		/**
49
		 * Nollställ parametrar.
50
		 */
51
		$delsumma = 0.0;
52
		[$this->dist->maxsumma, $this->dist->minsumma, $this->dist->andelssumma] = [0, 0, 0];
53
54
		/**
55
		 * Iterera över koordinater.
56
		 */
57
		foreach ($this->distribution as $xkoord => $ykoord) {
58
			/**
59
			 * Koordinater.
60
			 */
61
			[$x, $y] = [
62
				intval(($xkoord - $oddssumma_min) * $dx),
63
				intval($this->dist->graf->höjd - $ykoord * $dy)
64
			];
65
66
			/**
67
			 * Plussa andelssumma.
68
			 */
69
			if ($xkoord > $oddssumma_utfall) {
70
				$this->dist->andelssumma += $ykoord;
71
			}
72
73
			/**
74
			 * Gränslinjer.
75
			 */
76
			$delsumma += $ykoord;
77
			$andel = 100 * $delsumma / MATCHRYMD;
78
79
			/**
80
			 * Rendera linjer.
81
			 */
82
			$this->rendera_linjer($andel, $x, $y, $xkoord, $oddssumma_utfall);
83
		}
84
85
		/**
86
		 * Uppdatera procentandel för distribution, relativt totala matchrymden 3^13.
87
		 */
88
		$this->dist->procentandel = round(100 * $this->dist->andelssumma / MATCHRYMD, 3);
89
90
		/**
91
		 * Rendera text.
92
		 */
93
		$this->rendera_text($distmax_antal_rader, $oddssumma_min, $oddssumma_max, $oddssumma_utfall);
94
95
		/**
96
		 * Spara graf.
97
		 */
98
		$this->spara_graf();
99
	}
100
101
	/**
102
	 * Rendera gränslinjer i dist.
103
	 */
104
	private function rendera_linjer(float $andel, int $x, int $y, string $xkoord, float $oddssumma_utfall): void {
105
		/**
106
		 * Rita procentsatser med linjer i gröna nyanser.
107
		 * Intensitet ökar med avstånd från origo.
108
		 * Använder 0.5 %, 1 %, 2%, 3 %, 5 % och 10 %.
109
		 * En procentenhet motsvarar 15943 rader.
110
		 */
111
		$this->percentage_lines($andel, $x, $xkoord);
112
113
		/**
114
		 * Rita aktuellt utfall med röd linje.
115
		 * Motsvarar den momentana sannolikhetssumman för odds för omgången.
116
		 */
117
		if ($oddssumma_utfall == $xkoord) {
118
			$this->dist->graf->sätt_linje($x, 0, $x, $this->dist->graf->höjd, $this->dist->graf->röd);
119
		}
120
121
		/**
122
		 * Rita maxprocent med vit linje.
123
		 */
124
		if ($this->dist->maxsumma == 0 && $andel > $this->dist->minprocent) {
125
			$this->dist->maxsumma = (float) $xkoord;
126
			$this->dist->graf->sätt_linje($x, 0, $x, $this->dist->graf->höjd - 50, $this->dist->graf->vit);
127
		}
128
129
		/**
130
		 * Rita minprocent med vit linje.
131
		 * Reducera höjd på linje för att ge plats åt eventuell undre linje.
132
		 */
133
		if ($this->dist->minsumma == 0 && $andel > $this->dist->maxprocent) {
134
			$this->dist->minsumma = (float) $xkoord;
135
			$this->dist->graf->sätt_linje($x, 0, $x, $this->dist->graf->höjd - 50, $this->dist->graf->vit);
136
		}
137
138
		/**
139
		 * Rita kurva med blå färg.
140
		 */
141
		$this->dist->graf->sätt_pixel($x, $y, $this->dist->graf->blå);
142
	}
143
144
	/**
145
	 * Rita ut textinformation i graf.
146
	 */
147
	private function rendera_text(
148
		int $distmax_antal_rader,
149
		float $oddssumma_min,
150
		float $oddssumma_max,
151
		float $oddssumma_utfall
152
	): void {
153
		/**
154
		 * Rendera omgångsdata med gul text.
155
		 */
156
		$this->dist->graf->sätt_text(20, 20, "{$this->tips->odds->spel->speltyp->produktnamn()} {$this->tips->odds->spel->omgång}: utdelning {$this->tips->utdelning->utdelning[0]}", $this->dist->graf->gul);
157
158
		/**
159
		 * Rendera distributionsdata med gul text.
160
		 */
161
		$distmax = array_search($distmax_antal_rader, $this->distribution, true);
162
		$this->dist->graf->sätt_text(20, 40, "x_min: $oddssumma_min, mitt: $distmax ($distmax_antal_rader), x_max: $oddssumma_max", $this->dist->graf->gul);
163
164
		/**
165
		 * Rendera aktuellt utfall med röd text.
166
		 */
167
		if ($oddssumma_utfall > 0) {
168
			$this->dist->graf->sätt_text(20, 60, "utfall: $oddssumma_utfall ({$this->dist->andelssumma}) | andel: {$this->dist->procentandel} %", $this->dist->graf->röd);
169
		}
170
	}
171
172
	/**
173
	 * Spara distributionsgraf.
174
	 */
175
	private function spara_graf(): void {
176
		/**
177
		 * Spara och beräkna sannolikhetssummor.
178
		 * Logga händelse.
179
		 */
180
		$this->dist->graf->spara_tipsgraf($this->dist->bildfil);
181
		$this->dist->beräkna_sannolikhetssummor($this->tips->utdelning->tipsrad_012);
182
		$this->spara_distribution();
183
		$this->tips->odds->spel->db->logg->logga(self::class . ' ✅ Uppdaterade graf, sparade distribution.');
184
	}
185
}
186