begin rewriting first services to be domain agnostic

more rewrites
This commit is contained in:
2026-02-17 22:42:36 +01:00
parent b3766b9584
commit 071fe2f891
45 changed files with 915 additions and 521 deletions

View File

@@ -0,0 +1,14 @@
import type { MessagesServiceInterface } from "entities/messages/messages.service";
export class GreetingService<U = unknown> {
messagesService: MessagesServiceInterface<U>;
constructor(messagesService: MessagesServiceInterface<U>) {
this.messagesService = messagesService;
}
async sendGreeting(user: U, userName: string) {
const greetingMessage = `Hello, ${userName}! Welcome to the server!`;
await this.messagesService.sendToUser(user, greetingMessage);
}
}

View File

@@ -0,0 +1,9 @@
export type TextBasedFeatureHandleMessageSend = (
message: string,
channelId: string,
) => void | Promise<void>;
export type TextBasedFeatureInput = {
channelId: string;
handleMessageSend: TextBasedFeatureHandleMessageSend;
};

View File

@@ -0,0 +1,18 @@
import type {
TextBasedFeatureHandleMessageSend,
TextBasedFeatureInput,
} from "./text-based-feature.schema";
export class TextBasedFeature {
channelId: string;
handleMessageSend: TextBasedFeatureHandleMessageSend;
constructor(input: TextBasedFeatureInput) {
this.channelId = input.channelId;
this.handleMessageSend = input.handleMessageSend;
}
async sendMessage(input: { content: string }) {
this.handleMessageSend(input.content, this.channelId);
}
}

View File

@@ -0,0 +1,64 @@
import { TextBasedFeature } from "features/text-based-feature/text-based-feature";
import type { TextBasedFeatureInput } from "features/text-based-feature/text-based-feature.schema";
import { createLogger } from "lib/logger";
import { getRandomInt } from "lib/utils";
export class WaterMeService extends TextBasedFeature {
waterLevel: number;
private logger = createLogger("WaterMeService");
private thirsty = 3 as const;
private enough = 10 as const;
constructor(input: TextBasedFeatureInput) {
super(input);
this.waterLevel = 0;
}
getReply() {
const thirstyReplies = [
"... wow das wars schon??? ich brauche noch mehr wasser :(",
"dankeeeee!!!! ich waer fast verdurstet :(((",
"*roelpssssss*",
];
const fullReplies = [
"langsam reicht es :o",
"poah, das hat gut getan",
"das ist krass :3",
];
const tooMuchReplies = [
"ES REICHT!!!!",
"bitte hoer auf, ich platze gleich :(",
];
if (this.waterLevel <= this.thirsty) {
return thirstyReplies[getRandomInt(0, thirstyReplies.length - 1)];
}
if (this.waterLevel > this.thirsty && this.waterLevel <= this.enough) {
return fullReplies[getRandomInt(0, fullReplies.length - 1)];
}
if (this.waterLevel > this.enough) {
return tooMuchReplies[getRandomInt(0, tooMuchReplies.length - 1)];
}
}
async notifyIfThirsty() {
if (this.waterLevel <= this.thirsty) {
await this.sendMessage({ content: "ich brauche wasser :(" });
}
}
waterMe() {
const reply = this.getReply();
this.waterLevel++;
this.logger.info(`Water level increased to ${this.waterLevel}`);
return {
reply,
};
}
}