Skip to content

Commit

Permalink
Adding Research support, platform fixes
Browse files Browse the repository at this point in the history
Adding basic research support, including handling client events and
inclusion in simulation game loop. Also various fixes to platform with
more testing and scale
  • Loading branch information
rwampler committed Feb 27, 2023
1 parent e04869a commit f5e9831
Show file tree
Hide file tree
Showing 61 changed files with 1,677 additions and 809 deletions.
8 changes: 4 additions & 4 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

## v0.1.3 - in progress - updated 2023-02-05
## v0.1.5 - in progress - updated 2023-02-26
### platform
* [done] nodejs web-server with basic support [v0.1.0]
* [done] simple in-memory and lightweight databases [v0.1.0]
Expand All @@ -16,18 +16,18 @@
* [done] cashflow event API's (dummy) [v0.1.3]
* [done] visa and tycoon position support [v0.1.3]
* [done] rotating and robust logging [v0.1.3]
* [pending] corporation and company cash structure
* [in progress] corporation and company cash structure
* [pending] visitor ID and API support
* [pending] overlay and city zones API's
* [pending] road API's

### assets
* [done] configuration via starpeace-assets [v0.1.0]
* [in progress] town boundaries
* [done] town boundaries [v0.1.4]

### simulation
* [done] basic simulation platform
* [in progress] research progress
* [done] research progress [v0.1.4]
* [in progress] construction progress
* [pending] initial setup town roads
* [pending] rankings
Expand Down
30 changes: 30 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@starpeace/starpeace-server-multiverse-nodejs",
"version": "0.1.3",
"version": "0.1.4",
"description": "STARPEACE Multiverse game-play and simulation server created with NodeJS",
"author": "starpeace-project",
"license": "MIT",
Expand Down Expand Up @@ -32,6 +32,7 @@
"@tsconfig/node16": "^1.0.3",
"@types/bad-words": "^3.0.1",
"@types/bcrypt": "^5.0.0",
"@types/bmp-js": "^0.1.0",
"@types/compression": "^1.7.2",
"@types/cors": "^2.8.13",
"@types/fs-extra": "^11.0.1",
Expand Down Expand Up @@ -60,6 +61,7 @@
"@types/mocha": "^10.0.1",
"bad-words": "^3.0.4",
"bcrypt": "^5.1.0",
"bmp-js": "^0.1.0",
"body-parser": "^1.20.1",
"compression": "^1.7.4",
"cookie-parser": "^1.4.6",
Expand Down
60 changes: 51 additions & 9 deletions src/building/building-cache.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,57 @@
import { BuildingImageDefinition } from '@starpeace/starpeace-assets-types';

import Building from '../building/building';
import BuildingDao from '../building/building-dao';
import { BuildingConfigurations } from '../core/galaxy-manager';
import TownCache from '../planet/town-cache';
import Utils from '../utils/utils';

