From decd6b0f90c2cdacf4f8cb27aaa8ecbf863edd49 Mon Sep 17 00:00:00 2001
From: Marvin Borner
Date: Fri, 10 Apr 2020 19:19:07 +0200
Subject: Added very basic lichess connection
---
qml/Functions.qml | 67 ++++++++++++++++++++++++++++++++++++
qml/pages/Board.qml | 21 ++++++++++-
translations/harbour-sailchess-de.ts | 23 +++++++++++++
translations/harbour-sailchess.ts | 23 +++++++++++++
4 files changed, 133 insertions(+), 1 deletion(-)
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 @@
Login
+
+ Random player
+
+
+
+ Play against bot
+
+
+
+ Please start a game
+
+
CoverPage
@@ -19,4 +31,15 @@
Mein Cover
+
+ Functions
+
+ Game started!
+
+
+
+ Challenging
+
+
+
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 @@
Login
+
+ Random player
+
+
+
+ Play against bot
+
+
+
+ Please start a game
+
+
CoverPage
@@ -19,4 +31,15 @@
+
+ Functions
+
+ Game started!
+
+
+
+ Challenging
+
+
+
--
cgit v1.2.3