diff options
Diffstat (limited to 'game.js')
-rw-r--r-- | game.js | 89 |
1 files changed, 89 insertions, 0 deletions
@@ -0,0 +1,89 @@ +(function($) { + $.fn.randomize = function(childElem) { + return this.each(function() { + var $this = $(this); + var elems = $this.children(childElem); + + elems.each(function() { + $(this).data("sortIndex", Math.random()); + }); + elems.sort(function(a,b) { var as = $(a).data("sortIndex"); var bs = $(b).data("sortIndex"); return as < bs ? -1 : bs < as ? 1 : 0;}); + + $this.remove(childElem); + + for(var i=0; i < elems.length; i++) + $this.append(elems[i]); + + }); + }; + + $.fn.fullclick = function(callback) { + var mousedown = false; + this.mousedown(function() { + mousedown = true; + }); + this.mouseup(function() { + if (mousedown) { + callback.apply(this, arguments); + } + mousedown = false; + }); + }; +})(jQuery); + + +var startGame = (function () { + + return function(source, target, message) { + var $message = $(message); + + var $source = $(source); + var $target = $(target); + var $both = $source.add($target); + $source.randomize("li"); + + var areAllOver = function() { + return $source.find("li").size() == 0; + }; + + var areInOrder = function() { + var success = true; + $target.find("li").each(function(i, val) { + success = success && (i == $(val).data("index")); + }); + return success; + }; + + var checkGameResult = function() { + if (areAllOver() && areInOrder()) { + $message.text("Well done!"); + $target.addClass("success"); + } else { + $message.text(null); + $target.removeClass("success"); + } + }; + + + + $source.fullclick(function(event) { + if (event.target != $(source)[0]) { + $target.append(event.target); + checkGameResult(); + } + }); + $target.fullclick(function(event) { + if (event.target != $(target)[0]) { + $source.append(event.target); + checkGameResult(); + } + }); + $both.sortable({ + connectWith: ".bible-game", + update: function (event) { + checkGameResult(); + } + }); + }; + +})(); |