diff options
author | Marvin Borner | 2020-04-10 19:19:07 +0200 |
---|---|---|
committer | Marvin Borner | 2020-04-10 19:19:07 +0200 |
commit | decd6b0f90c2cdacf4f8cb27aaa8ecbf863edd49 (patch) | |
tree | fe72b7b88056c36f9fb5487c6588bd74bbec0577 | |
parent | 17927de45e769d4dbac5b1547464b2618150cfb4 (diff) |
Added very basic lichess connection
-rw-r--r-- | qml/Functions.qml | 67 | ||||
-rw-r--r-- | qml/pages/Board.qml | 21 | ||||
-rw-r--r-- | translations/harbour-sailchess-de.ts | 23 | ||||
-rw-r--r-- | translations/harbour-sailchess.ts | 23 |
4 files changed, 133 insertions, 1 deletions
diff --git a/qml/Functions.qml b/qml/Functions.qml index dde1cc7..970788b 100644 --- a/qml/Functions.qml +++ b/qml/Functions.qml @@ -51,4 +51,71 @@ Item { board.itemAt(from).piece = ""; selected = []; } + + // END LOGIC + + property var game_id: "" + + function event_stream() { + var xhr = new XMLHttpRequest(); + xhr.open("GET", "https://lichess.org/api/stream/event"); + xhr.seenBytes = 0; + xhr.setRequestHeader("Authorization", "Bearer " + access_token.value); + + xhr.onreadystatechange = function() { + if (xhr.readyState === 3) { + try { + const new_data = xhr.response.substr(xhr.seenBytes); + xhr.seenBytes = xhr.responseText.length; + + console.log(new_data); + const data = JSON.parse(new_data); + if (data["type"] === "gameStart") { + information.text = qsTr("Game started!"); + fill(); + game_id = data["game"]["id"]; + } + } catch (Exception) {} + } + }; + + xhr.send(); + } + + function post(path, params, callback) { + var xhr = new XMLHttpRequest(); + xhr.open("POST", "https://lichess.org/api/" + path); + xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); + xhr.setRequestHeader("Authorization", "Bearer " + access_token.value); + + xhr.onreadystatechange = function() { + if (xhr.readyState === 4 && xhr.status === 200) { + callback(JSON.parse(xhr.responseText)); + } else if (xhr.readyState === 4) { + console.error(xhr.responseText); + } + } + xhr.send(params); + } + + 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)); + }); + } + + function start_seek() { + var xhr = new XMLHttpRequest(); + xhr.open("GET", "https://lichess.org/api/account"); + xhr.setRequestHeader("Authorization", "Bearer " + access_token.value); + + xhr.onreadystatechange = function() { + if (xhr.readyState === 3) { + console.log(xhr.responseText); + } + }; + + xhr.send(); + } } diff --git a/qml/pages/Board.qml b/qml/pages/Board.qml index 35e2e5e..799f24c 100644 --- a/qml/pages/Board.qml +++ b/qml/pages/Board.qml @@ -24,6 +24,14 @@ Page { pageStack.push(Qt.resolvedUrl("Login.qml")) } } + MenuItem { + text: qsTr("Random player") + onClicked: functions.start_seek() + } + MenuItem { + text: qsTr("Play against bot") + onClicked: functions.challenge("GodelEscherBot") + } } Column { @@ -37,6 +45,13 @@ Page { title: qsTr("Chess") } + Label { + id: information + anchors.horizontalCenter: parent.horizontalCenter + anchors.bottom: grid.top + text: qsTr("Please start a game"); + } + Grid { property int row: 0 @@ -52,7 +67,11 @@ Page { i: index } } - Component.onCompleted: functions.fill() + Component.onCompleted: { + // functions.fill(); + functions.event_stream(); + // functions.challenge("GodelEscherBot"); + } } } } diff --git a/translations/harbour-sailchess-de.ts b/translations/harbour-sailchess-de.ts index 5f446ba..48964e3 100644 --- a/translations/harbour-sailchess-de.ts +++ b/translations/harbour-sailchess-de.ts @@ -11,6 +11,18 @@ <source>Login</source> <translation type="unfinished"></translation> </message> + <message> + <source>Random player</source> + <translation type="unfinished"></translation> + </message> + <message> + <source>Play against bot</source> + <translation type="unfinished"></translation> + </message> + <message> + <source>Please start a game</source> + <translation type="unfinished"></translation> + </message> </context> <context> <name>CoverPage</name> @@ -19,4 +31,15 @@ <translation>Mein Cover</translation> </message> </context> +<context> + <name>Functions</name> + <message> + <source>Game started!</source> + <translation type="unfinished"></translation> + </message> + <message> + <source>Challenging </source> + <translation type="unfinished"></translation> + </message> +</context> </TS> diff --git a/translations/harbour-sailchess.ts b/translations/harbour-sailchess.ts index f50285a..47db823 100644 --- a/translations/harbour-sailchess.ts +++ b/translations/harbour-sailchess.ts @@ -11,6 +11,18 @@ <source>Login</source> <translation type="unfinished"></translation> </message> + <message> + <source>Random player</source> + <translation type="unfinished"></translation> + </message> + <message> + <source>Play against bot</source> + <translation type="unfinished"></translation> + </message> + <message> + <source>Please start a game</source> + <translation type="unfinished"></translation> + </message> </context> <context> <name>CoverPage</name> @@ -19,4 +31,15 @@ <translation type="unfinished"></translation> </message> </context> +<context> + <name>Functions</name> + <message> + <source>Game started!</source> + <translation type="unfinished"></translation> + </message> + <message> + <source>Challenging </source> + <translation type="unfinished"></translation> + </message> +</context> </TS> |