-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathranking.class.valhalla.php
executable file
·182 lines (168 loc) · 5.65 KB
/
ranking.class.valhalla.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
if(!defined('WCF_DIR')) {
// For when the rankingarchive is opened by user directly via url /regnum/rankingarchive, not as a cronjob from the forum suite where WCF_DIR would be defined
define('WCF_DIR', "../../forum/");
}
require_once(WCF_DIR . '../regnum/rankingarchive/global.php');
require_once(WCF_DIR . '../regnum/lib/class.connect.valhalla.php');
class Ranking
{
public static $REALMS = [2012 => 'Alsius', 4 => 'Ignis', 3 => 'Syrtis']; // values sind
public static $CLASSES = [1 => 'Warrior', 2 => 'Archer', 3 => 'Mage']; // egal, nur zur übersicht
private $con;
public function __construct() {
$this->con = connecting::connect_rankarchive();
mysqli_set_charset($this->con, "latin1");
}
private function query($sql) {
$ret = mysqli_query($this->con, $sql);
if (mysqli_errno($this->con)) {
dieee("invalid sql: " . mysqli_error($this->con) . ", " . $sql);
}
if(!$ret) {
dieee("sql " . $sql . " is invalid3");
}
return $ret;
}
/** from all players as yyyy-mm-dd str */
public function get_last_insert_date() {
$sql = "SELECT max(date) AS maxdate FROM krp";
$result = $this->query($sql);
$last_insert = "";
while ($row = mysqli_fetch_object($result)) {
$last_insert = $row->maxdate;
}
return $last_insert;
}
/** get player id or make new player and return id */
public function insert_ignore_player($name, $realmId, $class) {
$name = mysqli_escape_string($this->con, $name);
$class = mysqli_escape_string($this->con, $class);
$realmId = intval($realmId);
if (!is_numeric($realmId) || empty($name) || empty($class) || !array_key_exists($realmId, self::$REALMS)) {
dieee("row values invalid: $class, $realmId, $name Abc.");
}
$sql = "insert ignore into player (name,realm,class) values (\"$name\", $realmId, \"$class\")"; // duplicates: handled by composite unique sql key
$this->query($sql);
$player_id = mysqli_insert_id($this->con);
if ($player_id < 1) {
// player existiert bereits, get id
$sql = "select id from player where name = \"$name\"";
$result = $this->query($sql);
while ($row = mysqli_fetch_object($result)) {
$player_id = $row->id;
}
}
if ($player_id < 1) {
dieee("wtf");
}
return $player_id;
}
/** for date=today */
public function insert_ignore_krp($player_id, $rlmp, $date = '') {
$player_id = intval($player_id);
$rlmp = intval($rlmp);
if (!is_numeric($player_id) || !is_numeric($rlmp)) {
dieee("invalid asioegw4 $player_id --- $rlmp");
}
if(empty($date)) {
$date = date('Y-m-d'); // für -12h: 2. param ,time()-43200);
}
$sql = "insert ignore into krp (player, date, rlmp) values ($player_id, \"" . $date . "\", $rlmp)"; // SILENTLY fails if already in existence
$this->query($sql);
}
/** return all players with as obj(name,realm,class)[] */
public function get_players() {
$players = [];
$sql = "SELECT name,realm,class FROM player";
$result = $this->query($sql);
while ($row = mysqli_fetch_object($result)) {
$players[] = [
"name" => utf8_encode($row->name),
"realm" => $row->realm,
"class" => utf8_encode($row->class)
];
}
return $players;
}
/** get all rlmps by name+realm as obj(time,rlmp)[] */
public function get_rlmps($name, $realm) {
$name = mysqli_escape_string($this->con, $name);
$realm = intval($realm);
if (!array_key_exists($realm, self::$REALMS)) {
dieee("get rlmp: invalid realm id: " . $realm);
}
if (empty($name)) {
dieee("get rlmp: char name empty");
}
$rlmps = [];
if(substr($name, 0, 13) === '[ALL PLAYERS ') {
$sql = "SELECT sum(rlmp) AS rlmp,date FROM krp LEFT JOIN player ON krp.player = player.id WHERE realm = $realm GROUP BY date ORDER BY date ASC";
} else {
$sql = "SELECT rlmp,date FROM krp LEFT JOIN player ON krp.player = player.id WHERE name = \"$name\" AND realm = $realm ORDER BY date ASC";
}
$result = $this->query($sql);
$last_time = 0;
while ($row = mysqli_fetch_object($result)) {
$time = strtotime($row->date);
if ($last_time != 0) {
$i = $last_time;
while (($i += 86400) < $time) {
// fill missing days (gaps) until next recorded date ($time) with null
$rlmps[] = [
"time" => $i,
"rlmp" => null
];
}
}
$rlmps[] = [
"time" => $time,
"rlmp" => $row->rlmp
];
$last_time = $time;
}
return $rlmps;
}
/** return top x players of all time by their max rp as obj(name,realm,maxrlmp,date)[] */
public function get_all_time_top($x) {
$x = intval($x);
if (!is_numeric($x)) {
dieee("asdklfjasdf 20170730");
}
$players = [];
$sql = "select max(date) as date, max(rlmp) as rlmp, name, realm, class from krp left join player on player.id = krp.player group by player order by rlmp desc limit $x";
$result = $this->query($sql);
while ($row = mysqli_fetch_object($result)) {
$players[] = [
"name" => utf8_encode($row->name),
"realm" => $row->realm,
"class" => utf8_encode($row->class),
"date" => utf8_encode(date('Y-m-d', strtotime($row->date))),
"rlmp" => $row->rlmp
];
}
return $players;
}
public function get_players_that_are_new_to_ranking_since_x_days($x) {
$x = intval($x);
if (!is_numeric($x)) {
dieee("ziotkklt 20170730");
}
$players = [];
$sql = "SELECT min(date) as date, id, name, realm, class
FROM krp LEFT JOIN player ON player.id = krp.player
GROUP BY player
HAVING min(date) > now() - INTERVAL $x DAY
ORDER BY min(date) DESC";
$result = $this->query($sql);
while ($row = mysqli_fetch_object($result)) {
$players[] = [
"name" => utf8_encode($row->name),
"realm" => $row->realm,
"class" => utf8_encode($row->class),
"date" => utf8_encode(date('Y-m-d', strtotime($row->date))),
];
}
return $players;
}
}