2012-02-06 22:00:24 +00:00
|
|
|
/*
|
2012-02-06 22:54:31 +00:00
|
|
|
* Copyright (c) 2012, Andy Janata
|
2012-02-02 22:47:23 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification, are permitted
|
|
|
|
* provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, this list of conditions
|
|
|
|
* and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice, this list of
|
|
|
|
* conditions and the following disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
|
|
|
|
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2012-01-12 22:25:04 +00:00
|
|
|
/**
|
|
|
|
* Builder for ajax requests. This contains methods to add every possible parameter to an ajax
|
|
|
|
* request, even if it doesn't make sense for the operation code.
|
|
|
|
*
|
2012-02-06 22:00:24 +00:00
|
|
|
* @author Andy Janata (ajanata@socialgamer.net)
|
2012-01-12 22:25:04 +00:00
|
|
|
* @param {string}
|
|
|
|
* op The operation code for the ajax request.
|
|
|
|
* @returns {cah.ajax.Builder}
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder = function(op) {
|
|
|
|
/**
|
|
|
|
* The data for this request.
|
|
|
|
*
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
this.data = {};
|
|
|
|
|
2012-01-17 00:28:21 +00:00
|
|
|
this.data[cah.$.AjaxRequest.OP] = op;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this request has been run or not.
|
|
|
|
*
|
|
|
|
* @type {boolean}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.run_ = false;
|
2012-01-12 22:25:04 +00:00
|
|
|
|
|
|
|
/**
|
2012-02-06 22:00:24 +00:00
|
|
|
* Error callback for this request. This is for communication-level errors. If this is not
|
|
|
|
* specified, the default handler of logging the error to the user will occur.
|
2012-01-12 22:25:04 +00:00
|
|
|
*
|
|
|
|
* @type {?function(jqXHR,textStatus,errorThrown)}
|
|
|
|
*/
|
|
|
|
this.errback = undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serial counter for ajax requests.
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.serial = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set an error callback for the request.
|
|
|
|
*
|
2012-01-21 03:11:39 +00:00
|
|
|
* @param {Function}
|
|
|
|
* opt_errback Optional error callback. (jqXHR,textStatus,errorThrown)
|
2012-01-12 22:25:04 +00:00
|
|
|
* @returns {cah.ajax.Builder}
|
|
|
|
*/
|
2012-01-21 03:11:39 +00:00
|
|
|
cah.ajax.Builder.prototype.withErrback = function(opt_errback) {
|
2012-02-06 22:00:24 +00:00
|
|
|
this.assertNotExecuted_();
|
2012-01-12 22:25:04 +00:00
|
|
|
this.errback = errback;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the ajax request.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.run = function() {
|
2012-02-06 22:00:24 +00:00
|
|
|
this.assertNotExecuted_();
|
2012-01-17 00:28:21 +00:00
|
|
|
this.run_ = true;
|
|
|
|
|
|
|
|
this.data[cah.$.AjaxRequest.SERIAL] = cah.ajax.Builder.serial++;
|
2012-01-13 01:05:09 +00:00
|
|
|
cah.Ajax.instance.requestWithBuilder(this);
|
2012-01-12 22:25:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string}
|
|
|
|
* nickname Nickname field to use in the request.
|
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.withNickname = function(nickname) {
|
2012-02-06 22:00:24 +00:00
|
|
|
this.assertNotExecuted_();
|
2012-01-17 00:28:21 +00:00
|
|
|
this.data[cah.$.AjaxRequest.NICKNAME] = nickname;
|
2012-01-12 22:25:04 +00:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string}
|
|
|
|
* message Message field to use in the request.
|
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.withMessage = function(message) {
|
2012-02-06 22:00:24 +00:00
|
|
|
this.assertNotExecuted_();
|
2012-01-17 00:28:21 +00:00
|
|
|
this.data[cah.$.AjaxRequest.MESSAGE] = message;
|
2012-01-12 22:25:04 +00:00
|
|
|
return this;
|
|
|
|
};
|
2012-01-17 00:28:21 +00:00
|
|
|
|
2012-01-19 00:42:18 +00:00
|
|
|
/**
|
|
|
|
* @param {number}
|
|
|
|
* gameId Game id field to use in the request.
|
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.withGameId = function(gameId) {
|
2012-02-06 22:00:24 +00:00
|
|
|
this.assertNotExecuted_();
|
2012-01-19 00:42:18 +00:00
|
|
|
this.data[cah.$.AjaxRequest.GAME_ID] = gameId;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2012-01-27 02:07:39 +00:00
|
|
|
/**
|
|
|
|
* @param {number}
|
|
|
|
* cardId Card id field to use in the request.
|
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.withCardId = function(cardId) {
|
2012-02-06 22:00:24 +00:00
|
|
|
this.assertNotExecuted_();
|
2012-01-27 02:07:39 +00:00
|
|
|
this.data[cah.$.AjaxRequest.CARD_ID] = cardId;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2012-03-16 03:06:23 +00:00
|
|
|
/**
|
2012-07-07 23:01:33 +01:00
|
|
|
* @param {Array}
|
|
|
|
* cardSets List of card set ids to use in the request.
|
2012-03-16 03:06:23 +00:00
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
2012-07-07 23:01:33 +01:00
|
|
|
cah.ajax.Builder.prototype.withCardSets = function(cardSets) {
|
2012-03-16 03:06:23 +00:00
|
|
|
this.assertNotExecuted_();
|
2012-07-07 23:01:33 +01:00
|
|
|
this.data[cah.$.AjaxRequest.CARD_SETS] = cardSets.join(',');
|
2012-03-16 03:06:23 +00:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number}
|
|
|
|
* playerLimit Player limit field to use in the request.
|
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.withPlayerLimit = function(playerLimit) {
|
|
|
|
this.assertNotExecuted_();
|
|
|
|
this.data[cah.$.AjaxRequest.PLAYER_LIMIT] = playerLimit;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2013-06-10 12:36:11 +01:00
|
|
|
/**
|
|
|
|
* @param {number}
|
|
|
|
* spectatorLimit Spectator limit field to use in the request.
|
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.withSpectatorLimit = function(spectatorLimit) {
|
|
|
|
this.assertNotExecuted_();
|
|
|
|
this.data[cah.$.AjaxRequest.SPECTATOR_LIMIT] = spectatorLimit;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2012-03-16 03:06:23 +00:00
|
|
|
/**
|
|
|
|
* @param {number}
|
|
|
|
* scoreLimit Score limit field to use in the request.
|
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.withScoreLimit = function(scoreLimit) {
|
|
|
|
this.assertNotExecuted_();
|
|
|
|
this.data[cah.$.AjaxRequest.SCORE_LIMIT] = scoreLimit;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2012-03-19 04:20:48 +00:00
|
|
|
/**
|
|
|
|
* @param {string}
|
|
|
|
* password Password field to use in the request.
|
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.withPassword = function(password) {
|
|
|
|
this.assertNotExecuted_();
|
|
|
|
this.data[cah.$.AjaxRequest.PASSWORD] = password;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2013-06-19 13:23:10 +01:00
|
|
|
/**
|
|
|
|
* @param {number}
|
2013-06-19 13:51:23 +01:00
|
|
|
* blanksLimit Blanks limit field to use in the request.
|
2013-06-19 13:23:10 +01:00
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.withBlanksLimit = function(blanksLimit) {
|
|
|
|
this.assertNotExecuted_();
|
|
|
|
this.data[cah.$.AjaxRequest.BLANKS_LIMIT] = blanksLimit;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2013-04-13 18:39:38 +01:00
|
|
|
/**
|
|
|
|
* @param {boolean}
|
|
|
|
* useTimer Whether or not the game should use the idle timer.
|
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.withUseTimer = function(useTimer) {
|
|
|
|
this.assertNotExecuted_();
|
|
|
|
this.data[cah.$.AjaxRequest.USE_TIMER] = useTimer;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2013-03-27 03:17:49 +00:00
|
|
|
/**
|
|
|
|
* @param {boolean}
|
|
|
|
* wall Whether or not this is a warn-all ("wall").
|
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.withWall = function(wall) {
|
|
|
|
this.assertNotExecuted_();
|
|
|
|
this.data[cah.$.AjaxRequest.WALL] = wall;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2013-10-02 12:28:16 +01:00
|
|
|
/**
|
|
|
|
* @param {boolean}
|
|
|
|
* emote Whether or not this is an emote
|
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.withEmote = function(emote) {
|
|
|
|
this.assertNotExecuted_();
|
|
|
|
this.data[cah.$.AjaxRequest.EMOTE] = emote;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Assert that the request from this builder has not already run. Throws an exception if it has.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.assertNotExecuted_ = function() {
|
2012-01-17 00:28:21 +00:00
|
|
|
if (this.run_) {
|
|
|
|
throw "Request already executed.";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Assert that the request from this builder has already run. Throws an exception if it has not.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.assertExecuted_ = function() {
|
2012-01-17 00:28:21 +00:00
|
|
|
if (!this.run_) {
|
|
|
|
throw "Request not yet executed.";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* @returns {string} The operation code of the request this builder built.
|
|
|
|
*/
|
2012-01-17 00:28:21 +00:00
|
|
|
cah.ajax.Builder.prototype.getOp = function() {
|
2012-02-06 22:00:24 +00:00
|
|
|
this.assertExecuted_();
|
2012-01-17 00:28:21 +00:00
|
|
|
return this.data[cah.$.AjaxRequest.OP];
|
|
|
|
};
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* @returns {number} The serial id of the request this builder built.
|
|
|
|
*/
|
2012-01-17 00:28:21 +00:00
|
|
|
cah.ajax.Builder.prototype.getSerial = function() {
|
2012-02-06 22:00:24 +00:00
|
|
|
this.assertExecuted_();
|
2012-01-17 00:28:21 +00:00
|
|
|
return this.data[cah.$.AjaxRequest.SERIAL];
|
|
|
|
};
|