-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
137 lines (111 loc) · 3.7 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"use strict";
define([
"/jquery.js",
"/js/drag.js"
], function () {
// Global variables
var self;
var pieces;
var xMin = 10, yMin = 70;
var x, y;
// Positions object
var positions = {
"black" : [],
"white" : []
};
// On load
function init(config) {
self = this;
pieces = $("#pieces");
pieces.html("");
self.link("getPositions", function(err, pos) {
// If positions were already set
if (!$.isEmptyObject(pos)) {
positions = pos;
var html = "";
for(var i = 0; i < 9; i++) {
// Black pieces
html += createPiece("black", "B" + i, positions.black[i].x, positions.black[i].y);
// White pieces
html += createPiece("white", "W" + i, positions.white[i].x, positions.white[i].y);
}
pieces.append(html);
}
// ... if not, create them
else {
createBlackPieces();
createWhitePieces();
self.link("setPositions", { data: positions });
}
setHandlers();
window.setInterval(function() {
animate();
}, 3000);
});
}
// Animate
function animate() {
self.link("getPositions", function(err, pos) {
positions = pos;
for (var i = 0; i < 9; i++) {
$("#B" + i).animate({ left : positions.black[i].x, top : positions.black[i].y }, 200);
$("#W" + i).animate({ left : positions.white[i].x, top : positions.white[i].y }, 200);
}
});
}
// Set handlers
function setHandlers() {
$(".piece").on("mouseup", function() {
var id = $(this).attr("id");
var x = $(this).css("left");
var y = $(this).css("top");
update(id, x, y);
});
$("#resetButton").on("click", function() {
self.link("resetGame", { data : $("#pass").val() }, function(err, res) {
if(res === "Refresh page") {
location.reload();
}
else {
$("#pass").val("");
alert("Wrong password.");
}
});
});
}
// Update the points
function update(id, x, y) {
var dataObject = {
"id" : id,
"x" : x,
"y" : y
};
self.link("update", { data : dataObject });
}
// Functions for creating pieces
function createBlackPieces(){
var html = "";
for(var i = 0; i<9; i++){
x = xMin + Math.floor(Math.random()*100);
y = yMin + Math.floor(Math.random()*100);
positions.black.push({ "x" : x + "px", "y" : y + "px" });
html += createPiece("black", "B" + i, x, y);
}
pieces.append(html);
}
function createWhitePieces(){
var html = "";
for(var i = 0; i<9; i++){
x = xMin + 150 + Math.floor(Math.random()*100);
y = yMin + Math.floor(Math.random()*100);
positions.white.push({ "x" : x + "px", "y" : y + "px" });
html += createPiece("white", "W" + i, x, y);
}
pieces.append(html);
}
// Create HTML code for one piece
function createPiece(color, id, x, y) {
return "<div id='" + id + "' style='left: " + x + "; top: " + y + ";' class='box content " + color + " piece' onmousedown='dragStart(event)'></div>";
}
return init;
});