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

HistorisktUtfall   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 5%

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 96
ccs 2
cts 40
cp 0.05
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A historiskt_utfall() 0 29 3
A resultat() 0 45 5
1
<?php
2
3
/**
4
 * Klass HistorisktUtfall.
5
 * @author Niklas Dougherty
6
 */
7
8
declare(strict_types=1);
9
10
namespace Tips\Moduler\TT;
11
12
use PDO;
13
use Tips\Moduler\TT;
14
15
/**
16
 * Klass HistorisktUtfall.
17
 * Ger faktiskt utfall av tidigare matcher i samma oddsintervall.
18
 * Data hämtas från samtliga stryk- och europatipsomgångar.
19
 */
20
class HistorisktUtfall {
21
	/**
22
	 * @var array<int, float[]> $utfallshistorik
23
	 */
24
	public array $utfallshistorik;
25
26
	/**
27
	 * Initiera.
28
	 */
29 3
	public function __construct(public TT $tt) {
30 3
	}
31
32
	/**
33
	 * Beräkna historiskt utfall.
34
	 * Redovisas i matchtabell under historik.
35
	 */
36
	public function historiskt_utfall(): void {
37
		/**
38
		 * Sammanställ historik.
39
		 */
40
		$this->tt->utfallshistorik = [];
41
		foreach ($this->resultat() as $index => $rad) {
42
			/**
43
			 * Nollvektor vid otillräcklig statistik.
44
			 */
45
			$this->tt->utfallshistorik[$index] = [0, 0, 0, 0];
46
			$summa = array_sum($rad);
47
			/**
48
			 * Format: andel 1X2 samt antal statistikposter.
49
			 * Andel i procent, avrundad till heltal.
50
			 */
51
			if ($summa > 0) {
52
				$this->tt->utfallshistorik[$index] = [
53
					round(100 * $rad[0] / $summa, 0),
54
					round(100 * $rad[1] / $summa, 0),
55
					round(100 * $rad[2] / $summa, 0),
56
					array_sum($rad)
57
				];
58
			}
59
		}
60
61
		$historik = array_map(fn ($utfall): string => implode(',', $utfall), $this->tt->utfallshistorik);
62
63
		$this->tt->db_preferenser->spara_preferens('topptips.historik', implode(',', $historik));
64
		$this->tt->utdelning->spel->db->logg->logga(self::class . ": ✅ Sparade TT-historik.");
65
	}
66
67
	/**
68
	 * Hämta oddsdata från databas.
69
	 * @return array<int, int[]>
70
	 */
71
	private function resultat(): array {
72
		[$pmin, $pmax] = [0.93, 1.07]; // ± 7 % oddsintervall
73
		$resultat = [];
74
75
		/**
76
		 * Analysera odds från alla tipsomgångar.
77
		 */
78
		foreach ($this->tt->tt_odds as $match => $odds) {
79
			$query = '';
80
81
			/**
82
			 * Iterera över matchnummer
83
			 */
84
			for ($index = 1; $index <= MATCHANTAL; $index++) {
0 ignored issues
show
Bug introduced by
The constant Tips\Moduler\TT\MATCHANTAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
85
				$bas = 3 * ($index - 1);
86
				$pred = [$bas + 1, $bas + 2, $bas + 3]; // p1–p39, d.v.s. alla enskilda odds
87
				$query .= "SELECT `resultat$index` AS `res` FROM `odds` NATURAL JOIN `matcher`
88
					NATURAL JOIN `utdelning` WHERE `tipsrad_012` AND `resultat$index` AND
89
					$pmin * `p$pred[0]` < $odds[0] AND $pmax * `p$pred[0]` > $odds[0] AND
90
					$pmin * `p$pred[1]` < $odds[1] AND $pmax * `p$pred[1]` > $odds[1] AND
91
					$pmin * `p$pred[2]` < $odds[2] AND $pmax * `p$pred[2]` > $odds[2]";
92
				if ($index < MATCHANTAL) {
93
					$query .= " UNION ALL ";
94
				}
95
			}
96
97
			$sats = $this->tt->utdelning->spel->db->instans->prepare($query);
98
			$sats->execute();
99
100
			/**
101
			 * Addera varje utfall per match.
102
			 */
103
			$resultat[$match] = [0, 0, 0];
104
			foreach ($sats->fetchAll(PDO::FETCH_ASSOC) as $rad) {
105
				$res = explode('-', $rad['res']);
106
				/**
107
				 * $r1 > $r2: 2; $r1 === $r2: 1; $r1 < $r2: 0
108
				 * Observera: omkastad ordning
109
				 */
110
				$tecken = ($res[1] <=> $res[0]) + 1;
111
				$resultat[$match][$tecken]++;
112
			}
113
		}
114
115
		return $resultat;
116
	}
117
}
118