even more functionality

This commit is contained in:
Alexander Munch-Hansen 2018-05-19 14:10:31 +02:00
parent 9903c73930
commit 3e8f5987aa

86
func.js
View File

@ -62,20 +62,31 @@ $(function () {
$( ".checkers_list" ).sortable({ $( ".checkers_list" ).sortable({
connectWith: ".checkers_list", connectWith: ".checkers_list",
receive: function (event, ui) { receive: function (event, ui) {
console.log(prev_board);
handleMove(); // Handle putting people on the bar correclty and getting off it.
// Something like look at the index you drop the checker at
board = convertToBoard();
prev_board = handleMove(prev_board, board);
convertToPins(prev_board);
if (board != convertToBoard()) {
prev_board = getBotMove(prev_board);
convertToPins(prev_board);
}
console.log(prev_board);
} }
}); });
}); });
function handleMove() { function handleMove(prev_board, board) {
var board = convertToBoard(); var new_board = postRequest(prev_board, board);
postRequest(board); return new_board;
prev_board = board;
}; };
@ -96,16 +107,25 @@ function convertToBoard() {
return board return board
} }
function convertToPins(board ) { function emptyLists() {
for (var i=0; i < 26; i++) {
$( '#checkers_list-'+i ).empty();
}
}
// We don't want to deal with bars RIGHT now, TODO: Fix this. function convertToPins(board ) {
for (var i = 1; i < 25; i++) { console.log(board);
for (var amt_at_pin = 0; amt_at_pin < Math.abs(board[i]); amt_at_pin++) {
var sign = Math.sign(board[i]); emptyLists();
for (var i = 0; i < 26; i++) {
var tmp = parseInt(board[i]);
for (var amt_at_pin = 0; amt_at_pin < Math.abs(tmp); amt_at_pin++) {
var sign = Math.sign(tmp);
if (sign > 0) { if (sign > 0) {
var list_obj = $( "<li><img src='black.png' width='40'></li>").addClass(Math.sign(board[i]).toString()); var list_obj = $( "<li><img src='black.png' width='40'></li>").addClass(Math.sign(tmp).toString());
} else if (sign < 0) { } else if (sign < 0) {
var list_obj = $( "<li><img src='white.png' width='40'></li>").addClass(Math.sign(board[i]).toString()); var list_obj = $( "<li><img src='white.png' width='40'></li>").addClass(Math.sign(tmp).toString());
} }
$( '#checkers_list-'+i ).append(list_obj); $( '#checkers_list-'+i ).append(list_obj);
@ -115,14 +135,34 @@ function convertToPins(board ) {
function onReceived(data) { function onReceived(data) {
var obj = JSON.stringify(data); var obj = JSON.parse(JSON.stringify(data));
alert(obj); alert(obj['board']);
}; };
function getBotMove(board) {
var data = {'board' : board.toString()}
var result = "";
$.ajax({
type : "POST",
async: false,
url : "http://127.0.0.1:5000/bot_move",
data: JSON.stringify(data),
success: function(data) {
result = data;
}
});
return result.split(',');
};
function getRequest() { function getRequest() {
$.ajax({ $.ajax({
url: 'http://127.0.0.1:5000/get_board', url: 'http://127.0.0.1:5000/get_board',
async: false,
dataType: 'JSONP', dataType: 'JSONP',
data: { data: {
format: 'json' format: 'json'
@ -138,13 +178,19 @@ function onPost(data) {
}; };
function postRequest(board) { function postRequest(prev_board, board) {
console.log(prev_board);
var data = {'board' : board.toString()} var result = "";
var data = {'board' : board.toString(), 'prev_board': prev_board.toString()}
$.ajax({ $.ajax({
type : "POST", type : "POST",
async: false,
url : "http://127.0.0.1:5000/post_board", url : "http://127.0.0.1:5000/post_board",
data: JSON.stringify(data), data: JSON.stringify(data),
success: onPost success: function(data) {
result = data;
}
}); });
console.log(result);
return result.split(',');
}; };