Skip to content

Commit

Permalink
rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
JadlionHD committed Jan 24, 2024
1 parent dcffbcd commit c7e93e2
Show file tree
Hide file tree
Showing 46 changed files with 892 additions and 846 deletions.
Binary file modified assets/dat/items.dat
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"migrate": "knex migrate:latest",
"seed": "knex seed:run",
"lint": "biome lint ./src",
"dev": "rimraf dist && rimraf assets/cache && npm run build && npm start",
"dev": "rimraf dist && rimraf assets/cache && biome lint ./src && swc ./src -d dist && node -r dotenv/config dist/src/app.js",
"install": "(node scripts/setup.js) && npm run migrate && npm run seed && npm run finish",
"finish": "echo \u001b[46mSetup Completed!\u001b[0m && echo \u001b[46mCheck out GrowServer github: https://github.com/JadlionHD/GrowServer\u001b[0m"
},
Expand Down
6 changes: 4 additions & 2 deletions src/abstracts/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { BaseServer } from "../structures/BaseServer";

export abstract class Action {
public config: ActionConfig;
public base: BaseServer;

constructor() {
constructor(base: BaseServer) {
this.base = base;
this.config = {
eventName: undefined
};
}

public handle(base: BaseServer, peer: Peer, action: ActionType<unknown>) {}
public handle(peer: Peer, action: ActionType<unknown>) {}
}
6 changes: 4 additions & 2 deletions src/abstracts/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { CommandOptions } from "../types/command";

export abstract class Command {
public opt: CommandOptions;
public base: BaseServer;

constructor() {
constructor(base: BaseServer) {
this.base = base;
this.opt = {
name: "",
description: "",
Expand All @@ -18,5 +20,5 @@ export abstract class Command {
};
}

public async execute(base: BaseServer, peer: Peer, text: string, args: string[]): Promise<void> {}
public async execute(peer: Peer, text: string, args: string[]): Promise<void> {}
}
6 changes: 4 additions & 2 deletions src/abstracts/Dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { DialogConfig, DialogReturnType } from "../types/dialog";

export abstract class Dialog {
public config: DialogConfig;
public base: BaseServer;

constructor() {
constructor(base: BaseServer) {
this.base = base;
this.config = {
dialogName: undefined
};
}

public handle(base: BaseServer, peer: Peer, action: DialogReturnType<unknown>) {}
public handle(peer: Peer, action: DialogReturnType<unknown>) {}
}
7 changes: 5 additions & 2 deletions src/abstracts/Listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { BaseServer } from "../structures/BaseServer";

export abstract class Listener<K extends keyof ListenerEventTypes> {
public name: keyof ListenerEventTypes | undefined;
constructor() {
public base: BaseServer;

constructor(base: BaseServer) {
this.base = base;
this.name = undefined;
}

public run(base: BaseServer, ...args: ListenerEventTypes[K]): void {}
public run(...args: ListenerEventTypes[K]): void {}
}
12 changes: 6 additions & 6 deletions src/actions/DialogReturn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import { BaseServer } from "../structures/BaseServer";
import { ActionType } from "../types/action";

export default class extends Action {
constructor() {
super();
constructor(base: BaseServer) {
super(base);
this.config = {
eventName: "dialog_return"
};
}

public handle(base: BaseServer, peer: Peer, action: ActionType<{ action: string; dialog_name: string }>): void {
public handle(peer: Peer, action: ActionType<{ action: string; dialog_name: string }>): void {
const name = action.dialog_name;
try {
if (!base.dialogs.has(name)) return;
base.dialogs.get(name)?.handle(base, peer, action);
if (!this.base.dialogs.has(name)) return;
this.base.dialogs.get(name)?.handle(peer, action);
} catch (err) {
base.log.error(err);
this.base.log.error(err);
}
}
}
8 changes: 4 additions & 4 deletions src/actions/Drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { DialogBuilder } from "../utils/builders/DialogBuilder";
import { Variant } from "growtopia.js";

export default class extends Action {
constructor() {
super();
constructor(base: BaseServer) {
super(base);
this.config = {
eventName: "drop"
};
}

public handle(base: BaseServer, peer: Peer, action: ActionType<{ action: string; itemID: string }>): void {
public handle(peer: Peer, action: ActionType<{ action: string; itemID: string }>): void {
const itemID = parseInt(action.itemID);
const item = base.items.metadata.items.find((v) => v.id === itemID);
const item = this.base.items.metadata.items.find((v) => v.id === itemID);

const peerItem = peer.data?.inventory?.items.find((v) => v.id === itemID);
const dialog = new DialogBuilder()
Expand Down
22 changes: 5 additions & 17 deletions src/actions/EnterGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,15 @@ import { DialogBuilder } from "../utils/builders/DialogBuilder";
import { ActionType } from "../types/action";

export default class extends Action {
constructor() {
super();
constructor(base: BaseServer) {
super(base);
this.config = {
eventName: "enter_game"
};
}

public handle(base: BaseServer, peer: Peer, action: ActionType<{ action: string }>): void {
const tes = new DialogBuilder()
.defaultColor()
.addLabelWithIcon("Hello", "1000", "big")
.addSpacer("small")
.addTextBox("Welcome to GrowServer")
.raw("add_image_button||interface/large/news_banner1.rttex|bannerlayout|||\n")
.addQuickExit()
.endDialog("gazzette_end", "Cancel", "Ok")
.str();
peer.send(
Variant.from("OnRequestWorldSelectMenu"),
Variant.from("OnConsoleMessage", `Welcome ${peer.name} Where would you like to go?`),
Variant.from({ delay: 100 }, "OnDialogRequest", tes)
);
public handle(peer: Peer, action: ActionType<{ action: string }>): void {
const tes = new DialogBuilder().defaultColor().addLabelWithIcon("Hello", "1000", "big").addSpacer("small").addTextBox("Welcome to GrowServer").raw("add_image_button||interface/large/news_banner1.rttex|bannerlayout|||\n").addQuickExit().endDialog("gazzette_end", "Cancel", "Ok").str();
peer.send(Variant.from("OnRequestWorldSelectMenu"), Variant.from("OnConsoleMessage", `Welcome ${peer.name} Where would you like to go?`), Variant.from({ delay: 100 }, "OnDialogRequest", tes));
}
}
24 changes: 12 additions & 12 deletions src/actions/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { BaseServer } from "../structures/BaseServer";
import { ActionType } from "../types/action";

export default class extends Action {
constructor() {
super();
constructor(base: BaseServer) {
super(base);
this.config = {
eventName: "input"
};
}

public getCommand(base: BaseServer, commandName: string) {
public getCommand(commandName: string) {
return (
base.commands.get(commandName) || {
this.base.commands.get(commandName) || {
execute: async () => {},
opt: {
name: "",
Expand All @@ -30,25 +30,25 @@ export default class extends Action {
);
}

public async handle(base: BaseServer, peer: Peer, action: ActionType<{ action: string; text: string }>): Promise<void> {
public async handle(peer: Peer, action: ActionType<{ action: string; text: string }>): Promise<void> {
const text = action.text.trim();
if (!text || text.replace(/`.|`/g, "").length < 1) return;

if (text.startsWith("/")) {
const args = text.slice("/".length).split(" ");
const commandName = args.shift()?.toLowerCase() || "";

if (!base.commands.has(commandName)) {
if (!this.base.commands.has(commandName)) {
return peer.send(Variant.from("OnConsoleMessage", "`4Unknown command.`` Enter `$/help`` for a list of valid commands"));
}
peer.send(Variant.from("OnConsoleMessage", `\`6/${commandName} ${args.join(" ")}\`\``));

// Cooldown & Ratelimit
const cmd = this.getCommand(base, commandName);
const cmd = this.getCommand(commandName);

const cmdCd = base.cooldown.get(`${commandName}-netID-${peer.data?.netID}`);
const cmdCd = this.base.cooldown.get(`${commandName}-netID-${peer.data?.netID}`);
if (!cmdCd) {
const cmdSet = base.cooldown.set(`${commandName}-netID-${peer.data?.netID}`, {
const cmdSet = this.base.cooldown.set(`${commandName}-netID-${peer.data?.netID}`, {
limit: 1,
time: Date.now()
});
Expand All @@ -63,19 +63,19 @@ export default class extends Action {

setTimeout(
() => {
base.cooldown.delete(`${commandName}-netID-${peer.data?.netID}`);
this.base.cooldown.delete(`${commandName}-netID-${peer.data?.netID}`);
},
cmd.opt.cooldown || 0 * 1000
);

try {
if (cmd.opt.permission.some((perm) => perm === peer.data?.role)) {
await cmd.execute(base, peer, text, args);
await cmd.execute(peer, text, args);
} else {
peer.send(Variant.from("OnConsoleMessage", "You dont have permission to use this command."));
}
} catch (err) {
base.log.error(err);
this.base.log.error(err);
}
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/actions/JoinRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { Peer } from "../structures/Peer";
import { ActionType } from "../types/action";

export default class extends Action {
constructor() {
super();
constructor(base: BaseServer) {
super(base);
this.config = {
eventName: "join_request"
};
}

public handle(base: BaseServer, peer: Peer, action: ActionType<{ action: string; name: string }>): void {
public handle(peer: Peer, action: ActionType<{ action: string; name: string }>): void {
const worldName: string = action.name || "";
if (worldName.length <= 0) {
peer.send(Variant.from("OnFailedToEnterWorld", 1), Variant.from("OnConsoleMessage", "That world name is uhh `9empty``"));
Expand Down
6 changes: 3 additions & 3 deletions src/actions/Quit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { BaseServer } from "../structures/BaseServer";
import { ActionType } from "../types/action";

export default class extends Action {
constructor() {
super();
constructor(base: BaseServer) {
super(base);
this.config = {
eventName: "quit"
};
}

public handle(base: BaseServer, peer: Peer, action: ActionType<{ action: string }>): void {
public handle(peer: Peer, action: ActionType<{ action: string }>): void {
peer.disconnect();
}
}
6 changes: 3 additions & 3 deletions src/actions/QuitToExit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { BaseServer } from "../structures/BaseServer";
import { ActionType } from "../types/action";

export default class extends Action {
constructor() {
super();
constructor(base: BaseServer) {
super(base);
this.config = {
eventName: "quit_to_exit"
};
}

public handle(base: BaseServer, peer: Peer, action: ActionType<{ action: string }>): void {
public handle(peer: Peer, action: ActionType<{ action: string }>): void {
peer.leaveWorld();
}
}
11 changes: 4 additions & 7 deletions src/actions/RefreshItemData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ import { TankTypes } from "../utils/enums/TankTypes";
import { ActionType } from "../types/action";

export default class extends Action {
constructor() {
super();
constructor(base: BaseServer) {
super(base);
this.config = {
eventName: "refresh_item_data"
};
}

public handle(base: BaseServer, peer: Peer, action: ActionType<{ action: string }>): void {
peer.send(
Variant.from("OnConsoleMessage", "One moment. Updating item data..."),
TankPacket.from({ type: TankTypes.PEER_ITEMS_DAT, data: () => base.items.content })
);
public handle(peer: Peer, action: ActionType<{ action: string }>): void {
peer.send(Variant.from("OnConsoleMessage", "One moment. Updating item data..."), TankPacket.from({ type: TankTypes.PEER_ITEMS_DAT, data: () => this.base.items.content }));
}
}
6 changes: 3 additions & 3 deletions src/actions/Respawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { BaseServer } from "../structures/BaseServer";
import { ActionType } from "../types/action";

export default class extends Action {
constructor() {
super();
constructor(base: BaseServer) {
super(base);
this.config = {
eventName: "respawn"
};
}

public handle(base: BaseServer, peer: Peer, action: ActionType<{ action: string }>): void {
public handle(peer: Peer, action: ActionType<{ action: string }>): void {
peer.respawn();
// TODO: respawn back to previous checkpoint
}
Expand Down
6 changes: 3 additions & 3 deletions src/actions/RespawnSpike.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { BaseServer } from "../structures/BaseServer";
import { ActionType } from "../types/action";

export default class extends Action {
constructor() {
super();
constructor(base: BaseServer) {
super(base);
this.config = {
eventName: "respawn_spike"
};
}

public handle(base: BaseServer, peer: Peer, action: ActionType<{ action: string }>): void {
public handle(peer: Peer, action: ActionType<{ action: string }>): void {
peer.respawn();
// TODO: respawn back to previous checkpoint
}
Expand Down
8 changes: 4 additions & 4 deletions src/actions/Trash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { DialogBuilder } from "../utils/builders/DialogBuilder";
import { Variant } from "growtopia.js";

export default class extends Action {
constructor() {
super();
constructor(base: BaseServer) {
super(base);
this.config = {
eventName: "trash"
};
}

public handle(base: BaseServer, peer: Peer, action: ActionType<{ action: string; itemID: string }>): void {
public handle(peer: Peer, action: ActionType<{ action: string; itemID: string }>): void {
const itemID = parseInt(action.itemID);
const item = base.items.metadata.items.find((v) => v.id === itemID);
const item = this.base.items.metadata.items.find((v) => v.id === itemID);
const peerItem = peer.data?.inventory?.items.find((v) => v.id === itemID);

const dialog = new DialogBuilder()
Expand Down
6 changes: 3 additions & 3 deletions src/actions/Wrench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { DialogBuilder } from "../utils/builders/DialogBuilder";
import { Variant } from "growtopia.js";

export default class extends Action {
constructor() {
super();
constructor(base: BaseServer) {
super(base);
this.config = {
eventName: "wrench"
};
}

public handle(base: BaseServer, peer: Peer, action: ActionType<{ action: string; netID: string }>): void {
public handle(peer: Peer, action: ActionType<{ action: string; netID: string }>): void {
const dialog = new DialogBuilder()
.defaultColor()
.addLabelWithIcon(peer.data?.tankIDName || "", "32", "small")
Expand Down
Loading

0 comments on commit c7e93e2

Please sign in to comment.