Implement resource manager

This commit is contained in:
Aelita4 2024-01-30 14:57:23 +01:00
parent 564a3942a3
commit 4bfa3bbb49
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
6 changed files with 53 additions and 29 deletions

View File

@ -1,6 +1,6 @@
---
import { ObjectId } from 'mongodb';
import { getUserResources } from '../lib/db/users';
import { getUserResources } from '../lib/utils/resourceManager';
import { getHighestWeightedLanguage, getLocales } from '../lib/lang/langDriver';
import resourceTypes from '../lib/data/resources.json';

View File

@ -39,31 +39,9 @@ export const getUserByAccessToken = async(accessToken: string | AccessToken): Pr
} else return getUserById(accessToken.user as ObjectId)
}
export const getUserResources = async (id: ObjectId): Promise<Resources> => {
const users = await Users();
const user = await users.findOne({ _id: id });
const defaultResources: Resources = {
coal: 0,
iron: 0,
gold: 0
};
return user?.resources === undefined ? defaultResources : user?.resources;
}
export const updateUserResources = async (username: string, resources: any) => {
const users = await Users();
await users.updateOne({ username }, {
$set: {
resources
}
});
}
export const getUserBuildings = async (username: string): Promise<Array<Building>> => {
const users = await Users();
const user = await users.findOne({ username });
export const getUserBuildings = async (user: User): Promise<Array<Building>> => {
// const users = await Users();
// const user = await users.findOne({ username });
const buildings: Array<Building> = [];

View File

@ -0,0 +1,41 @@
import { ObjectId } from "mongodb"
import { getUserById } from "../db/users";
import { Users } from "../db/mongodb";
import type DBResource from "../../types/DBResource";
import type Resource from "../../types/Resource";
export const getUserResources = async (id: ObjectId): Promise<Array<DBResource>> => {
const user = await getUserById(id);
const defaultResources: Array<DBResource> = [];
return user?.resources === undefined ? defaultResources : user?.resources;
}
export const updateUserResources = async (id: ObjectId, resources: Array<DBResource>) => {
const users = await Users();
await users.updateOne({ _id: id }, {
$set: {
resources
}
});
}
export const calculateCurrentAvailableResources = async (id: ObjectId): Promise<Array<Resource>> => {
const user = await getUserById(id);
if(user === null) return [];
const userResources = await getUserResources(id);
userResources.forEach(res => {
const timeDiff = Math.abs((new Date()).getTime() - res.lastUpdated.getTime());
const hours = timeDiff / (1000 * 60 * 60);
const amountToAdd = hours * res.perHourMiningRate;
res.amount += amountToAdd;
res.lastUpdated = new Date();
});
updateUserResources(id, userResources);
return userResources;
}

6
src/types/DBResource.ts Normal file
View File

@ -0,0 +1,6 @@
import type Resource from "./Resource";
export default interface DBResource extends Resource {
lastUpdated: Date;
perHourMiningRate: number;
}

View File

@ -1,5 +1,4 @@
export default interface Resource {
name: string;
type: "solid" | "liquid" | "gas";
amount: number;
}

View File

@ -1,6 +1,6 @@
import type { ObjectId } from "mongodb";
import type Resources from "./Resources";
import type Building from "./Building";
import type DBResource from "./DBResource";
export default interface User {
_id: ObjectId;
@ -8,7 +8,7 @@ export default interface User {
email: string;
password: string;
lastLogin: Date;
resources: Resources;
resources: Array<DBResource>;
buildings: Array<Building>;
createdAt: Date;
updatedAt: Date;