Skip to content

Commit

Permalink
v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
JadlionHD committed Oct 30, 2023
1 parent 71d9ff9 commit fb53f6f
Show file tree
Hide file tree
Showing 12 changed files with 603 additions and 17 deletions.
8 changes: 0 additions & 8 deletions install/prebuild.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
const { exec } = require("child_process");

const plat = [
{
platform: "darwin",
arch: "x64"
},
{
platform: "linux",
arch: "x64"
},
{
platform: "win32",
arch: "ia32"
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "growtopia.js",
"version": "1.2.7",
"version": "1.3.0",
"description": "A package to create a growtopia private servers.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"test": "npm run build && npm run build:ts",
"dev": "rimraf dist && npm run build && npm run build:ts && node test/index.js",
"dev:nonative": "rimraf dist && npm run build:ts && node test/index.js",
"dev": "rimraf dist && npm run build && npm run build:ts && node test/basic-example/index.js",
"dev:nonative": "rimraf dist && npm run build:ts && node test/basic-example/index.js",
"docs:gen": "rimraf docs && typedoc",
"build": "node-gyp rebuild",
"build:ts": "tsc",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export type * from "../types/index";

export * from "./structures/Client";
export * from "./structures/Peer";
export * from "./structures/ItemsDat";
export * from "./structures/GrowApi";
export * from "./util/Constants";
export * from "./util/Utils";

Expand Down
32 changes: 27 additions & 5 deletions src/structures/Client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import EventEmitter from "eventemitter3";
import { ClientOptions, ClientType } from "../../types/client";
import { Caching, ClientOptions, ClientType } from "../../types/client";
import { PacketTypes } from "../util/Constants";
import { parseText } from "../util/Utils";
import { Peer } from "./Peer";
Expand All @@ -11,11 +11,13 @@ const Native = require("../../build/Release/gtjs.node").Client;
class Client extends EventEmitter {
public _client: ClientType;
public config: ClientOptions;
public cache: Caching;

constructor(options?: ClientOptions) {
super();

this.config = {
plugins: options.plugins ?? [],
https: {
ip: options.https?.ip ?? "127.0.0.1",
port: options.https?.port ?? 17091,
Expand All @@ -32,7 +34,13 @@ class Client extends EventEmitter {
}
};

this.config.plugins;

this._client = new Native(this.config.enet.ip, this.config.enet.port) as ClientType;

this.cache = {
players: new Map<number, number>()
};
}

public on(event: "connect", listener: (netID: number) => void): this;
Expand Down Expand Up @@ -77,22 +85,36 @@ class Client extends EventEmitter {
};

loop();
this._startWeb();
this._handleEvent();
this.emit("ready");
this.startWeb();
this.handleEvent();
} catch {
this.emit("error", new Error("Failed to initialize ENet server"));
}
}

private startWeb() {
private _startWeb() {
if (!this.config.https) return;
if (this.config.https.enable)
WebServer(this.config.https.ip, this.config.https.port, this.config.https.type2);
}

private handleEvent() {
private _handleEvent() {
// Initialize plugins
if (this.config.plugins.length) this.config.plugins.forEach((v) => v.init(this));

this.on("connect", (netID) => {
this.cache.players.set(netID, netID);
if (this.config.plugins.length) this.config.plugins.forEach((v) => v.onConnect(netID));
});

this.on("disconnect", (netID) => {
this.cache.players.delete(netID);
if (this.config.plugins.length) this.config.plugins.forEach((v) => v.onDisconnect(netID));
});

this.on("raw", (netID, data) => {
if (this.config.plugins.length) this.config.plugins.forEach((v) => v.onRaw(netID, data));
const type = data.readInt32LE();
const peer = new Peer(this, netID);

Expand Down
18 changes: 18 additions & 0 deletions src/structures/GrowApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Caching } from "../../types";
import { Client } from "./Client";

class GrowApi {
public client: Client;

constructor() {}

public init(client: Client) {
this.client = client;
}

public onConnect(netID: number) {}
public onDisconnect(netID: number) {}
public onRaw(netID: number, data: Buffer) {}
}

export { GrowApi };
Loading

0 comments on commit fb53f6f

Please sign in to comment.