Skip to content

Commit

Permalink
V2.0.0 DEV 2 RELEASE
Browse files Browse the repository at this point in the history
  • Loading branch information
Abadima committed Aug 7, 2023
1 parent 7406940 commit 2fadd59
Show file tree
Hide file tree
Showing 20 changed files with 165 additions and 48 deletions.
6 changes: 3 additions & 3 deletions Mini_Docs/add.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<dt><a href="#addLevel">addLevel(userId, guildId, level, username)</a> ⇒ <code>Promise.&lt;UserResult&gt;</code></dt>
<dd><p>Add XP to a user</p>
</dd>
<dt><a href="#addXP">addXP(userId, guildId, xpData, username)</a> ⇒ <code>Promise.&lt;UserResult&gt;</code></dt>
<dt><a href="#addXP">addXP(userId, guildId, xpData, username)</a> ⇒ <code>Promise.&lt;XPResult&gt;</code></dt>
<dd><p>Add XP to a user.</p>
</dd>
</dl>
Expand Down Expand Up @@ -32,12 +32,12 @@ Add XP to a user

<a name="addXP"></a>

## addXP(userId, guildId, xpData, username) ⇒ <code>Promise.&lt;UserResult&gt;</code>
## addXP(userId, guildId, xpData, username) ⇒ <code>Promise.&lt;XPResult&gt;</code>

Add XP to a user.

**Kind**: global function
**Returns**: <code>Promise.&lt;UserResult&gt;</code> - - Object of user data on success.
**Returns**: <code>Promise.&lt;XPResult&gt;</code> - - Object of user data on success.
**Throws**:

- <code>XpFatal</code> - If parameters are not provided correctly.
Expand Down
1 change: 0 additions & 1 deletion Mini_Docs/deprecated/rank.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<a name="rank"></a>

## ~~rank(message, userId, _guildId, options) ⇒ <code>Promise.&lt;{attachment: Buffer, description: string, name:

string}&gt;</code>~~

***Deprecated***
Expand Down
18 changes: 9 additions & 9 deletions Mini_Docs/functions/utilities.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
## Functions

<dl>
<dt><a href="#convert">convert(type, value)</a> ⇒ <code>number</code></dt>
<dt><a href="#convertFrom">convertFrom(type)</a> ⇒ <code>number</code></dt>
<dd><p>Convert XP to level and vice versa.</p>
</dd>
<dt><a href="#updateOptions">updateOptions(clientOptions)</a> ⇒ <code>void</code></dt>
<dd><p>Updates the options of the XP client.</p>
</dd>
</dl>

<a name="convert"></a>
<a name="convertFrom"></a>

## convert(type, value) ⇒ <code>number</code>
## convertFrom(type) ⇒ <code>number</code>

Convert XP to level and vice versa.

Expand All @@ -21,12 +21,12 @@ Convert XP to level and vice versa.

- <code>XpFatal</code> If an invalid type is provided or if the value is not provided.

**Link**: `Documentation:` https://simplyxp.js.org/docs/convert
**Link**: `Documentation:` https://simplyxp.js.org/docs/utilities/convert

| Param | Type | Description |
|-------|---------------------------------------------------------------|---------------------------------------------------------|
| type | <code>&quot;xp&quot;</code> \| <code>&quot;level&quot;</code> | Type to coreFunctions from. Use either "xp" or "level". |
| value | <code>number</code> | Value to coreFunctions. |
| Param | Type | Description |
|--------|---------------------------------------------------------------|----------------------------------------|
| value. | <code>number</code> | |
| type | <code>&quot;xp&quot;</code> \| <code>&quot;level&quot;</code> | Type to convert from (Default: level). |

<a name="updateOptions"></a>

Expand All @@ -40,7 +40,7 @@ Updates the options of the XP client.

- <code>XpFatal</code> If an invalid option is provided.

**Link**: `Documentation:` https://simplyxp.js.org/docs/updateOptions
**Link**: `Documentation:` https://simplyxp.js.org/docs/utilities/updateOptions

