Skip to content

Commit

Permalink
fix peer data type
Browse files Browse the repository at this point in the history
  • Loading branch information
JadlionHD committed Jan 21, 2024
1 parent 1fe8d42 commit 841ab83
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "growtopia.js",
"version": "1.3.3",
"version": "1.3.4",
"description": "A package to create a growtopia private servers.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
22 changes: 11 additions & 11 deletions src/structures/Peer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { ClientType } from "../../types";
import { ClientType, PeerData } from "../../types";
import { Sendable } from "../../types/packets";
import { Variant } from "../packets/Variant";
import { Client } from "./Client";

class Peer<T> {
public data?: T;
class Peer<T extends PeerData> {
public data: T;

constructor(private client: Client, netID: number) {
(this.data as any) = { netID };
// this.netID = netID;
this.data = {} as T;
this.data.netID = netID;

this.client = client;
}
Expand All @@ -17,22 +17,22 @@ class Peer<T> {
* Get ENetPeer Round Trip Time (RTT).
*/
public get ping() {
return this.client._client.getPeerRTT((this.data as any).netID);
return this.client._client.getPeerRTT(this.data.netID);
}

/**
* Get [ENetPeerState](http://enet.bespin.org/enet_8h.html#a058bc368c507eb86cb47f3946f38d558).
*/
public get state() {
return this.client._client.getPeerState((this.data as any).netID);
return this.client._client.getPeerState(this.data.netID);
}

/**
* Sends multiple packets to a single peer.
* @param data An argument of packets that contains the `parse()` function or just an array of Buffers.
*/
public send(...data: Sendable[]) {
Peer.send(this.client._client, (this.data as any).netID, ...data);
Peer.send(this.client._client, this.data.netID, ...data);
}

/**
Expand Down Expand Up @@ -72,15 +72,15 @@ class Peer<T> {
public disconnect(type: "now" | "later" | "normal" = "later") {
switch (type) {
case "normal": {
this.client._client.disconnect((this.data as any).netID);
this.client._client.disconnect(this.data.netID);
break;
}
case "later": {
this.client._client.disconnectLater((this.data as any).netID);
this.client._client.disconnectLater(this.data.netID);
break;
}
case "now": {
this.client._client.disconnectNow((this.data as any).netID);
this.client._client.disconnectNow(this.data.netID);
break;
}
}
Expand Down
4 changes: 4 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ export interface DataObject {
[key: string]: string | number;
}

export interface PeerData {
netID: number;
}

export * from "./client";
export * from "./packets";
export * from "./events";
Expand Down

0 comments on commit 841ab83

Please sign in to comment.