Utfallshistorik::resultat()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 45
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 7
nop 0
dl 0
loc 45
ccs 23
cts 23
cp 1
crap 5
rs 9.2568
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Klass Utfallshistorik.
5
 * @author Niklas Dougherty
6
 */
7
8
declare(strict_types=1);
9
10
namespace Tips\Klasser;
11
12
use PDO;
13
14
/**
15
 * Klass Utfallshistorik.
16
 * Historik över utfall i matcher inom oddsintervall.
17
 * Nyttjas i grid för favoriter, i kontrast till givna odds.
18
 * Data hämtas från samtliga stryk- och europatipsomgångar.
19
 */
20
final class Utfallshistorik {
21
	/**
22
	 * @var array<int, float[]> $utfallshistorik
23
	 */
24
	public array $utfallshistorik;
25
	/**
26
	 * @var int[] $ordnad_historik
27
	 */
28
	public array $ordnad_historik;
29
30
	/**
31
	 * Initiera.
32
	 */
33 20
	public function __construct(public Prediktioner $prediktioner) {
34 20
		$this->utfallshistorik();
35
	}
36
37
	/**
38
	 * Beäkna historik över utfall.
39
	 * Redovisas i favorittabell under historik.
40
	 */
41 20
	private function utfallshistorik(): void {
42
		/**
43
		 * Sammanställ historik.
44
		 */
45 20
		$this->utfallshistorik = [];
46 20
		foreach ($this->resultat() as $index => $rad) {
47
			/**
48
			 * Format: andel 1X2 samt antal statistikposter.
49
			 * Andel i procent, avrundad till heltal.
50
			 * Nollvektor vid otillräcklig statistik.
51
			 */
52 20
			$summa = array_sum($rad);
53 20
			$this->utfallshistorik[$index] = match (true) {
54 20
				$summa > 0 => [
55 20
					round(100 * $rad[0] / $summa, 0),
56 20
					round(100 * $rad[1] / $summa, 0),
57 20
					round(100 * $rad[2] / $summa, 0),
58 20
					array_sum($rad)
59 20
				],
60 20
				default => [0, 0, 0, 0]
61 20
			};
62
		}
63
64
		/**
65
		 * Historikfält.
66
		 */
67 20
		$historik = array_map(fn (array $utfall): array =>
68 20
			[$utfall[0], $utfall[1], $utfall[2]], $this->utfallshistorik);
69
70
		/**
71
		 * Sortera.
72
		 */
73 20
		$sortering = array_map(fn (array $sort): float => (float) ne_max($sort), $historik);
74 20
		arsort($sortering);
75 20
		$this->ordnad_historik = array_flip(array_keys($sortering));
76
	}
77
78
	/**
79
	 * Hämta oddsdata från databas.
80
	 * @return array<int, int[]>
81
	 */
82 20
	private function resultat(): array {
83 20
		[$pmin, $pmax] = [0.93, 1.07]; // ± 7 % oddsintervall
84 20
		$resultat = [];
85
86
		/**
87
		 * Analysera odds från alla tipsomgångar.
88
		 */
89 20
		foreach ($this->prediktioner->prediktioner as $match => $odds) {
90 20
			$query = '';
91
92
			/**
93
			 * Iterera över matchnummer
94
			 */
95 20
			for ($index = 1; $index <= MATCHANTAL; $index++) {
96 20
				$bas = 3 * ($index - 1);
97 20
				$pred = [$bas + 1, $bas + 2, $bas + 3]; // p1–p39, d.v.s. alla enskilda odds
98 20
				$query .= "SELECT `resultat$index` AS `res` FROM `odds` NATURAL JOIN `matcher`
99 20
					NATURAL JOIN `utdelning` WHERE `tipsrad_012` AND `resultat$index` AND
100 20
					$pmin * `p$pred[0]` < $odds[0] AND $pmax * `p$pred[0]` > $odds[0] AND
101 20
					$pmin * `p$pred[1]` < $odds[1] AND $pmax * `p$pred[1]` > $odds[1] AND
102 20
					$pmin * `p$pred[2]` < $odds[2] AND $pmax * `p$pred[2]` > $odds[2]";
103 20
				if ($index < MATCHANTAL) {
104 20
					$query .= " UNION ALL ";
105
				}
106
			}
107
108 20
			$sats = $this->prediktioner->spel->db->instans->prepare($query);
109 20
			$sats->execute();
110
111
			/**
112
			 * Addera varje utfall per match.
113
			 */
114 20
			$resultat[$match] = [0, 0, 0];
115 20
			foreach ($sats->fetchAll(PDO::FETCH_ASSOC) as $rad) {
116 20
				$res = explode('-', $rad['res']);
117
				/**
118
				 * $r1 > $r2: 2; $r1 === $r2: 1; $r1 < $r2: 0
119
				 * Observera: omkastad ordning
120
				 */
121 20
				$tecken = ($res[1] <=> $res[0]) + 1;
122 20
				$resultat[$match][$tecken]++;
123
			}
124
		}
125
126 20
		return $resultat;
127
	}
128
}
129