diff --git a/src/main/java/com/gastineltpa/TpaCommands.java b/src/main/java/com/gastineltpa/TpaCommands.java index ea0e6fb..ba0ed76 100644 --- a/src/main/java/com/gastineltpa/TpaCommands.java +++ b/src/main/java/com/gastineltpa/TpaCommands.java @@ -150,9 +150,9 @@ public final class TpaCommands { .append(Component.literal(type == Type.TPA ? nomMoi + " souhaite se téléporter vers toi. " : nomMoi + " souhaite que tu te téléportes vers lui. ").withStyle(ChatFormatting.YELLOW)) - .append(bouton("[Accepter]", "/tpaccept " + PseudoMatcher.tapable(nomMoi), ChatFormatting.GREEN, "Accepter la demande")) + .append(bouton("[Accepter]", "/tpaccept " + PseudoMatcher.argumentBouton(moi.getUUID(), nomMoi), ChatFormatting.GREEN, "Accepter la demande")) .append(Component.literal(" ")) - .append(bouton("[Refuser]", "/tpadeny " + PseudoMatcher.tapable(nomMoi), ChatFormatting.RED, "Refuser la demande")) + .append(bouton("[Refuser]", "/tpadeny " + PseudoMatcher.argumentBouton(moi.getUUID(), nomMoi), ChatFormatting.RED, "Refuser la demande")) .append(Component.literal(" (expire dans 60 s)").withStyle(ChatFormatting.GRAY)); cible.sendSystemMessage(msg); return 1; @@ -171,6 +171,13 @@ public final class TpaCommands { if (trouvee.isEmpty()) { return echec(ctx, "Aucune demande de téléportation en attente."); } + } else if (PseudoMatcher.parseUuidStrict(nomDemandeur).isPresent()) { + // Bouton / UUID : recherche directe dans le registre des demandes, jamais par pseudo. + UUID id = PseudoMatcher.parseUuidStrict(nomDemandeur).get(); + trouvee = REGISTRE.find(moi.getUUID(), id); + if (trouvee.isEmpty()) { + return echec(ctx, "Aucune demande en attente de la part de ce joueur (peut-être expirée)."); + } } else { Cherche c = chercherEnLigne(server, nomDemandeur); if (c.ambigu != null) return echec(ctx, c.ambigu); @@ -220,6 +227,9 @@ public final class TpaCommands { if (nomCible == null) { annulee = REGISTRE.cancel(moi.getUUID(), null); if (annulee.isEmpty()) return echec(ctx, "Tu n'as aucune demande de téléportation en cours à annuler."); + } else if (PseudoMatcher.parseUuidStrict(nomCible).isPresent()) { + annulee = REGISTRE.cancel(moi.getUUID(), PseudoMatcher.parseUuidStrict(nomCible).get()); + if (annulee.isEmpty()) return echec(ctx, "Tu n'as aucune demande en cours vers ce joueur."); } else { Cherche c = chercherEnLigne(server, nomCible); if (c.ambigu != null) return echec(ctx, c.ambigu); diff --git a/src/main/java/com/gastineltpa/core/PseudoMatcher.java b/src/main/java/com/gastineltpa/core/PseudoMatcher.java index 24ed7e9..f5b9c79 100644 --- a/src/main/java/com/gastineltpa/core/PseudoMatcher.java +++ b/src/main/java/com/gastineltpa/core/PseudoMatcher.java @@ -46,6 +46,30 @@ public final class PseudoMatcher { return sb.toString(); } + /** Vrai si le pseudo ne contient que [A-Za-z0-9_] (sinon les boutons doivent passer par l'UUID). */ + public static boolean pseudoSimple(String nom) { + if (nom == null || nom.isEmpty()) return false; + for (int i = 0; i < nom.length(); i++) { + char c = nom.charAt(i); + if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_')) return false; + } + return true; + } + + private static final java.util.regex.Pattern UUID_STRICT = java.util.regex.Pattern.compile( + "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"); + + /** Parse strict d'un UUID texte (36 caractères, format canonique) ; vide sinon. Jamais un pseudo. */ + public static java.util.Optional parseUuidStrict(String s) { + if (s == null || !UUID_STRICT.matcher(s).matches()) return java.util.Optional.empty(); + try { return java.util.Optional.of(UUID.fromString(s)); } catch (IllegalArgumentException e) { return java.util.Optional.empty(); } + } + + /** Argument à mettre dans un bouton : le pseudo s'il est simple, sinon l'UUID du joueur. */ + public static String argumentBouton(UUID id, String nom) { + return pseudoSimple(nom) ? nom : id.toString(); + } + /** Forme tapable dans un argument word() de Brigadier : [0-9A-Za-z_.+-] seulement. */ public static String tapable(String nom) { if (nom == null) return ""; @@ -75,32 +99,41 @@ public final class PseudoMatcher { public static Resultat resoudre(String saisie, Collection enLigne, Collection cache) { if (saisie == null || saisie.isEmpty()) return INCONNU; - Resultat r = etape(saisie, dedoublonner(enLigne), false); - if (r.statut() != Statut.INCONNU) return r; - return etape(saisie, dedoublonner(cache), true); + List ligne = dedoublonner(enLigne); + List tous = dedoublonner(concat(ligne, cache)); + String norm = normaliser(saisie); + + List exacts = new ArrayList<>(); + for (Profil p : tous) if (p.nom().equalsIgnoreCase(saisie)) exacts.add(p); + if (!exacts.isEmpty()) { + // Jamais de « l'exact gagne » silencieux : si un AUTRE compte (en ligne ou en cache) a la même forme + // normalisée (usurpateur du pseudo sans guillemet, profil fantôme...), on refuse et on liste. + List candidats = new ArrayList<>(exacts); + if (!norm.isEmpty()) { + for (Profil p : tous) if (!candidats.contains(p) && normaliser(p.nom()).equals(norm)) candidats.add(p); + } + if (candidats.size() == 1) return new Resultat(Statut.TROUVE, exacts.get(0), List.of(exacts.get(0)), false); + return new Resultat(Statut.AMBIGU, null, candidats, false); + } + if (norm.isEmpty()) return INCONNU; + // Pas d'exact : forme normalisée, joueurs en ligne d'abord, puis cache. + List proches = filtrerNormalise(ligne, norm); + if (proches.isEmpty()) proches = filtrerNormalise(tous, norm); + if (proches.size() == 1) return new Resultat(Statut.TROUVE, proches.get(0), proches, true); + if (proches.size() > 1) return new Resultat(Statut.AMBIGU, null, proches, true); + return INCONNU; } - /** - * strict (cache) : un pseudo exact qui partage sa forme normalisée avec un AUTRE compte reste AMBIGU - * (profils fantômes d'anciens essais) : on ne désigne pas un compte au hasard. - */ - private static Resultat etape(String saisie, List profils, boolean strict) { - List exacts = new ArrayList<>(); - for (Profil p : profils) if (p.nom().equalsIgnoreCase(saisie)) exacts.add(p); - String norm = normaliser(saisie); - List proches = new ArrayList<>(); - if (!norm.isEmpty()) for (Profil p : profils) if (normaliser(p.nom()).equals(norm)) proches.add(p); + private static List filtrerNormalise(List profils, String norm) { + List out = new ArrayList<>(); + for (Profil p : profils) if (normaliser(p.nom()).equals(norm)) out.add(p); + return out; + } - if (exacts.size() == 1 && (!strict || proches.size() <= 1)) { - return new Resultat(Statut.TROUVE, exacts.get(0), List.of(exacts.get(0)), false); - } - if (!exacts.isEmpty()) { - // plusieurs exacts (casse différente) ou exact + comptes proches dans le cache : jamais au hasard - return new Resultat(Statut.AMBIGU, null, List.copyOf(strict ? proches : exacts), false); - } - if (proches.size() == 1) return new Resultat(Statut.TROUVE, proches.get(0), proches, true); - if (proches.size() > 1) return new Resultat(Statut.AMBIGU, null, List.copyOf(proches), true); - return INCONNU; + private static List concat(Collection a, Collection b) { + List l = new ArrayList<>(a); + l.addAll(b); + return l; } private static List dedoublonner(Collection profils) { diff --git a/src/test/java/com/gastineltpa/core/PseudoMatcherTest.java b/src/test/java/com/gastineltpa/core/PseudoMatcherTest.java index b37ef8c..87ab239 100644 --- a/src/test/java/com/gastineltpa/core/PseudoMatcherTest.java +++ b/src/test/java/com/gastineltpa/core/PseudoMatcherTest.java @@ -37,7 +37,7 @@ class PseudoMatcherTest { @Test void sansGuillemetVisePseudoAvecGuillemetEnLigne() { - var r = PseudoMatcher.resoudre("gabybou201", List.of(THEO_P, GABY_P), List.of(FANTOME_P, GABY_P)); + var r = PseudoMatcher.resoudre("gabybou201", List.of(THEO_P, GABY_P), List.of(GABY_P)); assertEquals(Statut.TROUVE, r.statut()); assertEquals(GABY, r.profil().id()); assertEquals("gabybou201\"", r.profil().nom()); @@ -45,20 +45,60 @@ class PseudoMatcherTest { } @Test - void enLigneEstPrioritaireSurLeCacheMemeAvecFantome() { - var r = PseudoMatcher.resoudre("GabyBou201", List.of(GABY_P), List.of(FANTOME_P, BIZARRE_P, GABY_P)); + void enLigneEstPrioritaireSurLeCacheSansExact() { + var r = PseudoMatcher.resoudre("GabyBou201", List.of(GABY_P), List.of(BIZARRE_P, GABY_P)); assertTrue(r.trouve()); assertEquals(GABY, r.profil().id()); } @Test - void exactEnLigneNeEstPasDetourne() { + void usurpateurExactEnLigneAvecVraiCompteEnLigneEstAmbigu() { + // l'usurpateur « gabybou201 » (sans guillemet) et le vrai « gabybou201" » sont connectés : jamais « l'exact gagne » var r = PseudoMatcher.resoudre("gabybou201", List.of(FANTOME_P, GABY_P), List.of()); + assertEquals(Statut.AMBIGU, r.statut()); + assertEquals(2, r.candidats().size()); + assertNull(r.profil()); + } + + @Test + void exactEnLigneAvecVraiCompteEnCacheEstAmbigu() { + // usurpateur « gabybou201 » en ligne, vrai « gabybou201" » hors ligne (cache) + var r = PseudoMatcher.resoudre("gabybou201", List.of(FANTOME_P), List.of(GABY_P)); + assertEquals(Statut.AMBIGU, r.statut()); + assertEquals(2, r.candidats().size()); + } + + @Test + void exactEnCacheAvecUsurpateurEnLigneProcheEstAmbigu() { + var r = PseudoMatcher.resoudre("gabybou201", List.of(GABY_P), List.of(FANTOME_P)); + assertEquals(Statut.AMBIGU, r.statut()); + } + + @Test + void exactSansCollisionResteExact() { + var r = PseudoMatcher.resoudre("TOTOCHE10", List.of(THEO_P, GABY_P), List.of()); assertTrue(r.trouve()); - assertEquals(GABY_FANTOME, r.profil().id()); + assertEquals(THEO, r.profil().id()); assertFalse(r.approximatif()); } + @Test + void pseudoSimpleEtArgumentBouton() { + assertTrue(PseudoMatcher.pseudoSimple("Steve_1")); + assertFalse(PseudoMatcher.pseudoSimple("gabybou201\"")); + assertEquals("Steve_1", PseudoMatcher.argumentBouton(THEO, "Steve_1")); + assertEquals(GABY.toString(), PseudoMatcher.argumentBouton(GABY, "gabybou201\"")); + } + + @Test + void parseUuidStrict() { + assertEquals(GABY, PseudoMatcher.parseUuidStrict(GABY.toString()).orElseThrow()); + assertTrue(PseudoMatcher.parseUuidStrict("gabybou201").isEmpty()); + assertTrue(PseudoMatcher.parseUuidStrict("128c0f10964337ff941dafa49cdb30ec").isEmpty()); + assertTrue(PseudoMatcher.parseUuidStrict(GABY + "x").isEmpty()); + assertTrue(PseudoMatcher.parseUuidStrict(null).isEmpty()); + } + @Test void deuxEnLigneProchesSansExactEstAmbigu() { var r = PseudoMatcher.resoudre("gabybou201", List.of(GABY_P, BIZARRE_P), List.of());