From e21722526f5d46a3bbc405e147bf3d7095052b4e 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 11:51:53 +0900 Subject: [PATCH] Add: DBService.js for client --- src/game/lib/db_service.js | 74 +++++++++++++++++++ ...collection.php => db_method_container.php} | 2 +- src/web/php/db/writing_collection.php | 2 +- src/web/php/writing/player_list.php | 2 +- test/tdd.html | 1 + test/tdd.js | 32 ++++---- 6 files changed, 97 insertions(+), 16 deletions(-) create mode 100644 src/game/lib/db_service.js rename src/web/php/db/{db_method_collection.php => db_method_container.php} (83%) diff --git a/src/game/lib/db_service.js b/src/game/lib/db_service.js new file mode 100644 index 0000000..bf941ca --- /dev/null +++ b/src/game/lib/db_service.js @@ -0,0 +1,74 @@ +function DBService() { + this.path = this.getPath(); + this.directoryName = this.getDirectoryName(this.path); + this.webPath = this.getWebPath(this.directoryName); + + this.maestroID = 0; + this.playerID = 0; + this.appName = ""; + this.dateAndTime = null; +} + +DBService.prototype.getPath = function() { + return window.location.pathname; +} + +DBService.prototype.getDirectoryName = function(path) { + var pathAndFileNames = path.split("/"); + return pathAndFileNames[pathAndFileNames.length - 2]; +} + +DBService.prototype.getWebPath = function(directoryName) { + if(directoryName === "test") // mouse_typing/test/ + return "../src/web/"; + else // mousetyping/src/web/client/ + return "../"; +} + + +DBService.prototype.setMaestroID = function(maestroID) { + this.maestroID = maestroID; +} + +DBService.prototype.setPlayerID = function(playerID) { + this.playerID = playerID; +} + +DBService.prototype.setAppName = function(appName) { + this.appName = appName; +} + +DBService.prototype.setDateTime = function(date, time) { + this.dateAndTime = date + time; +} + +DBService.prototype.resetDateTime = function() { + this.dateAndTime = null; +} + + + +DBService.prototype.makeXhr = function(filepath, onreadystatechange) { + var xhr = new XMLHttpRequest(); + xhr.open("POST", this.webPath + filepath, true); + xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); + xhr.onreadystatechange = onreadystatechange; + return xhr; +} + +DBService.prototype.requestWritingPlayerList = function(onSucceededListener, onFailedListener) { + var xhr = this.makeXhr( + "php/writing/player_list.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/web/php/db/db_method_collection.php b/src/web/php/db/db_method_container.php similarity index 83% rename from src/web/php/db/db_method_collection.php rename to src/web/php/db/db_method_container.php index 0bd05e7..0d6ff9f 100644 --- a/src/web/php/db/db_method_collection.php +++ b/src/web/php/db/db_method_container.php @@ -1,5 +1,5 @@ getMysqli()); diff --git a/test/tdd.html b/test/tdd.html index 9a0d8d4..83fdfe5 100644 --- a/test/tdd.html +++ b/test/tdd.html @@ -35,6 +35,7 @@ + diff --git a/test/tdd.js b/test/tdd.js index 4f007cd..7e1225a 100644 --- a/test/tdd.js +++ b/test/tdd.js @@ -2,21 +2,27 @@ // DBConnector QUnit.test( "DBConnector", function( assert ) { - var maestroID = "choco"; - var playerID = "jisangs"; + var dbService = new DBService(); + dbService.setMaestroID(3); // 삼화초 + dbService.setPlayerID(35); // 박지상 - var xhr = new XMLHttpRequest(); - xhr.open("POST", "./../src/web/php/writing/player_list.php", true); - xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); - xhr.onreadystatechange = function() { - if(xhr.readyState == 4 && xhr.status == 200) { - var replyJSON = JSON.parse(xhr.responseText); - console.log(replyJSON); - } - }; - xhr.send("maestroID=" + maestroID + "&playerID=" + playerID); + var done = assert.async(); + setTimeout(function() { + dbService.requestWritingPlayerList( + (jsonData) => { // onSucceeded + console.log(jsonData); - assert.equal(1, 0, "DBConnector"); + var writingArray = jsonData["writingArray"]; + assert.equal(writingArray[0].name, "봄 - 윤동주", "name"); + assert.equal(writingArray[3].name, "허생전 - 박지원", "name"); + done(); + }, + (jsonData) => { // onFailed + // console.log(jsonData); + done(); + } + ); + }); });