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.dateTime = 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.dateTime = date + time; } DBService.prototype.resetDateTime = function() { this.dateTime = 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; } // 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); } 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( "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); } DBService.prototype.requestWritingInfo = function(writingID, onSucceededListener, onFailedListener) { var xhr = this.makeXhr( "php/writing/writing_info.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("writingID=" + writingID); } // typing exam DBService.prototype.addPrevHourTypingExamRecord = function(writingID, record, onSucceededListener, onFailedListener) { var xhr = this.makeXhr( "php/record/update_typing_exam_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/record/update_typing_exam_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.getTypingExamHighestRecord = function(writingID, onSucceededListener, onFailedListener) { var xhr = this.makeXhr( "php/record/get_typing_exam_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 + "&writingID=" + writingID ); } DBService.prototype.requestTypingExamPlayerHistory = function(writingID, date, onSucceededListener, onFailedListener) { /* var historyRecordManager = new HistoryRecordManager(); this.loadTempPlayerHistory(historyRecordManager); if(onSucceededListener !== null) onSucceededListener(historyRecordManager); return; */ var xhr = this.makeXhr( "php/record/get_typing_exam_history_record.php", (function() { // onreadystatechange if(xhr.readyState == 4 && xhr.status == 200) { var replyJSON = JSON.parse(xhr.responseText); if(replyJSON != null) onSucceededListener(this.parseJSONtoHistoryRecord(replyJSON)); else onFailedListener(replyJSON); } }).bind(this) ); xhr.send( "maestroID=" + this.maestroID + "&playerID=" + this.playerID + "&writingID=" + writingID + "&date=" + date ); } DBService.prototype.parseJSONtoHistoryRecord = function(jsonData) { var historyRecordManager = new HistoryRecordManager(); if(typeof jsonData === "undefined") return historyRecordManager; if(typeof jsonData.history === "undefined") return historyRecordManager; var count = jsonData.history.length; for(var i = 0; i < count; i++) { var record = new HistoryRecord( jsonData.history[i]["Date"], jsonData.history[i]["AppName"], jsonData.history[i]["HighScore"] ); historyRecordManager.push(record); } return historyRecordManager; } DBService.prototype.loadTempPlayerHistory = function(historyRecordManager) { // historyRecordManager.push(new HistoryRecord("05/14", "space_invaders", 14460)); historyRecordManager.push(new HistoryRecord("2019-05-13 00:00:00", "space_invaders", 14460)); historyRecordManager.push(new HistoryRecord("2019-05-12 00:00:00", "space_invaders", 12860)); historyRecordManager.push(new HistoryRecord("2019-05-11 00:00:00", "typing_english_basic", 15460)); historyRecordManager.push(new HistoryRecord("2019-05-10 00:00:00", "typing_korean_basic", 13040)); historyRecordManager.push(new HistoryRecord("2019-05-09 00:00:00", "space_invaders", 15460)); historyRecordManager.push(new HistoryRecord("2019-05-08 00:00:00", "typing_english_basic", 14607)); historyRecordManager.push(new HistoryRecord("2019-05-07 00:00:00", "typing_korean_basic", 13058)); } DBService.prototype.loadTempRanking = function(rankingRecordManager) { rankingRecordManager.push(new RankingRecord(1, 7, "강경모", 4400)); rankingRecordManager.push(new RankingRecord(2, 2, "강성태", 3400)); rankingRecordManager.push(new RankingRecord(3, 3, "김기덕", 2400)); rankingRecordManager.push(new RankingRecord(4, 4, "김남희", 1400)); rankingRecordManager.push(new RankingRecord(5, 5, "고봉순", 900)); rankingRecordManager.push(new RankingRecord(6, 6, "파르마", 800)); rankingRecordManager.push(new RankingRecord(7, 1, "박지상", 700)); rankingRecordManager.push(new RankingRecord(8, 8, "신현주", 600)); rankingRecordManager.push(new RankingRecord(9, 9, "꼬봉이", 500)); rankingRecordManager.push(new RankingRecord(10, 10, "거북이", 400)); rankingRecordManager.push(new RankingRecord(11, 11, "다람쥐", 300)); rankingRecordManager.push(new RankingRecord(12, 12, "사시미", 200)); rankingRecordManager.push(new RankingRecord(13, 13, "태권도", 100)); rankingRecordManager.push(new RankingRecord(14, 14, "합기도", 40)); rankingRecordManager.push(new RankingRecord(15, 15, "우습지", 10)); rankingRecordManager.push(new RankingRecord(16, 16, "하하하", 4)); } DBService.prototype.requestTypingExamRanking = function(type, writingID, date, time, onSucceededListener, onFailedListener) { /* var rankingRecordManager = new RankingRecordManager(); this.loadTempRanking(rankingRecordManager); if(listener !== null) listener(rankingRecordManager); return; } */ var xhr = this.makeXhr( this.getRankingRecordUrl(type), (function() { // onreadystatechange if(xhr.readyState == 4 && xhr.status == 200) { var replyJSON = JSON.parse(xhr.responseText); if(replyJSON != null && replyJSON.result == "success") onSucceededListener(type, this.parseJSONtoRankingRecord(type, replyJSON)); else onFailedListener(replyJSON); } }).bind(this) ); xhr.send( "maestroID=" + this.maestroID + "&writingID=" + writingID + "&date=" + date + "&time=" + time ); } DBService.prototype.getRankingRecordUrl = function(type) { var rankingRecordURL = this.webPath + "web/php/record/"; switch(type) { case DBConnectManager.TYPE_MY_RANKING_HOUR: case DBConnectManager.TYPE_ALL_RANKING_HOUR: rankingRecordURL += "get_typing_exam_ranking_record_hour.php"; break; case DBConnectManager.TYPE_MY_RANKING_DAY: case DBConnectManager.TYPE_ALL_RANKING_DAY: rankingRecordURL += "get_typing_exam_ranking_record_day.php"; break; case DBConnectManager.TYPE_MY_RANKING_MONTH: case DBConnectManager.TYPE_ALL_RANKING_MONTH: rankingRecordURL += "get_typing_exam_ranking_record_month.php"; break; } return rankingRecordURL; } DBService.prototype.parseJSONtoRankingRecord = function(type, jsonData) { var rankingRecordManager = new RankingRecordManager(); if(typeof jsonData === "undefined") return rankingRecordManager; var replyJSON = null; switch(type) { case DBConnectManager.TYPE_MY_RANKING_HOUR: case DBConnectManager.TYPE_ALL_RANKING_HOUR: replyJSON = jsonData.rankingHour; break; case DBConnectManager.TYPE_MY_RANKING_DAY: case DBConnectManager.TYPE_ALL_RANKING_DAY: replyJSON = jsonData.rankingDay; break; case DBConnectManager.TYPE_MY_RANKING_MONTH: case DBConnectManager.TYPE_ALL_RANKING_MONTH: replyJSON = jsonData.rankingMonth; break; } if(replyJSON === null) return rankingRecordManager; var count = replyJSON.length; for(var i = 0; i < count; i++) { var record = new RankingRecord( i + 1, replyJSON[i]["PlayerID"], replyJSON[i]["Name"], replyJSON[i]["HighScore"] ); rankingRecordManager.push(record); } return rankingRecordManager; } // menu DBService.prototype.requestMainMenuAppData = function(appGroup, language, onSucceededListener, onFailedListener) { var xhr = this.makeXhr( "php/menu/menu_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 + "&appGroup=" + appGroup + "&language=" + language ); }