Add: php server files for timer

This commit is contained in:
2018-12-13 00:36:48 +09:00
parent edbd4743af
commit 537b0c1db1
10 changed files with 374 additions and 88 deletions
+43
View File
@@ -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));
+2
View File
@@ -1,4 +1,6 @@
function Chart() {
this.dbConnectManager = new DBConnectManager();
this.chartGraphics = game.add.graphics(0, 0);
this.printChartTitle();
this.printChartBaseLine();
+1 -1
View File
@@ -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() {
+7 -15
View File
@@ -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);
}
@@ -301,14 +304,3 @@ 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;
}
*/
+2 -2
View File
@@ -80,7 +80,7 @@ var Loading = {
// return;
// }
// this.state.start('Login');
this.state.start('LicenseTimer');
this.state.start('Login');
// this.state.start('LicenseTimer');
},
}
+134 -68
View File
@@ -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.dbConnectManager.requestLicenseTimeData(
sessionStorageManager.getMaestroID(),
sessionStorageManager.getPlayerID(),
Timer.TIME_DEFAULT_SEC,
(function(replyJson) {
console.log(replyJson);
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();
// if( there is saved time record) {
// this.isTimerGoingOn = false;
// this.setTimerTextColor(0xffffff);
// }
// else
this.setTimerTextColor(Timer.TEXT_COLOR_CYAN);
})
);
}
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); })
);
},
@@ -0,0 +1,78 @@
<?php
header("Content-Type: application/json");
$maestro_id = $_POST["MaestroID"];
$player_id = $_POST["PlayerID"];
$default_start_time = $_POST["DefaultStartTime"];
// echo $maestro_id." / ".$player_id;
// if($default_start_time === null)
// $default_start_time = 60 * 60; // 60 min * 60 sec
include "./../lib/send_reply_json.php";
include "./../setup/connect_db.php";
$start_time = $default_start_time;
$left_time = null;
$saved_date_time = null;
// get license_time data
$time_data = get_time_data($maestro_id, $player_id);
// set_data("timeData", $time_data);
// if license_time data is not exist
// then make new license_time data
if($time_data["StartTime"] == null) {
insert_default_time_data($maestro_id, $player_id, $default_start_time);
$start_time = $default_start_time;
} else {
$start_time = $time_data["StartTime"];
$left_time = $time_data["LeftTime"];
$saved_date_time = $time_data["SavedDateTime"];
}
// send license_time data back to client
set_data("startTime", $start_time);
set_data("leftTime", $left_time);
set_data("savedDateTime", $saved_date_time);
send_result_success();
exit;
function get_time_data($maestro_id, $player_id) {
global $db_conn;
$query = "
SELECT StartTime, LeftTime, SavedDateTime
FROM license_time
WHERE MaestroID = ? AND PlayerID = ?
";
$stmt = $db_conn->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();
}
?>
@@ -0,0 +1,49 @@
<?php
header('Content-Type: application/json');
$maestro_id = $_POST["MaestroID"];
$player_id = $_POST["PlayerID"];
$left_time = $_POST["LeftTime"];
// echo $maestro_id." / ".$player_id;
include "./../lib/send_reply_json.php";
include "./../setup/connect_db.php";
if($left_time == 0)
reset_left_time($maestro_id, $player_id);
else
update_left_time($maestro_id, $player_id, $left_time);
send_result_success();
exit;
function reset_left_time($maestro_id, $player_id) {
global $db_conn;
$query = "
UPDATE license_time
SET LeftTime = null, SavedDateTime = null
WHERE MaestroID = ? AND PlayerID = ?
";
$stmt = $db_conn->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();
}
?>
@@ -0,0 +1,33 @@
<?php
header('Content-Type: application/json');
$maestro_id = $_POST["MaestroID"];
$player_id = $_POST["PlayerID"];
$start_time = $_POST["StartTime"];
// echo $maestro_id." / ".$player_id;
include "./../lib/send_reply_json.php";
include "./../setup/connect_db.php";
update_start_time($maestro_id, $player_id, $start_time);
send_result_success();
exit;
function update_start_time($maestro_id, $player_id, $start_time) {
global $db_conn;
$query = "
UPDATE license_time
SET StartTime = ?
WHERE MaestroID = ? AND PlayerID = ?
";
$stmt = $db_conn->prepare($query);
$stmt->bind_param('iii', $start_time, $maestro_id, $player_id);
$stmt->execute();
}
?>
+23
View File
@@ -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)
);