import { v4 as uuid } from 'uuid';
import { ERROR_CODE, InsufficientResourceError, NotFoundError } from '../errors';
import {Repository} from './base';
+import * as config from '../config';
import { BuildQueue, BuildQueueRepository } from './build-queue';
import { DateTime, Duration } from 'luxon';
import { UnitTrainingQueue, UnitTrainingQueueRepository } from './training-queue';
import { coalesce } from '../lib/util';
import { Building, BuildingRepository } from './buildings';
import { Unit, UnitRepository } from './unit';
-import _ from 'lodash';
+import _, { random } from 'lodash';
import { Army, ArmyQueue, ArmyRepository } from './army';
export type City = {
owner: string;
totalSpace: number;
usedSpace: number;
- gold: number;
- ore: number;
- logs: number;
- bushels: number;
+ credits: number;
+ alloys: number;
+ energy: number;
+ food: number;
population: number;
soldiers: number;
attackers: number;
barracks: number;
special_attacker_trainer: number;
special_defender_trainer: number;
+ icon: string;
+}
+
+export type CityWithLocation = {
+ sector_id: number;
location_x: number;
location_y: number;
-}
+} & City;
export class CityRepository extends Repository<City> {
buildQueue: BuildQueueRepository;
this.armyRepository = new ArmyRepository();
}
- async create(accountId: string): Promise<City> {
+ async create(accountId: string, rebel: boolean = false): Promise<CityWithLocation> {
+ const icon = rebel ? `/colony-ships/rebels/${random(1, 6)}.png` : '/colony-ships/01.png';
const info: City = {
id: uuid(),
owner: accountId,
totalSpace: 100,
usedSpace: 0,
- gold: 10000,
- ore: 10000,
- logs: 10000,
- bushels: 10000,
+ credits: 10000,
+ alloys: 10000,
+ energy: 10000,
+ food: 10000,
population: 1000,
soldiers: 100,
attackers: 0,
barracks: 0,
special_attacker_trainer: 0,
special_defender_trainer: 0,
- location_x: _.random(0, 100),
- location_y: _.random(0, 100)
+ icon
};
+ await this.Insert(info);
+
// placement can happen randomly
+ const availableSectors = config.AVAILABLE_SECTORS;
+ const sector = _.random(1, availableSectors);
- await this.Insert(info);
+ const location = {
+ sector_id: await this.getAvailableSector(),
+ location_x: random(0, 25),
+ location_y: random(0, 25)
+ }
- return info;
+ await this.db.raw('insert into locations (sector_id, city_id, location_x, location_y) values (?, ?, ?, ?)', [
+ location.sector_id,
+ info.id,
+ location.location_x,
+ location.location_y
+ ]);
+
+ return {
+ ...info,
+ sector_id: location.sector_id,
+ location_x: location.location_x,
+ location_y: location.location_y
+ };
}
- async save(city: City) {
+ async getAvailableSector(): Promise<number> {
+ // figure out which sectors have space (40% fill rate at 25x25);
+ const availableSectors = await this.db.raw<{count: Number, sector_id: number}[]>(`select count(sector_id) as count, sector_id from locations group by sector_id`);
+ const sample = _.sample(availableSectors.filter(sector => sector.count < 250)) as {count: number, sector_id: number};
+
+ if(!sample) {
+ return _.sortBy(availableSectors, 'sector_id').pop().sector_id+1;
+ }
+
+ return sample.sector_id;
+ }
+
+ async save(city: Partial<City>) {
await this.Save(city, {id: city.id});
return city;
}
- async getUsersCity(owner: string): Promise<City> {
- const city = await this.FindOne({
- owner
- });
+ async findById(cityId: string): Promise<CityWithLocation> {
+ const city = await this.db.raw<CityWithLocation[]>(`select c.*, l.sector_id, l.location_x, l.location_y from cities c
+ join locations l on c.id = l.city_id
+ where id = ? limit 1`, [cityId]);
if(!city) {
throw new NotFoundError('User has no city', ERROR_CODE.NO_CITY);
}
- return city;
+ return city.pop();
+
+ }
+
+ async getUsersCity(owner: string): Promise<CityWithLocation> {
+ const city = await this.db.raw<CityWithLocation[]>(`select c.*, l.sector_id, l.location_x, l.location_y from cities c
+ join locations l on c.id = l.city_id
+ where owner = ? limit 1`, [owner]);
+
+ if(!city) {
+ throw new NotFoundError('User has no city', ERROR_CODE.NO_CITY);
+ }
+
+ return city.pop();
+ }
+
+ findAllInSector(sector_id: number): Promise<CityWithLocation[]> {
+ return this.db.raw<CityWithLocation[]>(`select c.*, l.sector_id, l.location_x, l.location_y from cities c
+join locations l on c.id = l.city_id
+where l.sector_id = ?`, [sector_id]);
}
async buildBuilding(building: Building, amount: number, city: City): Promise<BuildQueue> {
throw new InsufficientResourceError('land', building.land, freeSpace);
}
- if(city.gold < building.gold) {
- throw new InsufficientResourceError('gold', building.gold, city.gold);
+ if(city.credits < building.credits) {
+ throw new InsufficientResourceError('credits', building.credits, city.credits);
}
- if(city.ore < building.ore) {
- throw new InsufficientResourceError('ore', building.ore, city.ore);
+ if(city.alloys < building.alloys) {
+ throw new InsufficientResourceError('alloys', building.alloys, city.alloys);
}
- if(city.logs < building.logs) {
- throw new InsufficientResourceError('logs', building.logs, city.logs);
+ if(city.energy < building.energy) {
+ throw new InsufficientResourceError('energy', building.energy, city.energy);
}
city.usedSpace += (building.land * amount);
- city.gold -= (building.gold * amount);
- city.ore -= (building.ore * amount);
- city.logs -= (building.logs * amount);
+ city.credits -= (building.credits * amount);
+ city.alloys -= (building.alloys * amount);
+ city.energy -= (building.energy * amount);
await this.save(city);
* @param city1
* @param city2
*/
- distanceInSeconds(city1: City, city2: City): number {
+ distanceInSeconds(city1: CityWithLocation, city2: CityWithLocation): number {
return this.distanceInHours(city1, city2) * 60 * 60;
}
- distanceInHours(city1: City, city2: City): number {
+ distanceInHours(city1: CityWithLocation, city2: CityWithLocation): number {
const dist = Math.sqrt(
Math.pow((city2.location_x - city1.location_x), 2)
+
Math.pow((city2.location_y - city1.location_y), 2)
);
- return _.round(dist/4, 2);
+ // sectors always add 4 hours
+ const sector_dist = Math.abs(city1.sector_id - city2.sector_id) * 6;
+
+ return _.round(dist/4, 2) + sector_dist;
}
async train(unit: Unit, amount: number, city: City): Promise<UnitTrainingQueue> {
- if(city.gold < unit.gold) {
- throw new InsufficientResourceError('gold', unit.gold, city.gold);
+ if(city.credits < unit.credits) {
+ throw new InsufficientResourceError('credits', unit.credits, city.credits);
}
- if(city.bushels < unit.bushels) {
- throw new InsufficientResourceError('bushels', unit.bushels, city.bushels);
+ if(city.food < unit.food) {
+ throw new InsufficientResourceError('food', unit.food, city.food);
}
if(city.population < coalesce(unit.population, 0)) {
// ok they have everything, lets update their city
// and create the entry in the training queue
- city.gold -= unit.gold * amount;
- city.bushels -= unit.bushels * amount;
+ city.credits -= unit.credits * amount;
+ city.food -= unit.food * amount;
city.population -= coalesce(unit.population, 0) * amount;
city.soldiers -= coalesce(unit.soldiers, 0) * amount;
city.attackers -= coalesce(unit.attackers, 0) * amount;
return power
}
- async attack(attacker: City, attacked: City, army: Army): Promise<ArmyQueue> {
+ async foodProductionPerTick(city: City): Promise<number> {
+ // eventually we should supply the warehouse formula
+ // to calculate the max amount of food created per tick
+ return _.max([
+ city.population + _.round(city.farms * 50)
+ ])
+ }
+
+ async foodUsagePerTick(city: City): Promise<number> {
+ return (
+ (city.soldiers * 0.5) +
+ (city.population * 0.25) +
+ (city.attackers * 0.75) +
+ (city.attackers * 0.75) +
+ (city.sp_attackers * 1.3) +
+ (city.sp_defenders * 1.3)
+ )
+ }
+
+ async energyProductionPerTic(city: City): Promise<number> {
+ return 0;
+ }
+
+ async energyUsagePerTick(city: City): Promise<number> {
+ return 0;
+ }
+
+ async attack(attacker: CityWithLocation, attacked: CityWithLocation, army: Army): Promise<ArmyQueue> {
// validate the user has enough of a military!
if(attacker.soldiers < army.soldiers) {
throw new InsufficientResourceError('soldiers', army.soldiers, attacker.soldiers);