| Param | Type | Description |
|---------------|-------------------------------|----------------------------|
Expand Down
6 changes: 3 additions & 3 deletions Mini_Docs/set.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<dt><a href="#setLevel">setLevel(userId, guildId, level, username)</a> ⇒ <code>Promise.&lt;UserResult&gt;</code></dt>
<dd><p>Set user level</p>
</dd>
<dt><a href="#setXP">setXP(userId, guildId, xpData, username)</a> ⇒ <code>Promise.&lt;UserResult&gt;</code></dt>
<dt><a href="#setXP">setXP(userId, guildId, xpData, username)</a> ⇒ <code>Promise.&lt;XPResult&gt;</code></dt>
<dd><p>Set user XP</p>
</dd>
</dl>
Expand Down Expand Up @@ -32,12 +32,12 @@ Set user level

<a name="setXP"></a>

## setXP(userId, guildId, xpData, username) ⇒ <code>Promise.&lt;UserResult&gt;</code>
## setXP(userId, guildId, xpData, username) ⇒ <code>Promise.&lt;XPResult&gt;</code>

Set user XP

**Kind**: global function
**Returns**: <code>Promise.&lt;UserResult&gt;</code> - - Object of user data on success
**Returns**: <code>Promise.&lt;XPResult&gt;</code> - - Object of user data on success
**Throws**:

- <code>XpFatal</code> - If parameters are not provided correctly
Expand Down
21 changes: 21 additions & 0 deletions Mini_Docs/types/LevelRoleResult.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# LevelRoleResult

### Properties

- `_id` (string, optional): The ID of the document.
- `guild` (string): The guild ID of the level role.
- `level` (number): The level of the level role.
- `roles` (string[] | string): The roles of the level role.
- `timestamp` (string): The timestamp of the level role.

### Example

```typescript
export type LevelRoleResult = {
_id?: string,
guild: string;
level: number;
roles: Array<string>;
timestamp: string;
}
```
23 changes: 23 additions & 0 deletions Mini_Docs/types/UserResult.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# UserResult

### Properties

- `_id` (string, optional): The ID of the document.
- `user` (string): The ID of the user.
- `name` (string, optional): The name of the user.
- `guild` (string): The ID of the guild.
- `level` (number): The level of the user in the guild.
- `xp` (number): The XP of the user in the guild.

### Example

```typescript
export interface UserResult {
_id?: string,
user: string;
name?: string;
guild: string;
level: number;
xp: number;
}
```
19 changes: 19 additions & 0 deletions Mini_Docs/types/XPResult.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# XPResult

### Properties

- `_id` (string, optional): The ID of the document.
- `user` (string): The ID of the user.
- `name` (string, optional): The name of the user.
- `guild` (string): The ID of the guild.
- `level` (number): The level of the user in the guild.
- `xp` (number): The XP of the user in the guild.
- `hasLevelledUp` (boolean): Whether or not the user has levelled up.

### Example

```typescript
interface XPResult extends UserResult {
hasLevelledUp: boolean;
}
```
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ yarn add simply-xp@dev
- Added `debug`, `auto_create`, `auto_purge` options for `connect()` function
- Added `db` class for extended database functionality
- Added `leaderboardCard()` function
- Added `coreFunctions()` function
- Added `convertFrom()` function
- Added `migrate` class

# 🎉 V2 Changes 🎉
Expand All @@ -67,4 +67,4 @@ yarn add simply-xp@dev
- `rank()` is **deprecated**, use `rankCard()` instead.
- `rankCard()` Requires completely new arguments.
- `roleSetup()` functions loses `client` argument.
- `leaderboard()` loses `client` argument.
- `leaderboard()` loses `client` argument, and returns `user` instead of `userID`
Binary file modified Tests/leaderboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Tests/rankcard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# VERSION 2@DEV CHANGELOGS

## [DEV 2](https://github.com/Abadima/simply-xp/releases/tag/v2.0.0-dev.2)

### Additions

- Added `MongoDB` V4 Support
- `setXP()` and `addXP()` now also return `hasLevelledUp` property

### ⚠️ Breaking Changes

- `convert(type, value)` => `convertFrom(value, type)`

### Changes

- Updated JSDocs, changed some types to interfaces.
- Optimized font file, lowers package size.

