Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuyk authored Jul 2, 2023
1 parent 9d3e273 commit 71224e6
Showing 1 changed file with 2 additions and 126 deletions.
128 changes: 2 additions & 126 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,130 +17,6 @@ This library is highly geared towards `TypeScript`.
npm i @stuyk/cross-resource-cache
```

# Examples
# Documentation

## Connect to Database

* This should be the only `connection` instance for your whole gamemode.

* The other database instances in other resources will connect automatically.

In your `server` folder you need to import this library and start the MongoDB connection process.

Make sure you have a MongoDB Community Server instance setup locally, or through MongoDB Atlas.

_The example belows uses a URL for localhost_

```ts
import * as alt from 'alt-server';
import * as crc from '@stuyk/cross-resource-cache';

crc.database.connect('mongodb://127.0.0.1:27017', 'mydatabase');

crc.onReady(() => {
alt.log('Connected!');
});
```

## Sync Data

For the sake of this tutorial; imagine a single account is one character.

The function below is a simple `login/register` function combined into one.

* If the `username` exists, and the password is correct. They login.

* If the `username` does not exist it will use the password to register.

```ts
async function loginOrRegister(player: alt.Player, username: string, password: string) {
if (!username || !password) {
console.log(`username or password not provided`);
return;
}

let document = await crc.database.get<Account>({ username }, 'account');
if (!document) {
const accountIdentifier = await crc.database.create<Account>(
{
username,
password: crc.utility.password.create(password),
},
'account'
);

document = await crc.database.get<Account>({ _id: accountIdentifier }, 'account');
}

if (!crc.utility.password.check(password, document.password)) {
console.log(`invalid password`);
return;
}

await crc.data.sync(player, document._id, 'account');
}
```

## Writing Data

Simply put, when you `sync` data on an entity like a `Player`; you can now set data on the player and it will automatically save it to the database.

Here's how we can save cash values, or simply modify them.

```ts
async function addToBank(player: alt.Player, amount: number) {
let originalValue = crc.data.getValue<number | undefined>(player, 'bank');
if (!originalValue) {
originalValue = 0;
}

await crc.data.setValue(player, 'bank', originalValue + amount);
}
```

## Event Driven Development

How about some events based on when a `key` for an entity is set.

Automatically synchronize new changes with ease.

```ts
interface Appearance {
face1?: number;
face2?: number;
hair?: number;
}

async function setHairStyle(player: alt.Player, value: number) {
let appearance = crc.data.getValue<Appearance>(player, 'appearance');
if (!appearance) {
appearance = {};
}

await crc.data.setValue(player, 'appearance', appearance);
}

crc.events.onKeyChange('appearance', (entity: alt.Player, newData: Appearance, oldData: Appearance) => {
if (!(entity instanceof alt.Player)) {
return;
}

if (newData.hair) {
entity.setClothes(2, newData.hair, 0, 0);
}

// Do more processing
});
```

## Data Synced to Client

Make certain data available on client-side with ease.

```ts
crc.data.setKeysAsLocal(['cash', 'bank'])

crc.data.setKeysAsStreamSynced(['object-attachments']);

crc.data.setKeysAsSynced(['name'])
```
[Check out the Documentation on GitHub](https://github.com/altv-crc/lib-cross-resource-cache/wiki)

0 comments on commit 71224e6

Please sign in to comment.