From cfb843f91c1dd2e3d46db9053f7bd0e0857eb9ba 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 09:50:08 +0900 Subject: [PATCH] Add: writing exam - save and update record (incompleted) --- src/game/lib/db_service.js | 91 ++++++++++++- src/game/typing/examination/game.js | 20 ++- src/web/php/db/tdd_setup.php | 52 ++++++++ src/web/php/db/typing_exam_collection.php | 133 +++++++++++++++++++ src/web/php/writing/get_highest_record.php | 36 +++++ src/web/php/writing/update_result_record.php | 62 +++++++++ test/tdd.js | 63 ++++++++- 7 files changed, 447 insertions(+), 10 deletions(-) create mode 100644 src/web/php/db/tdd_setup.php create mode 100644 src/web/php/db/typing_exam_collection.php create mode 100644 src/web/php/writing/get_highest_record.php create mode 100644 src/web/php/writing/update_result_record.php diff --git a/src/game/lib/db_service.js b/src/game/lib/db_service.js index c573fef..f8e7c41 100644 --- a/src/game/lib/db_service.js +++ b/src/game/lib/db_service.js @@ -6,7 +6,7 @@ function DBService() { this.maestroID = 0; this.playerID = 0; this.appName = ""; - this.dateAndTime = null; + this.dateTime = null; } DBService.prototype.getPath = function() { @@ -39,11 +39,11 @@ DBService.prototype.setAppName = function(appName) { } DBService.prototype.setDateTime = function(date, time) { - this.dateAndTime = date + time; + this.dateTime = date + time; } DBService.prototype.resetDateTime = function() { - this.dateAndTime = null; + this.dateTime = null; } @@ -56,6 +56,25 @@ DBService.prototype.makeXhr = function(filepath, onreadystatechange) { return xhr; } +// tdd setup +DBService.prototype.tddSetup = function(command, onSucceededListener, onFailedListener) { + var xhr = this.makeXhr( + "php/db/tdd_setup.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( "php/writing/player_list.php", @@ -88,4 +107,70 @@ DBService.prototype.requestWritingInfo = function(writingID, onSucceededListener }).bind(this) ); xhr.send("writingID=" + writingID); +} + +// typing exam +DBService.prototype.addPrevHourTypingExamRecord = function(writingID, record, onSucceededListener, onFailedListener) { + var xhr = this.makeXhr( + "php/writing/update_result_record.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( + "maestroID=" + this.maestroID + + "&playerID=" + this.playerID + + "&writingID=" + writingID + + "&record=" + record + + "&isPrevHourFlag=true" + ); +} + +DBService.prototype.updateTypingExamRecord = function(writingID, record, onSucceededListener, onFailedListener) { + var xhr = this.makeXhr( + "php/writing/update_result_record.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( + "maestroID=" + this.maestroID + + "&playerID=" + this.playerID + + "&writingID=" + writingID + + "&record=" + record + ); +} + +DBService.prototype.getHighestRecord = function(onSucceededListener, onFailedListener) { + var xhr = this.makeXhr( + "php/writing/get_highest_record.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( + "maestroID=" + this.maestroID + + "&playerID=" + this.playerID + ); } \ No newline at end of file diff --git a/src/game/typing/examination/game.js b/src/game/typing/examination/game.js index 0f9af3f..b6c1106 100644 --- a/src/game/typing/examination/game.js +++ b/src/game/typing/examination/game.js @@ -17,7 +17,11 @@ var TypingExamination = { - this.dbConnectManager = new DBConnectManager(); + // this.dbConnectManager = new DBConnectManager(); + this.dbService = new DBService(); + this.dbService.setMaestroID(sessionStorageManager.getMaestroID()); + this.dbService.setPlayerID(sessionStorageManager.getPlayerID()); + sessionStorageManager.setRecord(0); var self = this; @@ -304,12 +308,20 @@ var TypingExamination = { updateResultRecord: function() { if(sessionStorageManager.getMaestroAccountType() < 100) { // experience account + this.dbService.updateTypingExamRecord( + sessionStorageManager.getMaestroID(), + sessionStorageManager.getPlayerID(), + sessionStorageManager.getPlayingAppID(), + sessionStorageManager.getRecord() + ); + /* this.dbConnectManager.updateResultRecord( sessionStorageManager.getMaestroID(), sessionStorageManager.getPlayerID(), sessionStorageManager.getPlayingAppID(), sessionStorageManager.getRecord() ); + */ } }, @@ -333,11 +345,7 @@ var TypingExamination = { loadExaminationContent: function() { - var dbService = new DBService(); - dbService.setMaestroID(sessionStorageManager.getMaestroID()); - dbService.setPlayerID(sessionStorageManager.getPlayerID()); - - dbService.requestWritingInfo( + this.dbService.requestWritingInfo( sessionStorageManager.getWritingID(), (function(jsonData) { // onSucceeded this.downloadListSucceeded(jsonData); diff --git a/src/web/php/db/tdd_setup.php b/src/web/php/db/tdd_setup.php new file mode 100644 index 0000000..9d54931 --- /dev/null +++ b/src/web/php/db/tdd_setup.php @@ -0,0 +1,52 @@ +connectDB(); +if (!$isConnected) { + $jsonBuilder->setErrorMessage("DB connection : failed"); + $jsonBuilder->sendResultFail(); + exit; +} + + +const MAESTRO_ID = 3; // 삼화초 +const PLAYER_ID = 35; // 박지상 + +if ($command === "deleteWritingRecordAll") { + include "./db_method_container.php"; + include "./typing_exam_collection.php"; + + $typingExam = new TypingExamCollection($dbConnector->getMysqli()); + $typingExam->deleteTypingExamRecordAll(MAESTRO_ID, PLAYER_ID); + $typingExam->deleteTypingExamHighestRecordAll(MAESTRO_ID, PLAYER_ID); + $jsonBuilder->setData("deleteWritingRecordAll", "succeeded"); +} elseif ($command === "deleteTypingExamRecordAll") { + include "./db_method_container.php"; + include "./typing_exam_collection.php"; + + $typingExam = new TypingExamCollection($dbConnector->getMysqli()); + $typingExam->deleteTypingExamRecordAll(MAESTRO_ID, PLAYER_ID); + $jsonBuilder->setData("deleteTypingExamRecordAll", "succeeded"); +} elseif ($command === "deleteTypingExamHighestRecordAll") { + include "./db_method_container.php"; + include "./typing_exam_collection.php"; + + $typingExam = new TypingExamCollection($dbConnector->getMysqli()); + $typingExam->deleteTypingExamHighestRecordAll(MAESTRO_ID, PLAYER_ID); + $jsonBuilder->setData("deleteTypingExamHighestRecordAll", "succeeded"); +} + + +$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 new file mode 100644 index 0000000..6278971 --- /dev/null +++ b/src/web/php/db/typing_exam_collection.php @@ -0,0 +1,133 @@ +mysqli->prepare($query); + $stmt->bind_param('ii', $maestroID, $playerID); + $stmt->execute(); + $stmt->fetch(); + $stmt->close(); + } + + public function deleteTypingExamHighestRecordAll($maestroID, $playerID) { + $query = " + DELETE + FROM typing_exam_highest_record + WHERE MaestroID = ? AND PlayerID = ?"; + $stmt = $this->mysqli->prepare($query); + $stmt->bind_param('ii', $maestroID, $playerID); + $stmt->execute(); + $stmt->fetch(); + $stmt->close(); + } + + public function addPrevHourRecord($maestroID, $playerID, $writingID, $record) + { + $query = " + INSERT INTO typing_exam_record (MaestroID, PlayerID, WritingID, Record, RecordDateTime) + VALUES (?, ?, ?, ?, DATE_SUB(NOW(), INTERVAL 1 HOUR))"; + $stmt = $this->mysqli->prepare($query); + $stmt->bind_param("iiid", $maestroID, $playerID, $writingID, $record); + $stmt->execute(); + $stmt->close(); + } + // end - for TDD + + public function addRecord($maestroID, $playerID, $writingID, $record) + { + $query = " + INSERT INTO typing_exam_record (MaestroID, PlayerID, WritingID, Record, RecordDateTime) + VALUES (?, ?, ?, ?, NOW())"; + $stmt = $this->mysqli->prepare($query); + $stmt->bind_param("iiid", $maestroID, $playerID, $writingID, $record); + $stmt->execute(); + $stmt->close(); + } + + public function deleteRecord($typingExamRecordID) + { + $query = " + DELETE + FROM typing_exam_record + WHERE TypingExamRecordID = ? + "; + $stmt = $this->mysqli->prepare($query); + $stmt->bind_param('i', $typingExamRecordID); + $stmt->execute(); + $stmt->close(); + } + + public function addHighestRecord($maestroID, $playerID, $writingID, $record) + { + $query = " + INSERT INTO typing_exam_highest_record (MaestroID, PlayerID, WritingID, HighestRecord, RecordDateTime) + VALUES (?, ?, ?, ?, NOW())"; + $stmt = $this->mysqli->prepare($query); + $stmt->bind_param("iiid", $maestroID, $playerID, $writingID, $record); + $stmt->execute(); + $stmt->close(); + } + + public function updateHighestRecord($typingExamHighestRecordID, $record) + { + $query = " + UPDATE typing_exam_highest_record + SET HighestRecord = ?, RecordDateTime = NOW() + WHERE TypingExamHighestRecordID = ?"; + $stmt = $this->mysqli->prepare($query); + $stmt->bind_param("id", $typingExamHighestRecordID, $record); + $stmt->execute(); + $stmt->close(); + } + + public function getThisHourRecord($maestroID, $playerID) + { + $query = " + SELECT TypingExamRecordID, Record + FROM typing_exam_record + WHERE MaestroID = ? AND PlayerID = ? AND DATE(RecordDateTime) = DATE(NOW()) AND HOUR(RecordDateTime) = HOUR(NOW())"; + $stmt = $this->mysqli->prepare($query); + $stmt->bind_param("ii", $maestroID, $playerID); + $stmt->execute(); + $stmt->bind_result($typingExamRecordID, $record); + + $stmt->fetch(); + $result = array(); + $result["typingExamRecordID"] = $typingExamRecordID; + $result["record"] = $record; + $stmt->close(); + + return $result; + } + + public function getHighestRecord($maestroID, $playerID) + { + $query = " + SELECT WritingID, HighestRecord + FROM typing_exam_highest_record + WHERE MaestroID = ? AND PlayerID = ?"; + $stmt = $this->mysqli->prepare($query); + $stmt->bind_param("ii", $maestroID, $playerID); + $stmt->execute(); + $stmt->bind_result($writingID, $highestRecord); + + $resultArray = array(); + while($stmt->fetch()) { + $rowArray = array(); + $rowArray["writingID"] = $writingID; + $rowArray["highestRecord"] = $highestRecord; + array_push($resultArray, $rowArray); + } + $stmt->close(); + + return $resultArray; + } + +} + +?> \ No newline at end of file diff --git a/src/web/php/writing/get_highest_record.php b/src/web/php/writing/get_highest_record.php new file mode 100644 index 0000000..1771622 --- /dev/null +++ b/src/web/php/writing/get_highest_record.php @@ -0,0 +1,36 @@ +connectDB(); +if (!$isConnected) { + $jsonBuilder->setErrorMessage("DB connection : failed"); + $jsonBuilder->sendResultFail(); + exit; +} + + +include "./../db/db_method_container.php"; +include "./../db/typing_exam_collection.php"; + +$typingExam = new TypingExamCollection($dbConnector->getMysqli()); + +$highestRecordArray = $typingExam->getHighestRecord(); +$jsonBuilder->setData("highestRecordArray", $highestRecordArray); + +if ($highestRecordArray === null) { + $jsonBuilder->sendResultFail(); + exit; +} + +$jsonBuilder->sendResultSuccess(); + +?> \ No newline at end of file diff --git a/src/web/php/writing/update_result_record.php b/src/web/php/writing/update_result_record.php new file mode 100644 index 0000000..23a47d9 --- /dev/null +++ b/src/web/php/writing/update_result_record.php @@ -0,0 +1,62 @@ +connectDB(); +if (!$isConnected) { + $jsonBuilder->setErrorMessage("DB connection : failed"); + $jsonBuilder->sendResultFail(); + exit; +} + + +include "./../db/db_method_container.php"; +include "./../db/typing_exam_collection.php"; + +$typingExam = new TypingExamCollection($dbConnector->getMysqli()); + +if (isset($isPrevHourFlag)) { + $typingExam->addPrevHourRecord($maestroID, $playerID, $writingID, $record); + $jsonBuilder->setData("prevHourRecord", "added"); + $jsonBuilder->sendResultSuccess(); + exit; +} + +$thisHourRecordData = $typingExam->getThisHourRecord($maestroID, $playerID); +if ($thisHourRecordData === null) { + $typingExam->addRecord($maestroID, $playerID, $writingID, $record); + $jsonBuilder->setData("record", "added"); +} else { + if ($record > $thisHourRecordData["record"]) { + $typingExam->deleteRecord($thisHourRecordData["ID"]); + $typingExam->addRecord($maestroID, $playerID, $writingID, $record); + $jsonBuilder->setData("record", "updated"); + } +} + +$highestRecordData = $typingExam->getHighestRecord($maestroID, $playerID); +$jsonBuilder->setData("highestRecordData", $highestRecordData); +if (count($highestRecordData) === 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"); + } +} + +$jsonBuilder->sendResultSuccess(); + +?> \ No newline at end of file diff --git a/test/tdd.js b/test/tdd.js index 6803782..1de136b 100644 --- a/test/tdd.js +++ b/test/tdd.js @@ -1,3 +1,64 @@ +////////////////////////////////////////////////// +// typing exam DB + +QUnit.test( "TypingExam", function( assert ) { + var dbService = new DBService(); + dbService.setMaestroID(3); // 삼화초 + dbService.setPlayerID(35); // 박지상 + + dbService.tddSetup( + "deleteTypingExamRecordAll", + (function(jsonData) { // onSucceeded + console.log(jsonData); + }).bind(this), + (function(jsonData) { // onFailed + console.log(jsonData); + }).bind(this) + ); + + dbService.addPrevHourTypingExamRecord( + 1, //writingRecord + 120.35, // record + (function(jsonData) { // onSucceeded + console.log(jsonData); + }).bind(this), + (function(jsonData) { // onFailed + console.log(jsonData); + }).bind(this) + ); + + dbService.updateTypingExamRecord( + 1, //writingRecord + 105.53, // record + (function(jsonData) { // onSucceeded + console.log(jsonData); + }).bind(this), + (function(jsonData) { // onFailed + console.log(jsonData); + }).bind(this) + ); + +/* + var done = assert.async(); + setTimeout(function() { + dbService.getHighestRecord( + (function(jsonData) { // onSucceeded + console.log(jsonData); + + var record = jsonData["highestRecord"]; + assert.equal(record, 100, "highest record"); + done(); + }).bind(this), + (function(jsonData) { // onFailed + // console.log(jsonData); + done(); + }).bind(this) + ); + }); + */ +}); + + ////////////////////////////////////////////////// // DBConnector @@ -10,7 +71,7 @@ QUnit.test( "DBConnector", function( assert ) { setTimeout(function() { dbService.requestWritingPlayerList( (jsonData) => { // onSucceeded - console.log(jsonData); + // console.log(jsonData); var writingArray = jsonData["writingArray"]; assert.equal(writingArray[0].name, "봄2", "name");