diff --git a/README.md b/README.md index 59992e6..6686253 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ npm i growtopia.js ```js const { Client, TextPacket, Peer } = require("growtopia.js"); -const client = new Client("127.0.0.1", 17091, { https: true }); +const client = new Client(); client.on("ready", () => { console.log(`Starting ENet server ${client.config.port} on ${client.config.ip}`); @@ -36,14 +36,17 @@ client.on("error", (err) => { client.on("connect", (netID) => { console.log(`Connected netID ${netID}`); const peer = new Peer(client, netID); - peer.send(TextPacket.from(0x1)); // Send Hello Packet + peer.send(TextPacket.from(0x1)); }); client.on("disconnect", (netID) => { console.log(`Disconnected netID ${netID}`); }); -// Receive Game message packet +client.on("raw", (netID, data) => { + console.log("raw", data); +}); + client.on("action", (peer, data) => { console.log(`Peer (${peer.data.netID}) action`, { data }); if (data.action === "quit") { @@ -51,17 +54,17 @@ client.on("action", (peer, data) => { } }); -// Receive Tank update packet client.on("tank", (peer, tank) => { console.log(`Peer (${peer.data.netID}) tank`, { tank }); }); -// Receive Generic text packet client.on("text", (peer, data) => { + console.log(peer.ping); + console.log(`Peer (${peer.data.netID}) text`, { data }); }); -client.listen(1024); +client.listen(); ``` ## Links diff --git a/lib/client.cc b/lib/client.cc index a8600eb..0a57d9b 100644 --- a/lib/client.cc +++ b/lib/client.cc @@ -50,6 +50,7 @@ void Client::create(NAPI_CB) Napi::Env env = info.Env(); uint32_t maxPeers = info[0].As().Uint32Value(); + bool isClient = info[1].As().Value(); if (enet_initialize() != 0) return Napi::Error::New(env, "ENet failed to initialize").ThrowAsJavaScriptException(); @@ -61,7 +62,10 @@ void Client::create(NAPI_CB) this->client = enet_host_create(&address, maxPeers, 2, 0, 0); this->client->checksum = enet_crc32; - this->client->usingNewPacket = usingNewPacket; + if (isClient) + this->client->usingNewPacket = usingNewPacket; + else + this->client->usingNewPacketForServer = usingNewPacket; enet_host_compress_with_range_coder(this->client); } diff --git a/package.json b/package.json index 6f2d87d..fbd0390 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "growtopia.js", - "version": "1.1.7", + "version": "1.2.0", "description": "A package to create a growtopia private servers.", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/structures/Client.ts b/src/structures/Client.ts index dc531d2..634f342 100644 --- a/src/structures/Client.ts +++ b/src/structures/Client.ts @@ -10,17 +10,31 @@ const Native = require("../../lib/build/Release/index.node").Client; class Client extends EventEmitter { public _client: ClientType; - public config: { ip: string; port: number }; + public config: ClientOptions; - constructor(ip = "127.0.0.1", port = 17091, public options: ClientOptions) { + constructor(options: ClientOptions) { super(); - this._client = new Native(ip, port) as ClientType; - this.config = { - ip, - port - }; - this.options = Object.assign({ https: false }, options); + this.config = Object.assign( + { + https: { + enable: true, + url: "127.0.0.1", + type2: false + }, + ip: "127.0.0.1", + port: 17091, + enet: { + maxPeers: 1024, + useNewPacket: { + asClient: false + } + } + }, + options + ); + + this._client = new Native(this.config.ip, this.config.port) as ClientType; } public on(event: "connect", listener: (netID: number) => void): this; @@ -55,9 +69,9 @@ class Client extends EventEmitter { return this._client.setEmit(emit); } - public listen(maxPeers: number) { + public listen() { try { - this._client.create(maxPeers); + this._client.create(this.config.enet.maxPeers, this.config.enet.useNewPacket.asClient); this.emitter(this.emit.bind(this)); @@ -78,8 +92,8 @@ class Client extends EventEmitter { } private startWeb() { - if (this.options.https) { - WebServer(this.config.ip, this.config.port); + if (this.config.https.enable) { + WebServer(this.config.ip, this.config.port, this.config.https.type2); } } diff --git a/src/structures/WebServer.ts b/src/structures/WebServer.ts index dde249c..1eab92d 100644 --- a/src/structures/WebServer.ts +++ b/src/structures/WebServer.ts @@ -9,7 +9,7 @@ const options = { cert: readFileSync(path.join(__dirname, "..", "..", "misc", "ssl", "server.crt")) }; -export function WebServer(ip: string, port: number) { +export function WebServer(ip: string, port: number, type2 = false) { const app = express(); let done = false; @@ -18,7 +18,9 @@ export function WebServer(ip: string, port: number) { app.use("/growtopia/server_data.php", (req, res) => { res.send( - `server|${ip}\nport|${port}\ntype|1\n#maint|Server is Maintenance\nmeta|growtopiajs\nRTENDMARKERBS1001` + `server|${ip}\nport|${port}\ntype|1\n${ + type2 ? "type2|1" : "" + }\n#maint|Server is Maintenance\nmeta|growtopiajs\nRTENDMARKERBS1001` ); }); diff --git a/types/client.d.ts b/types/client.d.ts index 1e31f7c..92abd74 100644 --- a/types/client.d.ts +++ b/types/client.d.ts @@ -2,7 +2,7 @@ export interface ClientType { ip: string; port: number; setEmit: (emit: (...args: any[]) => void) => void; - create: (maxPeers: number) => void; + create: (maxPeers: number, isClient: boolean) => void; service: () => void; deInit: () => void; send: (peerID: number, count: number, packets: Buffer[]) => void; @@ -16,5 +16,17 @@ export interface ClientType { export interface ClientOptions { /** Built-in https web server */ - https: boolean; + https: { + enable: boolean; + url: string; + type2: boolean; + }; + ip: string; + port: number; + enet: { + maxPeers?: number; + useNewPacket?: { + asClient?: boolean; + }; + }; }