-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.php
147 lines (134 loc) · 4 KB
/
stats.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
require_once('skills.php');
require_once('ehp.php');
class Stats {
public $xp;
public $ranks;
public $time;
private $_clamp_xp, $_lowest_xp;
private $_rank_sum, $_nn_count, $_thm_count;
private $_virtual_total, $_time_to_nn, $_time_to_thm, $_ehp, $_combat_level;
function __construct($xp, $ranks, $time=0) {
global $current_time, $TIME_TO_THM;
$this->xp = $xp;
$this->ranks = $ranks;
$this->time = ($time ? $time : $current_time);
}
function __get($name) {
global $TIME_TO_THM;
switch($name) {
case 'virtual_total':
if(isset($this->_virtual_total)) return $this->_virtual_total;
$this->_virtual_total = 0;
for($i = 1; $i < 24; $i++) {
$this->_virtual_total += level_from_xp($this->xp[$i]);
}
return $this->_virtual_total;
case 'time_to_nn':
if(isset($this->_time_to_nn)) return $this->_time_to_nn;
$this->_time_to_nn = calc_time($this->xp, 13034431);
break;
case 'time_to_thm':
if(isset($this->_time_to_thm)) return $this->_time_to_thm;
return $this->_time_to_thm = calc_time($this->xp, 200000000);
break;
case 'ehp':
if(isset($this->_ehp)) return $this->_ehp;
return ($this->_ehp = $TIME_TO_THM - $this->time_to_thm);
break;
case 'combat_level':
if(isset($this->_combat_level)) return $this->_combat_level;
$this->combat_level = combat_level(
level_from_xp($this->xp[1]), level_from_xp($this->xp[2]), level_from_xp($this->xp[3]),
level_from_xp($this->xp[4]), level_from_xp($this->xp[5]), level_from_xp($this->xp[6]),
level_from_xp($this->xp[7]));
break;
case 'clamp_xp':
case 'rank_sum':
case 'nn_count':
case 'thm_count':
case 'lowest_xp':
$hidden_var = "_{$name}";
if(isset($this->$hidden_var)) return $this->$hidden_var;
for($i = 1; $i < 24; $i++) {
if($xp[$i] == 200000000) {
$this->_thm_count++;
}
if($xp[$i] >= 13034431) {
$this->_nn_count++;
$this->_clamp_xp += 13034431;
} else {
$this->_clamp_xp += $xp[$i];
}
if($xp[$i] < $this->_lowest_xp) {
$this->_lowest_xp = $xp[$i];
}
$this->_rank_sum += $ranks[$i];
}
return $this->$hidden_var;
}
}
function save($id) {
global $SKILLS, $SKILL_COUNT;
$sql = "INSERT INTO stats VALUES ($id";
for($i = 0; $i < $SKILL_COUNT; $i++) {
$sql .= ",{$this->xp[$i]},{$this->ranks[$i]}";
}
$sql .= ') ON DUPLICATE KEY UPDATE ';
for($i = 0; $i < $SKILL_COUNT; $i++) {
$sql .= "{$SKILLS[$i]}XP={$this->xp[$i]},{$SKILLS[$i]}Rank={$this->ranks[$i]}";
if($i != $SKILL_COUNT - 1) {
$sql .= ',';
}
}
return mysql_query($sql);
}
static function from_hiscores($player) {
$xp = array();
$ranks = array();
$file = fopen(HISCORES_LITE_URL . $player, 'r');
if($file) {
for($i = 0; $i < 24; $i++) {
$line = explode(',', fgets($file));
$ranks[$i] = (int)$line[0];
$xp[$i] = (int)$line[2];
}
return new Stats($xp, $ranks);
}
return null;
}
static function from_database($player) {
if(is_string($player)) {
$result = mysql_query("SELECT LastCheckTime AS Time,stats.* FROM players JOIN stats ON players.ID=stats.ID WHERE Name='{$player}'" );
} else {
$result = mysql_query("SELECT LastCheckTime AS Time,stats.* FROM players JOIN stats ON players.ID=stats.ID WHERE players.ID={$player}");
}
if($result && ($row = mysql_fetch_array($result))) {
return self::from_database_row($row);
}
return null;
}
static function from_database_row($row) {
global $SKILLS;
$xp = array();
$ranks = array();
for($i = 0; $i < 24; $i++) {
$xp[$i] = (int)$row["{$SKILLS[$i]}XP"];
$ranks[$i] = (int)$row["{$SKILLS[$i]}Rank"];
}
return new Stats($xp, $ranks, (isset($row['Time']) ? $row['Time'] : 0));
}
static function subtract($stats, $stats_earlier) {
if(!($stats && $stats_earlier)) return array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
$xp = array();
for($i = 0; $i < 24; $i++) {
if($stats->xp[$i] >= $stats_earlier->xp[$i]) {
$xp[$i] = $stats->xp[$i] - $stats_earlier->xp[$i];
} else {
return null; //negative xp gain
}
}
return $xp;
}
}
?>