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
|
|
|
/**
|
2014-04-02 11:46:27 +01:00
|
|
|
* @param {Object}
|
|
|
|
* options Game options to use in the request.
|
2012-03-16 03:06:23 +00:00
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
2014-04-02 11:46:27 +01:00
|
|
|
cah.ajax.Builder.prototype.withGameOptions = function(options) {
|
2012-03-16 03:06:23 +00:00
|
|
|
this.assertNotExecuted_();
|
2014-04-02 11:46:27 +01:00
|
|
|
this.data[cah.$.AjaxRequest.GAME_OPTIONS] = $.toJSON(options);
|
2012-03-16 03:06:23 +00:00
|
|
|
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-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;
|
|
|
|
};
|
|
|
|
|
2014-08-11 06:16:15 +01:00
|
|
|
/**
|
|
|
|
* @param {string}
|
|
|
|
* id The Cardcast ID of the deck to add.
|
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.withCardcastId = function(id) {
|
|
|
|
this.assertNotExecuted_();
|
|
|
|
this.data[cah.$.AjaxRequest.CARDCAST_ID] = id;
|
|
|
|
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];
|
|
|
|
};
|