Improve mining rate module
This commit is contained in:
		
							parent
							
								
									3d55f7060c
								
							
						
					
					
						commit
						08a2371196
					
				| 
						 | 
					@ -19,43 +19,12 @@ if(!planet) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
await planet.resources.calculateCurrentAvailableResources();
 | 
					await planet.resources.calculateCurrentAvailableResources();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const resourceArray: (Resource & { capacity: number })[] = [];
 | 
					const miningRate = planet.resources.getPerHourMiningRate();
 | 
				
			||||||
 | 
					const storage = planet.resources.getStorageCapacities();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const resourceArray: (Resource & { capacity: number, rate: number })[] = [];
 | 
				
			||||||
for(const key of planet.resources.resources) {
 | 
					for(const key of planet.resources.resources) {
 | 
				
			||||||
    resourceArray.push({ ...key, capacity: 10_000 });
 | 
					    resourceArray.push({ ...key, capacity: storage.find(r => r.id === key.id)?.capacity ?? 10_000, rate: miningRate.find(x => x.id === key.id)?.rate ?? 0 });
 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
if(!(planet instanceof SystemManager)) {
 | 
					 | 
				
			||||||
    const mapStorageToResource: { [key: string]: string } = {
 | 
					 | 
				
			||||||
        "coal-storage": "coal",
 | 
					 | 
				
			||||||
        "iron-storage": "iron",
 | 
					 | 
				
			||||||
        "gold-storage": "gold",
 | 
					 | 
				
			||||||
        "water-tank": "water",
 | 
					 | 
				
			||||||
        "acid-tank": "sulfuric-acid",
 | 
					 | 
				
			||||||
        "nitrogen-tank": "liquid-nitrogen",
 | 
					 | 
				
			||||||
        "hydrogen-tank": "hydrogen",
 | 
					 | 
				
			||||||
        "oxygen-tank": "oxygen",
 | 
					 | 
				
			||||||
        "helium3-tank": "helium-3",
 | 
					 | 
				
			||||||
        "uranium-container": "uranium",
 | 
					 | 
				
			||||||
        "oil-tank": "oil",
 | 
					 | 
				
			||||||
        "diesel-tank": "diesel",
 | 
					 | 
				
			||||||
        "kerosene-tank": "kerosene",
 | 
					 | 
				
			||||||
        "deuterium-tank": "deuterium",
 | 
					 | 
				
			||||||
        "tritium-tank": "tritium",
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    resourceArray.forEach(res => {
 | 
					 | 
				
			||||||
        res.capacity = 10_000;
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    for(const building of planet.buildings.buildings) {
 | 
					 | 
				
			||||||
        if(building.data.category === 'storage') {
 | 
					 | 
				
			||||||
            const resource = mapStorageToResource[building.data.id];
 | 
					 | 
				
			||||||
            if(resource) {
 | 
					 | 
				
			||||||
                const res = resourceArray.find(x => x.id === resource);
 | 
					 | 
				
			||||||
                if(res) res.capacity += building.level * 10_000;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
---
 | 
					---
 | 
				
			||||||
<div id="resourcebar">
 | 
					<div id="resourcebar">
 | 
				
			||||||
| 
						 | 
					@ -83,7 +52,7 @@ if(!(planet instanceof SystemManager)) {
 | 
				
			||||||
          data-res-type={resourceTypes.find(x => x.id === res.id)?.type ?? "solid"}
 | 
					          data-res-type={resourceTypes.find(x => x.id === res.id)?.type ?? "solid"}
 | 
				
			||||||
          data-res-id={res.id}
 | 
					          data-res-id={res.id}
 | 
				
			||||||
          data-res-amount={res.amount}
 | 
					          data-res-amount={res.amount}
 | 
				
			||||||
          data-res-mining-rate={res.amount >= res.capacity ? 0 : res.perHourMiningRate}
 | 
					          data-res-mining-rate={res.amount >= res.capacity ? 0 : res.rate}
 | 
				
			||||||
          data-res-capacity={res.capacity}
 | 
					          data-res-capacity={res.capacity}
 | 
				
			||||||
        >
 | 
					        >
 | 
				
			||||||
            <Image src={resourceTypes.find(x => x.id === res.id)?.icon ?? "#"} alt={res.id} class="icon" width={32} height={32} />
 | 
					            <Image src={resourceTypes.find(x => x.id === res.id)?.icon ?? "#"} alt={res.id} class="icon" width={32} height={32} />
 | 
				
			||||||
| 
						 | 
					@ -91,7 +60,7 @@ if(!(planet instanceof SystemManager)) {
 | 
				
			||||||
            <div class="resourcebar-item-tooltip">
 | 
					            <div class="resourcebar-item-tooltip">
 | 
				
			||||||
                <span class="resourcebar-item-tooltip-title">{getName(lang, 'resources', res.data.id)}</span>
 | 
					                <span class="resourcebar-item-tooltip-title">{getName(lang, 'resources', res.data.id)}</span>
 | 
				
			||||||
                <span class="resourcebar-item-tooltip-name">{getName(lang, 'general', 'avaliable')}</span><span class="resourcebar-item-tooltip-amount resourcebar-item-tooltip-avaliable">{Math.floor(res.amount).toString()}</span>
 | 
					                <span class="resourcebar-item-tooltip-name">{getName(lang, 'general', 'avaliable')}</span><span class="resourcebar-item-tooltip-amount resourcebar-item-tooltip-avaliable">{Math.floor(res.amount).toString()}</span>
 | 
				
			||||||
                <span class="resourcebar-item-tooltip-name">{getName(lang, 'general', 'production')}</span><span class={`resourcebar-item-tooltip-amount resourcebar-item-tooltip-production ${res.amount >= res.capacity ? "prod-full" : res.amount >= (res.capacity * 0.9) ? "prod-almost-full" : ""}`}>{res.amount >= res.capacity ? "0" : res.perHourMiningRate.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") ?? "0"}</span>
 | 
					                <span class="resourcebar-item-tooltip-name">{getName(lang, 'general', 'production')}</span><span class={`resourcebar-item-tooltip-amount resourcebar-item-tooltip-production ${res.amount >= res.capacity ? "prod-full" : res.amount >= (res.capacity * 0.9) ? "prod-almost-full" : ""}`}>{res.amount >= res.capacity ? "0" : res.rate.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") ?? "0"}</span>
 | 
				
			||||||
                <span class="resourcebar-item-tooltip-name">{getName(lang, 'general', 'capacity')}</span><span class="resourcebar-item-tooltip-amount resourcebar-item-tooltip-capacity">{res.capacity.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}</span>
 | 
					                <span class="resourcebar-item-tooltip-name">{getName(lang, 'general', 'capacity')}</span><span class="resourcebar-item-tooltip-amount resourcebar-item-tooltip-capacity">{res.capacity.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}</span>
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -11,7 +11,7 @@ export default class PlanetResourceManager extends ResourceManager {
 | 
				
			||||||
        this.planet = planet;
 | 
					        this.planet = planet;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async init(resourceData: { id: string, amount: number, lastUpdated: Date, perHourMiningRate: number }[]) {
 | 
					    async init(resourceData: { id: string, amount: number, lastUpdated: Date }[]) {
 | 
				
			||||||
        const resources = await getAllResources();
 | 
					        const resources = await getAllResources();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        this.resourcesDB = resources;
 | 
					        this.resourcesDB = resources;
 | 
				
			||||||
| 
						 | 
					@ -21,56 +21,47 @@ export default class PlanetResourceManager extends ResourceManager {
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    id: "coal",
 | 
					                    id: "coal",
 | 
				
			||||||
                    amount: 11,
 | 
					                    amount: 11,
 | 
				
			||||||
                    lastUpdated: new Date(),
 | 
					                    lastUpdated: new Date()
 | 
				
			||||||
                    perHourMiningRate: 11
 | 
					 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    id: "iron",
 | 
					                    id: "iron",
 | 
				
			||||||
                    amount: 22,
 | 
					                    amount: 22,
 | 
				
			||||||
                    lastUpdated: new Date(),
 | 
					                    lastUpdated: new Date()
 | 
				
			||||||
                    perHourMiningRate: 22
 | 
					 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    id: "gold",
 | 
					                    id: "gold",
 | 
				
			||||||
                    amount: 33,
 | 
					                    amount: 33,
 | 
				
			||||||
                    lastUpdated: new Date(),
 | 
					                    lastUpdated: new Date()
 | 
				
			||||||
                    perHourMiningRate: 33
 | 
					 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    id: "water",
 | 
					                    id: "water",
 | 
				
			||||||
                    amount: 44,
 | 
					                    amount: 44,
 | 
				
			||||||
                    lastUpdated: new Date(),
 | 
					                    lastUpdated: new Date()
 | 
				
			||||||
                    perHourMiningRate: 44
 | 
					 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    id: "sulfuric-acid",
 | 
					                    id: "sulfuric-acid",
 | 
				
			||||||
                    amount: 55,
 | 
					                    amount: 55,
 | 
				
			||||||
                    lastUpdated: new Date(),
 | 
					                    lastUpdated: new Date()
 | 
				
			||||||
                    perHourMiningRate: 55
 | 
					 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    id: "liquid-nitrogen",
 | 
					                    id: "liquid-nitrogen",
 | 
				
			||||||
                    amount: 66,
 | 
					                    amount: 66,
 | 
				
			||||||
                    lastUpdated: new Date(),
 | 
					                    lastUpdated: new Date()
 | 
				
			||||||
                    perHourMiningRate: 66
 | 
					 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    id: "hydrogen",
 | 
					                    id: "hydrogen",
 | 
				
			||||||
                    amount: 77,
 | 
					                    amount: 77,
 | 
				
			||||||
                    lastUpdated: new Date(),
 | 
					                    lastUpdated: new Date()
 | 
				
			||||||
                    perHourMiningRate: 77
 | 
					 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    id: "oxygen",
 | 
					                    id: "oxygen",
 | 
				
			||||||
                    amount: 88,
 | 
					                    amount: 88,
 | 
				
			||||||
                    lastUpdated: new Date(),
 | 
					                    lastUpdated: new Date()
 | 
				
			||||||
                    perHourMiningRate: 88
 | 
					 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    id: "helium-3",
 | 
					                    id: "helium-3",
 | 
				
			||||||
                    amount: 99,
 | 
					                    amount: 99,
 | 
				
			||||||
                    lastUpdated: new Date(),
 | 
					                    lastUpdated: new Date()
 | 
				
			||||||
                    perHourMiningRate: 99
 | 
					 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            ];
 | 
					            ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -83,7 +74,6 @@ export default class PlanetResourceManager extends ResourceManager {
 | 
				
			||||||
                id: resource.id,
 | 
					                id: resource.id,
 | 
				
			||||||
                amount: resource.amount,
 | 
					                amount: resource.amount,
 | 
				
			||||||
                lastUpdated: resource.lastUpdated,
 | 
					                lastUpdated: resource.lastUpdated,
 | 
				
			||||||
                perHourMiningRate: resource.perHourMiningRate,
 | 
					 | 
				
			||||||
                data: resFromDB
 | 
					                data: resFromDB
 | 
				
			||||||
            })
 | 
					            })
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
| 
						 | 
					@ -102,6 +92,12 @@ export default class PlanetResourceManager extends ResourceManager {
 | 
				
			||||||
            "hydrogen-tank": "hydrogen",
 | 
					            "hydrogen-tank": "hydrogen",
 | 
				
			||||||
            "oxygen-tank": "oxygen",
 | 
					            "oxygen-tank": "oxygen",
 | 
				
			||||||
            "helium3-tank": "helium-3",
 | 
					            "helium3-tank": "helium-3",
 | 
				
			||||||
 | 
					            "uranium-container": "uranium",
 | 
				
			||||||
 | 
					            "oil-tank": "oil",
 | 
				
			||||||
 | 
					            "diesel-tank": "diesel",
 | 
				
			||||||
 | 
					            "kerosene-tank": "kerosene",
 | 
				
			||||||
 | 
					            "deuterium-tank": "deuterium",
 | 
				
			||||||
 | 
					            "tritium-tank": "tritium",
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const output: { id: string, capacity: number }[] = [];
 | 
					        const output: { id: string, capacity: number }[] = [];
 | 
				
			||||||
| 
						 | 
					@ -118,13 +114,36 @@ export default class PlanetResourceManager extends ResourceManager {
 | 
				
			||||||
        return output;
 | 
					        return output;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    getPerHourMiningRate() {
 | 
				
			||||||
 | 
					        const mapMinerToResource: { [key: string]: string } = {
 | 
				
			||||||
 | 
					            "coal-mine": "coal",
 | 
				
			||||||
 | 
					            "iron-mine": "iron",
 | 
				
			||||||
 | 
					            "gold-mine": "gold"
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const output = [];
 | 
				
			||||||
 | 
					        const buildings = this.planet.buildings.buildings;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for(const building of buildings) {
 | 
				
			||||||
 | 
					            if(Object.keys(mapMinerToResource).includes(building.data.id)) {
 | 
				
			||||||
 | 
					                const miningRate = 20 * Math.pow(building.level, 2) + 40 * building.level + 15;
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                output.push({
 | 
				
			||||||
 | 
					                    id: mapMinerToResource[building.data.id],
 | 
				
			||||||
 | 
					                    rate: miningRate
 | 
				
			||||||
 | 
					                });
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return output;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async sync() {
 | 
					    async sync() {
 | 
				
			||||||
        await updatePlanetResources(this.planet._id, this.resources.map(res => {
 | 
					        await updatePlanetResources(this.planet._id, this.resources.map(res => {
 | 
				
			||||||
            return {
 | 
					            return {
 | 
				
			||||||
                id: res.id,
 | 
					                id: res.id,
 | 
				
			||||||
                amount: res.amount,
 | 
					                amount: res.amount,
 | 
				
			||||||
                lastUpdated: res.lastUpdated,
 | 
					                lastUpdated: res.lastUpdated,
 | 
				
			||||||
                perHourMiningRate: res.perHourMiningRate
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }));
 | 
					        }));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -65,7 +65,6 @@ export default class SystemResourceManager extends ResourceManager {
 | 
				
			||||||
                id: resource.id,
 | 
					                id: resource.id,
 | 
				
			||||||
                amount: resource.amount,
 | 
					                amount: resource.amount,
 | 
				
			||||||
                lastUpdated: null,
 | 
					                lastUpdated: null,
 | 
				
			||||||
                perHourMiningRate: 0,
 | 
					 | 
				
			||||||
                data: resFromDB
 | 
					                data: resFromDB
 | 
				
			||||||
            })
 | 
					            })
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
| 
						 | 
					@ -74,21 +73,74 @@ export default class SystemResourceManager extends ResourceManager {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    getStorageCapacities(): { id: string; capacity: number; }[] {
 | 
					    getStorageCapacities(): { id: string; capacity: number; }[] {
 | 
				
			||||||
        return this.resources.map(res => {
 | 
					        const output: { id: string, capacity: number }[] = [
 | 
				
			||||||
            return {
 | 
					            {
 | 
				
			||||||
                id: res.id,
 | 
					                id: "coal",
 | 
				
			||||||
                capacity: 10_000 //TODO: add structure for storage
 | 
					                capacity: 10_000
 | 
				
			||||||
 | 
					            }, {
 | 
				
			||||||
 | 
					                id: "iron",
 | 
				
			||||||
 | 
					                capacity: 10_000
 | 
				
			||||||
 | 
					            }, {
 | 
				
			||||||
 | 
					                id: "gold",
 | 
				
			||||||
 | 
					                capacity: 10_000
 | 
				
			||||||
 | 
					            }, {
 | 
				
			||||||
 | 
					                id: "water",
 | 
				
			||||||
 | 
					                capacity: 10_000
 | 
				
			||||||
 | 
					            }, {
 | 
				
			||||||
 | 
					                id: "sulfuric-acid",
 | 
				
			||||||
 | 
					                capacity: 10_000
 | 
				
			||||||
 | 
					            }, {
 | 
				
			||||||
 | 
					                id: "liquid-nitrogen",
 | 
				
			||||||
 | 
					                capacity: 10_000
 | 
				
			||||||
 | 
					            }, {
 | 
				
			||||||
 | 
					                id: "hydrogen",
 | 
				
			||||||
 | 
					                capacity: 10_000
 | 
				
			||||||
 | 
					            }, {
 | 
				
			||||||
 | 
					                id: "oxygen",
 | 
				
			||||||
 | 
					                capacity: 10_000
 | 
				
			||||||
 | 
					            }, {
 | 
				
			||||||
 | 
					                id: "helium-3",
 | 
				
			||||||
 | 
					                capacity: 10_000
 | 
				
			||||||
 | 
					            }, {
 | 
				
			||||||
 | 
					                id: "uranium",
 | 
				
			||||||
 | 
					                capacity: 10_000
 | 
				
			||||||
 | 
					            }, {
 | 
				
			||||||
 | 
					                id: "oil",
 | 
				
			||||||
 | 
					                capacity: 10_000
 | 
				
			||||||
 | 
					            }, {
 | 
				
			||||||
 | 
					                id: "diesel",
 | 
				
			||||||
 | 
					                capacity: 10_000
 | 
				
			||||||
 | 
					            }, {
 | 
				
			||||||
 | 
					                id: "kerosene",
 | 
				
			||||||
 | 
					                capacity: 10_000
 | 
				
			||||||
 | 
					            }, {
 | 
				
			||||||
 | 
					                id: "deuterium",
 | 
				
			||||||
 | 
					                capacity: 10_000
 | 
				
			||||||
 | 
					            }, {
 | 
				
			||||||
 | 
					                id: "tritium",
 | 
				
			||||||
 | 
					                capacity: 10_000
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        });
 | 
					        ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const storageStation = this.system.structures.structures.find(s => s.data.id === 'storage-station');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if(typeof storageStation !== 'undefined') {
 | 
				
			||||||
 | 
					            output.forEach(res => {
 | 
				
			||||||
 | 
					                res.capacity += storageStation.level * 40_000;
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return output;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    getPerHourMiningRate() { return []; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async sync() {
 | 
					    async sync() {
 | 
				
			||||||
        await updateSystemResources(this.system.data._id, this.resources.map(res => {
 | 
					        await updateSystemResources(this.system.data._id, this.resources.map(res => {
 | 
				
			||||||
            return {
 | 
					            return {
 | 
				
			||||||
                id: res.id,
 | 
					                id: res.id,
 | 
				
			||||||
                amount: res.amount,
 | 
					                amount: res.amount,
 | 
				
			||||||
                lastUpdated: res.lastUpdated,
 | 
					                lastUpdated: res.lastUpdated,
 | 
				
			||||||
                perHourMiningRate: res.perHourMiningRate
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }));
 | 
					        }));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,7 +4,6 @@ export type Resource = {
 | 
				
			||||||
    id: string,
 | 
					    id: string,
 | 
				
			||||||
    amount: number,
 | 
					    amount: number,
 | 
				
			||||||
    lastUpdated: Date | null,
 | 
					    lastUpdated: Date | null,
 | 
				
			||||||
    perHourMiningRate: number,
 | 
					 | 
				
			||||||
    data: DBResource
 | 
					    data: DBResource
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -13,7 +12,7 @@ export default abstract class ResourceManager {
 | 
				
			||||||
    resourcesDB: DBResource[] = [];
 | 
					    resourcesDB: DBResource[] = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    abstract sync(): Promise<void>;
 | 
					    abstract sync(): Promise<void>;
 | 
				
			||||||
 | 
					    abstract getPerHourMiningRate(): { id: string, rate: number }[];
 | 
				
			||||||
    abstract getStorageCapacities(): { id: string, capacity: number }[];
 | 
					    abstract getStorageCapacities(): { id: string, capacity: number }[];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    getResourceById(resId: string) {
 | 
					    getResourceById(resId: string) {
 | 
				
			||||||
| 
						 | 
					@ -22,13 +21,16 @@ export default abstract class ResourceManager {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async calculateCurrentAvailableResources() {
 | 
					    async calculateCurrentAvailableResources() {
 | 
				
			||||||
        const storage = this.getStorageCapacities();
 | 
					        const storage = this.getStorageCapacities();
 | 
				
			||||||
 | 
					        const miningRates = this.getPerHourMiningRate();
 | 
				
			||||||
        for(const res of this.resources) {
 | 
					        for(const res of this.resources) {
 | 
				
			||||||
            if(!res.lastUpdated || !res.perHourMiningRate) continue;
 | 
					            if(!res.lastUpdated) continue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const miningRate = miningRates.find(r => r.id === res.id)?.rate ?? 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            const maxStorage = 10_000 + (storage.find(s => s.id === res.id)?.capacity ?? 0);
 | 
					            const maxStorage = 10_000 + (storage.find(s => s.id === res.id)?.capacity ?? 0);
 | 
				
			||||||
            const timeDiff = Math.abs((new Date()).getTime() - res.lastUpdated.getTime());
 | 
					            const timeDiff = Math.abs((new Date()).getTime() - res.lastUpdated.getTime());
 | 
				
			||||||
            const hours = timeDiff / (1000 * 60 * 60);
 | 
					            const hours = timeDiff / (1000 * 60 * 60);
 | 
				
			||||||
            let amountToAdd = hours * res.perHourMiningRate;
 | 
					            let amountToAdd = hours * miningRate;
 | 
				
			||||||
            if(res.amount + amountToAdd > maxStorage) amountToAdd = Math.max(0, maxStorage - res.amount);
 | 
					            if(res.amount + amountToAdd > maxStorage) amountToAdd = Math.max(0, maxStorage - res.amount);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            res.amount += amountToAdd;
 | 
					            res.amount += amountToAdd;
 | 
				
			||||||
| 
						 | 
					@ -50,7 +52,6 @@ export default abstract class ResourceManager {
 | 
				
			||||||
                id: res.id,
 | 
					                id: res.id,
 | 
				
			||||||
                amount: res.amount - currentRes.amount,
 | 
					                amount: res.amount - currentRes.amount,
 | 
				
			||||||
                lastUpdated: res.lastUpdated,
 | 
					                lastUpdated: res.lastUpdated,
 | 
				
			||||||
                perHourMiningRate: res.perHourMiningRate,
 | 
					 | 
				
			||||||
                data: res.data
 | 
					                data: res.data
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
            else difference.push(res);
 | 
					            else difference.push(res);
 | 
				
			||||||
| 
						 | 
					@ -76,7 +77,6 @@ export default abstract class ResourceManager {
 | 
				
			||||||
                id: res.id,
 | 
					                id: res.id,
 | 
				
			||||||
                amount: res.amount,
 | 
					                amount: res.amount,
 | 
				
			||||||
                lastUpdated: new Date(),
 | 
					                lastUpdated: new Date(),
 | 
				
			||||||
                perHourMiningRate: 0,
 | 
					 | 
				
			||||||
                data: this.resourcesDB.find(r => r.id === res.id) as DBResource
 | 
					                data: this.resourcesDB.find(r => r.id === res.id) as DBResource
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					@ -90,7 +90,6 @@ export default abstract class ResourceManager {
 | 
				
			||||||
                id: res.id,
 | 
					                id: res.id,
 | 
				
			||||||
                amount: res.amount,
 | 
					                amount: res.amount,
 | 
				
			||||||
                lastUpdated: new Date(),
 | 
					                lastUpdated: new Date(),
 | 
				
			||||||
                perHourMiningRate: 0,
 | 
					 | 
				
			||||||
                data: this.resourcesDB.find(r => r.id === res.id) as DBResource
 | 
					                data: this.resourcesDB.find(r => r.id === res.id) as DBResource
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -89,29 +89,12 @@ export const POST: APIRoute = async({ request }) => {
 | 
				
			||||||
            id: res.id,
 | 
					            id: res.id,
 | 
				
			||||||
            amount: res.amount,
 | 
					            amount: res.amount,
 | 
				
			||||||
            lastUpdated: res.lastUpdated,
 | 
					            lastUpdated: res.lastUpdated,
 | 
				
			||||||
            perHourMiningRate: res.perHourMiningRate,
 | 
					 | 
				
			||||||
            data
 | 
					            data
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    userPlanet.resources.setAmount(resourcesAfter.map(res => { return { id: res.id, amount: res.amount } }));
 | 
					    userPlanet.resources.setAmount(resourcesAfter.map(res => { return { id: res.id, amount: res.amount } }));
 | 
				
			||||||
    userPlanet.buildings.addBuilding(building);
 | 
					    userPlanet.buildings.addBuilding(building);
 | 
				
			||||||
    if(building.data.category === "mines") {
 | 
					 | 
				
			||||||
        switch(building.data.id) {
 | 
					 | 
				
			||||||
            case 'coal-mine':
 | 
					 | 
				
			||||||
                const coalResource = userPlanet.resources.resources.find(r => r.id === "coal");
 | 
					 | 
				
			||||||
                if(coalResource) coalResource.perHourMiningRate += 10;
 | 
					 | 
				
			||||||
                break;
 | 
					 | 
				
			||||||
            case 'iron-mine':
 | 
					 | 
				
			||||||
                const ironResource = userPlanet.resources.resources.find(r => r.id === "iron");
 | 
					 | 
				
			||||||
                if(ironResource) ironResource.perHourMiningRate += 10;
 | 
					 | 
				
			||||||
                break;
 | 
					 | 
				
			||||||
            case 'gold-mine':
 | 
					 | 
				
			||||||
                const goldResource = userPlanet.resources.resources.find(r => r.id === "gold");
 | 
					 | 
				
			||||||
                if(goldResource) goldResource.perHourMiningRate += 10;
 | 
					 | 
				
			||||||
                break;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    await userPlanet.buildings.sync();
 | 
					    await userPlanet.buildings.sync();
 | 
				
			||||||
    await userPlanet.resources.sync();
 | 
					    await userPlanet.resources.sync();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -126,7 +126,6 @@ export const POST: APIRoute = async({ request }) => {
 | 
				
			||||||
            id: res.id,
 | 
					            id: res.id,
 | 
				
			||||||
            amount: res.amount,
 | 
					            amount: res.amount,
 | 
				
			||||||
            lastUpdated: res.lastUpdated,
 | 
					            lastUpdated: res.lastUpdated,
 | 
				
			||||||
            perHourMiningRate: res.perHourMiningRate,
 | 
					 | 
				
			||||||
            data
 | 
					            data
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -110,7 +110,6 @@ export const POST: APIRoute = async({ request }) => {
 | 
				
			||||||
            id: res.id,
 | 
					            id: res.id,
 | 
				
			||||||
            amount: res.amount,
 | 
					            amount: res.amount,
 | 
				
			||||||
            lastUpdated: res.lastUpdated,
 | 
					            lastUpdated: res.lastUpdated,
 | 
				
			||||||
            perHourMiningRate: res.perHourMiningRate,
 | 
					 | 
				
			||||||
            data
 | 
					            data
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -60,46 +60,45 @@ export const POST: APIRoute = async({ request }) => {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const structure = new Structure(userSystem.structures, structureObj, 1);
 | 
					    const structure = new Structure(userSystem.structures, structureObj, 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // const requirements = await structure.checkRequirements();
 | 
					    const requirements = await structure.checkRequirements();
 | 
				
			||||||
    // const resources = await building.checkRequiredResources((userPlanet.buildings.getBuildingById(buildingId)?.level ?? 0) + 1);
 | 
					    const resources = await structure.checkRequiredResources((userSystem.structures.getStructureById(structureId)?.level ?? 0) + 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // if(!requirements.canBuild || !resources) {
 | 
					    if(!requirements.canBuild || !resources) {
 | 
				
			||||||
    //     return new Response(
 | 
					        return new Response(
 | 
				
			||||||
    //         JSON.stringify({
 | 
					            JSON.stringify({
 | 
				
			||||||
    //             code: 400,
 | 
					                code: 400,
 | 
				
			||||||
    //             message: "Bad Request",
 | 
					                message: "Bad Request",
 | 
				
			||||||
    //             error: `${requirements.error} | ${resources ? "" : "Not enough resources"}`
 | 
					                error: `${requirements.error} | ${resources ? "" : "Not enough resources"}`
 | 
				
			||||||
    //         }), { status: 400 }
 | 
					            }), { status: 400 }
 | 
				
			||||||
    //     )
 | 
					        )
 | 
				
			||||||
    // }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // const resourcesDiff = await userPlanet.resources.getDifference(building.data.requirements.resources.map(res => {
 | 
					    const resourcesDiff = await userSystem.resources.getDifference(structure.data.requirements.resources.map(res => {
 | 
				
			||||||
    //     return {
 | 
					        return {
 | 
				
			||||||
    //         id: res.id,
 | 
					            id: res.id,
 | 
				
			||||||
    //         amount: Math.pow(building.data.multiplier, (userPlanet.buildings.getBuildingById(buildingId)?.level ?? 0) + 1) * res.amount
 | 
					            amount: Math.pow(structure.data.multiplier, (userSystem.structures.getStructureById(structureId)?.level ?? 0) + 1) * res.amount
 | 
				
			||||||
    //     }
 | 
					        }
 | 
				
			||||||
    // }));
 | 
					    }));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // const resourceDB = await getAllResources();
 | 
					    const resourceDB = await getAllResources();
 | 
				
			||||||
    // const resourcesAfter = resourcesDiff.map(res => {
 | 
					    const resourcesAfter = resourcesDiff.map(res => {
 | 
				
			||||||
    //     const data = resourceDB.find(r => r.id === res.id);
 | 
					        const data = resourceDB.find(r => r.id === res.id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //     if(!data) throw new Error("Resource not found");
 | 
					        if(!data) throw new Error("Resource not found");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //     return {
 | 
					        return {
 | 
				
			||||||
    //         id: res.id,
 | 
					            id: res.id,
 | 
				
			||||||
    //         amount: res.amount,
 | 
					            amount: res.amount,
 | 
				
			||||||
    //         lastUpdated: res.lastUpdated,
 | 
					            lastUpdated: res.lastUpdated,
 | 
				
			||||||
    //         perHourMiningRate: res.perHourMiningRate,
 | 
					            data
 | 
				
			||||||
    //         data
 | 
					        }
 | 
				
			||||||
    //     }
 | 
					    });
 | 
				
			||||||
    // });
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // userPlanet.resources.update(resourcesAfter);
 | 
					    userSystem.resources.setAmount(resourcesAfter.map(res => { return { id: res.id, amount: res.amount } }));
 | 
				
			||||||
    // userSystem.structures.addStructure(structure);
 | 
					    userSystem.structures.addStructure(structure);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // await userSystem.structures.sync();
 | 
					    await userSystem.structures.sync();
 | 
				
			||||||
    // await userPlanet.resources.sync();
 | 
					    await userSystem.resources.sync();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return new Response(
 | 
					    return new Response(
 | 
				
			||||||
        JSON.stringify({
 | 
					        JSON.stringify({
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,7 +5,7 @@ export default interface DBPlanet {
 | 
				
			||||||
    name: string;
 | 
					    name: string;
 | 
				
			||||||
    owner: ObjectId; // shouldn't be here
 | 
					    owner: ObjectId; // shouldn't be here
 | 
				
			||||||
    fields: number;
 | 
					    fields: number;
 | 
				
			||||||
    resources: Array<{ id: string, amount: number, lastUpdated: Date, perHourMiningRate: number }>;
 | 
					    resources: Array<{ id: string, amount: number, lastUpdated: Date }>;
 | 
				
			||||||
    buildings: Array<{ id: string, level: number, activePercent?: number }>;
 | 
					    buildings: Array<{ id: string, level: number, activePercent?: number }>;
 | 
				
			||||||
    ships: Array<{ id: string, amount: number }>;
 | 
					    ships: Array<{ id: string, amount: number }>;
 | 
				
			||||||
    defenses: Array<{ id: string, amount: number }>;
 | 
					    defenses: Array<{ id: string, amount: number }>;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue