Add: app highest record

This commit is contained in:
2018-10-23 22:29:24 +09:00
parent e384627c54
commit d45e44c6b2
12 changed files with 295 additions and 20 deletions
@@ -101,10 +101,10 @@ function get_high_score_list($maestro_id, $player_id, $appList) {
$app_id_list = $appList[$i]['AppID'];
$query = "
SELECT MAX(BR.BestRecord)
SELECT MAX(AHR.HighestRecord)
FROM app AS A
INNER JOIN best_record AS BR
ON A.AppID = ? AND A.AppID = BR.AppID AND BR.MaestroID = ? AND BR.PlayerID = ?
INNER JOIN app_highest_record AS AHR
ON A.AppID = ? AND A.AppID = AHR.AppID AND AHR.MaestroID = ? AND AHR.PlayerID = ?
ORDER BY A.AppID ASC";
$stmt = $db_conn->prepare($query);
$stmt->bind_param('iii', $app_id_list, $maestro_id, $player_id);
+52
View File
@@ -0,0 +1,52 @@
<?php
function get_app_highest_record($maestro_id, $app_id, $player_id) {
global $db_conn;
$query = "
SELECT HighestRecord
FROM app_highest_record
WHERE MaestroID = ? AND AppID = ? AND PlayerID = ?;
";
$stmt = $db_conn->prepare($query);
$stmt->bind_param("iii", $maestro_id, $app_id, $player_id);
$stmt->execute();
$stmt->bind_result($highest_record);
$stmt->fetch();
$stmt->close();
return $highest_record;
echo "\nget_app_highest_record : $highest_record";
}
function remove_app_highest_record($maestro_id, $app_id, $player_id) {
global $db_conn;
$query = "
DELETE FROM app_highest_record
WHERE MaestroID = ? AND AppID = ? AND PlayerID = ?;
";
$stmt = $db_conn->prepare($query);
$stmt->bind_param("iii", $maestro_id, $app_id, $player_id);
$stmt->execute();
echo "\ndelete_app_highest_record";
}
function insert_app_highest_record($maestro_id, $app_id, $player_id, $highest_record) {
global $db_conn;
$query = "
INSERT INTO app_highest_record (MaestroID, PlayerID, AppID, HighestRecord, RecordDateTime)
VALUES (?, ?, ?, ?, NOW());
";
$stmt = $db_conn->prepare($query);
$stmt->bind_param('iiid', $maestro_id, $player_id, $app_id, $highest_record);
$stmt->execute();
echo "\ninsert_app_highest_record";
}
?>
@@ -0,0 +1,23 @@
<?php
header('Content-Type: application/json');
$maestro_id = $_POST["MaestroID"];
$player_id = $_POST["PlayerID"];
$app_id = $_POST["AppID"];
include "./../setup/connect_db.php";
include "./../lib/app_highest_record.php";
$replyJSON = array();
$replyJSON["AppHighestRecord"] = get_app_highest_record($maestro_id, $app_id, $player_id);
if($replyJSON.length === 0) {
send_error_message($replyJSON, "히스토리 기록 없음");
$db_conn->close();
exit;
}
echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE);
$db_conn->close();
?>
@@ -0,0 +1,101 @@
<?php
header('Content-Type: application/json');
$maestro_id = $_POST["MaestroID"];
$player_id = $_POST["PlayerID"];
$app_id = $_POST["AppID"];
$record = $_POST["Record"];
// echo $record." / ";
include "./../setup/connect_db.php";
include "./../lib/app_highest_record.php";
// best record is for the saved data by every hour
$prev_best_record_id = -1;
$prev_best_record = 0;
$prev_best_record_id = get_best_record($maestro_id, $app_id, $player_id);
echo "id : ".$prev_best_record_id." ".$prev_best_record;
if($prev_best_record_id === null || $prev_best_record_id < 0) {
// echo "insert";
insert_best_record($maestro_id, $app_id, $player_id, $record);
} else {
// echo $record." ".$prev_best_record;
if($record > $prev_best_record) {
// echo "update";
update_best_record($prev_best_record_id, $record);
}
}
// highest record is the highest record
$app_highest_record = get_app_highest_record($maestro_id, $app_id, $player_id);
echo "\napp_highest_record : $app_highest_record";
echo "\nrecord : $record";
if($app_highest_record == null) {
insert_app_highest_record($maestro_id, $app_id, $player_id, $record);
echo "\ninsert_app_highest_record : $record";
}
else if($record > $app_highest_record) {
remove_app_highest_record($maestro_id, $app_id, $player_id);
echo "\nremove_app_highest_record";
insert_app_highest_record($maestro_id, $app_id, $player_id, $record);
echo "\ninsert_app_highest_record : $record";
}
$db_conn->close();
function get_best_record($maestro_id, $app_id, $player_id) {
global $db_conn;
global $prev_best_record_id, $prev_best_record;
$query = "
SELECT BestRecordID, BestRecord
FROM best_record
WHERE MaestroID = ? AND AppID = ? AND PlayerID = ? AND DATE(RecordDateTime) = DATE(NOW())
/*AND HOUR(RecordDateTime) = HOUR(NOW())*/
";
$stmt = $db_conn->prepare($query);
$stmt->bind_param("iii", $maestro_id, $app_id, $player_id);
$stmt->execute();
$stmt->bind_result($prev_best_record_id, $prev_best_record);
$stmt->fetch();
$stmt->close();
$GLOBALS["prev_best_record_id"] = $prev_best_record_id;
$GLOBALS["prev_best_record"] = $prev_best_record;
return $prev_best_record_id;
}
function insert_best_record($maestro_id, $app_id, $player_id, $record) {
global $db_conn;
$query = "
INSERT INTO best_record (MaestroID, PlayerID, AppID, BestRecord, RecordDateTime)
VALUES (?, ?, ?, ?, NOW());
";
$stmt = $db_conn->prepare($query);
$stmt->bind_param('iiid', $maestro_id, $player_id, $app_id, $record);
$stmt->execute();
}
function update_best_record($prev_best_record_id, $record) {
global $db_conn;
$query = "
UPDATE best_record
SET BestRecord = ?, RecordDateTime = NOW()
WHERE BestRecordID = ? AND DATE(RecordDateTime) = DATE(NOW())
/* AND HOUR(RecordDateTime) = HOUR(NOW()) */
";
$stmt = $db_conn->prepare($query);
$stmt->bind_param('di', $record, $prev_best_record_id);
$stmt->execute();
}
?>
@@ -8,6 +8,8 @@ $best_record = $_POST["BestRecord"];
// echo $best_record." / ";
include "./../setup/connect_db.php";
include "./../lib/app_highest_record.php";
$prev_best_record_id = -1;
$prev_best_record = 0;
@@ -26,6 +28,15 @@ if($prev_best_record_id === null || $prev_best_record_id < 0) {
}
}
$app_highest_record = get_app_highest_record($maestro_id, $app_id, $player_id);
if($app_highest_record == null) {
insert_app_highest_record($maestro_id, $app_id, $player_id, $best_record);
}
else if($best_record > $app_highest_record) {
remove_app_highest_record($maestro_id, $app_id, $player_id);
insert_app_highest_record($maestro_id, $app_id, $player_id, $best_record);
}
$db_conn->close();
+13
View File
@@ -76,6 +76,19 @@ CREATE TABLE best_record (
FOREIGN KEY (PlayerID) REFERENCES player(PlayerID)
);
CREATE TABLE app_highest_record (
AppHighestRecordID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
MaestroID INT UNSIGNED NOT NULL,
AppID INT UNSIGNED NOT NULL,
PlayerID INT UNSIGNED NOT NULL,
HighestRecord FLOAT NOT NULL,
RecordDateTime DATETIME NOT NULL,
FOREIGN KEY (MaestroID) REFERENCES maestro(MaestroID),
FOREIGN KEY (AppID) REFERENCES app(AppID),
FOREIGN KEY (PlayerID) REFERENCES player(PlayerID)
);
CREATE TABLE ranking (
RankingID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
MaestroID INT UNSIGNED NOT NULL,