From 3cb72799d4dff7a8969cb9d89776a2ff91e16f1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=A2=E1=84=91=E1=85=B3=E1=86=AF=20=E1=84=82?= =?UTF-8?q?=E1=85=A9=E1=84=90=E1=85=B3=E1=84=87=E1=85=AE=E1=86=A8?= Date: Mon, 15 Jul 2019 22:52:13 +0900 Subject: [PATCH] Fix: getHighestRecord -> getHighestRecordForWriting --- src/game/lib/db_service.js | 20 ++- src/web/php/db/tdd_run.php | 59 +++++++++ src/web/php/db/typing_exam_collection.php | 33 ++++- src/web/php/writing/get_highest_record.php | 7 +- src/web/php/writing/update_result_record.php | 28 +++- test/tdd.js | 129 +++++++++++++++++-- 6 files changed, 246 insertions(+), 30 deletions(-) create mode 100644 src/web/php/db/tdd_run.php diff --git a/src/game/lib/db_service.js b/src/game/lib/db_service.js index f8e7c41..6267cd6 100644 --- a/src/game/lib/db_service.js +++ b/src/game/lib/db_service.js @@ -74,6 +74,23 @@ DBService.prototype.tddSetup = function(command, onSucceededListener, onFailedLi 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 DBService.prototype.requestWritingPlayerList = function(onSucceededListener, onFailedListener) { 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( "php/writing/get_highest_record.php", (function() { // onreadystatechange @@ -172,5 +189,6 @@ DBService.prototype.getHighestRecord = function(onSucceededListener, onFailedLis xhr.send( "maestroID=" + this.maestroID + "&playerID=" + this.playerID + + "&writingID=" + writingID ); } \ No newline at end of file diff --git a/src/web/php/db/tdd_run.php b/src/web/php/db/tdd_run.php new file mode 100644 index 0000000..2ada015 --- /dev/null +++ b/src/web/php/db/tdd_run.php @@ -0,0 +1,59 @@ +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(); + +?> \ No newline at end of file diff --git a/src/web/php/db/typing_exam_collection.php b/src/web/php/db/typing_exam_collection.php index 6278971..cb66ff2 100644 --- a/src/web/php/db/typing_exam_collection.php +++ b/src/web/php/db/typing_exam_collection.php @@ -80,7 +80,7 @@ class TypingExamCollection extends DBMethodContainer SET HighestRecord = ?, RecordDateTime = NOW() WHERE TypingExamHighestRecordID = ?"; $stmt = $this->mysqli->prepare($query); - $stmt->bind_param("id", $typingExamHighestRecordID, $record); + $stmt->bind_param("di", $record, $typingExamHighestRecordID); $stmt->execute(); $stmt->close(); } @@ -105,21 +105,42 @@ class TypingExamCollection extends DBMethodContainer return $result; } - public function getHighestRecord($maestroID, $playerID) + public function getHighestRecordForWriting($maestroID, $playerID, $writingID) { $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 WHERE MaestroID = ? AND PlayerID = ?"; $stmt = $this->mysqli->prepare($query); - $stmt->bind_param("ii", $maestroID, $playerID); + $stmt->bind_param("iii", $maestroID, $playerID); $stmt->execute(); - $stmt->bind_result($writingID, $highestRecord); + $stmt->bind_result($typingExamHighestRecordID, $writingID, $highestRecord); $resultArray = array(); while($stmt->fetch()) { $rowArray = array(); - $rowArray["writingID"] = $writingID; + $rowArray["typingExamHighestRecordID"] = $typingExamHighestRecordID; + // $rowArray["writingID"] = $writingID; $rowArray["highestRecord"] = $highestRecord; array_push($resultArray, $rowArray); } diff --git a/src/web/php/writing/get_highest_record.php b/src/web/php/writing/get_highest_record.php index 1771622..21ca1f7 100644 --- a/src/web/php/writing/get_highest_record.php +++ b/src/web/php/writing/get_highest_record.php @@ -3,6 +3,7 @@ header('Content-Type: application/json'); $maestroID = $_POST["maestroID"]; $playerID = $_POST["playerID"]; +$writingID = $_POST["writingID"]; include "./../lib/json_builder.php"; include "./../lib/connect_db.php"; @@ -23,10 +24,10 @@ include "./../db/typing_exam_collection.php"; $typingExam = new TypingExamCollection($dbConnector->getMysqli()); -$highestRecordArray = $typingExam->getHighestRecord(); -$jsonBuilder->setData("highestRecordArray", $highestRecordArray); +$highestRecordData = $typingExam->getHighestRecordForWriting($maestroID, $playerID, $writingID); +$jsonBuilder->setData("highestRecordData", $highestRecordData); -if ($highestRecordArray === null) { +if ($highestRecordData === null) { $jsonBuilder->sendResultFail(); exit; } diff --git a/src/web/php/writing/update_result_record.php b/src/web/php/writing/update_result_record.php index 23a47d9..24ded11 100644 --- a/src/web/php/writing/update_result_record.php +++ b/src/web/php/writing/update_result_record.php @@ -45,16 +45,30 @@ if ($thisHourRecordData === null) { } } -$highestRecordData = $typingExam->getHighestRecord($maestroID, $playerID); -$jsonBuilder->setData("highestRecordData", $highestRecordData); -if (count($highestRecordData) === 0) { +$highestRecordArray = $typingExam->getHighestRecordForWriting($maestroID, $playerID, $writingID); +$jsonBuilder->setData("highestRecordArray", $highestRecordArray); +if (count($highestRecordArray) === 0) { $typingExam->addHighestRecord($maestroID, $playerID, $writingID, $record); $jsonBuilder->setData("highestRecord", "added"); } else { - if ($record > $highestRecordData["record"]) { - $typingExam->updateHighestRecord($highestRecordData["ID"], $record); - $jsonBuilder->setData("highestRecord", "updated"); - } + // $highestRecord = null; + // $count = count($highestRecordArray); + // 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(); diff --git a/test/tdd.js b/test/tdd.js index 1de136b..5cd1269 100644 --- a/test/tdd.js +++ b/test/tdd.js @@ -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 @@ -6,30 +85,54 @@ QUnit.test( "TypingExam", function( assert ) { dbService.setMaestroID(3); // 삼화초 dbService.setPlayerID(35); // 박지상 + var writingIDForTest = 1; + var recordForTest = 117.89; + dbService.tddSetup( "deleteTypingExamRecordAll", (function(jsonData) { // onSucceeded - console.log(jsonData); + // console.log(jsonData); }).bind(this), (function(jsonData) { // onFailed - console.log(jsonData); + // console.log(jsonData); }).bind(this) ); + // dbService.tddSetup( + // "deleteTypingExamHighestRecordAll", + // (function(jsonData) { // onSucceeded + // // console.log(jsonData); + // }).bind(this), + // (function(jsonData) { // onFailed + // // console.log(jsonData); + // }).bind(this) + // ); + dbService.addPrevHourTypingExamRecord( - 1, //writingRecord - 120.35, // record + writingIDForTest, //writingRecord + recordForTest + 10, // record (function(jsonData) { // onSucceeded - console.log(jsonData); + // console.log(jsonData); }).bind(this), (function(jsonData) { // onFailed - console.log(jsonData); + // console.log(jsonData); }).bind(this) ); dbService.updateTypingExamRecord( - 1, //writingRecord - 105.53, // record + writingIDForTest, //writingRecord + 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 console.log(jsonData); }).bind(this), @@ -38,15 +141,16 @@ QUnit.test( "TypingExam", function( assert ) { }).bind(this) ); -/* var done = assert.async(); setTimeout(function() { - dbService.getHighestRecord( + dbService.getTypingExamHighestRecord( + writingIDForTest, (function(jsonData) { // onSucceeded console.log(jsonData); - var record = jsonData["highestRecord"]; - assert.equal(record, 100, "highest record"); + var highestRecord = jsonData["highestRecordData"]; + assert.equal(highestRecord["writingID"], writingIDForTest, "highest record"); + assert.equal(highestRecord["highestRecord"], recordForTest, "highest record"); done(); }).bind(this), (function(jsonData) { // onFailed @@ -55,7 +159,6 @@ QUnit.test( "TypingExam", function( assert ) { }).bind(this) ); }); - */ });