2017-03-23 18:58:09 +00:00
|
|
|
module TS.SpaceTac {
|
|
|
|
/**
|
|
|
|
* A shop is a place to buy/sell equipments
|
|
|
|
*/
|
|
|
|
export class Shop {
|
2017-05-10 15:29:10 +00:00
|
|
|
// Average level of equipment
|
|
|
|
private level: number
|
|
|
|
|
|
|
|
// Approximative number of equipments
|
|
|
|
private count: number
|
|
|
|
|
2017-03-23 18:58:09 +00:00
|
|
|
// Equipment in stock
|
2017-05-10 15:29:10 +00:00
|
|
|
private stock: Equipment[]
|
|
|
|
|
|
|
|
// Random generator
|
|
|
|
private random: RandomGenerator
|
|
|
|
|
|
|
|
constructor(level = 1, stock: Equipment[] = [], count = 40) {
|
|
|
|
this.level = level;
|
|
|
|
this.stock = stock;
|
|
|
|
this.count = count;
|
|
|
|
this.random = new RandomGenerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get available stock to display
|
|
|
|
*/
|
|
|
|
getStock() {
|
|
|
|
if (this.stock.length < this.count * 0.5) {
|
|
|
|
let count = this.random.randInt(Math.floor(this.count * 0.8), Math.ceil(this.count * 1.2));
|
|
|
|
this.stock = this.stock.concat(this.generateStock(count - this.stock.length, this.level, this.random));
|
|
|
|
this.sortStock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.stock;
|
|
|
|
}
|
2017-03-23 18:58:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a random stock
|
2017-04-20 21:20:50 +00:00
|
|
|
*
|
|
|
|
* *level* is the preferential level, but equipment around it may be generated
|
2017-03-23 18:58:09 +00:00
|
|
|
*/
|
2017-05-10 15:29:10 +00:00
|
|
|
private generateStock(items: number, level: number, random = RandomGenerator.global): Equipment[] {
|
2017-04-20 21:20:50 +00:00
|
|
|
let generator = new LootGenerator(random);
|
|
|
|
|
2017-05-10 15:29:10 +00:00
|
|
|
return nna(range(items).map(() => {
|
2017-04-20 21:20:50 +00:00
|
|
|
let equlevel = random.weighted(range(level + 3).map(i => i + 1).map(i => (i > level) ? 1 : i)) + 1;
|
|
|
|
let quality = random.weighted([1, 7, 2]);
|
|
|
|
return generator.generate(equlevel, quality);
|
|
|
|
}));
|
2017-03-23 18:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort the stock by equipment level, then by value
|
|
|
|
*/
|
|
|
|
sortStock() {
|
2017-04-20 21:20:50 +00:00
|
|
|
this.stock.sort((a, b) => (a.level == b.level) ? cmp(a.getPrice(), b.getPrice()) : cmp(a.level, b.level));
|
2017-03-23 18:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the buy/sell price for an equipment
|
|
|
|
*/
|
|
|
|
getPrice(equipment: Equipment): number {
|
2017-04-20 21:20:50 +00:00
|
|
|
return equipment.getPrice();
|
2017-03-23 18:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A fleet buys an item
|
|
|
|
*
|
|
|
|
* This does not put the item anywhere on the fleet, only remove the item from stock, and make the payment
|
|
|
|
*/
|
|
|
|
sellToFleet(equipment: Equipment, fleet: Fleet) {
|
|
|
|
let price = this.getPrice(equipment);
|
|
|
|
if (price <= fleet.credits) {
|
|
|
|
if (remove(this.stock, equipment)) {
|
|
|
|
fleet.credits -= price;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A fleet sells an item
|
|
|
|
*
|
|
|
|
* This does not check if the item is anywhere on the fleet, only add the item to the shop stock, and make the payment
|
|
|
|
*/
|
|
|
|
buyFromFleet(equipment: Equipment, fleet: Fleet) {
|
|
|
|
let price = this.getPrice(equipment);
|
|
|
|
if (add(this.stock, equipment)) {
|
2017-04-25 20:54:23 +00:00
|
|
|
this.sortStock();
|
2017-03-23 18:58:09 +00:00
|
|
|
fleet.credits += price;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|