From 82f753e9fd2c4e676528574bdf0333a3e95db39c Mon Sep 17 00:00:00 2001 From: Manuel Frohn Date: Wed, 15 May 2024 18:37:17 +0200 Subject: [PATCH] Implemented FilterTree --- .../steamwar/bungeecore/chat/ChatFilter.java | 4 + .../steamwar/bungeecore/chat/FilterTree.java | 84 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/de/steamwar/bungeecore/chat/ChatFilter.java create mode 100644 src/de/steamwar/bungeecore/chat/FilterTree.java diff --git a/src/de/steamwar/bungeecore/chat/ChatFilter.java b/src/de/steamwar/bungeecore/chat/ChatFilter.java new file mode 100644 index 0000000..2af6cd3 --- /dev/null +++ b/src/de/steamwar/bungeecore/chat/ChatFilter.java @@ -0,0 +1,4 @@ +package de.steamwar.bungeecore.chat; + +public class ChatFilter { +} diff --git a/src/de/steamwar/bungeecore/chat/FilterTree.java b/src/de/steamwar/bungeecore/chat/FilterTree.java new file mode 100644 index 0000000..087350a --- /dev/null +++ b/src/de/steamwar/bungeecore/chat/FilterTree.java @@ -0,0 +1,84 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2024 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bungeecore.chat; + +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.Map; + +public class FilterTree { + /** + * Root of this prefix tree + */ + final private TreeNode root = new TreeNode(); + + /** + * Embeds the given word into the tree + * + * @param word the word to embed + * @param value the value given to this word + */ + public void addWord(@NotNull String word, int value) { + char[] chars = word.toCharArray(); + + TreeNode current = root; + for (char c : chars) { + if (current.children.containsKey(c)) { + current = current.children.get(c); + } else { + TreeNode next = new TreeNode(); + current.children.put(c, next); + current = next; + } + } + + current.value = value; + } + + /** + * Uses the tree to find the value that should be assigned to + * the given word + * + * @param word the word to evaluate + * @return the value assigned to the given word + */ + public int evaluateWord(@NotNull String word) { + char[] chars = word.toCharArray(); + + int value = 0; + TreeNode current = root; + for (char c : chars) { + if (current.children.containsKey(c)) { + current = current.children.get(c); + value += current.value; + } else { + break; + } + } + + return value; + } + + static class TreeNode { + final Map children = new HashMap<>(); + int value; + } +}