dice = {}; dice.v = {}; dice.roll = { count: 1, plus: 0, history: [], guid: '', initiate: function( n ) { var data = new Array(); var total = 0; for (var i = 0; i < dice.roll.count; i++) { var thisRoll = dice.roll.action( n ); data.push( thisRoll ); total += thisRoll; } $('#total').html( total + dice.roll.plus ); $('#rolls').html( data.join() ); var record = { 'number': dice.roll.count, 'dice': n, 'rolls': data.join(), 'plus': dice.roll.plus, 'total': total + dice.roll.plus }; // dice.roll.history.push( record ); dice.roll.history.push( record ); $('#historyData table').html(''); for (var i = dice.roll.history.length - 1; i >= 0; i--) { var record = dice.roll.history[i]; $('#historyData table').append('\ \ '+record['number']+' \ '+record['dice']+' \ '+record['rolls']+' \ '+record['plus']+' \ '+record['total']+' \ \ '); } $('#historyData table').prepend('\ \ number \ dice \ rolls \ plus \ total \ \ '); $('#history').css( 'display', 'block' ); // dice.roll.initiate(); }, //============================================== action: function( n ) { var data = new Array(); for (var i = 1; i < (n+1); i++) { data.push({ 'number': i, 'sortBy': dice.roll.uuid() }); // console.log( i ); } var newData = dice.roll.sort( data, 'sortBy', 'asc' ); console.log( newData ); return newData[0].number; }, //============================================== uuid: function() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); // dice.roll.uuid(); // alert( dice.roll.uuid() ); }, //============================================== sort: function( arr, key, direction ) { direction = direction.toLowerCase(); // var exec = 'a.'+key+' - b.'+key+';'; var exec = 'arr.sort((a, b) => (a.'+key+' > b.'+key+') ? 1 : -1)' var data = eval( exec ); if ( direction.includes('d') ) { data.reverse(); } return data; // dice.roll.sort( arr, key ); }, //============================================== change: function( key, direction ) { if ( key.includes('c') ) { if ( direction.includes('+') ) { dice.roll.count++; } else { dice.roll.count--; } } if ( key.includes('p') ) { if ( direction.includes('+') ) { dice.roll.plus++; } else { dice.roll.plus--; } } if ( dice.roll.count < 2 ) { dice.roll.count = 1; } // if ( dice.roll.plus < 1 ) { // dice.roll.plus = 0; // } $('#count span').html( dice.roll.count ); $('#plus span').html( dice.roll.plus ); $('#plusTotal span').html( dice.roll.plus ); // dice.roll.change( 'count', '+' ); // dice.roll.change( 'count', '-' ); // dice.roll.change( 'plus', '+' ); // dice.roll.change( 'plus', '-' ); }, //============================================== printHistory: function() { }, //============================================== null: function(data) { $.post( "file.php", { 'id': 0 }, function(data) { dice.roll.two(data) } ); console.log(data); // dice.roll.two(); } //============================================== };