Skip to content

Commit

Permalink
Mostly working, needs testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxankey committed Aug 31, 2019
1 parent b20e47f commit ae0d0a6
Show file tree
Hide file tree
Showing 271 changed files with 11,280 additions and 1,396 deletions.
178 changes: 118 additions & 60 deletions defaults/browser.js → common/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ function get_team_zone_packets() {
return packets;
}


/**
* Scramble the supplied pieces, like rolling dice: randomizes locations in a pattern determined by the
* last piece's diameter, minimizing overlap.
Expand All @@ -149,7 +150,8 @@ function get_team_zone_packets() {
* @param {int} space space occupied by each piece on average
*/
function scramble_pieces(pieces, x, y, space, scale) {

if(!pieces || pieces.length==0) return;

// First find the center of the pieces and the space taken by each
var c = get_center_of_pieces(pieces);
var x = or_default(x, c.x);
Expand All @@ -160,7 +162,7 @@ function scramble_pieces(pieces, x, y, space, scale) {
// Now find the basis vectors based on the radius of the last piece
var d = pieces[pieces.length-1].get_dimensions()
var D = scale*Math.sqrt(d.width*d.width+d.height*d.height);
var ax = 2*D;
var ax = 1.5*D;
var ay = 0;
var bx = ax*cos(60);
var by = ax*sin(60);
Expand Down Expand Up @@ -1218,7 +1220,7 @@ function BOARD(canvas) {
this.new_piece_peakable_by = null;
this.new_piece_private_images_everywhere = false;
this.new_piece_active_image = 0;
this.new_piece_rotates_with_canvas = false;
this.new_piece_rotates_with_canvas = true;
this.new_piece_zooms_with_canvas = true;
this.new_piece_physical_shape = 'rectangle';
this.new_piece_alpha = 1.0;
Expand Down Expand Up @@ -1520,7 +1522,7 @@ BOARD.prototype.add_team = function(name, hand_image_paths, color) {
for (var i in hand_image_paths) {

// Get the path
path = hand_image_paths[i];
path = 'hands/'+hand_image_paths[i];

// make sure we haven't loaded it already (this.images is a lookup dictionary)
if( path in this.images ) {
Expand All @@ -1546,6 +1548,28 @@ BOARD.prototype.add_team = function(name, hand_image_paths, color) {
} // end of loop over hand image paths
}

/**
* Sorts the supplied piece list by piece_id, then pops them all to the top.
* @param {list} pieces list of pieces to sort, then pop to the top.
*/

BOARD.prototype.sort_and_pop_pieces = function(pieces) {

var my_index = get_my_client_index();
var pieces = or_default(pieces, this.client_selected_pieces[my_index]);

// Sort them
pieces.sort(function(a, b){return a.piece_id-b.piece_id});

// Loop over them, putting them on top of the stack
for(n=0; n<pieces.length; n++) {
i = this.find_piece_index(sps[n].piece_id);
this.pop_piece(i);
this.insert_piece(sps[n], this.pieces.length);
}
}


/**
* Shuffles the supplied pieces, creating a stack at the current location of the bottom card
* @param {list} pieces // list of piece objects
Expand Down Expand Up @@ -1639,6 +1663,79 @@ BOARD.prototype.collect_pieces = function(pieces,x,y,shuffle,active_image,r_piec
}
}


/**
* Distributes the supplied pieces in a grid centered at x,y.
* @param {list} pieces list of pieces to expand.
* @param {int} number_per_row how many pieces to put in each row; defaults to board.expand_number_per_row.
* @param {float} x center x-coordinate (defaults to pieces center)
* @param {float} y center y-coordinate (defaults to pieces center)
* @param {float} spacing_x spacing in x-direction (defaults to this.expand_spacing_x)
* @param {float} spacing_y spacing in y-direction (defaults to this.expand_spacing_y)
* @param {int} active_image optional image index.
* @param {float} r_piece rotation of the pieces (defaults to this.expand_r)
*/

BOARD.prototype.expand_pieces = function(pieces, number_per_row, x, y, spacing_x, spacing_y, active_image, r_piece) {

// Things will move, so let's trigger a redraw to be safe / responsive.
this.trigger_redraw = true;

// Defaults
var my_index = get_my_client_index();
var pieces = or_default(pieces, this.client_selected_pieces[my_index]);
var r_piece = or_default(r_piece, this.expand_r);
var active_image = or_default(active_image, null);
var spacing_x = or_default(spacing_x, this.expand_spacing_x);
var spacing_y = or_default(spacing_y, this.expand_spacing_y);
var number_per_row = or_default(number_per_row, this.expand_number_per_row);
var c = get_center_of_pieces(pieces);
var x = or_default(x, c.x);
var y = or_default(y, c.y);

// Now do the expansion.

// Will hold lists of pieces for rows
rows = [];

// make a copy to destroy.
var sps = [...pieces];

// loop over the selected pieces, splicing rows until it's empty
while(sps.length > 0) rows.push(sps.splice(0,Math.max(1,number_per_row)));

// loop over the rows, setting the coordinates
for(ny in rows) {
dy = spacing_y*(ny-0.5*rows.length+0.5);

// loop over each piece
for(nx in rows[ny]) {

// Get the dx and dy
dx = spacing_x*(nx-0.5*rows[ny].length+0.5);

// Rotate the dx,dy vector
d = rotate_vector(dx,dy,this.r_target);

// Push the piece on the top of the stack
p = rows[ny][nx];
this.pop_piece(this.pieces.indexOf(p)); // doesn't trigger a resend for higher pieces
this.insert_piece(p,this.pieces.length);

// Push to the top of selected pieces
var n = sps.indexOf(p);
if(n>=0) sps.splice(n,1);
sps.push(p);

// Now set the coordinates x,y,r,angle,disable_snap,immediate
p.set_target(x+d.x,y+d.y,r_piece-this.r_target, null, null, false);

// Set the image
if(active_image != null) p.active_image = active_image;
}
}
}

BOARD.prototype.new_client_hand = function() {

// create the hand
Expand Down Expand Up @@ -2583,60 +2680,13 @@ BOARD.prototype.event_keydown = function(e) {

break;

case 88: // x for xpose
case 88: // x for xpand

// If the shift key is held, sort / pop to top first
if(e.shiftKey) {
var sps = this.client_selected_pieces[my_index];

// Sort them
sps.sort(function(a, b){return a.piece_id-b.piece_id});

// Loop over them, putting them on top of the stack
for(n in sps) {
i = this.find_piece_index(sps[n].piece_id);
this.pop_piece(i);
this.insert_piece(sps[n], this.pieces.length);
}
}

// Will hold lists of pieces for rows
var rows = [];

// make a copy to destroy.
var sps = [...this.client_selected_pieces[my_index]];

// loop over the selected pieces, splicing rows until it's empty
while(sps.length > 0) {
rows.push(sps.splice(0,Math.max(1,this.expand_number_per_row)));
}
if(e.shiftKey) this.sort_and_pop_pieces(this.client_selected_pieces[my_index]);

// loop over the rows, setting the coordinates
for(ny in rows) {
dy = this.expand_spacing_y*(ny-0.5*rows.length+0.5);

// loop over each piece
for(nx in rows[ny]) {
// Get the dx and dy
dx = this.expand_spacing_x*(nx-0.5*rows[ny].length+0.5);

// Rotate the dx,dy vector
d = rotate_vector(dx,dy,this.r_target);

// Push the piece on the top of the stack
p = rows[ny][nx];
this.pop_piece(this.pieces.indexOf(p)); // doesn't trigger a resend for higher pieces
this.insert_piece(p,this.pieces.length);

// Push to the top of selected pieces
var n = sps.indexOf(p);
if(n>=0) sps.splice(n,1);
sps.push(p);

// Now set the coordinates x,y,r,angle,disable_snap,immediate
p.set_target(this.mouse.x+d.x,this.mouse.y+d.y,this.expand_r-this.r_target, null, null, false);
}
}
this.expand_pieces(this.client_selected_pieces[my_index], this.expand_number_per_row,
this.mouse.x, this.mouse.y);
break;

case 90: // z for zhuffle
Expand Down Expand Up @@ -3594,14 +3644,15 @@ my_socket.on('id', server_assigned_id);

// Function to handle when the server sends a piece update ('u')
server_update = function(piece_datas){

// Board is not ready yet. Call board.go() after pieces are defined to start receiving.
if(!board._ready_for_packets) return;

// server has sent a pieces update
console.log('Received u:', piece_datas.length, 'pieces');
board.last_update_ms = Date.now();

// If we got nothing, send a full update to populate
// Special case: if we got nothing, send a full update to populate the server.
if(piece_datas.length == 0) {
board.send_full_update(true); // force it.
return;
Expand All @@ -3610,17 +3661,20 @@ server_update = function(piece_datas){
// Sort the piece datas by n's (must be increasing!)
piece_datas.sort(function(a, b){return a.n-b.n});

//for(var i in piece_datas) console.log(' ', i, piece_datas[i].id, piece_datas[i].n);

// run through the list of ids, find the index m in the stack of the pieces by id
var ps = [];
var pd;
var m;
var p;
for(var i in piece_datas) {
pd = piece_datas[i];

// find the current index
var m = board.find_piece_index(pd.id);
m = board.find_piece_index(pd.id);

// Remove the actual piece from the main stack, to be re-inserted later.
// We do this for ALL updating pieces, even those that are held, just to preserve
// the order
p = board.pop_piece(m,true);
ps.push(p); // save this as an ordered list, matching piece_datas, for later sorting by n & re-insertion

Expand All @@ -3630,6 +3684,7 @@ server_update = function(piece_datas){
// set the new values
p.set_target(pd.x, pd.y, pd.r, null, true); // disable snap
p.active_image = pd.i;
// There is no p.n, only the index in the stack

// store the new coordinates so we don't re-update the server!
p.previous_x = p.x_target;
Expand All @@ -3644,7 +3699,10 @@ server_update = function(piece_datas){
// Loop over the pieces again to insert them into the main stack, which currently should not contain them. We do this
// in separate loops so that pieces removed from random locations and sent to
// random locations do not interact. The value of ns is the final value in the pieces array.
for(var i in ps) board.insert_piece(ps[i], piece_datas[i].n, true);
for(var i in ps) {
board.insert_piece(ps[i], piece_datas[i].n, true);
ps[i].previous_n = board.pieces.indexOf(ps[i]); // Don't want to resend this info!
}
}
my_socket.on('u', server_update);

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Loading

0 comments on commit ae0d0a6

Please sign in to comment.