export default class BuildingCache {
dao: BuildingDao;
townCache: TownCache;
planetWidth: number;
buildingConfigurations: BuildingConfigurations;

loaded: boolean = false;
dirtyIds: Set<string> = new Set();

byId: Record<string, Building>;
byId: Record<string, Building> = {};

idsByChunkId: Record<string, Set<string>>;
idsByCompanyId: Record<string, Set<string>>;
idsByTownId: Record<string, Set<string>>;
idByPositionIndex: Record<number, string> = {}
idsByChunkId: Record<string, Set<string>> = {};
idsByCompanyId: Record<string, Set<string>> = {};
idsByTownId: Record<string, Set<string>> = {};

constructor (dao: BuildingDao, townCache: TownCache) {
constructor (dao: BuildingDao, planetWidth: number, buildingConfigurations: BuildingConfigurations, townCache: TownCache) {
this.dao = dao;
this.planetWidth = planetWidth;
this.buildingConfigurations = buildingConfigurations;
this.townCache = townCache;
this.byId = {};
this.idsByChunkId = {};
this.idsByCompanyId = {};
this.idsByTownId = {};
}

close (): Promise<any> {
return this.dao.close();
}

flush (): Promise<void> {
return new Promise((resolve, reject) => {
if (!this.dirtyIds.size) {
return resolve();
}

Promise.all(Array.from(this.dirtyIds).map(id => {
return this.dao.set(this.byId[id]);
}))
.then((buildings: Building[]) => {
for (const building of buildings) {
this.dirtyIds.delete(building.id);
}
})
.then(resolve)
.catch(reject);
});
}

load (): Promise<void> {
return Utils.withRetries(10, async () => {
for (const town of this.townCache.all()) {
Expand All @@ -43,6 +66,15 @@ export default class BuildingCache {
loadBuilding (building: Building): Building {
this.byId[building.id] = building;

const imageDefinition: BuildingImageDefinition | null = this.buildingConfigurations.imageForDefinitionId(building.definitionId);
if (imageDefinition) {
for (let y = 0; y < imageDefinition.tileHeight; y++) {
for (let x = 0; x < imageDefinition.tileWidth; x++) {
this.idByPositionIndex[(building.mapY - y) * this.planetWidth + (building.mapX - x)] = building.id;
}
}
}

if (!this.idsByChunkId[building.chunkId]) this.idsByChunkId[building.chunkId] = new Set();
this.idsByChunkId[building.chunkId].add(building.id)

Expand Down Expand Up @@ -80,4 +112,14 @@ export default class BuildingCache {
}
}

isPositionOccupied (mapX: number, mapY: number, width: number, height: number): boolean {
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
if (this.idByPositionIndex[(mapY - y) * this.planetWidth + (mapX - x)]) {
return true;
}
}
}
return false;
}
}
5 changes: 3 additions & 2 deletions src/building/building-dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import Building from './building';
export function asBuildingDao (client: ModelEventClient, planetId: string): BuildingDao {
return {
close: Utils.PROMISE_NOOP_VOID,
forTownId (townId: string) { return client.listTownBuildings(planetId, townId); }
forTownId (townId: string) { return client.listTownBuildings(planetId, townId); },
set: Utils.PROMISE_NOOP_ANY
}
}

export default interface BuildingDao {
close (): Promise<void>;

forTownId (townId: string): Promise<Building[]>;
set (building: Building): Promise<Building>;
}
18 changes: 17 additions & 1 deletion src/building/building.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from 'lodash';
import Utils from '../utils/utils';

// import BuildingDefinition from './building-definition';
// import SimulationDefinition from './simulation/simulation-definition';
Expand Down Expand Up @@ -109,12 +110,27 @@ export default class Building {
json.tycoonId,
json.corporationId,
json.companyId,
json.townId,
json.definitionId,
json.townId,
json.name ?? null,
json.mapX,
json.mapY,
json.stage ?? 0
);
}

static create (tycoonId: string, corporationId: string, companyId: string, definitionId: string, townId: string, name: string | null, mapX: number, mapY: number): Building {
return new Building(
Utils.uuid(),
tycoonId,
corporationId,
companyId,
definitionId,
townId,
name ?? null,
mapX,
mapY,
-1
);
}
}
26 changes: 25 additions & 1 deletion src/company/company-cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Company from '../company/company';
import { CompanyDao } from '../company/company-store';
import CompanyDao from './company-dao';
import Utils from '../utils/utils';

export default class CompanyCache {
Expand Down Expand Up @@ -31,6 +31,25 @@ export default class CompanyCache {
});
}

flush (): Promise<void> {
return new Promise((resolve, reject) => {
if (!this.dirtyIds.size) {
return resolve();
}

Promise.all(Array.from(this.dirtyIds).map(id => {
return this.dao.set(this.byId[id]);
}))
.then((companies: Company[]) => {
for (const company of companies) {
this.dirtyIds.delete(company.id);
}
})
.then(resolve)
.catch(reject);
});
}

loadCompany (company: Company): Company {
this.byId[company.id] = company;

Expand Down Expand Up @@ -60,4 +79,9 @@ export default class CompanyCache {
return companyOrCompanys;
}

updateCashflow (companyId: string, cashflow: number): Company | null {
const company: Company | null = this.forId(companyId)?.withCashflow(cashflow) ?? null;
if (company) this.dirtyIds.add(companyId);
return company;
}
}
5 changes: 3 additions & 2 deletions src/company/company-dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import Company from '../company/company';
export function asCompanyDao (client: ModelEventClient, planetId: string): CompanyDao {
return {
close: Utils.PROMISE_NOOP_VOID,
all () { return client.allCompanies(planetId); }
all () { return client.allCompanies(planetId); },
set: Utils.PROMISE_NOOP_ANY
}
}

export default interface CompanyDao {
close (): Promise<void>;

all (): Promise<Company[]>;
set (building: Company): Promise<Company>;
}
9 changes: 2 additions & 7 deletions src/company/company-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ import _ from 'lodash';
import * as sqlite3 from 'sqlite3';

import Company from '../company/company';
import CompanyDao from './company-dao';


export interface CompanyDao {
close (): Promise<void>;

all (): Promise<Company[]>;
}

export default class CompanyStore {
export default class CompanyStore implements CompanyDao {
readOnly: boolean;
db: sqlite3.Database;

Expand Down
Loading

0 comments on commit f5e9831

Please sign in to comment.