Skip to content

Commit

Permalink
feat: useServerWeather, update deps, fix _id deprecation (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuyk authored Jun 5, 2024
1 parent 4543f8e commit 9d9fcac
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 40 deletions.
25 changes: 25 additions & 0 deletions docs/api/server/systems/useServerWeather.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# useServerWeather

This stores server weather during runtime, and allows for weather to be shared to other plugins.

Additionally, when these functions are used the internal RebarEvents for weather changes are invoked.

```ts
import * as alt from 'alt-server';
import { useRebar } from '@Server/index.js';

const Rebar = useRebar();
const serverWeather = Rebar.useServerWeather();

function whatever() {
// Used as a way to store the current weather on server-side for other plugins
serverWeather.set('CLEARING');

// Used as a way to show a weather forecast for the upcoming weather events
serverWeather.setForecast(['CLEAR', 'CLOUDS', 'RAIN', 'THUNDER', 'FOGGY', 'CLOUDS', 'CLEARING', 'EXTRASUNNY'])

const currentWeather = serverWeather.get();

const currentForecast = serverWeather.getForecast();
}
```
15 changes: 15 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ order: -5

# Changelog

## Version 15

### Code Changes

- Update dependencies
- Update `_id` in database functions to use a non-deprecated ObjectId handler
- Added `useServerWeather` function to allow setting weather and weather forecast
- This does not automatically sync for players, it's just a global way to set the data

### Docs Changes

- Added `useServerWeather` docs

---

## Version 14

### Code Changes
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "stuyk",
"type": "module",
"version": "14",
"version": "15",
"scripts": {
"dev": "nodemon -x pnpm start",
"dev:linux": "nodemon -x pnpm start:linux",
Expand All @@ -18,12 +18,13 @@
"@altv/types-server": "^16.2.1",
"@altv/types-shared": "^16.2.1",
"@altv/types-webview": "^16.2.1",
"@types/node": "^20.14.0",
"@altv/types-worker": "^16.2.0",
"@types/node": "^20.14.1",
"altv-pkg": "^2.7.5",
"autoprefixer": "^10.4.19",
"fast-glob": "^3.3.2",
"fkill": "^9.0.0",
"nodemon": "^3.1.2",
"nodemon": "^3.1.3",
"postcss": "^8.4.38",
"prettier": "^3.3.0",
"prettier-plugin-tailwindcss": "^0.5.14",
Expand All @@ -33,7 +34,6 @@
"tailwindcss": "^3.4.3"
},
"dependencies": {
"@altv/types-worker": "^16.2.0",
"@vitejs/plugin-vue": "^5.0.5",
"dotenv": "^16.4.5",
"mongodb": "^6.7.0",
Expand Down
59 changes: 31 additions & 28 deletions pnpm-lock.yaml

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

7 changes: 5 additions & 2 deletions src/main/client/player/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ function destroy() {
alt.clearInterval(interval);
} catch(err) {}

interval = undefined;
try {
native.setCamActive(camera, false);
} catch(err) {}

native.destroyAllCams(true);
native.setCamActive(camera, false);
native.renderScriptCams(false, false, 0, false, false, 0);
interval = undefined;
camera = undefined;
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/server/controllers/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ function isValid(entity: alt.Entity, interaction: InteractionInternal) {
valid = true;
}

if (interaction.type === 'any') {
valid = true;
}

return valid;
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/server/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export function useDatabase() {
try {
const result = await client
.collection(collection)
.findOneAndUpdate({ _id: new ObjectId(data._id) }, { $set: dataClone });
.findOneAndUpdate({ _id: ObjectId.createFromHexString(data._id) }, { $set: dataClone });
return result.ok ? true : false;
} catch (err) {
return false;
Expand All @@ -131,7 +131,7 @@ export function useDatabase() {
const client = await getClient();

try {
const result = await client.collection(collection).deleteOne({ _id: new ObjectId(_id) });
const result = await client.collection(collection).deleteOne({ _id: ObjectId.createFromHexString(_id) });
return result.deletedCount >= 1;
} catch (err) {
return false;
Expand All @@ -151,7 +151,7 @@ export function useDatabase() {
* @param {string} collection
* @return {(Promise<T | undefined>)}
*/
async function get<T extends { [key: string]: any; _id?: string | ObjectId }>(
async function get<T extends { [key: string]: any; _id?: string }>(
dataToMatch: Partial<T>,
collection: string,
): Promise<T | undefined> {
Expand All @@ -161,7 +161,7 @@ export function useDatabase() {
const dataLookup: any = { ...dataToMatch };

if (dataToMatch._id) {
dataLookup._id = new ObjectId(dataToMatch._id);
dataLookup._id = ObjectId.createFromHexString(dataToMatch._id)
}

const document = await client.collection(collection).findOne<T>(dataLookup);
Expand Down Expand Up @@ -195,7 +195,7 @@ export function useDatabase() {
const dataLookup: any = { ...dataToMatch };

if (dataToMatch._id) {
dataLookup._id = new ObjectId(dataToMatch._id);
dataLookup._id = ObjectId.createFromHexString(dataToMatch._id);
}

const cursor = await client.collection(collection).find<T>(dataLookup);
Expand Down Expand Up @@ -246,7 +246,7 @@ export function useDatabase() {
const client = await getClient();

try {
const result = await client.collection(collection).deleteOne({ _id: new ObjectId(_id) });
const result = await client.collection(collection).deleteOne({ _id: ObjectId.createFromHexString(_id) });
return result.acknowledged;
} catch (err) {
return false;
Expand Down
5 changes: 5 additions & 0 deletions src/main/server/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import * as alt from 'alt-server';
import { Account } from '@Shared/types/account.js';
import { Character } from '@Shared/types/character.js';
import { Vehicle } from '@Shared/types/vehicle.js';
import { Weathers } from '@Shared/data/weathers.js';

type RebarEvents = {
'weather-forecast-changed': (weather: Weathers[])=> void;
'weather-changed': (weather: Weathers) => void;
'time-changed': (hour: number, minute: number, second: number) => void;
'time-second-changed': (minute: number) => void;
'time-minute-changed': (minute: number) => void;
Expand All @@ -17,6 +20,8 @@ type RebarEvents = {
type EventCallbacks<K extends keyof RebarEvents> = { [key in K]: RebarEvents[K][] };

const eventCallbacks: EventCallbacks<keyof RebarEvents> = {
'weather-forecast-changed': [],
'weather-changed': [],
'time-changed': [],
'time-second-changed': [],
'time-hour-changed': [],
Expand Down
2 changes: 2 additions & 0 deletions src/main/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import { check, hash } from './utility/password.js';
import { useVehicle } from './vehicle/index.js';
import { useProtectCallback } from './utility/protectCallback.js';
import { useServerTime } from './systems/serverTime.js';
import { useServerWeather } from './systems/serverWeather.js';

export function useRebar() {
return {
Expand Down Expand Up @@ -135,6 +136,7 @@ export function useRebar() {
},
usePlayer,
useServerTime,
useServerWeather,
utility: {
sha256,
sha256Random,
Expand Down
Loading

0 comments on commit 9d9fcac

Please sign in to comment.