From 537b0c1db10baaa6f2cdcb8642a2c2032b13249f 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, 13 Dec 2018 00:36:48 +0900 Subject: [PATCH] Add: php server files for timer --- src/game/lib/db_connect_manager.js | 43 ++++ src/game/license_timer/chart.js | 2 + src/game/license_timer/game.js | 2 +- src/game/license_timer/input_score.js | 24 +-- src/game/license_timer/loading.js | 4 +- src/game/license_timer/timer.js | 204 ++++++++++++------ .../license_timer/get_license_time_data.php | 78 +++++++ .../update_license_left_time.php | 49 +++++ .../update_license_start_time.php | 33 +++ src/web/sql/make_db_license_timer.sql | 23 ++ 10 files changed, 374 insertions(+), 88 deletions(-) create mode 100644 src/web/server/license_timer/get_license_time_data.php create mode 100644 src/web/server/license_timer/update_license_left_time.php create mode 100644 src/web/server/license_timer/update_license_start_time.php create mode 100644 src/web/sql/make_db_license_timer.sql diff --git a/src/game/lib/db_connect_manager.js b/src/game/lib/db_connect_manager.js index 2f865dc..1fb4275 100644 --- a/src/game/lib/db_connect_manager.js +++ b/src/game/lib/db_connect_manager.js @@ -420,6 +420,49 @@ DBConnectManager.prototype.requestLicenseTimeData = function(maestroID, playerID ); } +DBConnectManager.prototype.updateLicenseStartTime = function(maestroID, playerID, startTime, onSucceededListener, onFailedListener) { + var xhr = new XMLHttpRequest(); + xhr.open("POST", this.phpPath + "server/license_timer/update_license_start_time.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); + + if(replyJSON != null) + onSucceededListener(replyJSON); + else + onFailedListener(replyJSON); + } + }; + xhr.send( + "MaestroID=" + maestroID + + "&PlayerID=" + playerID + + "&StartTime=" + startTime + ); +} + +DBConnectManager.prototype.updateLicenseLeftTime = function(maestroID, playerID, leftTime, onSucceededListener, onFailedListener) { + var xhr = new XMLHttpRequest(); + xhr.open("POST", this.phpPath + "server/license_timer/update_license_left_time.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); + + if(replyJSON != null) + onSucceededListener(replyJSON); + else + onFailedListener(replyJSON); + } + }; + xhr.send( + "MaestroID=" + maestroID + + "&PlayerID=" + playerID + + "&LeftTime=" + leftTime + ); +} + + DBConnectManager.prototype.loadTempPlayerHistory = function(historyRecordManager) { historyRecordManager.push(new HistoryRecord("05/14", "space_invaders", 14460)); diff --git a/src/game/license_timer/chart.js b/src/game/license_timer/chart.js index a9a0b00..aab8601 100644 --- a/src/game/license_timer/chart.js +++ b/src/game/license_timer/chart.js @@ -1,4 +1,6 @@ function Chart() { + this.dbConnectManager = new DBConnectManager(); + this.chartGraphics = game.add.graphics(0, 0); this.printChartTitle(); this.printChartBaseLine(); diff --git a/src/game/license_timer/game.js b/src/game/license_timer/game.js index 2d52df8..b6e6c07 100644 --- a/src/game/license_timer/game.js +++ b/src/game/license_timer/game.js @@ -27,7 +27,7 @@ var LicenseTimer = { this.chart = new Chart(); this.chart.printContents(); - this.inputScore = new InputScore(this.chart); + this.inputScore = new InputScore(this.timer, this.chart); }, back: function() { diff --git a/src/game/license_timer/input_score.js b/src/game/license_timer/input_score.js index b8bf745..f6388d7 100644 --- a/src/game/license_timer/input_score.js +++ b/src/game/license_timer/input_score.js @@ -1,4 +1,5 @@ -function InputScore(chart) { +function InputScore(timer, chart) { + this.timer = timer; this.chart = chart; this.licenseDataManager = new LicenseDataManager(); @@ -64,7 +65,6 @@ function InputScore(chart) { (function() { this.onClickSendToServerButton(); }).bind(this) ); - // this.loadLicenseCompanySubjectData(); this.makeCompanyPanel(); this.makeSubjectPanel(); this.setVisibleCompanyPanel(false); @@ -195,8 +195,8 @@ InputScore.prototype.makeSubjectPanel = function() { setting.width = InputScore.SUBJECT_BUTTON_WIDTH; var subjectData = this.licenseDataManager.getSubjectDataByIndex(i); - var subjectData = subjectData.companyName + " " + subjectData.subjectName; - this.licenseSubjectButtonList[i] = new RoundRectButton(setting, null, subjectData, null); + var subjectName = subjectData.companyName + " " + subjectData.subjectName; + this.licenseSubjectButtonList[i] = new RoundRectButton(setting, null, subjectName, null); } } @@ -265,6 +265,9 @@ InputScore.prototype.setVisibleSubjectPanel = function(flag, companyName) { InputScore.prototype.onClickCompanySubjectButton = function(subjectFullname) { this.setSubjectFullname(subjectFullname); this.selectedSubjectData = this.licenseDataManager.getSubjectDataByName(subjectFullname); + // console.log(this.selectedSubjectData); + + this.timer.setStartTime(this.selectedSubjectData.timeLimit * Timer.TIME_SECONDS_FOR_MINUTE); } @@ -300,15 +303,4 @@ InputScore.SUBJECT_BUTTON_WIDTH = 260; InputScore.BUTTON_GAP_WIDTH = 10; InputScore.BUTTON_GAP_HEIGHT = 64; -InputScore.SUBJECT_BUTTON_MAX_COUNT = 6; - - -/* -function LicenseCompanySubjectData(companyName, subjectName, timeLimit, maxScore, gradeList) { - this.companyName = companyName; - this.subjectName = subjectName; - this.timeLimit = timeLimit; - this.maxScore = maxScore; - this.gradeList = gradeList; -} -*/ \ No newline at end of file +InputScore.SUBJECT_BUTTON_MAX_COUNT = 6; \ No newline at end of file diff --git a/src/game/license_timer/loading.js b/src/game/license_timer/loading.js index 98b007f..04fd55b 100644 --- a/src/game/license_timer/loading.js +++ b/src/game/license_timer/loading.js @@ -80,7 +80,7 @@ var Loading = { // return; // } - // this.state.start('Login'); - this.state.start('LicenseTimer'); + this.state.start('Login'); + // this.state.start('LicenseTimer'); }, } diff --git a/src/game/license_timer/timer.js b/src/game/license_timer/timer.js index 21af141..617c8a0 100644 --- a/src/game/license_timer/timer.js +++ b/src/game/license_timer/timer.js @@ -1,6 +1,8 @@ function Timer() { this.initVariables(); + this.dbConnectManager = new DBConnectManager(); + // bar var bar = game.add.graphics(); bar.beginFill(0x202020); @@ -24,8 +26,8 @@ Timer.prototype.initVariables = function() { this.isTimerGoingOn = false; this.isTimeOver = false; - this.timeStart = 0 - this.timeLeft = 0; + this.startTime = 0 + this.leftTime = 0; this.timerEvent = null; this.prevTime = null; @@ -44,16 +46,36 @@ Timer.prototype.initVariables = function() { } Timer.prototype.loadDataFromServer = function() { - this.timeStart = Timer.TIME_DEFAULT_SEC; - this.timeLeft = Timer.TIME_DEFAULT_SEC; - this.updateTimeText(); + this.dbConnectManager.requestLicenseTimeData( + sessionStorageManager.getMaestroID(), + sessionStorageManager.getPlayerID(), + Timer.TIME_DEFAULT_SEC, + (function(replyJson) { + console.log(replyJson); - // if( there is saved time record) { - // this.isTimerGoingOn = false; - // this.setTimerTextColor(0xffffff); - // } - // else - this.setTimerTextColor(Timer.TEXT_COLOR_CYAN); + if(replyJson.leftTime == null || replyJson.leftTime == 0) { + this.startTime = replyJson.startTime; + this.leftTime = replyJson.timeStart; + this.resetTimer(); + return; + } + + // this.setTimerTextColor(Timer.TEXT_COLOR_CYAN); + // this.updateTimeText(); + this.startTime = replyJson.startTime; + this.leftTime = replyJson.leftTime; + this.pauseTimer(); + }).bind(this), + + (function(replyJson) { + console.log(replyJson); + + this.startTime = Timer.TIME_DEFAULT_SEC; + this.leftTime = this.startTime; + this.updateTimeText(); + + }) + ); } Timer.prototype.makeTimer = function() { @@ -129,7 +151,10 @@ Timer.prototype.makeResetButton = function() { return new RoundRectButton( setting, null, "C", - (function() { this.resetTimer(); }).bind(this) + (function() { + this.resetTimer(); + this.resetLeftTimeToServer(); + }).bind(this) ); } @@ -205,7 +230,10 @@ Timer.prototype.makePauseButton = function() { return new RoundRectButton( setting, null, "∥", - (function() { this.pauseTimer(); }).bind(this) + (function() { + this.updateLeftTimeToServer(); + this.pauseTimer(); + }).bind(this) ); } @@ -252,16 +280,16 @@ Timer.prototype.setTimerTextColor = function(color) { Timer.prototype.startTimer = function() { this.isTimerGoingOn = true; - if(this.timeLeft < 0) + if(this.leftTime < 0) this.setTimerTextColor(Timer.TEXT_COLOR_RED); else this.setTimerTextColor(Timer.TEXT_COLOR_YELLOW); this.updateTimeText(); - ShowNotifyTimeOver(this.timeLeft * Timer.TIME_MILLISECONDS); + ShowNotifyTimeOver(this.leftTime * Timer.TIME_MILLISECONDS); this.prevTime = this.getNowTime(); - this.timerEvent = game.time.events.loop(1000, this.tickTimeLeft, this); + this.timerEvent = game.time.events.loop(1000, this.tickLeftTime, this); this.showButton(this.playButton, false); this.showButton(this.pauseButton, true); @@ -281,12 +309,14 @@ Timer.prototype.pauseTimer = function() { this.showButton(this.playButton, true); this.showButton(this.pauseButton, false); this.showTimerButtons(); + + // this.updateLeftTimeToServer(); } Timer.prototype.resetTimer = function() { this.isTimeOver = false; - this.timeLeft = this.timeStart; + this.leftTime = this.startTime; this.pauseTimer(); this.isTimerGoingOn = false; @@ -295,37 +325,32 @@ Timer.prototype.resetTimer = function() { // this.loadDataFromServer(); } -Timer.prototype.tickTimeLeft = function() { - this.timeLeft--; +Timer.prototype.tickLeftTime = function() { + this.leftTime--; var outFocusedTime = this.getOutFocusedTime(); if(outFocusedTime > 2) { - this.timeLeft -= outFocusedTime - 1; + this.leftTime -= outFocusedTime - 1; - if(this.timeLeft > 0) { + if(this.leftTime > 0) { CancelNotifyTimeOver(); - ShowNotifyTimeOver(this.timeLeft * Timer.TIME_MILLISECONDS); + ShowNotifyTimeOver(this.leftTime * Timer.TIME_MILLISECONDS); } } this.updateTimeText(); - if(!this.isTimeOver && this.timeLeft < 0) + if(!this.isTimeOver && this.leftTime < 0) this.timeOver(); } Timer.prototype.timeOver = function() { - this.isTimeOver = true; - - this.setTimerTextColor(Timer.TEXT_COLOR_RED); - console.log("time over"); - // xhr = new XMLHttpRequest(); - // xhr.open('POST', 'delete_last_timestamp.php', true); - // xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); - // var param = 'UserID=' + playerUserID; - // xhr.send(param); + this.isTimeOver = true; + this.setTimerTextColor(Timer.TEXT_COLOR_RED); + + this.resetLeftTimeToServer(); } Timer.prototype.getNowTime = function() { @@ -443,15 +468,18 @@ Timer.prototype.timePlus = function(timePlusSecond, type) { } // console.log(timeAmount); - this.timeLeft += timeAmount; + this.leftTime += timeAmount; var maxTime = 99 * Timer.TIME_MINUTES_FOR_HOUR * Timer.TIME_SECONDS_FOR_MINUTE * Timer.TIME_SECONDS_FOR_MINUTE; - if(this.timeLeft > maxTime) - this.timeLeft = maxTime; + if(this.leftTime > maxTime) + this.leftTime = maxTime; this.updateTimeText(); - if(!this.isTimerGoingOn) - this.sendStartTimeToServer(this.timeLeft); - this.sendTimeLeftToServer(); + if(!this.isTimerGoingOn) { + this.startTime = this.leftTime; + this.updateStartTimeToServer(); + } else { + this.updateLeftTimeToServer(); + } } Timer.prototype.timeMinus = function(timeMinusSecond, type) { @@ -467,37 +495,45 @@ Timer.prototype.timeMinus = function(timeMinusSecond, type) { } // console.log(timeAmount); - this.timeLeft -= timeAmount; - if(this.timeLeft < 0) - this.timeLeft = 0; + this.leftTime -= timeAmount; + if(this.leftTime < 0) + this.leftTime = 0; this.updateTimeText(); - if(!this.isTimerGoingOn) - this.sendStartTimeToServer(this.timeLeft); - this.sendTimeLeftToServer(); + if(!this.isTimerGoingOn) { + this.startTime = this.leftTime; + this.updateStartTimeToServer(); + } else { + this.updateLeftTimeToServer(); + } } -Timer.prototype.setTime =function(sec) { - // console.log("time up : " + min); - - this.timeLeft += sec; +Timer.prototype.setTime =function(timeSecond) { + this.leftTime = timeSecond; this.updateTimeText(); + + if(!this.isTimerGoingOn) { + this.startTime = this.leftTime; + this.updateStartTimeToServer(); + } else { + this.updateLeftTimeToServer(); + } } Timer.prototype.updateTimeText = function() { var secondsForHour = Timer.TIME_MINUTES_FOR_HOUR * Timer.TIME_SECONDS_FOR_MINUTE; - var timeLeftHour = parseInt(this.timeLeft / secondsForHour); - var timeLeftMinute = parseInt((this.timeLeft % secondsForHour) / Timer.TIME_SECONDS_FOR_MINUTE); - var timeLeftSecond = this.timeLeft % Timer.TIME_SECONDS_FOR_MINUTE; + var leftTimeHour = parseInt(this.leftTime / secondsForHour); + var leftTimeMinute = parseInt((this.leftTime % secondsForHour) / Timer.TIME_SECONDS_FOR_MINUTE); + var leftTimeSecond = this.leftTime % Timer.TIME_SECONDS_FOR_MINUTE; - this.timeHourText.text = this.getTimeWithTimeFormat(timeLeftHour, Timer.TYPE_HOUR); - // console.log(this.timeLeft); + this.timeHourText.text = this.getTimeWithTimeFormat(leftTimeHour, Timer.TYPE_HOUR); + // console.log(this.leftTime); // console.log(this.timeHourText.text); - this.timeMinuteText.text = this.getTimeWithTimeFormat(timeLeftMinute, Timer.TYPE_MINUTE); - this.timeSecondText.text = this.getTimeWithTimeFormat(timeLeftSecond, Timer.TYPE_SECOND); + this.timeMinuteText.text = this.getTimeWithTimeFormat(leftTimeMinute, Timer.TYPE_MINUTE); + this.timeSecondText.text = this.getTimeWithTimeFormat(leftTimeSecond, Timer.TYPE_SECOND); // this.updateTimeVariables(); // this.timerText.text = this.getTimeWithTimeFormat(); } @@ -518,7 +554,7 @@ Timer.prototype.getTimeWithTimeFormat = function(timeValue, type) { return this.getTwoDigitNumber(timeValue); } - if(this.timeLeft < 0) { + if(this.leftTime < 0) { return "-" + this.getTwoDigitNumber(timeValue); } else { return this.getTwoDigitNumber(timeValue); @@ -526,25 +562,55 @@ Timer.prototype.getTimeWithTimeFormat = function(timeValue, type) { } +Timer.prototype.setStartTime = function(time) { + this.startTime = time; -Timer.prototype.sendStartTimeToServer = function(time) { - this.timeStart = time; + if(!this.isTimerGoingOn) { + this.leftTime = this.startTime; + this.updateTimeText(); + } - // send to server - console.log("send to server : " + this.timeStart); + this.updateStartTimeToServer(); } -Timer.prototype.sendTimeLeftToServer = function() { - return; - // console.log(this.timeLeft); - // console.log(playerUserID); - xhr = new XMLHttpRequest(); - xhr.open('POST', 'make_last_timestamp.php', true); - xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); - var param = 'UserID=' + playerUserID + '&Time=' + this.timeLeft; - xhr.send(param); + +Timer.prototype.updateStartTimeToServer = function() { + // send to server + console.log("send start time to server : " + this.startTime); + + this.dbConnectManager.updateLicenseStartTime( + sessionStorageManager.getMaestroID(), + sessionStorageManager.getPlayerID(), + this.startTime, + (function(replyJson) { console.log(replyJson); }).bind(this), + (function(replyJson) { console.log(replyJson); }) + ); +} + +Timer.prototype.resetLeftTimeToServer = function() { + console.log("send left time to server : " + this.leftTime); + + this.dbConnectManager.updateLicenseLeftTime( + sessionStorageManager.getMaestroID(), + sessionStorageManager.getPlayerID(), + 0, + (function(replyJson) { console.log(replyJson); }).bind(this), + (function(replyJson) { console.log(replyJson); }) + ); +}, + +Timer.prototype.updateLeftTimeToServer = function() { + console.log("send left time to server : " + this.leftTime); + + this.dbConnectManager.updateLicenseLeftTime( + sessionStorageManager.getMaestroID(), + sessionStorageManager.getPlayerID(), + this.leftTime, + (function(replyJson) { console.log(replyJson); }).bind(this), + (function(replyJson) { console.log(replyJson); }) + ); }, diff --git a/src/web/server/license_timer/get_license_time_data.php b/src/web/server/license_timer/get_license_time_data.php new file mode 100644 index 0000000..423daa6 --- /dev/null +++ b/src/web/server/license_timer/get_license_time_data.php @@ -0,0 +1,78 @@ +prepare($query); + $stmt->bind_param("ii", $maestro_id, $player_id); + $stmt->execute(); + $stmt->bind_result($start_time, $left_time, $saved_date_time); + $stmt->fetch(); + $stmt->close(); + + $license_time["StartTime"] = $start_time; + $license_time["LeftTime"] = $left_time; + $license_time["SavedDateTime"] = $saved_date_time; + + return $license_time; +} + +function insert_default_time_data($maestro_id, $player_id, $default_start_time) { + global $db_conn; + + $query = " + INSERT INTO license_time (MaestroID, PlayerID, StartTime) + VALUES (?, ?, ?); + "; + $stmt = $db_conn->prepare($query); + $stmt->bind_param("iii", $maestro_id, $player_id, $default_start_time); + $stmt->execute(); +} + +?> \ No newline at end of file diff --git a/src/web/server/license_timer/update_license_left_time.php b/src/web/server/license_timer/update_license_left_time.php new file mode 100644 index 0000000..5e3afb6 --- /dev/null +++ b/src/web/server/license_timer/update_license_left_time.php @@ -0,0 +1,49 @@ +prepare($query); + $stmt->bind_param('ii', $maestro_id, $player_id); + $stmt->execute(); +} + +function update_left_time($maestro_id, $player_id, $left_time) { + global $db_conn; + + $query = " + UPDATE license_time + SET LeftTime = ?, SavedDateTime = NOW() + WHERE MaestroID = ? AND PlayerID = ? + "; + $stmt = $db_conn->prepare($query); + $stmt->bind_param('iii', $left_time, $maestro_id, $player_id); + $stmt->execute(); +} + +?> \ No newline at end of file diff --git a/src/web/server/license_timer/update_license_start_time.php b/src/web/server/license_timer/update_license_start_time.php new file mode 100644 index 0000000..416ad17 --- /dev/null +++ b/src/web/server/license_timer/update_license_start_time.php @@ -0,0 +1,33 @@ +prepare($query); + $stmt->bind_param('iii', $start_time, $maestro_id, $player_id); + $stmt->execute(); +} + +?> \ No newline at end of file diff --git a/src/web/sql/make_db_license_timer.sql b/src/web/sql/make_db_license_timer.sql new file mode 100644 index 0000000..b516090 --- /dev/null +++ b/src/web/sql/make_db_license_timer.sql @@ -0,0 +1,23 @@ +CREATE TABLE license_time ( + LicenseTimeID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, + MaestroID INT UNSIGNED NOT NULL, + PlayerID INT UNSIGNED NOT NULL, + StartTime INT UNSIGNED NOT NULL, + LeftTime INT NOT NULL, + SavedDateTime DATETIME NOT NULL, + + FOREIGN KEY (MaestroID) REFERENCES maestro(MaestroID), + FOREIGN KEY (PlayerID) REFERENCES player(PlayerID) +); + +CREATE TABLE license_score ( + LicenseScoreID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, + MaestroID INT UNSIGNED NOT NULL, + PlayerID INT UNSIGNED NOT NULL, + SubjectName CHAR(50) NOT NULL, + Score INT UNSIGNED NOT NULL, + ScoreDateTime DATETIME NOT NULL, + + FOREIGN KEY (MaestroID) REFERENCES maestro(MaestroID), + FOREIGN KEY (PlayerID) REFERENCES player(PlayerID) +); \ No newline at end of file