2048: Tweaks before production (#4027)

* Added additional trigger.

* Fixed container placement / alignment.

* Updated tests.

* Add async start.

* Revert "Add async start."

This reverts commit 54b3edb88221051393d818ccff35d9ae45d10049.

* Check for mobile device and return before onShow

* Ensure we return value from DDH.failed

* remove trailing spaces
master
PJ Hampton 2017-06-05 21:49:57 +01:00 committed by Zaahir Moolla
parent 8c5ac1d651
commit 6d9896cb8b
5 changed files with 38 additions and 22 deletions

View File

@ -5,7 +5,7 @@ use DDG::Goodie;
zci answer_type => "2048"; zci answer_type => "2048";
zci is_cached => 1; zci is_cached => 1;
triggers start => "play 2048", "game 2048"; triggers start => "2048", "play 2048", "game 2048";
handle query_lc => sub { handle query_lc => sub {

View File

@ -1,5 +1,6 @@
<div class="game2048__container"> <div class="game2048__container">
<div id="game2048__area_container"> <div id="game2048__area_container">
<div class="game2048__message"> <div class="game2048__message">
<p></p> <p></p>
@ -10,8 +11,7 @@
{{/loop}} {{/loop}}
</table> </table>
</div> </div>
<div class="game2048__info"> <div class="game2048__info">
<div class="game2048__counter"> <div class="game2048__counter">
<div class="game2048__points_addition"></div> <div class="game2048__points_addition"></div>
@ -24,4 +24,5 @@
<button class="game2048__new_game">New Game</button> <button class="game2048__new_game">New Game</button>
</div> </div>
</div> </div>

View File

@ -113,7 +113,7 @@
.zci--game2048 .game2048__info { .zci--game2048 .game2048__info {
float: right; float: right;
width: 40%; width: 35%;
} }
.zci--game2048 .game2048__help { .zci--game2048 .game2048__help {

View File

@ -2,12 +2,17 @@ DDH.game2048 = DDH.game2048 || {};
DDH.game2048.build = function(ops) { DDH.game2048.build = function(ops) {
"use strict"; "use strict";
//Hide this goodie on mobile devices for now
if (DDG.device.isMobile || DDG.device.isMobileDevice) {
return DDH.failed('game2048');
}
// Global Variables Declaration // Global Variables Declaration
var WINNUM = 2048, var WINNUM = 2048,
SIZE = 4, SIZE = 4,
TILE_COUNT = SIZE * SIZE, TILE_COUNT = SIZE * SIZE,
started = false, started = false,
tiles = init_area(), tiles = init_area(),
$game_area, $game_area,
$game_area_container, $game_area_container,
@ -38,7 +43,7 @@ DDH.game2048.build = function(ops) {
swap_cols_area(); swap_cols_area();
swapped = true; swapped = true;
} }
var result = handle_move(transposed, swapped); var result = handle_move(transposed, swapped);
if (dir === 'd' || dir === 's') if (dir === 'd' || dir === 's')
@ -65,7 +70,7 @@ DDH.game2048.build = function(ops) {
if (tiles[i].val === 0) { if (tiles[i].val === 0) {
++moves; ++moves;
continue; continue;
} }
//Move across empty tiles //Move across empty tiles
if(moves > 0) { if(moves > 0) {
@ -86,7 +91,7 @@ DDH.game2048.build = function(ops) {
if(tile_b.val === 0) if(tile_b.val === 0)
continue; continue;
if(tile_a.val === tile_b.val) { if(tile_a.val === tile_b.val) {
merge(tile_a, tile_b, row, col - moves, transposed, swapped); merge(tile_a, tile_b, row, col - moves, transposed, swapped);
result.points = tile_a.val; result.points = tile_a.val;
@ -109,21 +114,21 @@ DDH.game2048.build = function(ops) {
tile_b.val = 0; tile_b.val = 0;
//Recalculate the real world pos //Recalculate the real world pos
if(swapped) if(swapped)
col = SIZE - 1 - col; col = SIZE - 1 - col;
if(transposed) { if(transposed) {
var tmp = col; var tmp = col;
col = row; col = row;
row = tmp; row = tmp;
} }
var translate_string = gen_translate_string(row, col); var translate_string = gen_translate_string(row, col);
tile_b.tile_div.css({ "-ms-transform" : translate_string, tile_b.tile_div.css({ "-ms-transform" : translate_string,
"-webkit-transform" : translate_string, "-webkit-transform" : translate_string,
"transform" : translate_string, "transform" : translate_string,
"opacity" : 0.00 }); "opacity" : 0.00 });
tile_b.tile_div.on("transitionend webkitTransitionEnd oTransitionEnd otransitionend MSTransitionEnd", function () { tile_b.tile_div.on("transitionend webkitTransitionEnd oTransitionEnd otransitionend MSTransitionEnd", function () {
$(this).remove(); $(this).remove();
@ -149,7 +154,7 @@ DDH.game2048.build = function(ops) {
for(var i = 0; i < TILE_COUNT; ++i) { for(var i = 0; i < TILE_COUNT; ++i) {
var pos = index_to_rc(i); var pos = index_to_rc(i);
var tile = tiles[i]; var tile = tiles[i];
if("undefined" !== typeof(tile.tile_div) && tile.val > 0) { if("undefined" !== typeof(tile.tile_div) && tile.val > 0) {
var translate_string = gen_translate_string(pos.row, pos.col); var translate_string = gen_translate_string(pos.row, pos.col);
tile.tile_div tile.tile_div
@ -185,7 +190,7 @@ DDH.game2048.build = function(ops) {
for(var i = 0; i < TILE_COUNT; ++i) { for(var i = 0; i < TILE_COUNT; ++i) {
var pos = index_to_rc(i); var pos = index_to_rc(i);
if(pos.col >= pos.row) if(pos.col >= pos.row)
continue; continue;
var index_to_swap = rc_to_index(pos.col, pos.row); var index_to_swap = rc_to_index(pos.col, pos.row);
@ -212,7 +217,7 @@ DDH.game2048.build = function(ops) {
if(tiles[i].val === 0) { if(tiles[i].val === 0) {
unused_tiles.push(i); unused_tiles.push(i);
} }
} }
var rand_tile = unused_tiles[Math.floor(Math.random() * unused_tiles.length)]; var rand_tile = unused_tiles[Math.floor(Math.random() * unused_tiles.length)];
var rand_val = Math.floor(Math.random() * 11) < 2 ? 4 : 2; var rand_val = Math.floor(Math.random() * 11) < 2 ? 4 : 2;
@ -316,12 +321,6 @@ DDH.game2048.build = function(ops) {
return { return {
onShow: function() { onShow: function() {
//Hide this goodie on mobile devices for now
if(DDG.device.isMobile || DDG.device.isMobileDevice) {
DDH.spice_tabs.game2048.hideLink();
DDH.spice_tabs.game2048.hide();
return;
}
//'started' is a boolean variable used in order to avoid the //'started' is a boolean variable used in order to avoid the
//duplication of the gaming tiles. Moving around the DDG tabs the //duplication of the gaming tiles. Moving around the DDG tabs the

View File

@ -12,6 +12,19 @@ zci is_cached => 1;
ddg_goodie_test( ddg_goodie_test(
[qw( DDG::Goodie::Game2048 )], [qw( DDG::Goodie::Game2048 )],
'2048' => test_zci(
'',
structured_answer => {
data => ignore(),
templates => {
group => "text",
item => 0,
options => {
content => "DDH.game2048.content"
},
}
}
),
'play 2048' => test_zci( 'play 2048' => test_zci(
'', '',
structured_answer => { structured_answer => {
@ -25,7 +38,10 @@ ddg_goodie_test(
} }
} }
), ),
'2048 online' => undef 'what is 2048?' => undef,
'how to play 2048' => undef,
'204823 34232' => undef
); );
done_testing; done_testing;