diff --git a/src/game/lib/db_connect_manager.js b/src/game/lib/db_connect_manager.js index 58b95b9..46172fa 100644 --- a/src/game/lib/db_connect_manager.js +++ b/src/game/lib/db_connect_manager.js @@ -352,6 +352,39 @@ class DBConnectManager { 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) { diff --git a/src/game/result/result.js b/src/game/result/result.js index 0658a74..a874099 100644 --- a/src/game/result/result.js +++ b/src/game/result/result.js @@ -8,7 +8,7 @@ class Result { } create() { - this.historyRecordManager = new HistoryRecordManager(); + this.dbConnectManager = new DBConnectManager(); this.game.stage.backgroundColor = '#4d4d4d'; this.chartGraphics = game.add.graphics(100, game.world.height - 120); @@ -24,6 +24,8 @@ class Result { // contents this.printRecord(); + this.uploadRecord(); + this.makeStartButton(); let recordBoard = new RecordBoard(); @@ -63,7 +65,15 @@ class Result { // .setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2); bestRecordText.stroke = "#333"; bestRecordText.strokeThickness = 5; + } + uploadRecord() { + this.dbConnectManager.updateTodayBestRecord( + sessionStorageManager.maestroID, + sessionStorageManager.playerUserID, + sessionStorageManager.playingAppID, + sessionStorageManager.record + ); } makeStartButton() { diff --git a/src/web/server/record/request_best_record.php b/src/web/server/record/request_best_record.php new file mode 100644 index 0000000..fa2ac40 --- /dev/null +++ b/src/web/server/record/request_best_record.php @@ -0,0 +1,43 @@ +close(); + exit; +} + +echo json_encode($replyJSON, JSON_UNESCAPED_UNICODE); +$db_conn->close(); + + +function get_best_record($maestro_id, $app_id, $user_id) { + global $db_conn; + + $query = " + SELECT BestRecord + FROM moty_best_record + WHERE MaestroID = ? AND AppID = ? AND UserID = ? AND DATE(RecordDateTime) = Date(NOW()) + "; + $stmt = $db_conn->prepare($query); + $stmt->bind_param("iii", $maestro_id, $app_id, $user_id); + $stmt->execute(); + $stmt->bind_result($best_record); + $stmt->fetch(); + $stmt->close(); + + return $best_record; +} + +?> \ No newline at end of file diff --git a/src/web/server/record/stage_insert_record.php b/src/web/server/record/stage_insert_record.php deleted file mode 100644 index f726573..0000000 --- a/src/web/server/record/stage_insert_record.php +++ /dev/null @@ -1,32 +0,0 @@ -prepare($query); -$stmt->bind_param('ids', $user_id, $record, $stage_name); -$stmt->execute(); - -$db_conn->close(); - -?> \ No newline at end of file diff --git a/src/web/server/record/update_best_record.php b/src/web/server/record/update_best_record.php new file mode 100644 index 0000000..d313fd1 --- /dev/null +++ b/src/web/server/record/update_best_record.php @@ -0,0 +1,82 @@ + $prev_best_record) { + echo "update"; + update_best_record($prev_best_record_id, $best_record); + } +} + +$db_conn->close(); + + +function get_best_record($maestro_id, $app_id, $user_id) { + global $db_conn; + global $prev_best_record_id, $prev_best_record; + + $query = " + SELECT BestRecordID, BestRecord + FROM moty_best_record + WHERE MaestroID = ? AND AppID = ? AND UserID = ? AND DATE(RecordDateTime) = DATE(NOW()) + "; + $stmt = $db_conn->prepare($query); + $stmt->bind_param("iii", $maestro_id, $app_id, $user_id); + $stmt->execute(); + $stmt->bind_result($prev_best_record_id, $prev_best_record); + $stmt->fetch(); + $stmt->close(); + + $GLOBALS["prev_best_record_id"] = $prev_best_record_id; + $GLOBALS["prev_best_record"] = $prev_best_record; + + return $prev_best_record_id; +} + + +function insert_best_record($maestro_id, $app_id, $user_id, $best_record) { + global $db_conn; + + $query = " + INSERT INTO moty_best_record (MaestroID, UserID, AppID, BestRecord, RecordDateTime) + VALUES (?, ?, ?, ?, NOW()); + "; + $stmt = $db_conn->prepare($query); + $stmt->bind_param('iiii', $maestro_id, $user_id, $app_id, $best_record); + $stmt->execute(); +} + +function update_best_record($prev_best_record_id, $best_record) { + global $db_conn; + + $query = " + UPDATE moty_best_record + SET BestRecord = ?, RecordDateTime = now() + WHERE BestRecordID = ? AND DATE(RecordDateTime) = DATE(NOW()) + "; + $stmt = $db_conn->prepare($query); + $stmt->bind_param('ii', $best_record, $prev_best_record_id); + $stmt->execute(); +} + +?> \ No newline at end of file diff --git a/test/test_db_connect_manager.js b/test/test_db_connect_manager.js index f7587e3..0ded091 100644 --- a/test/test_db_connect_manager.js +++ b/test/test_db_connect_manager.js @@ -131,11 +131,12 @@ QUnit.test( "HowToPlay", function( assert ) { let howToPlayForSpaceInvaders = "외계인이 나타났다!\\n마우스 왼쪽 버튼으로\\n외계인을 클릭해서 물리쳐 주세요."; let done = assert.async(2); + setTimeout(function() { dbConnectManager.requestHowToPlay( 101, // space_invaders app ID (jsonData) => { - howToPlay = jsonData["HowToPlay"]; + let howToPlay = jsonData["HowToPlay"]; assert.equal(howToPlay, howToPlayForSpaceInvaders); done(); }, @@ -148,7 +149,7 @@ QUnit.test( "HowToPlay", function( assert ) { dbConnectManager.requestHowToPlay( 1, // korean basic typing (jsonData) => { - howToPlay = jsonData["HowToPlay"]; + let howToPlay = jsonData["HowToPlay"]; assert.equal(howToPlay, ""); done(); }, @@ -160,3 +161,50 @@ QUnit.test( "HowToPlay", function( assert ) { }); }); + +QUnit.test( "UpdateRecord", function( assert ) { + let todayBestRecord = 0; + + let done = assert.async(); + + setTimeout(function() { + dbConnectManager.requestTodayBestRecord( + 1, // maestro ID for "jisangs" + 1, // user ID for jisangs + 101, // space_invaders app ID + (jsonData) => { + todayBestRecord = jsonData["BestRecord"]; + }, + (jsonData) => { + console.log(jsonData); + todayBestRecord = 0; + } + ); + }); + + setTimeout(function() { + dbConnectManager.updateTodayBestRecord( + 1, // maestro ID for "jisangs" + 1, // user ID for jisangs + 101, // space_invaders app ID + todayBestRecord + 1000 // new record + ); + }, 100); + + setTimeout(function() { + dbConnectManager.requestTodayBestRecord( + 1, // maestro ID for "jisangs" + 1, // user ID for jisangs + 101, // space_invaders app ID + (jsonData) => { + let bestRecord = jsonData["BestRecord"]; + assert.equal(bestRecord, todayBestRecord + 1000); + done(); + }, + (jsonData) => { + console.log(jsonData); + done(); + } + ); + }, 200); +});