diff options
author | Marvin Borner | 2020-04-11 01:05:55 +0200 |
---|---|---|
committer | Marvin Borner | 2020-04-11 01:05:55 +0200 |
commit | 53abf21bad1d10ff8a62da25578c116780d9d20a (patch) | |
tree | 7f6eb493db9040b078b5f95bbf5c1b7eef14aeb7 | |
parent | f4b275b744c22953c0c5dab1573df1bae4f11927 (diff) |
Added multiline support and mate detection
-rw-r--r-- | qml/Functions.qml | 87 | ||||
-rw-r--r-- | qml/pages/Board.qml | 2 | ||||
-rw-r--r-- | translations/harbour-sailchess-de.ts | 4 | ||||
-rw-r--r-- | translations/harbour-sailchess.ts | 4 |
4 files changed, 58 insertions, 39 deletions
diff --git a/qml/Functions.qml b/qml/Functions.qml index 9355af8..292df3f 100644 --- a/qml/Functions.qml +++ b/qml/Functions.qml @@ -96,14 +96,18 @@ Item { xhr.seenBytes = xhr.responseText.length; if (new_data !== "\n") { - 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"]; - game_stream(); - } + // TODO: Fix resign + console.log("[" + new_data.replace("\n", ",").replace(/.$/, "]")); + + const data_arr = JSON.parse("[" + new_data.replace("\n", ",").replace(/.$/, "]")); + data_arr.forEach(function (data) { + if (data["type"] === "gameStart") { + information.text = qsTr("Game started!"); + fill(); + game_id = data["game"]["id"]; + game_stream(); + } + }); } } }; @@ -124,30 +128,35 @@ Item { game_xhr.seenBytes = game_xhr.responseText.length; if (new_data !== "\n") { - 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; - turn = moves.split(" ").length % 2 === (start ? 0 : 1); - console.log(moves.split(" ").length % 2 === (start ? 1 : 0)); - - console.log(moves); - - new_moves.split(" ").forEach(function(move) { - if (move !== "") { - const arr = convert_movement(move); - move_piece(arr[0], arr[1]); + console.log("[" + new_data.replace("\n", ",").replace(/.$/, "]")); + + const data_arr = JSON.parse("[" + new_data.replace("\n", ",").replace(/.$/, "]")); + data_arr.forEach(function (data) { + // TODO: Implement castling: e1c1, e1, g1, e8c8, e8g8 + var all_moves, status; + if (data["type"] === "gameFull") { + all_moves = data["state"]["moves"]; + status = data["state"]["status"]; + } else if (data["type"] === "gameState") { + all_moves = data["moves"]; + status = data["status"]; + } + + const new_moves = all_moves.slice(moves.length) + moves += new_moves; + + turn = (moves.split(" ").length === 1 && start) || moves.split(" ").length % 2 === (start ? 0 : 1); + + new_moves.split(" ").forEach(function(move) { + if (move !== "") { + const arr = convert_movement(move); + move_piece(arr[0], arr[1]); + } + }); + + if (status !== "started") { + stop_game(); + information.text = qsTr("Game over: ") + status; } }); } @@ -164,22 +173,24 @@ Item { xhr.setRequestHeader("Authorization", "Bearer " + access_token.value); xhr.onreadystatechange = function() { - if (xhr.readyState === 4) { + if (xhr.readyState === 4) callback(JSON.parse(xhr.responseText)); - } } xhr.send(params); } + function stop_game() { + game_id = ""; + moves = ""; + game_xhr.abort(); + } + 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)); + stop_game(); } }) } diff --git a/qml/pages/Board.qml b/qml/pages/Board.qml index 68dc56d..58af5ee 100644 --- a/qml/pages/Board.qml +++ b/qml/pages/Board.qml @@ -32,7 +32,7 @@ Page { MenuItem { text: qsTr("Play against bot") visible: functions.game_id === "" ? true : false - onClicked: functions.challenge("GarboBot") + onClicked: functions.challenge("awesomelvin") } MenuItem { text: qsTr("Abort game") diff --git a/translations/harbour-sailchess-de.ts b/translations/harbour-sailchess-de.ts index 4826908..8b746b1 100644 --- a/translations/harbour-sailchess-de.ts +++ b/translations/harbour-sailchess-de.ts @@ -57,5 +57,9 @@ <source>Waiting for </source> <translation type="unfinished"></translation> </message> + <message> + <source>Game over: </source> + <translation type="unfinished"></translation> + </message> </context> </TS> diff --git a/translations/harbour-sailchess.ts b/translations/harbour-sailchess.ts index 7c7c19f..4b0d9c0 100644 --- a/translations/harbour-sailchess.ts +++ b/translations/harbour-sailchess.ts @@ -57,5 +57,9 @@ <source>Waiting for </source> <translation type="unfinished"></translation> </message> + <message> + <source>Game over: </source> + <translation type="unfinished"></translation> + </message> </context> </TS> |