Fix: getHighestRecord -> getHighestRecordForWriting

This commit is contained in:
2019-07-15 22:52:13 +09:00
parent cfb843f91c
commit 3cb72799d4
6 changed files with 246 additions and 30 deletions
+19 -1
View File
@@ -74,6 +74,23 @@ DBService.prototype.tddSetup = function(command, onSucceededListener, onFailedLi
xhr.send("command=" + command); xhr.send("command=" + command);
} }
DBService.prototype.tddRun = function(command, onSucceededListener, onFailedListener) {
var xhr = this.makeXhr(
"php/db/tdd_run.php",
(function() { // onreadystatechange
if(xhr.readyState == 4 && xhr.status == 200) {
var replyJSON = JSON.parse(xhr.responseText);
if(replyJSON != null)
onSucceededListener(replyJSON);
else
onFailedListener(replyJSON);
}
}).bind(this)
);
xhr.send("command=" + command);
}
// writing // writing
DBService.prototype.requestWritingPlayerList = function(onSucceededListener, onFailedListener) { DBService.prototype.requestWritingPlayerList = function(onSucceededListener, onFailedListener) {
var xhr = this.makeXhr( var xhr = this.makeXhr(
@@ -155,7 +172,7 @@ DBService.prototype.updateTypingExamRecord = function(writingID, record, onSucce
); );
} }
DBService.prototype.getHighestRecord = function(onSucceededListener, onFailedListener) { DBService.prototype.getTypingExamHighestRecord = function(writingID, onSucceededListener, onFailedListener) {
var xhr = this.makeXhr( var xhr = this.makeXhr(
"php/writing/get_highest_record.php", "php/writing/get_highest_record.php",
(function() { // onreadystatechange (function() { // onreadystatechange
@@ -172,5 +189,6 @@ DBService.prototype.getHighestRecord = function(onSucceededListener, onFailedLis
xhr.send( xhr.send(
"maestroID=" + this.maestroID "maestroID=" + this.maestroID
+ "&playerID=" + this.playerID + "&playerID=" + this.playerID
+ "&writingID=" + writingID
); );
} }
+59
View File
@@ -0,0 +1,59 @@
<?php
header('Content-Type: application/json');
// get parameters from client
$command = $_POST["command"];
include "./../lib/json_builder.php";
include "./../lib/connect_db.php";
$jsonBuilder = new JsonBuilder();
$dbConnector = new DBConnector();
$isConnected = $dbConnector->connectDB();
if (!$isConnected) {
$jsonBuilder->setErrorMessage("DB connection : failed");
$jsonBuilder->sendResultFail();
exit;
}
const MAESTRO_ID = 3; // 삼화초
const PLAYER_ID = 35; // 박지상
const WRITING_ID = 1;
const WRITING_RECORD = 123.456;
if ($command === "addTypingExamHighestRecord") {
include "./db_method_container.php";
include "./typing_exam_collection.php";
$typingExam = new TypingExamCollection($dbConnector->getMysqli());
$typingExam->addHighestRecord(MAESTRO_ID, PLAYER_ID, WRITING_ID, WRITING_RECORD);
// $typingExamHighestRecordData = $typingExam->getHighestRecordForWriting(MAESTRO_ID, PLAYER_ID, WRITING_ID);
// $jsonBuilder->setData("typingExamHighestRecordData", $typingExamHighestRecordData);
} elseif ($command === "updateTypingExamHighestRecord") {
include "./db_method_container.php";
include "./typing_exam_collection.php";
$typingExam = new TypingExamCollection($dbConnector->getMysqli());
$typingExamHighestRecordData = $typingExam->getHighestRecordForWriting(MAESTRO_ID, PLAYER_ID, WRITING_ID);
// $jsonBuilder->setData("typingExamHighestRecordData", $typingExamHighestRecordData);
$typingExam->updateHighestRecord($typingExamHighestRecordData["typingExamHighestRecordID"], WRITING_RECORD + 10);
// $typingExamHighestRecordData = $typingExam->getHighestRecordForWriting(MAESTRO_ID, PLAYER_ID, WRITING_ID);
// $jsonBuilder->setData("updateTypingExamHighestRecord", $typingExamHighestRecordData);
} elseif ($command === "getTypingExamHigestRecord") {
include "./db_method_container.php";
include "./typing_exam_collection.php";
$typingExam = new TypingExamCollection($dbConnector->getMysqli());
$typingExamHighestRecordData = $typingExam->getHighestRecordForWriting(MAESTRO_ID, PLAYER_ID, WRITING_ID);
$jsonBuilder->setData("typingExamHighestRecordData", $typingExamHighestRecordData);
}
$jsonBuilder->sendResultSuccess();
?>
+27 -6
View File
@@ -80,7 +80,7 @@ class TypingExamCollection extends DBMethodContainer
SET HighestRecord = ?, RecordDateTime = NOW() SET HighestRecord = ?, RecordDateTime = NOW()
WHERE TypingExamHighestRecordID = ?"; WHERE TypingExamHighestRecordID = ?";
$stmt = $this->mysqli->prepare($query); $stmt = $this->mysqli->prepare($query);
$stmt->bind_param("id", $typingExamHighestRecordID, $record); $stmt->bind_param("di", $record, $typingExamHighestRecordID);
$stmt->execute(); $stmt->execute();
$stmt->close(); $stmt->close();
} }
@@ -105,21 +105,42 @@ class TypingExamCollection extends DBMethodContainer
return $result; return $result;
} }
public function getHighestRecord($maestroID, $playerID) public function getHighestRecordForWriting($maestroID, $playerID, $writingID)
{ {
$query = " $query = "
SELECT WritingID, HighestRecord SELECT TypingExamHighestRecordID, WritingID, HighestRecord
FROM typing_exam_highest_record
WHERE MaestroID = ? AND PlayerID = ? AND WritingID = ?";
$stmt = $this->mysqli->prepare($query);
$stmt->bind_param("iii", $maestroID, $playerID, $writingID);
$stmt->execute();
$stmt->bind_result($typingExamHighestRecordID, $writingID, $highestRecord);
$stmt->fetch();
$recordData = array();
$recordData["typingExamHighestRecordID"] = $typingExamHighestRecordID;
$recordData["highestRecord"] = $highestRecord;
$stmt->close();
return $recordData;
}
public function getHighestRecordArrayForAllWriting($maestroID, $playerID)
{
$query = "
SELECT TypingExamHighestRecordID, WritingID, HighestRecord
FROM typing_exam_highest_record FROM typing_exam_highest_record
WHERE MaestroID = ? AND PlayerID = ?"; WHERE MaestroID = ? AND PlayerID = ?";
$stmt = $this->mysqli->prepare($query); $stmt = $this->mysqli->prepare($query);
$stmt->bind_param("ii", $maestroID, $playerID); $stmt->bind_param("iii", $maestroID, $playerID);
$stmt->execute(); $stmt->execute();
$stmt->bind_result($writingID, $highestRecord); $stmt->bind_result($typingExamHighestRecordID, $writingID, $highestRecord);
$resultArray = array(); $resultArray = array();
while($stmt->fetch()) { while($stmt->fetch()) {
$rowArray = array(); $rowArray = array();
$rowArray["writingID"] = $writingID; $rowArray["typingExamHighestRecordID"] = $typingExamHighestRecordID;
// $rowArray["writingID"] = $writingID;
$rowArray["highestRecord"] = $highestRecord; $rowArray["highestRecord"] = $highestRecord;
array_push($resultArray, $rowArray); array_push($resultArray, $rowArray);
} }
+4 -3
View File
@@ -3,6 +3,7 @@ header('Content-Type: application/json');
$maestroID = $_POST["maestroID"]; $maestroID = $_POST["maestroID"];
$playerID = $_POST["playerID"]; $playerID = $_POST["playerID"];
$writingID = $_POST["writingID"];
include "./../lib/json_builder.php"; include "./../lib/json_builder.php";
include "./../lib/connect_db.php"; include "./../lib/connect_db.php";
@@ -23,10 +24,10 @@ include "./../db/typing_exam_collection.php";
$typingExam = new TypingExamCollection($dbConnector->getMysqli()); $typingExam = new TypingExamCollection($dbConnector->getMysqli());
$highestRecordArray = $typingExam->getHighestRecord(); $highestRecordData = $typingExam->getHighestRecordForWriting($maestroID, $playerID, $writingID);
$jsonBuilder->setData("highestRecordArray", $highestRecordArray); $jsonBuilder->setData("highestRecordData", $highestRecordData);
if ($highestRecordArray === null) { if ($highestRecordData === null) {
$jsonBuilder->sendResultFail(); $jsonBuilder->sendResultFail();
exit; exit;
} }
+21 -7
View File
@@ -45,16 +45,30 @@ if ($thisHourRecordData === null) {
} }
} }
$highestRecordData = $typingExam->getHighestRecord($maestroID, $playerID); $highestRecordArray = $typingExam->getHighestRecordForWriting($maestroID, $playerID, $writingID);
$jsonBuilder->setData("highestRecordData", $highestRecordData); $jsonBuilder->setData("highestRecordArray", $highestRecordArray);
if (count($highestRecordData) === 0) { if (count($highestRecordArray) === 0) {
$typingExam->addHighestRecord($maestroID, $playerID, $writingID, $record); $typingExam->addHighestRecord($maestroID, $playerID, $writingID, $record);
$jsonBuilder->setData("highestRecord", "added"); $jsonBuilder->setData("highestRecord", "added");
} else { } else {
if ($record > $highestRecordData["record"]) { // $highestRecord = null;
$typingExam->updateHighestRecord($highestRecordData["ID"], $record); // $count = count($highestRecordArray);
$jsonBuilder->setData("highestRecord", "updated"); // for ($i = 0; $i < $count; $i++) {
} // $data = $highestRecordArray[$i];
// if ($data["writingID"] === $writingID) {
// $highestRecordData = $data;
// break;
// }
// }
// if ($highestRecord === null) {
// $typingExam->addHighestRecord($maestroID, $playerID, $writingID, $record);
// $jsonBuilder->setData("highestRecord", "added");
// } else {
if ($record > $highestRecordArray["highestRecord"]) {
$typingExam->updateHighestRecord($highestRecordArray["typingExamHighestRecordID"], $record);
$jsonBuilder->setData("highestRecord", "updated");
}
// }
} }
$jsonBuilder->sendResultSuccess(); $jsonBuilder->sendResultSuccess();
+116 -13
View File
@@ -1,3 +1,82 @@
//////////////////////////////////////////////////
// typing exam highest record DB
QUnit.test( "TypingExamHighestRecord", function( assert ) {
var MAESTRO_ID = 3; // 삼화초
var PLAYER_ID = 35; // 박지상
var dbService = new DBService();
dbService.setMaestroID(MAESTRO_ID);
dbService.setPlayerID(PLAYER_ID);
var WRITING_ID = 1;
var RECORD = 123.456;
dbService.tddSetup(
"deleteTypingExamHighestRecordAll",
(function(jsonData) { // onSucceeded
// console.log(jsonData);
addTypingExamHighestRecord();
}).bind(this),
(function(jsonData) { // onFailed
// console.log(jsonData);
}).bind(this)
);
function addTypingExamHighestRecord()
{
dbService.tddRun(
"addTypingExamHighestRecord",
(function(jsonData) { // onSucceeded
// console.log(jsonData);
updateTypingExamHighestRecord();
}).bind(this),
(function(jsonData) { // onFailed
// console.log(jsonData);
}).bind(this)
);
}
function updateTypingExamHighestRecord()
{
dbService.tddRun(
"updateTypingExamHighestRecord",
(function(jsonData) { // onSucceeded
// console.log(jsonData);
getTypingExamHighestRecord();
}).bind(this),
(function(jsonData) { // onFailed
// console.log(jsonData);
}).bind(this)
);
}
var done = assert.async();
function getTypingExamHighestRecord()
{
setTimeout(function() {
var UPDATED_RECORD = 133.456;
dbService.tddRun(
"getTypingExamHigestRecord",
(function(jsonData) { // onSucceeded
// console.log(jsonData);
var highestRecord = jsonData["typingExamHighestRecordData"];
assert.equal(highestRecord["highestRecord"], UPDATED_RECORD, "highest record");
done();
}).bind(this),
(function(jsonData) { // onFailed
// console.log(jsonData);
done();
}).bind(this)
);
});
}
});
////////////////////////////////////////////////// //////////////////////////////////////////////////
// typing exam DB // typing exam DB
@@ -6,30 +85,54 @@ QUnit.test( "TypingExam", function( assert ) {
dbService.setMaestroID(3); // 삼화초 dbService.setMaestroID(3); // 삼화초
dbService.setPlayerID(35); // 박지상 dbService.setPlayerID(35); // 박지상
var writingIDForTest = 1;
var recordForTest = 117.89;
dbService.tddSetup( dbService.tddSetup(
"deleteTypingExamRecordAll", "deleteTypingExamRecordAll",
(function(jsonData) { // onSucceeded (function(jsonData) { // onSucceeded
console.log(jsonData); // console.log(jsonData);
}).bind(this), }).bind(this),
(function(jsonData) { // onFailed (function(jsonData) { // onFailed
console.log(jsonData); // console.log(jsonData);
}).bind(this) }).bind(this)
); );
// dbService.tddSetup(
// "deleteTypingExamHighestRecordAll",
// (function(jsonData) { // onSucceeded
// // console.log(jsonData);
// }).bind(this),
// (function(jsonData) { // onFailed
// // console.log(jsonData);
// }).bind(this)
// );
dbService.addPrevHourTypingExamRecord( dbService.addPrevHourTypingExamRecord(
1, //writingRecord writingIDForTest, //writingRecord
120.35, // record recordForTest + 10, // record
(function(jsonData) { // onSucceeded (function(jsonData) { // onSucceeded
console.log(jsonData); // console.log(jsonData);
}).bind(this), }).bind(this),
(function(jsonData) { // onFailed (function(jsonData) { // onFailed
console.log(jsonData); // console.log(jsonData);
}).bind(this) }).bind(this)
); );
dbService.updateTypingExamRecord( dbService.updateTypingExamRecord(
1, //writingRecord writingIDForTest, //writingRecord
105.53, // record recordForTest - 10, // record
(function(jsonData) { // onSucceeded
// console.log(jsonData);
}).bind(this),
(function(jsonData) { // onFailed
// console.log(jsonData);
}).bind(this)
);
dbService.updateTypingExamRecord(
writingIDForTest, //writingRecord
recordForTest, // record
(function(jsonData) { // onSucceeded (function(jsonData) { // onSucceeded
console.log(jsonData); console.log(jsonData);
}).bind(this), }).bind(this),
@@ -38,15 +141,16 @@ QUnit.test( "TypingExam", function( assert ) {
}).bind(this) }).bind(this)
); );
/*
var done = assert.async(); var done = assert.async();
setTimeout(function() { setTimeout(function() {
dbService.getHighestRecord( dbService.getTypingExamHighestRecord(
writingIDForTest,
(function(jsonData) { // onSucceeded (function(jsonData) { // onSucceeded
console.log(jsonData); console.log(jsonData);
var record = jsonData["highestRecord"]; var highestRecord = jsonData["highestRecordData"];
assert.equal(record, 100, "highest record"); assert.equal(highestRecord["writingID"], writingIDForTest, "highest record");
assert.equal(highestRecord["highestRecord"], recordForTest, "highest record");
done(); done();
}).bind(this), }).bind(this),
(function(jsonData) { // onFailed (function(jsonData) { // onFailed
@@ -55,7 +159,6 @@ QUnit.test( "TypingExam", function( assert ) {
}).bind(this) }).bind(this)
); );
}); });
*/
}); });