Add: DBService.js for client
This commit is contained in:
@@ -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);
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
class DBMethodCollection
|
class DBMethodContainer
|
||||||
{
|
{
|
||||||
protected $mysqli;
|
protected $mysqli;
|
||||||
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
class WritingCollection extends DBMethodCollection
|
class WritingCollection extends DBMethodContainer
|
||||||
{
|
{
|
||||||
|
|
||||||
public function getSystemWritingList()
|
public function getSystemWritingList()
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ if (!$isConnected) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
include "./../db/db_method_collection.php";
|
include "./../db/db_method_container.php";
|
||||||
include "./../db/writing_collection.php";
|
include "./../db/writing_collection.php";
|
||||||
|
|
||||||
$writingCollection = new WritingCollection($dbConnector->getMysqli());
|
$writingCollection = new WritingCollection($dbConnector->getMysqli());
|
||||||
|
|||||||
@@ -35,6 +35,7 @@
|
|||||||
<script src="../src/game/typing/lib/keyboard.js"></script>
|
<script src="../src/game/typing/lib/keyboard.js"></script>
|
||||||
<script src="../src/game/lib/history_record_manager.js"></script>
|
<script src="../src/game/lib/history_record_manager.js"></script>
|
||||||
<script src="../src/game/lib/heart_manager.js"></script>
|
<script src="../src/game/lib/heart_manager.js"></script>
|
||||||
|
<script src="../src/game/lib/db_service.js"></script>
|
||||||
<script src="../src/game/lib/db_connect_manager.js"></script>
|
<script src="../src/game/lib/db_connect_manager.js"></script>
|
||||||
<script src="tdd.js"></script>
|
<script src="tdd.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
+19
-13
@@ -2,21 +2,27 @@
|
|||||||
// DBConnector
|
// DBConnector
|
||||||
|
|
||||||
QUnit.test( "DBConnector", function( assert ) {
|
QUnit.test( "DBConnector", function( assert ) {
|
||||||
var maestroID = "choco";
|
var dbService = new DBService();
|
||||||
var playerID = "jisangs";
|
dbService.setMaestroID(3); // 삼화초
|
||||||
|
dbService.setPlayerID(35); // 박지상
|
||||||
|
|
||||||
var xhr = new XMLHttpRequest();
|
var done = assert.async();
|
||||||
xhr.open("POST", "./../src/web/php/writing/player_list.php", true);
|
setTimeout(function() {
|
||||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
dbService.requestWritingPlayerList(
|
||||||
xhr.onreadystatechange = function() {
|
(jsonData) => { // onSucceeded
|
||||||
if(xhr.readyState == 4 && xhr.status == 200) {
|
console.log(jsonData);
|
||||||
var replyJSON = JSON.parse(xhr.responseText);
|
|
||||||
console.log(replyJSON);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
xhr.send("maestroID=" + maestroID + "&playerID=" + playerID);
|
|
||||||
|
|
||||||
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();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user