Files
chocomae/src/game/lib/db_connect_manager.js
T

427 lines
12 KiB
JavaScript

class DBConnectManager {
constructor() {
this.path = this.getPath();
this.directoryName = this.getDirectoryName(this.path);
this.phpPath = this.getPHPPath(this.directoryName);
this.maestroID = 0;
this.playerUserID = 0;
this.appName = "";
this.dateAndTime = null;
}
setMaestroID(maestroID) {
this.maestroID = maestroID;
}
setPlayerID(userID) {
this.playerUserID = userID;
}
setAppName(appName) {
this.appName = appName;
}
setDateTime(date, time) {
this.dateAndTime = date + time;
}
resetDateTime() {
this.dateAndTime = null;
}
getPath() {
return window.location.pathname;
}
getDirectoryName(path) {
let pathAndFileNames = path.split("/");
return pathAndFileNames[pathAndFileNames.length - 2];
}
getPHPPath(directoryName) {
if(directoryName === "test") // mouse_typing/test/
return "../src/web/";
else // mousetyping/src/web/client/
return "../";
}
requestCheckUserLogin(maestroName, userName, enterCode, onSucceededListener, onFailedListener) {
let xhr = new XMLHttpRequest();
xhr.open("POST", this.phpPath + "server/user/login.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
let replyJSON = JSON.parse(xhr.responseText);
if(replyJSON != null && replyJSON["UserID"] != null)
onSucceededListener(replyJSON);
else
onFailedListener(replyJSON);
}
};
xhr.send("maestro_name=" + maestroName + "&name=" + userName + "&enter_code=" + enterCode);
}
/*
requestAppList(maestroID, onSucceededListener, onFailedListener) {
let xhr = new XMLHttpRequest();
xhr.open("POST", this.phpPath + "server/app/app_data.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
let replyJSON = JSON.parse(xhr.responseText);
if(replyJSON != null)
onSucceededListener(replyJSON);
else
onFailedListener(replyJSON);
}
};
xhr.send("maestro_id=" + maestroID);
}
requestActiveAppList(maestroID, onSucceededListener, onFailedListener) {
let xhr = new XMLHttpRequest();
xhr.open("POST", this.phpPath + "server/app/active_app.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
let replyJSON = JSON.parse(xhr.responseText);
if(replyJSON != null)
onSucceededListener(replyJSON);
else
onFailedListener(replyJSON);
}
};
xhr.send("maestro_id=" + maestroID);
}
*/
requestMenuAppList(maestroID, onSucceededListener, onFailedListener) {
let xhr = new XMLHttpRequest();
xhr.open("POST", this.phpPath + "server/app/menu_active_app_list.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
let replyJSON = JSON.parse(xhr.responseText);
if(replyJSON != null)
onSucceededListener(replyJSON);
else
onFailedListener(replyJSON);
}
};
xhr.send("maestro_id=" + maestroID);
}
requestTypingPracticeAppList(maestroID, onSucceededListener, onFailedListener) {
let xhr = new XMLHttpRequest();
xhr.open("POST", this.phpPath + "server/app/active_typing_practice_app_list.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
let replyJSON = JSON.parse(xhr.responseText);
if(replyJSON != null)
onSucceededListener(replyJSON);
else
onFailedListener(replyJSON);
}
};
xhr.send("maestro_id=" + maestroID);
}
requestPlayerHistory(maestroID, date, listener) {
let historyRecordManager = new HistoryRecordManager();
/*
if(isDebugMode()) {
this.loadTempPlayerHistory(historyRecordManager);
if(listener !== null)
listener(historyRecordManager);
return;
}
*/
let self = this;
let xhr = new XMLHttpRequest();
xhr.open("POST", this.phpPath + "server/record/history_record.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
let replyJSON = JSON.parse(xhr.responseText);
if(replyJSON != null)
listener(self.parseJSONtoHistoryRecord(replyJSON));
else
onFailedListener(replyJSON);
}
};
let params = "MaestroID=" + sessionStorageManager.maestroID
+ "&AppID=" + sessionStorageManager.playingAppID
+ "&UserID=" + sessionStorageManager.playerUserID
+ "&Date=" + date;
xhr.send(params);
}
parseJSONtoHistoryRecord(json) {
let historyRecordManager = new HistoryRecordManager();
if(typeof json === "undefined")
return historyRecordManager;
if(typeof json.history === "undefined")
return historyRecordManager;
let count = json.history.length;
for(let i = 0; i < count; i++) {
let record = new HistoryRecord(
json.history[i]["Date"],
json.history[i]["AppName"],
json.history[i]["HighScore"]
);
historyRecordManager.push(record);
}
return historyRecordManager;
/*
var startIndex = 0;
var endIndex = jsonRankingList.length;
var recordArray = [];
for(var i = startIndex; i < endIndex; i++) {
var bestRecordRow = [];
var strDate = jsonRankingList[i]['Date'];
var dateArray = strDate.split("-");
var recordDate = new Date(dateArray[0], dateArray[1], dateArray[2]);
bestRecordRow[0] = recordDate.getMonth() + "월 " + recordDate.getDate() + "일";
bestRecordRow[1] = Math.floor(jsonRankingList[i]['BestRecord']);
bestRecordRow[2] = getStageNameForKorean(jsonRankingList[i]['StageName']);
// console.log("$BestRecordRow : " + bestRecordRow[0] + ", " + bestRecordRow[1] + ", " + bestRecordRow[2]);
recordArray.push(bestRecordRow);
}
return recordArray;
*/
}
requestRanking(type, maestroID, date, time, listener) {
let rankingRecordManager = new RankingRecordManager();
/*
if(isDebugMode()) {
this.loadTempRanking(rankingRecordManager);
if(listener !== null)
listener(rankingRecordManager);
return;
}
*/
let self = this;
let xhr = new XMLHttpRequest();
xhr.open("POST", this.getRankingRecordUrl(type), true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
let replyJSON = JSON.parse(xhr.responseText);
if(replyJSON != null) {
listener(type, self.parseJSONtoRankingRecord(type, replyJSON));
}
else
onFailedListener(replyJSON);
}
};
let params = "MaestroID=" + sessionStorageManager.maestroID
+ "&AppID=" + sessionStorageManager.playingAppID
+ "&Date=" + date
+ "&Time=" + time;
xhr.send(params);
}
getRankingRecordUrl(type) {
let rankingRecordURL = this.phpPath;
switch(type) {
case DBConnectManager.TYPE_MY_RANKING_HOUR:
case DBConnectManager.TYPE_ALL_RANKING_HOUR:
rankingRecordURL += "server/record/ranking_record_hour.php";
break;
case DBConnectManager.TYPE_MY_RANKING_DAY:
case DBConnectManager.TYPE_ALL_RANKING_DAY:
rankingRecordURL += "server/record/ranking_record_day.php";
break;
case DBConnectManager.TYPE_MY_RANKING_MONTH:
case DBConnectManager.TYPE_ALL_RANKING_MONTH:
rankingRecordURL += "server/record/ranking_record_month.php";
break;
}
return rankingRecordURL;
}
parseJSONtoRankingRecord(type, json) {
let rankingRecordManager = new RankingRecordManager();
if(typeof json === "undefined")
return rankingRecordManager;
let replyJSON = null;
switch(type) {
case DBConnectManager.TYPE_MY_RANKING_HOUR:
case DBConnectManager.TYPE_ALL_RANKING_HOUR:
replyJSON = json.rankingHour;
break;
case DBConnectManager.TYPE_MY_RANKING_DAY:
case DBConnectManager.TYPE_ALL_RANKING_DAY:
replyJSON = json.rankingDay;
break;
case DBConnectManager.TYPE_MY_RANKING_MONTH:
case DBConnectManager.TYPE_ALL_RANKING_MONTH:
replyJSON = json.rankingMonth;
break;
}
if(replyJSON === null)
return rankingRecordManager;
let count = replyJSON.length;
for(let i = 0; i < count; i++) {
let record = new RankingRecord(
i + 1,
replyJSON[i]["UserID"],
replyJSON[i]["Name"],
replyJSON[i]["HighScore"]
);
rankingRecordManager.push(record);
}
return rankingRecordManager;
}
onFailed(errorMessage) {
console.log("failed : " + errorMessage);
}
makeXHRWithParam(url, param, onSucceededListener, onFailedListener) {
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
let replyJSON = JSON.parse(xhr.responseText);
if(replyJSON != null && replyJSON["UserID"] != null)
onSucceededListener(replyJSON);
else
onFailedListener(replyJSON);
}
}
return xhr;
}
requestHowToPlay(appID, onSucceededListener, onFailedListener) {
let xhr = new XMLHttpRequest();
xhr.open("POST", this.phpPath + "server/app/how_to_play.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
let replyJSON = JSON.parse(xhr.responseText);
if(replyJSON != null && replyJSON["HowToPlay"] != null)
onSucceededListener(replyJSON);
else
onFailedListener(replyJSON);
}
};
xhr.send("AppID=" + appID);
}
updateTodayBestRecord(maestroID, userID, appID, record) {
let xhr = new XMLHttpRequest();
xhr.open("POST", this.phpPath + "server/record/update_best_record.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(
"MaestroID=" + maestroID
+ "&UserID=" + userID
+ "&AppID=" + appID
+ "&BestRecord=" + record
);
}
requestTodayBestRecord(maestroID, userID, appID, onSucceededListener, onFailedListener) {
let xhr = new XMLHttpRequest();
xhr.open("POST", this.phpPath + "server/record/request_best_record.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
let replyJSON = JSON.parse(xhr.responseText);
if(replyJSON != null && replyJSON["BestRecord"] != null)
onSucceededListener(replyJSON);
else
onFailedListener(replyJSON);
}
};
xhr.send(
"MaestroID=" + maestroID
+ "&UserID=" + userID
+ "&AppID=" + appID
);
}
loadTempPlayerHistory(historyRecordManager) {
historyRecordManager.push(new HistoryRecord("05/14", "space_invaders", 14460));
historyRecordManager.push(new HistoryRecord("05/13", "space_invaders", 12860));
historyRecordManager.push(new HistoryRecord("05/12", "typing_english_basic", 15460));
historyRecordManager.push(new HistoryRecord("05/11", "typing_korean_basic", 13040));
historyRecordManager.push(new HistoryRecord("05/10", "space_invaders", 15460));
historyRecordManager.push(new HistoryRecord("05/09", "typing_english_basic", 14607));
historyRecordManager.push(new HistoryRecord("05/08", "typing_korean_basic", 13058));
}
loadTempRanking(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));
}
}
DBConnectManager.TYPE_MY_RANKING_HOUR = 0;
DBConnectManager.TYPE_MY_RANKING_DAY = 1;
DBConnectManager.TYPE_MY_RANKING_MONTH = 2;
DBConnectManager.TYPE_ALL_RANKING_HOUR = 3;
DBConnectManager.TYPE_ALL_RANKING_DAY = 4;
DBConnectManager.TYPE_ALL_RANKING_MONTH = 5;