-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.php
90 lines (84 loc) · 2.32 KB
/
Entity.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
include( "Hit.php" );
class Entity {
protected $name = "";
protected $color = "";
/* A table with the history of hits? */
protected $hits = array();
protected $todays_score = 0;
protected $proportion = 0;
public function __construct( /*string*/ $file_name, /*string*/ $color ) {
$this->name = $file_name;
$this->color = $color;
$handle = fopen($file_name, "r");
$i = 0;
if ($handle) {
while (($line = fgets($handle)) !== false) {
if( $i !== 0 && substr( $line, 0, 1 ) != "%" ){
// process the hit line read.
$array = explode( ":", $line );
$this->hits[ $array[0] ] = $array[1];
} else if( $i === 0 ){
$score_space = explode( ":", $line );
$score = explode( " ", $score_space[1] );
$this->todays_score = $score[0];
}
$i++;
}
} else {
// error opening the file.
}
fclose($handle);
}
public function get_name(){
return $this->name;
}
public function get_color(){
return $this->color;
}
public function get_score(){
return $this->todays_score;
}
public function get_proportion(){
return $this->proportion;
}
public function set_proportion( /*float*/ $proportion ){
$this->proportion = $proportion;
}
public function get_hits(){
return $this->hits;
}
public function get_latest_hit(){
$datetime = new DateTime();
return new Hit( end( $this->hits ), $datetime->setTimestamp( key( $this->hits ) ) );
}
public function get_first_hit(){
$datetime = new DateTime();
return new Hit( reset($this->hits) , $dqtetime->setTimestamp( key( $this->hits ) ) );
}
public function get_todays_hits(){
$arr = $this->hits;
$input = strtotime('today midnight');
$the_key = get_closest_key( $arr, $input );
return array_slice( $arr, $the_key );
}
private function get_closest_key( $arr, $input){
if (isset($arr[$input])) {
return $input;
}
foreach ($arr as $key => $value) {
if ($key > $input) {
if (prev($arr) === FALSE) {
// If the input is smaller than the first key
return $key;
}
$prevKey = key($arr);
if (abs($key - $input) < abs($prevKey - $input)) {
return $key;
} else {
return $prevKey;
}
}
}
}
}