Implement resource manager
This commit is contained in:
parent
564a3942a3
commit
4bfa3bbb49
|
@ -1,6 +1,6 @@
|
||||||
---
|
---
|
||||||
import { ObjectId } from 'mongodb';
|
import { ObjectId } from 'mongodb';
|
||||||
import { getUserResources } from '../lib/db/users';
|
import { getUserResources } from '../lib/utils/resourceManager';
|
||||||
import { getHighestWeightedLanguage, getLocales } from '../lib/lang/langDriver';
|
import { getHighestWeightedLanguage, getLocales } from '../lib/lang/langDriver';
|
||||||
|
|
||||||
import resourceTypes from '../lib/data/resources.json';
|
import resourceTypes from '../lib/data/resources.json';
|
||||||
|
|
|
@ -39,31 +39,9 @@ export const getUserByAccessToken = async(accessToken: string | AccessToken): Pr
|
||||||
} else return getUserById(accessToken.user as ObjectId)
|
} else return getUserById(accessToken.user as ObjectId)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getUserResources = async (id: ObjectId): Promise<Resources> => {
|
export const getUserBuildings = async (user: User): Promise<Array<Building>> => {
|
||||||
const users = await Users();
|
// const users = await Users();
|
||||||
const user = await users.findOne({ _id: id });
|
// const user = await users.findOne({ username });
|
||||||
|
|
||||||
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 });
|
|
||||||
|
|
||||||
const buildings: Array<Building> = [];
|
const buildings: Array<Building> = [];
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
import type Resource from "./Resource";
|
||||||
|
|
||||||
|
export default interface DBResource extends Resource {
|
||||||
|
lastUpdated: Date;
|
||||||
|
perHourMiningRate: number;
|
||||||
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
export default interface Resource {
|
export default interface Resource {
|
||||||
name: string;
|
name: string;
|
||||||
type: "solid" | "liquid" | "gas";
|
|
||||||
amount: number;
|
amount: number;
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
import type { ObjectId } from "mongodb";
|
import type { ObjectId } from "mongodb";
|
||||||
import type Resources from "./Resources";
|
|
||||||
import type Building from "./Building";
|
import type Building from "./Building";
|
||||||
|
import type DBResource from "./DBResource";
|
||||||
|
|
||||||
export default interface User {
|
export default interface User {
|
||||||
_id: ObjectId;
|
_id: ObjectId;
|
||||||
|
@ -8,7 +8,7 @@ export default interface User {
|
||||||
email: string;
|
email: string;
|
||||||
password: string;
|
password: string;
|
||||||
lastLogin: Date;
|
lastLogin: Date;
|
||||||
resources: Resources;
|
resources: Array<DBResource>;
|
||||||
buildings: Array<Building>;
|
buildings: Array<Building>;
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
updatedAt: Date;
|
updatedAt: Date;
|
||||||
|
|
Loading…
Reference in New Issue