Passed
Push — main ( d29be2...38376b )
by N.
03:16
created

Graf::rendera_text()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 4
dl 0
loc 22
ccs 0
cts 6
cp 0
crap 6
rs 10
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
			[$x, $y] = [
59
				intval(($xkoord - $oddssumma_min) * $dx),
60
				intval($this->dist->graf->höjd - $ykoord * $dy)
61
			];
62
63
			if ($xkoord > $oddssumma_utfall) {
64
				$this->dist->andelssumma += $ykoord;
65
			}
66
67
			$delsumma += $ykoord;
68
			$andel = 100 * $delsumma / MATCHRYMD;
0 ignored issues
show
Bug introduced by
The constant Tips\Moduler\DistributionGenerera\MATCHRYMD was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
69
70
			/**
71
			 * Rita procentsatser med linjer i gröna nyanser.
72
			 * Använder 0.5 %, 1 %, 2%, 3 %, 5 % och 10 %.
73
			 * En procentenhet motsvarar 15943 rader.
74
			 */
75
			$this->percentage_lines($andel, $x, $xkoord);
76
77
			/**
78
			 * Rita aktuellt utfall med röd linje.
79
			 */
80
			if ($oddssumma_utfall == $xkoord) {
81
				$this->dist->graf->sätt_linje($x, 0, $x, $this->dist->graf->höjd, $this->dist->graf->röd);
82
			}
83
84
			/**
85
			 * Rita maxprocent med vit linje.
86
			 */
87
			if ($this->dist->maxsumma == 0 && $andel > $this->dist->minprocent) {
88
				$this->dist->maxsumma = (float) $xkoord;
89
				$this->dist->graf->sätt_linje($x, 0, $x, $this->dist->graf->höjd - 50, $this->dist->graf->vit);
90
			}
91
92
			/**
93
			 * Rita minprocent med vit linje.
94
			 */
95
			if ($this->dist->minsumma == 0 && $andel > $this->dist->maxprocent) {
96
				$this->dist->minsumma = (float) $xkoord;
97
				$this->dist->graf->sätt_linje($x, 0, $x, $this->dist->graf->höjd - 50, $this->dist->graf->vit);
98
			}
99
100
			/**
101
			 * Rita kurva med blå färg.
102
			 */
103
			$this->dist->graf->sätt_pixel($x, $y, $this->dist->graf->blå);
104
		}
105
106
		/**
107
		 * Uppdatera procentandel för distribution, relativt totala matchrymden 3^13.
108
		 */
109
		$this->dist->procentandel = round(100 * $this->dist->andelssumma / MATCHRYMD, 3);
110
111
		/**
112
		 * Rendera text.
113
		 */
114
		$this->rendera_text($distmax_antal_rader, $oddssumma_min, $oddssumma_max, $oddssumma_utfall);
115
116
		/**
117
		 * Spara graf.
118
		 */
119
		$this->spara_graf();
120
	}
121
122
	/**
123
	 * Rita ut textinformation i graf.
124
	 */
125
	private function rendera_text(
126
		int $distmax_antal_rader,
127
		float $oddssumma_min,
128
		float $oddssumma_max,
129
		float $oddssumma_utfall
130
	): void {
131
		/**
132
		 * Rendera omgångsdata med gul text.
133
		 */
134
		$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);
135
136
		/**
137
		 * Rendera distributionsdata med gul text.
138
		 */
139
		$distmax = array_search($distmax_antal_rader, $this->distribution, true);
140
		$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);
141
142
		/**
143
		 * Rendera aktuellt utfall med röd text.
144
		 */
145
		if ($oddssumma_utfall > 0) {
146
			$this->dist->graf->sätt_text(20, 60, "utfall: $oddssumma_utfall ({$this->dist->andelssumma}) | andel: {$this->dist->procentandel} %", $this->dist->graf->röd);
147
		}
148
	}
149
150
	/**
151
	 * Spara distributionsgraf.
152
	 */
153
	private function spara_graf(): void {
154
		/**
155
		 * Spara och beräkna sannolikhetssummor.
156
		 * Logga händelse.
157
		 */
158
		$this->dist->graf->spara_tipsgraf($this->dist->bildfil);
159
		$this->dist->beräkna_sannolikhetssummor($this->tips->utdelning->tipsrad_012);
160
		$this->spara_distribution();
161
		$this->tips->odds->spel->db->logg->logga(self::class . ' ✅ Uppdaterade graf, sparade distribution.');
162
	}
163
}
164