Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //set everything up, including the WebSocket
- // (function setup(){
- var socket = new WebSocket("ws://localhost:3000");
- var localb;
- socket.onopen = function(){
- // socket.send("Hi!!");
- document.getElementById("status").innerHTML = 'CONNECTED';
- };
- socket.onmessage = function(event){
- //var a = new game();
- localb = JSON.parse(event.data);
- console.log(localb + " teh localb");
- for (var i = 0; i < 8; i++){
- for(var j = 0; j < 8; j++){
- var position = "#" + i + j + "r.checkerpiece";
- var position2 = "#" + i + j + "b.checkerpiece";
- $(position2).hide();
- $(position).hide();
- if(localb[i][j] == 1){
- var position = "#" + i + j + "r.checkerpiece";
- $(position).show();
- }
- if(localb[i][j] == 2){
- var position = "#" + i + j + "b.checkerpiece";
- $(position).show();
- }
- }
- }
- };
- var oldpos;
- $(".checkerpiece").on("click", function(event){
- oldpos = event.target.id;
- });
- $(".checkerr").on("click", function(event){
- var newpos = event.target.id;
- var x1 = oldpos.substring(0,1);
- var y1 = oldpos.substring(1,2);
- var x2 = newpos.substring(0,1);
- var y2 = newpos.substring(1,2);
- var color = oldpos.substring(2,3);
- var d = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1-y2,2))
- if(d === Math.sqrt(2)){
- if(color === "r" && x2 > x1 && !has(localb, x2, y2)){
- localb[x1][y1] = 0;
- localb[x2][y2] = 1;
- var old = "#" + oldpos + ".checkerpiece";
- var updated = "#" +newpos + color +".checkerpiece";
- $(old).hide();
- $(updated).show();
- }
- if(color === "b" && x2 < x1 && !has(localb, x2, y2)){
- localb[x1][y1] = 0;
- localb[x2][y2] = 2;
- var old = "#" + oldpos + ".checkerpiece";
- var updated = "#" +newpos + color +".checkerpiece";
- $(old).hide();
- $(updated).show();
- }
- socket.send(JSON.stringify(localb));
- }})
- socket.onclose = function(){
- document.getElementById("status").innerHTML = 'NOT CONNECTED';
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement