Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
Fix Vehicle ID Increment
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuyk committed Mar 26, 2023
1 parent 1b4c82f commit 454eef8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/core/server/document/character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const DEBUG_MODE = false; // Use this to see what state is being set.

/**
* Binds a player identifier to a Character document.
*
* This document is cleared on disconnected automatically.
*
* This should be the first thing you do after having a user authenticate and select a character.
*
* @param {alt.Player} player
Expand Down Expand Up @@ -105,8 +107,22 @@ export function getField<T = {}, ReturnType = any>(

/**
* Sets a player document value, and saves it automatically to the selected character's database.
*
* Automatically calls all callbacks associated with the field name.
*
* @example
* ```ts
* await Athena.document.character.set(somePlayer, 'cash', 50);
*
* // Alternatively
*
* interface CustomCharacter {
* someKey: string;
* }
*
* await Athena.document.character.set<CustomCharacter>(somePlayer, 'someKey', 'hello world');
* ```
*
* @template T
* @param {alt.Player} player
* @param {(keyof KnownKeys<Character & T>)} fieldName
Expand Down
34 changes: 34 additions & 0 deletions src/core/server/getters/vehicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,40 @@ import * as players from './players';
export function byID(id: number): alt.Vehicle | undefined {
return alt.Vehicle.all.find((x) => x.id === id);
}

/**
* Return a spawned vehicle by is incremental data id.
*
* This only works for persistent vehicles.
*
* @export
* @param {(number | string)} id
* @return {(alt.Vehicle | undefined)}
*/
export function byIncrementalDatabaseID(id: number | string): alt.Vehicle | undefined {
if (typeof id === 'string') {
id = parseInt(id);
}

if (isNaN(id)) {
return undefined;
}

return alt.Vehicle.all.find((x) => {
const data = Athena.document.vehicle.get(x);

if (typeof data === 'undefined') {
return false;
}

if (data.id !== id) {
return false;
}

return true;
});
}

/**
* Get a vehicle based on their database _id
* May return undefined if the vehicle is not currently spawned.
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/systems/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export async function increase(key: string, increaseByValue = 1, startValue = 0)
await isReady();

const document = await Database.fetchData<IGlobal>('_id', uid, CollectionName);
if (!document[key]) {
if (typeof document[key] === 'undefined') {
document[key] = startValue;
} else {
if (typeof document[key] !== 'number') {
Expand Down
2 changes: 2 additions & 0 deletions src/core/server/vehicle/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export async function toPlayer(

await Athena.systems.global.increase('vehicleId');
const id = await Athena.systems.global.getKey<number>('vehicleId');

const ownedVehicle: OwnedVehicle = {
dimension: 0,
keys: options && options.keys ? options.keys : [],
Expand Down Expand Up @@ -146,6 +147,7 @@ export async function toDatabase(

await Athena.systems.global.increase('vehicleId');
const id = await Athena.systems.global.getKey<number>('vehicleId');

const ownedVehicle: OwnedVehicle = {
dimension: 0,
keys: options && options.keys ? options.keys : [],
Expand Down

0 comments on commit 454eef8

Please sign in to comment.