From 84637c0ad061d7c69420dca5a97708b4b1d494e6 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Fri, 10 Apr 2020 23:35:15 +0200 Subject: Working challenge interaction! --- .gitignore | 1 + harbour-sailchess.pro.user | 799 ----------------------------------- qml/Functions.qml | 102 ++++- qml/pages/Board.qml | 12 +- qml/pages/Login.qml | 3 +- qml/resources/css/external.css | 2 +- translations/harbour-sailchess-de.ts | 18 +- translations/harbour-sailchess.ts | 18 +- 8 files changed, 132 insertions(+), 823 deletions(-) create mode 100644 .gitignore delete mode 100644 harbour-sailchess.pro.user diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8a9d35c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.user diff --git a/harbour-sailchess.pro.user b/harbour-sailchess.pro.user deleted file mode 100644 index 42ac1ca..0000000 --- a/harbour-sailchess.pro.user +++ /dev/null @@ -1,799 +0,0 @@ - - - - - - EnvironmentId - {165e634c-1493-4e4a-a669-d4ec7547e75a} - - - ProjectExplorer.Project.ActiveTarget - 1 - - - ProjectExplorer.Project.EditorSettings - - true - false - true - - Cpp - - CppGlobal - - - - QmlJS - - QmlJSGlobal - - - 2 - UTF-8 - false - 4 - false - 80 - true - true - 1 - true - false - 0 - true - true - 0 - 8 - true - 1 - true - true - true - false - - - - ProjectExplorer.Project.PluginSettings - - - - ProjectExplorer.Project.Target.0 - - SailfishOS-3.2.1.20-armv7hl (in Sailfish OS Build Engine) - SailfishOS-3.2.1.20-armv7hl (in Sailfish OS Build Engine) - SailfishOS-3.2.1.20-armv7hl - 0 - 1 - 0 - - /home/melvin/code/build-harbour-sailchess-SailfishOS_3_2_1_20_armv7hl_in_Sailfish_OS_Build_Engine-Debug - - - true - Start Build Engine - - Mer.MerSdkStartStep - - - true - qmake - - QtProjectManager.QMakeBuildStep - true - - false - false - false - - - true - Make - - Qt4ProjectManager.MakeStep - - false - - - false - - 3 - Build - - ProjectExplorer.BuildSteps.Build - - - - true - Start Build Engine - - Mer.MerSdkStartStep - - - true - Make - - Qt4ProjectManager.MakeStep - - true - clean - - false - - 2 - Clean - - ProjectExplorer.BuildSteps.Clean - - 2 - false - - Debug - Debug - Qt4ProjectManager.Qt4BuildConfiguration - 2 - true - - - /home/melvin/code/build-harbour-sailchess-SailfishOS_3_2_1_20_armv7hl_in_Sailfish_OS_Build_Engine-Release - - - true - Start Build Engine - - Mer.MerSdkStartStep - - - true - qmake - - QtProjectManager.QMakeBuildStep - false - - false - false - false - - - true - Make - - Qt4ProjectManager.MakeStep - - false - - - false - - 3 - Build - - ProjectExplorer.BuildSteps.Build - - - - true - Start Build Engine - - Mer.MerSdkStartStep - - - true - Make - - Qt4ProjectManager.MakeStep - - true - clean - - false - - 2 - Clean - - ProjectExplorer.BuildSteps.Clean - - 2 - false - - Release - Release - Qt4ProjectManager.Qt4BuildConfiguration - 0 - true - - - /home/melvin/code/build-harbour-sailchess-SailfishOS_3_2_1_20_armv7hl_in_Sailfish_OS_Build_Engine-Profile - - - true - Start Build Engine - - Mer.MerSdkStartStep - - - true - qmake - - QtProjectManager.QMakeBuildStep - true - - false - true - false - - - true - Make - - Qt4ProjectManager.MakeStep - - false - - - false - - 3 - Build - - ProjectExplorer.BuildSteps.Build - - - - true - Start Build Engine - - Mer.MerSdkStartStep - - - true - Make - - Qt4ProjectManager.MakeStep - - true - clean - - false - - 2 - Clean - - ProjectExplorer.BuildSteps.Clean - - 2 - false - - Profile - Profile - Qt4ProjectManager.Qt4BuildConfiguration - 0 - true - - 3 - - - - true - RPM - - QmakeProjectManager.MerRpmBuildStep - - - true - RPM Validation - - QmakeProjectManager.MerRpmValidationStep - - 2 - Deploy - - ProjectExplorer.BuildSteps.Deploy - - 1 - Build RPM Package For Manual Deployment - - QmakeProjectManager.MerMb2RpmBuildConfiguration - - - - - true - Prepare Target - - QmakeProjectManager.MerPrepareTargetStep - - - true - Rsync - - QmakeProjectManager.MerRsyncDeployStep - - 2 - Deploy - - ProjectExplorer.BuildSteps.Deploy - - 1 - Deploy By Copying Binaries - - QmakeProjectManager.MerRSyncDeployConfiguration - - - - - true - Prepare Target - - QmakeProjectManager.MerPrepareTargetStep - - - true - RPM - - QmakeProjectManager.MerRpmDeployStep - - 2 - Deploy - - ProjectExplorer.BuildSteps.Deploy - - 1 - Deploy As RPM Package - - QmakeProjectManager.MerRpmDeployConfiguration - - 3 - - - dwarf - - cpu-cycles - - - 250 - -F - true - 4096 - false - false - 1000 - - true - - false - false - false - false - true - 0.01 - 10 - true - kcachegrind - 1 - 25 - - 1 - true - false - true - valgrind - - 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12 - 13 - 14 - - /home/melvin/code/harbour-sailchess - false - -1 - 3 - - 1 - - - harbour-sailchess (on Xperia X) - QmakeProjectManager.MerRunConfiguration:/home/melvin/code/harbour-sailchess/harbour-sailchess.pro - 1 - - false - - 3768 - false - true - false - false - true - - - - 1 - - - - ProjectExplorer.Project.Target.1 - - SailfishOS-3.2.1.20-i486 (in Sailfish OS Build Engine) - SailfishOS-3.2.1.20-i486 (in Sailfish OS Build Engine) - SailfishOS-3.2.1.20-i486 - 0 - 2 - 0 - - /home/melvin/code/build-harbour-sailchess-SailfishOS_3_2_1_20_i486_in_Sailfish_OS_Build_Engine-Debug - - - true - Start Build Engine - - Mer.MerSdkStartStep - - - true - qmake - - QtProjectManager.QMakeBuildStep - true - - false - false - false - - - true - Make - - Qt4ProjectManager.MakeStep - - false - - - false - - 3 - Build - - ProjectExplorer.BuildSteps.Build - - - - true - Start Build Engine - - Mer.MerSdkStartStep - - - true - Make - - Qt4ProjectManager.MakeStep - - true - clean - - false - - 2 - Clean - - ProjectExplorer.BuildSteps.Clean - - 2 - false - - Debug - Debug - Qt4ProjectManager.Qt4BuildConfiguration - 2 - true - - - /home/melvin/code/build-harbour-sailchess-SailfishOS_3_2_1_20_i486_in_Sailfish_OS_Build_Engine-Release - - - true - Start Build Engine - - Mer.MerSdkStartStep - - - true - qmake - - QtProjectManager.QMakeBuildStep - false - - false - false - false - - - true - Make - - Qt4ProjectManager.MakeStep - - false - - - false - - 3 - Build - - ProjectExplorer.BuildSteps.Build - - - - true - Start Build Engine - - Mer.MerSdkStartStep - - - true - Make - - Qt4ProjectManager.MakeStep - - true - clean - - false - - 2 - Clean - - ProjectExplorer.BuildSteps.Clean - - 2 - false - - Release - Release - Qt4ProjectManager.Qt4BuildConfiguration - 0 - true - - - /home/melvin/code/build-harbour-sailchess-SailfishOS_3_2_1_20_i486_in_Sailfish_OS_Build_Engine-Profile - - - true - Start Build Engine - - Mer.MerSdkStartStep - - - true - qmake - - QtProjectManager.QMakeBuildStep - true - - false - true - false - - - true - Make - - Qt4ProjectManager.MakeStep - - false - - - false - - 3 - Build - - ProjectExplorer.BuildSteps.Build - - - - true - Start Build Engine - - Mer.MerSdkStartStep - - - true - Make - - Qt4ProjectManager.MakeStep - - true - clean - - false - - 2 - Clean - - ProjectExplorer.BuildSteps.Clean - - 2 - false - - Profile - Profile - Qt4ProjectManager.Qt4BuildConfiguration - 0 - true - - 3 - - - - true - RPM - - QmakeProjectManager.MerRpmBuildStep - - - true - RPM Validation - - QmakeProjectManager.MerRpmValidationStep - - 2 - Deploy - - ProjectExplorer.BuildSteps.Deploy - - 1 - Build RPM Package For Manual Deployment - - QmakeProjectManager.MerMb2RpmBuildConfiguration - - - - - true - Prepare Target - - QmakeProjectManager.MerPrepareTargetStep - - - true - Rsync - - QmakeProjectManager.MerRsyncDeployStep - - 2 - Deploy - - ProjectExplorer.BuildSteps.Deploy - - 1 - Deploy By Copying Binaries - - QmakeProjectManager.MerRSyncDeployConfiguration - - - - - true - Prepare Target - - QmakeProjectManager.MerPrepareTargetStep - - - true - RPM - - QmakeProjectManager.MerRpmDeployStep - - 2 - Deploy - - ProjectExplorer.BuildSteps.Deploy - - 1 - Deploy As RPM Package - - QmakeProjectManager.MerRpmDeployConfiguration - - 3 - - - dwarf - - cpu-cycles - - - 250 - -F - true - 4096 - false - false - 1000 - - true - - false - false - false - false - true - 0.01 - 10 - true - kcachegrind - 1 - 25 - - 1 - true - false - true - valgrind - - 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12 - 13 - 14 - - /home/melvin/code/harbour-sailchess - false - -1 - 3 - - 1 - - - harbour-sailchess (on Sailfish OS Emulator 3.2.1.20) - QmakeProjectManager.MerRunConfiguration:/home/melvin/code/harbour-sailchess/harbour-sailchess.pro - 1 - - false - - 3768 - false - true - false - false - true - - - - 1 - - - - ProjectExplorer.Project.TargetCount - 2 - - - ProjectExplorer.Project.Updater.FileVersion - 21 - - - Version - 21 - - diff --git a/qml/Functions.qml b/qml/Functions.qml index 970788b..7e1c576 100644 --- a/qml/Functions.qml +++ b/qml/Functions.qml @@ -24,9 +24,15 @@ Item { board.itemAt(60).piece = [board.itemAt(59).piece, board.itemAt(59).piece = board.itemAt(60).piece][0]; } + function clear() { + for (var i = 0; i < 64; i++) { + board.itemAt(i).piece = ""; + } + } + property var selected: [] function select(i) { - if (selected.length < 2) { + if (selected.length < 2 && game_id !== "") { if (selected.indexOf(i) === -1) selected.push(i) else selected.splice(selected.indexOf(i), 1) @@ -39,22 +45,41 @@ Item { } } - function convert(i) { + function convert_index(i) { const first = (i % 8) + 'a'.charCodeAt(0); const second = (7 - parseInt(i / 8)) + '1'.charCodeAt(0); return String.fromCharCode(first, second); } + function convert_movement(movement) { + const a = movement[0].charCodeAt(0) - 'a'.charCodeAt(0); + const b = 8 - parseInt(movement[1]) + const c = movement[2].charCodeAt(0) - 'a'.charCodeAt(0); + const d = 8 - parseInt(movement[3]) + return [b * 8 + a, d * 8 + c]; + } + + function move_piece(from, to) { + if (board.itemAt(from).piece !== "") { + board.itemAt(to).piece = board.itemAt(from).piece; + board.itemAt(from).piece = ""; + } + } + function move(from, to) { - console.log(convert(from) + "-" + convert(to)); - board.itemAt(to).piece = board.itemAt(from).piece; - board.itemAt(from).piece = ""; + console.log(convert_index(from) + "-" + convert_index(to)); + post("board/game/" + game_id + "/move/" + convert_index(from) + convert_index(to), "", function (response) { + console.log(JSON.stringify(response)); + if (response["ok"]) + move_piece(from, to); + }) selected = []; } // END LOGIC property var game_id: "" + property var moves: "" function event_stream() { var xhr = new XMLHttpRequest(); @@ -74,6 +99,7 @@ Item { information.text = qsTr("Game started!"); fill(); game_id = data["game"]["id"]; + game_stream(); } } catch (Exception) {} } @@ -82,6 +108,49 @@ Item { xhr.send(); } + property var game_xhr; + function game_stream() { + game_xhr = new XMLHttpRequest(); + game_xhr.open("GET", "https://lichess.org/api/board/game/stream/" + game_id); + game_xhr.seenBytes = 0; + game_xhr.setRequestHeader("Authorization", "Bearer " + access_token.value); + + game_xhr.onreadystatechange = function() { + if (game_xhr.readyState === 3) { + try { + const new_data = game_xhr.response.substr(game_xhr.seenBytes); + game_xhr.seenBytes = game_xhr.responseText.length; + + console.log(new_data); + const data = JSON.parse(new_data); + var all_moves; + if (data["type"] === "gameFull") { + all_moves = data["state"]["moves"]; + } else if (data["type"] === "gameState") { + all_moves = data["moves"]; + } + + console.log(moves); + console.log(all_moves); + + const new_moves = all_moves.slice(moves.length) + moves += new_moves; + + console.log(moves); + + new_moves.split(" ").forEach(function(move) { + if (move !== "") { + const arr = convert_movement(move); + move_piece(arr[0], arr[1]); + } + }); + } catch (Exception) {} + } + }; + + game_xhr.send(); + } + function post(path, params, callback) { var xhr = new XMLHttpRequest(); xhr.open("POST", "https://lichess.org/api/" + path); @@ -89,19 +158,32 @@ Item { xhr.setRequestHeader("Authorization", "Bearer " + access_token.value); xhr.onreadystatechange = function() { - if (xhr.readyState === 4 && xhr.status === 200) { + if (xhr.readyState === 4) { callback(JSON.parse(xhr.responseText)); - } else if (xhr.readyState === 4) { - console.error(xhr.responseText); } } xhr.send(params); } + function abort() { + post("board/game/" + game_id + "/abort", "", function (response) { + if (response["ok"]) { + information.text = qsTr("Please start a game"); + game_id = ""; + moves = ""; + clear(); + game_xhr.abort(); + console.log(JSON.stringify(response)); + } + }) + } + function challenge(username) { post("challenge/" + username, "rated=false&clock.limit=10800&clock.increment=60&days=14&color=white", function (response) { - information.text = qsTr("Challenging ") + response["challenge"]["destUser"]["name"]; - console.log(JSON.stringify(response)); + if (!response["error"]) { + information.text = qsTr("Waiting for ") + response["challenge"]["destUser"]["name"]; + console.log(JSON.stringify(response)); + } }); } diff --git a/qml/pages/Board.qml b/qml/pages/Board.qml index 799f24c..bbf41c3 100644 --- a/qml/pages/Board.qml +++ b/qml/pages/Board.qml @@ -17,7 +17,7 @@ Page { PullDownMenu { MenuItem { - text: qsTr("Login") + text: qsTr("Refresh login") onClicked: { access_token.value = ""; refresh_token.value = ""; @@ -26,11 +26,18 @@ Page { } MenuItem { text: qsTr("Random player") + visible: functions.game_id === "" ? true : false onClicked: functions.start_seek() } MenuItem { text: qsTr("Play against bot") - onClicked: functions.challenge("GodelEscherBot") + visible: functions.game_id === "" ? true : false + onClicked: functions.challenge("GarboBot") + } + MenuItem { + text: qsTr("Abort game") + visible: functions.game_id === "" ? false : true + onClicked: functions.abort() } } @@ -49,6 +56,7 @@ Page { id: information anchors.horizontalCenter: parent.horizontalCenter anchors.bottom: grid.top + bottomPadding: Theme.paddingMedium text: qsTr("Please start a game"); } diff --git a/qml/pages/Login.qml b/qml/pages/Login.qml index 2f57e39..e493f2f 100644 --- a/qml/pages/Login.qml +++ b/qml/pages/Login.qml @@ -9,8 +9,9 @@ Page { id: login anchors.fill: parent - experimental.preferences.javascriptEnabled: false + experimental.preferences.javascriptEnabled: true experimental.userStyleSheets: [Qt.resolvedUrl("qrc:///css/external.css")] + // experimental.preferences.privateBrowsingEnabled: true experimental.customLayoutWidth: parent.width / device_ratio url: "https://marvinborner.de/lichess/" diff --git a/qml/resources/css/external.css b/qml/resources/css/external.css index b1281e1..b1bff76 100644 --- a/qml/resources/css/external.css +++ b/qml/resources/css/external.css @@ -1,3 +1,3 @@ nav, .alternative, header { - display: none; + display: none; } diff --git a/translations/harbour-sailchess-de.ts b/translations/harbour-sailchess-de.ts index 48964e3..19923ee 100644 --- a/translations/harbour-sailchess-de.ts +++ b/translations/harbour-sailchess-de.ts @@ -7,10 +7,6 @@ Chess - - Login - - Random player @@ -23,6 +19,14 @@ Please start a game + + Abort game + + + + Refresh login + + CoverPage @@ -38,7 +42,11 @@ - Challenging + Please start a game + + + + Waiting for diff --git a/translations/harbour-sailchess.ts b/translations/harbour-sailchess.ts index 47db823..269bcbb 100644 --- a/translations/harbour-sailchess.ts +++ b/translations/harbour-sailchess.ts @@ -7,10 +7,6 @@ Chess - - Login - - Random player @@ -23,6 +19,14 @@ Please start a game + + Abort game + + + + Refresh login + + CoverPage @@ -38,7 +42,11 @@ - Challenging + Please start a game + + + + Waiting for -- cgit v1.2.3