Skip to content

Instantly share code, notes, and snippets.

@VirtuosiMedia
Created April 6, 2016 22:39
Show Gist options
  • Save VirtuosiMedia/18f2d2764ffc356d6d76906e818187ef to your computer and use it in GitHub Desktop.
Save VirtuosiMedia/18f2d2764ffc356d6d76906e818187ef to your computer and use it in GitHub Desktop.
/**
* Places an order for a good from a holding.
* @param {int} holdingId The holding id.
* @param {string} good The good id.
* @param {string} goodCategory The good category: resource, component.
* @param {float} price The price of the order.
* @param {int} quantity The quantity of goods ordered.
* @param {string} orderType The type of purchase: trade or order.
* @return {boolean} TRUE if the order was placed, FALSE if there were insufficient resources.
*/
placeTradeOrder: function(holdingId, good, goodCategory, price, quantity, orderType){
var holding = jg.data.holdings[holdingId];
var totalCost = price * quantity;
var canOrder = (((orderType === 'order') && (holding.resources.supply.credits >= totalCost)) || (orderType === 'trade'));
if (canOrder){
holding.data.economic.trade.orders[good] = {
faction: holding.data.faction,
holding: holding.data.id,
good: good,
goodCategory: goodCategory,
price: price,
quantity: quantity,
total: totalCost,
orderType: orderType,
timestamp: jg.data.state.timestamp
};
if (orderType === 'order'){
holding.resources.supply.credits -= totalCost;
}
}
return canOrder;
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment