From 4f02030c46e8c25b6ac5441d253c6e028811bc37 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: Thu, 11 Jul 2019 10:45:27 +0900 Subject: [PATCH] Fix: player_list.php reivsed --- src/web/php/lib/NA_service_db_setting.php | 10 -- src/web/php/lib/connect_db.php | 29 ++-- src/web/php/writing/player_list.php | 167 +++++----------------- test/tdd.js | 4 +- 4 files changed, 56 insertions(+), 154 deletions(-) delete mode 100644 src/web/php/lib/NA_service_db_setting.php diff --git a/src/web/php/lib/NA_service_db_setting.php b/src/web/php/lib/NA_service_db_setting.php deleted file mode 100644 index 9551f6c..0000000 --- a/src/web/php/lib/NA_service_db_setting.php +++ /dev/null @@ -1,10 +0,0 @@ - \ No newline at end of file diff --git a/src/web/php/lib/connect_db.php b/src/web/php/lib/connect_db.php index 1acf559..ea49af0 100644 --- a/src/web/php/lib/connect_db.php +++ b/src/web/php/lib/connect_db.php @@ -8,21 +8,23 @@ if ($has_file) { class DBConnector { - private $db_conn; + private $mysqli, $stmt, $query, $paramTypeList, $paramValueArray; private $db_host, $db_user, $db_passwd, $db_name; function __construct() { global $db_host, $db_user, $db_passwd, $db_name; + $this->stmt = null; + $this->paramTypeList = ""; + $this->paramValueArray = null; + if (isset($db_host) && $db_host !== null) { - // echo "db_host variable\n"; $this->db_host = $db_host; $this->db_user = $db_user; $this->db_passwd = $db_passwd; $this->db_name = $db_name; } else { - // echo "no db_host variable\n"; $this->db_host = "localhost"; $this->db_user = "chocomae"; $this->db_passwd = "sEobMPuJ2A8KTfwU"; @@ -37,27 +39,29 @@ class DBConnector public function connectDB() { // connect db - $this->db_conn = new mysqli($this->db_host, $this->db_user, $this->db_passwd, $this->db_name); - if ($this->db_conn->connect_error) { + $this->mysqli = new mysqli($this->db_host, $this->db_user, $this->db_passwd, $this->db_name); + // if ($this->mysqli->connect_error) { + if (mysqli_connect_errno()) { return false; } $query = "USE chocomae"; - $result = mysqli_query($this->db_conn, $query); - if (!$result) + $result = $this->mysqli->query($query); + if (!$result) { return false; + } return true; } public function closeDB() { - if (isset($this->db_conn) || $this->db_conn === null) { + if (isset($this->mysqli) || $this->mysqli === null) { return; } - if ($this->db_conn->connect_error) { - $this->db_conn->close(); + if ($this->mysqli->connect_error) { + $this->mysqli->close(); } } @@ -70,6 +74,11 @@ class DBConnector $variables["name"] = $this->db_name; return $variables; } + + public function getMysqli() + { + return $this->mysqli; + } } ?> \ No newline at end of file diff --git a/src/web/php/writing/player_list.php b/src/web/php/writing/player_list.php index a7d23d7..76e57ae 100644 --- a/src/web/php/writing/player_list.php +++ b/src/web/php/writing/player_list.php @@ -1,152 +1,53 @@ connectDB(); +include "./../lib/connect_db.php"; $jsonBuilder = new JsonBuilder(); -$jsonBuilder->setData("isConnected", $isConnected); -$jsonBuilder->setData("dbVariables", $dbConnector->getDBVariables()); +$dbConnector = new DBConnector(); + +$isConnected = $dbConnector->connectDB(); +if (!$isConnected) { + $jsonBuilder->setErrorMessage("DB connection : failed"); + $jsonBuilder->sendResultFail(); + exit; +} + +$writer = "system"; +$systemWritingArray = getSystemWritingList($dbConnector, $writer); + +$jsonBuilder->setData("writingArray", $systemWritingArray); $jsonBuilder->sendResultSuccess(); - -/* - -$maestro_id = $_POST["maestro_id"]; -$player_id = $_POST["player_id"]; - -include "./../lib/send_reply_json.php"; -include "./../setup/connect_db.php"; - - -$koreanAppList = get_typing_practice_app_list(11); -if($koreanAppList === null) { - set_error_code("no_app_list"); - set_error_message("앱 목록을 가져오지 못했습니다."); - send_result_fail(); - exit; -} -$englishAppList = get_typing_practice_app_list(12); -if($englishAppList === null) { - set_error_code("no_app_list"); - set_error_message("앱 목록을 가져오지 못했습니다."); - send_result_fail(); - exit; -} - -$koreanActiveAppList = get_active_typing_practice_app_list($maestro_id, 11); -$englishActiveAppList = get_active_typing_practice_app_list($maestro_id, 12); - -$koreanHighScoreList = get_high_score_list($maestro_id, $player_id, $koreanAppList); -$englishHighScoreList = get_high_score_list($maestro_id, $player_id, $englishAppList); - -set_data("KoreanAppList", $koreanAppList); -set_data("EnglishAppList", $englishAppList); -set_data("KoreanActiveAppList", $koreanActiveAppList); -set_data("EnglishActiveAppList", $englishActiveAppList); -set_data("KoreanHighScoreList", $koreanHighScoreList); -set_data("EnglishHighScoreList", $englishHighScoreList); -send_result_success(); -exit; - - - -function get_typing_practice_app_list($appType) { - global $db_conn; - +function getSystemWritingList($dbConnector, $writer) +{ $query = " - SELECT AppID, AppName, KoreanName - FROM app - WHERE AppType = ? AND Status = 1 - ORDER BY AppID ASC"; - $stmt = $db_conn->prepare($query); - $stmt->bind_param('i', $appType); + SELECT Name, Filename, LetterCount + FROM writing + WHERE Writer = ?"; + $stmt = $dbConnector->getMysqli()->prepare($query); + $stmt->bind_param("s", $writer); $stmt->execute(); - $stmt->bind_result($app_id, $app_name, $korean_name); + $stmt->bind_result($name, $filename, $letterCount); - $return_array = array(); + $resultArray = array(); while($stmt->fetch()) { - $row_array['AppID'] = $app_id; - $row_array['AppName'] = $app_name; - $row_array['KoreanName'] = $korean_name; - array_push($return_array, $row_array); + $rowArray = array(); + $rowArray["name"] = $name; + $rowArray["filename"] = $filename; + $rowArray["letterCount"] = $letterCount; + array_push($resultArray, $rowArray); } $stmt->close(); - return $return_array; + return $resultArray; } -function get_active_typing_practice_app_list($maestro_id, $appType) { - global $db_conn; - - $query = " - SELECT A.AppID, A.AppName, A.KoreanName - FROM app AS A - INNER JOIN active_app AS AA - ON A.AppID = AA.AppID AND AA.MaestroID = ? AND A.AppType = ? - ORDER BY A.AppID ASC"; - $stmt = $db_conn->prepare($query); - $stmt->bind_param('ii', $maestro_id, $appType); - $stmt->execute(); - $stmt->bind_result($app_id, $app_name, $korean_name); - - $return_array = array(); - while($stmt->fetch()) { - $row_array['AppID'] = $app_id; - $row_array['AppName'] = $app_name; - $row_array['KoreanName'] = $korean_name; - array_push($return_array, $row_array); - } - $stmt->close(); - - return $return_array; -} - -function get_high_score_list($maestro_id, $player_id, $appList) { - global $db_conn; - - $return_array = array(); - $count = count($appList); - - for($i = 0; $i < $count; $i++) { - $app_id_list = $appList[$i]['AppID']; - - $query = " - SELECT MAX(AHR.HighestRecord) - FROM app AS A - 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); - $stmt->execute(); - $stmt->bind_result($app_highest_record); - - $returnValue = $stmt->fetch(); - $stmt->close(); - - if($returnValue === NULL || $returnValue === FALSE) - continue; - - // $row_array['maestro_id'] = $maestro_id; - // $row_array['player_id'] = $player_id; - $row_array['AppID'] = $app_id_list; - if($app_highest_record === NULL) - $row_array['AppHighestRecord'] = 0; - else - $row_array['AppHighestRecord'] = $app_highest_record; - - array_push($return_array, $row_array); - } - - return $return_array; -} - -*/ - ?> \ No newline at end of file diff --git a/test/tdd.js b/test/tdd.js index 50af472..4f007cd 100644 --- a/test/tdd.js +++ b/test/tdd.js @@ -2,6 +2,8 @@ // DBConnector QUnit.test( "DBConnector", function( assert ) { + var maestroID = "choco"; + var playerID = "jisangs"; var xhr = new XMLHttpRequest(); xhr.open("POST", "./../src/web/php/writing/player_list.php", true); @@ -12,7 +14,7 @@ QUnit.test( "DBConnector", function( assert ) { console.log(replyJSON); } }; - xhr.send(); + xhr.send("maestroID=" + maestroID + "&playerID=" + playerID); assert.equal(1, 0, "DBConnector"); });