## [DEV 1](https://github.com/Abadima/simply-xp/releases/tag/v2.0.0-dev.1)

### Additions
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simply-xp",
"version": "2.0.0-dev.1",
"version": "2.0.0-dev.2",
"description": "The easiest way to implement xp system",
"main": "lib/xp.js",
"scripts": {
Expand Down
Binary file added src/Fonts/Baloo-Regular.eot
Binary file not shown.
Binary file removed src/Fonts/Baloo-Regular.ttf
Binary file not shown.
35 changes: 26 additions & 9 deletions src/add.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {convert} from "./functions/utilities";
import {convertFrom} from "./functions/utilities";
import {XpFatal} from "./functions/xplogs";
import {db, UserResult} from "./functions/database";
import {xp} from "../xp";
Expand Down Expand Up @@ -30,7 +30,7 @@ export async function addLevel(userId: string, guildId: string, level: number, u
if (xp.auto_create && username) return await db.createOne({
collection: "simply-xps",
data: {
guild: guildId, user: userId, name: username, level, xp: convert("level", level)
guild: guildId, user: userId, name: username, level, xp: convertFrom(level)
}
}) as UserResult;
else throw new XpFatal({function: "addLevel()", message: "User does not exist"});
Expand All @@ -43,12 +43,19 @@ export async function addLevel(userId: string, guildId: string, level: number, u
data: {
user: userId, guild: guildId,
level: user.level + level,
xp: convert("level", level + user.level)
xp: convertFrom(level + user.level)
}
}) as UserResult;
}
}

/**
* XP Results
* @property {boolean} hasLevelledUp - Whether the user has levelled up or not.
*/
interface XPResult extends UserResult {
hasLevelledUp: boolean;
}

