Files
chocomae/src/web/server/record/app_ranking.php
T

122 lines
3.9 KiB
PHP

<?php
header('Content-Type: application/json');
include "./../lib/send_reply_json.php";
include "./../setup/connect_db.php";
include "./../lib/util_app.php";
$maestroID = $_POST["MaestroID"];
$appID = $_POST["AppID"];
$rankingHour = get_ranking_hour($maestroID, $appID);
$rankingDay = get_ranking_day($maestroID, $appID);
$rankingMonth = get_ranking_month($maestroID, $appID);
set_data("rankingHour", $rankingHour);
set_data("rankingDay", $rankingDay);
set_data("rankingMonth", $rankingMonth);
send_result_success();
exit;
function get_ranking_hour($maestroID, $appID) {
global $db_conn;
$query = "
FROM best_record BR, player P
WHERE BR.MaestroID = ? AND BR.AppID = ?
AND BR.RecordDateTime >= CAST(DATE_FORMAT(NOW(), '%Y-%m-%d %H:00:00') AS DATETIME)
AND BR.RecordDateTime < CAST(DATE_FORMAT(NOW(), '%Y-%m-%d %H:00:00') AS DATETIME) + INTERVAL 1 HOUR
AND BR.PlayerID = P.PlayerID
GROUP BY BR.PlayerID
";
if(is_highest_record_prefer_app($appID)) {
$query = "SELECT BR.PlayerID As PlayerID, P.Name AS Name, MAX(BR.BestRecord) AS BestRecord".$query;
$query = $query."ORDER BY max(BR.BestRecord) DESC;";
} else {
$query = "SELECT BR.PlayerID As PlayerID, P.Name AS Name, MIN(BR.BestRecord) AS BestRecord".$query;
$query = $query."ORDER BY min(BR.BestRecord) ASC;";
}
$stmt = $db_conn->prepare($query);
$stmt->bind_param('ii', $maestroID, $appID);
$stmt->execute();
$stmt->bind_result($playerID, $name, $bestRecord);
$bestRecordArray = array();
while($stmt->fetch()) {
$best_record['PlayerID'] = $playerID;
$best_record['Name'] = $name;
$best_record['BestRecord'] = $bestRecord;
array_push($bestRecordArray, $best_record);
}
return $bestRecordArray;
}
function get_ranking_day($maestroID, $appID) {
global $db_conn;
$query = "
FROM best_record BR, player P
WHERE BR.MaestroID = ? AND BR.AppID = ?
AND BR.RecordDateTime >= CURDATE()
AND BR.RecordDateTime < CURDATE() + INTERVAL 1 DAY
AND BR.PlayerID = P.PlayerID
GROUP BY BR.PlayerID
";
if(is_highest_record_prefer_app($appID)) {
$query = "SELECT BR.PlayerID As PlayerID, P.Name AS Name, MAX(BR.BestRecord) AS BestRecord".$query;
$query = $query."ORDER BY max(BR.BestRecord) DESC;";
} else {
$query = "SELECT BR.PlayerID As PlayerID, P.Name AS Name, MIN(BR.BestRecord) AS BestRecord".$query;
$query = $query."ORDER BY min(BR.BestRecord) ASC;";
}
$stmt = $db_conn->prepare($query);
$stmt->bind_param('ii', $maestroID, $appID);
$stmt->execute();
$stmt->bind_result($playerID, $name, $bestRecord);
$bestRecordArray = array();
while($stmt->fetch()) {
$best_record['PlayerID'] = $playerID;
$best_record['Name'] = $name;
$best_record['BestRecord'] = $bestRecord;
array_push($bestRecordArray, $best_record);
}
return $bestRecordArray;
}
function get_ranking_month($maestroID, $appID) {
global $db_conn;
$query = "
FROM best_record BR, player P
WHERE BR.MaestroID = ? AND BR.AppID = ?
AND BR.RecordDateTime >= CAST(DATE_FORMAT(CURDATE(), '%Y-%m-01') AS DATE)
AND BR.RecordDateTime < CAST(DATE_FORMAT(CURDATE(), '%Y-%m-01') AS DATE) + INTERVAL 1 MONTH
AND BR.PlayerID = P.PlayerID
GROUP BY BR.PlayerID
";
if(is_highest_record_prefer_app($appID)) {
$query = "SELECT BR.PlayerID As PlayerID, P.Name AS Name, MAX(BR.BestRecord) AS BestRecord".$query;
$query = $query."ORDER BY max(BR.BestRecord) DESC;";
} else {
$query = "SELECT BR.PlayerID As PlayerID, P.Name AS Name, MIN(BR.BestRecord) AS BestRecord".$query;
$query = $query."ORDER BY min(BR.BestRecord) ASC;";
}
$stmt = $db_conn->prepare($query);
$stmt->bind_param('ii', $maestroID, $appID);
$stmt->execute();
$stmt->bind_result($playerID, $name, $bestRecord);
$bestRecordArray = array();
while($stmt->fetch()) {
$best_record['PlayerID'] = $playerID;
$best_record['Name'] = $name;
$best_record['BestRecord'] = $bestRecord;
array_push($bestRecordArray, $best_record);
}
return $bestRecordArray;
}
?>