/**
* Add XP to a user.
Expand All @@ -58,10 +65,12 @@ export async function addLevel(userId: string, guildId: string, level: number, u
* @param {number | {min: number, max: number}} xpData - The XP to add, can be a number or an object with min and max properties.
* @param {string} username - Username to use if auto_create is enabled.
* @link `Documentation:` https://simplyxp.js.org/docs/addxp
* @returns {Promise<UserResult>} - Object of user data on success.
* @returns {Promise<XPResult>} - Object of user data on success.
* @throws {XpFatal} - If parameters are not provided correctly.
*/
export async function addXP(userId: string, guildId: string, xpData: number | { min: number, max: number }, username?: string): Promise<UserResult> {
export async function addXP(userId: string, guildId: string,
xpData: number | { min: number, max: number }, username?: string): Promise<XPResult> {

if (typeof xpData !== "number" && (typeof xp !== "object" || !xpData.min || !xpData.max)) throw new XpFatal({
function: "addXP()", message: "XP is not a number or object, make sure you are using the correct syntax"
});
Expand All @@ -76,25 +85,33 @@ export async function addXP(userId: string, guildId: string, xpData: number | {
collection: "simply-xps", data: {user: userId, guild: guildId}
}) as UserResult;

let data;

if (!user) {
if (xp.auto_create && username) return await db.createOne({
if (xp.auto_create && username) data = await db.createOne({
collection: "simply-xps",
data: {
guild: guildId, user: userId, name: username, level: convert("xp", xpData), xp: xpData
guild: guildId, user: userId, name: username, level: convertFrom(xpData, "xp"), xp: xpData
}
}).catch((err) => {
throw new XpFatal({function: "addXP()", message: err.stack});
}) as UserResult;
else throw new XpFatal({function: "addXP()", message: "User does not exist"});
} else {
return await db.updateOne({
data = await db.updateOne({
collection: "simply-xps",
data: {user: userId, guild: guildId}
}, {
collection: "simply-xps",
data: {
user: userId, guild: guildId,
level: convert("xp", user.xp + xpData),
level: convertFrom(user.xp + xpData, "xp"),
xp: user.xp + xpData
}
}).catch((err) => {
throw new XpFatal({function: "addXP()", message: err.stack});
}) as UserResult;
}

return {...data, hasLevelledUp: data.level > user.level};
}
10 changes: 6 additions & 4 deletions src/cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Canvas, createCanvas, GlobalFonts, Image, loadImage, SKRSContext2D} from
import {XpFatal, XpLog} from "./functions/xplogs";
import {db} from "./functions/database";
import {User} from "./leaderboard";
import {convert} from "./functions/utilities";
import {convertFrom} from "./functions/utilities";
import {join} from "path";
import {create, xp} from "../xp";

Expand Down Expand Up @@ -106,7 +106,7 @@ export async function rankCard(guild: { id: string, name: string }, user: UserOp
});
}

GlobalFonts.registerFromPath(options?.font || join(__dirname, "Fonts", "Baloo-Regular.ttf"), "Sans Serif");
GlobalFonts.registerFromPath(options?.font || join(__dirname, "Fonts", "Baloo-Regular.eot"), "Sans Serif");

if (!cachedRankImage) cachedRankImage = await loadImage(options?.background || "https://i.ibb.co/dck2Tnt/rank-card.webp");

Expand All @@ -128,8 +128,8 @@ export async function rankCard(guild: { id: string, name: string }, user: UserOp
TextEXP = shortener(dbUser.xp) + ` ${locales.xp}`,
LvlText = locales.level + ` ${shortener(dbUser.level)}`,
TextXpNeeded = "{current} / {needed}",
nextLevelXP = convert("level", dbUser.level + 1),
currentLevelXP = convert("level", dbUser.level),
nextLevelXP = convertFrom(dbUser.level + 1),
currentLevelXP = convertFrom(dbUser.level),
progress = (((100 * (dbUser.xp - currentLevelXP)) / (nextLevelXP - currentLevelXP)) * 660) / 100;


Expand Down Expand Up @@ -292,6 +292,8 @@ export async function leaderboardCard(data: Array<User>, options: LeaderboardOpt
if (!cachedLeaderboardArtwork && options?.artworkImage) cachedLeaderboardArtwork = await loadImage(options.artworkImage);
if (!cachedLeaderboardImage && options?.backgroundImage) cachedLeaderboardImage = await loadImage(options.backgroundImage);

GlobalFonts.registerFromPath(options?.font || join(__dirname, "Fonts", "Baloo-Regular.eot"), "Sans Serif");

if (!locales.level) locales.level = "LEVEL";
if (!locales.members) locales.members = "Members";

Expand Down
11 changes: 10 additions & 1 deletion src/functions/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ export interface UserOptions {
};
}

export type UserResult = {
/**
* The result of a user document.
* @property {string} [_id] - The ID of the document.
* @property {string} user - The user ID.
* @property {string} [name] - The username.
* @property {string} guild - The guild ID.
* @property {number} level - The level.
* @property {number} xp - The XP.
*/
export interface UserResult {
_id?: string,
user: string;
name?: string;
Expand Down
10 changes: 5 additions & 5 deletions src/functions/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ interface NewClientOptions {
/**
* Convert XP to level and vice versa.
*
* @param {"xp" | "level"} type - Type to coreFunctions from. Use either "xp" or "level".
* @param {number} value - Value to coreFunctions.
* @param {number} value.
* @param {"xp" | "level"} type - Type to convert from (Default: level).
* @link `Documentation:` https://simplyxp.js.org/docs/utilities/convert
* @returns {number} - The converted value. (XP to level or level to XP)
* @throws {XpFatal} If an invalid type is provided or if the value is not provided.
*/
export function convert(type: "xp" | "level", value: number): number {
export function convertFrom(value: number, type: "xp" | "level" = "level"): number {
if (!value) throw new XpFatal({function: "convertFrom()", message: "Value was not provided"});
if (type !== "xp" && type !== "level") throw new XpFatal({
function: "convert()", message: "Invalid type provided"
});
if (!value) throw new XpFatal({function: "coreFunctions()", message: "Value was not provided"});

if (type === "level") return Math.pow(value / 0.1, 2);
if (type === "xp") return Math.floor(0.1 * Math.sqrt(value));

throw new XpFatal({function: "coreFunctions()", message: "Invalid type provided"});
throw new XpFatal({function: "convertFrom()", message: "Invalid type provided"});
}


Expand Down
Loading

0 comments on commit 2fadd59

Please sign in to comment.