GastinelGarde : rejeu des traces (Rejeu), masque de mouvement enrichi, 98 tests
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Cette révision appartient à :
1 Parent
a70809df6c
révision
5631ebf506
5 fichiers modifiés
+334
-5
Aucun fichier ne correspond à votre recherche
@@ -0,0 +1,175 @@
|
||||
package com.gastinelgarde.core;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Rejeu des traces JSONL dans le moteur (décision D11 : des seuils mesurés, pas devinés). On enregistre une
|
||||
* session honnête et une session de triche, on les rejoue avec des seuils différents et on regarde ce qui
|
||||
* lève des infractions.
|
||||
*
|
||||
* Usage : {@code java -cp gastinelgarde.jar com.gastinelgarde.core.Rejeu trace-2026-10-14.jsonl [autres...]}
|
||||
*
|
||||
* Limites : le timer (débit réel des paquets) n'est pas rejouable, la trace n'ayant pas le temps réel des
|
||||
* paquets. Le rejeu des mouvements suppose une trace par tick ; l'écart entre deux traces d'un même joueur
|
||||
* donne le nombre de ticks.
|
||||
*/
|
||||
public final class Rejeu {
|
||||
|
||||
public record Resume(int lignes, int mouvements, int attaques, int ignorees,
|
||||
Map<Controle, Integer> infractions, Map<Controle, Double> vlMax,
|
||||
List<String> exemples) { }
|
||||
|
||||
private Rejeu() { }
|
||||
|
||||
public static Resume rejouer(Iterable<String> lignes, GardeConfig cfg) {
|
||||
Moteur moteur = new Moteur(cfg);
|
||||
Map<String, Long> dernierTick = new HashMap<>();
|
||||
Map<Controle, Integer> infractions = new EnumMap<>(Controle.class);
|
||||
Map<Controle, Double> vlMax = new EnumMap<>(Controle.class);
|
||||
List<String> exemples = new ArrayList<>();
|
||||
int n = 0, moves = 0, atks = 0, ignorees = 0;
|
||||
|
||||
for (String ligne : lignes) {
|
||||
if (ligne == null || ligne.isBlank()) continue;
|
||||
n++;
|
||||
String ev = texte(ligne, "ev");
|
||||
String nom = texte(ligne, "j");
|
||||
if (ev == null || nom == null || Double.isNaN(nombre(ligne, "t"))) {
|
||||
ignorees++;
|
||||
continue;
|
||||
}
|
||||
long tick = (long) nombre(ligne, "t");
|
||||
int f = (int) nombre(ligne, "f", 0);
|
||||
UUID id = UUID.nameUUIDFromBytes(nom.getBytes(StandardCharsets.UTF_8));
|
||||
EtatJoueur etat = etatDepuisMasque(f);
|
||||
|
||||
List<Verdict> verdicts = List.of();
|
||||
if (ev.equals("move")) {
|
||||
moves++;
|
||||
Long precedent = dernierTick.put(nom, tick);
|
||||
int ticks = precedent == null ? 1 : (int) Math.max(1, tick - precedent);
|
||||
double vx = nombre(ligne, "vx", 0), vy = nombre(ligne, "vy", 0), vz = nombre(ligne, "vz", 0);
|
||||
EtatMouvement em = new EtatMouvement(Math.sqrt(vx * vx + vz * vz), vy, ticks,
|
||||
nombre(ligne, "sc", 0) != 0, nombre(ligne, "ss", 0) != 0,
|
||||
(f & TraceLigne.F_SPRINT) != 0, (f & TraceLigne.F_FURTIF) != 0,
|
||||
(f & TraceLigne.F_UTILISE_OBJET) != 0,
|
||||
(f >> TraceLigne.DECALAGE_VITESSE) & 15, (f >> TraceLigne.DECALAGE_LENTEUR) & 15,
|
||||
(f & TraceLigne.F_MILIEU) != 0, (f & TraceLigne.F_FRICTION) != 0);
|
||||
verdicts = precedent == null ? List.of() : moteur.mouvement(id, tick * 50L, etat, em);
|
||||
} else if (ev.equals("atk")) {
|
||||
atks++;
|
||||
String msg = texte(ligne, "m");
|
||||
verdicts = moteur.attaque(id, tick * 50L, etat, nombre(ligne, "a", -1),
|
||||
(int) nombre(ligne, "c", 0), !"non-visible".equals(msg));
|
||||
} else {
|
||||
ignorees++;
|
||||
}
|
||||
|
||||
for (Verdict v : verdicts) {
|
||||
infractions.merge(v.controle(), 1, Integer::sum);
|
||||
vlMax.merge(v.controle(), v.vl(), Math::max);
|
||||
if (exemples.size() < 20) {
|
||||
exemples.add(String.format(Locale.ROOT, "t=%d %s %s vl=%.1f %s", tick, nom, v.controle().id(),
|
||||
v.vl(), v.details()));
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Resume(n, moves, atks, ignorees, infractions, vlMax, exemples);
|
||||
}
|
||||
|
||||
/** État du joueur déduit du masque enregistré (les durées de grâce valent 0 si la marque est présente). */
|
||||
static EtatJoueur etatDepuisMasque(int f) {
|
||||
return EtatJoueur.builder()
|
||||
.creatif((f & TraceLigne.F_CREATIF) != 0)
|
||||
.spectateur((f & TraceLigne.F_SPECTATEUR) != 0)
|
||||
.peutVoler((f & TraceLigne.F_PEUT_VOLER) != 0)
|
||||
.riptide((f & TraceLigne.F_RIPTIDE) != 0)
|
||||
.elytres((f & TraceLigne.F_ELYTRES) != 0)
|
||||
.levitation((f & TraceLigne.F_LEVITATION) != 0)
|
||||
.chuteLente((f & TraceLigne.F_CHUTE_LENTE) != 0)
|
||||
.enVehicule((f & TraceLigne.F_VEHICULE) != 0)
|
||||
.pocheContraption((f & TraceLigne.F_CONTRAPTION) != 0)
|
||||
.utiliseObjet((f & TraceLigne.F_UTILISE_OBJET) != 0)
|
||||
.ticksDepuisTeleport((f & TraceLigne.F_TELEPORT) != 0 ? 0 : EtatJoueur.JAMAIS)
|
||||
.ticksDepuisRecul((f & TraceLigne.F_RECUL) != 0 ? 0 : EtatJoueur.JAMAIS)
|
||||
.ticksDepuisGel((f & TraceLigne.F_GEL) != 0 ? 0 : EtatJoueur.JAMAIS)
|
||||
.build();
|
||||
}
|
||||
|
||||
// --- Lecture minimale du JSON produit par TraceLigne (clés connues, pas de bibliothèque) ---
|
||||
|
||||
static double nombre(String ligne, String cle) {
|
||||
return nombre(ligne, cle, Double.NaN);
|
||||
}
|
||||
|
||||
static double nombre(String ligne, String cle, double defaut) {
|
||||
int i = ligne.indexOf("\"" + cle + "\":");
|
||||
if (i < 0) return defaut;
|
||||
int debut = i + cle.length() + 3;
|
||||
int fin = debut;
|
||||
while (fin < ligne.length() && "-+.0123456789eE".indexOf(ligne.charAt(fin)) >= 0) fin++;
|
||||
if (fin == debut) return defaut;
|
||||
try {
|
||||
return Double.parseDouble(ligne.substring(debut, fin));
|
||||
} catch (NumberFormatException e) {
|
||||
return defaut;
|
||||
}
|
||||
}
|
||||
|
||||
static String texte(String ligne, String cle) {
|
||||
int i = ligne.indexOf("\"" + cle + "\":\"");
|
||||
if (i < 0) return null;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int p = i + cle.length() + 4; p < ligne.length(); p++) {
|
||||
char c = ligne.charAt(p);
|
||||
if (c == '"') return sb.toString();
|
||||
if (c == '\\' && p + 1 < ligne.length()) {
|
||||
char s = ligne.charAt(++p);
|
||||
switch (s) {
|
||||
case 'n' -> sb.append('\n');
|
||||
case 'r' -> sb.append('\r');
|
||||
case 't' -> sb.append('\t');
|
||||
case 'u' -> {
|
||||
if (p + 4 < ligne.length()) {
|
||||
sb.append((char) Integer.parseInt(ligne.substring(p + 1, p + 5), 16));
|
||||
p += 4;
|
||||
}
|
||||
}
|
||||
default -> sb.append(s);
|
||||
}
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
if (args.length == 0) {
|
||||
System.err.println("Usage : Rejeu <trace.jsonl> [autres traces...]");
|
||||
System.exit(2);
|
||||
}
|
||||
List<String> toutes = new ArrayList<>();
|
||||
for (String a : args) toutes.addAll(Files.readAllLines(Path.of(a), StandardCharsets.UTF_8));
|
||||
Resume r = rejouer(toutes, new GardeConfig());
|
||||
System.out.printf(Locale.ROOT, "%d lignes, %d mouvements, %d attaques, %d ignorées%n",
|
||||
r.lignes(), r.mouvements(), r.attaques(), r.ignorees());
|
||||
for (Controle c : Controle.values()) {
|
||||
Integer n = r.infractions().get(c);
|
||||
if (n == null) continue;
|
||||
System.out.printf(Locale.ROOT, " %-20s %6d infractions, VL max %.1f%n", c.id(), n, r.vlMax().get(c));
|
||||
}
|
||||
if (r.infractions().isEmpty()) System.out.println(" aucune infraction");
|
||||
for (String e : r.exemples()) System.out.println(" " + e);
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,12 @@ public record TraceLigne(long tick, String joueur, String evenement,
|
||||
public static final int F_CREATIF = 1, F_SPECTATEUR = 2, F_PEUT_VOLER = 4, F_RIPTIDE = 8, F_ELYTRES = 16,
|
||||
F_LEVITATION = 32, F_CHUTE_LENTE = 64, F_VEHICULE = 128, F_CONTRAPTION = 256, F_UTILISE_OBJET = 512;
|
||||
|
||||
// Bits propres aux traces de mouvement (rejeu, voir Rejeu) : état déduit au moment de l'enregistrement.
|
||||
public static final int F_SPRINT = 1 << 10, F_FURTIF = 1 << 11, F_MILIEU = 1 << 12, F_FRICTION = 1 << 13,
|
||||
F_TELEPORT = 1 << 14, F_RECUL = 1 << 15, F_GEL = 1 << 16;
|
||||
/** Bits 17 à 20 : niveau de Vitesse (0 à 15). Bits 21 à 24 : niveau de Lenteur (0 à 15). */
|
||||
public static final int DECALAGE_VITESSE = 17, DECALAGE_LENTEUR = 21;
|
||||
|
||||
public static int masque(EtatJoueur e) {
|
||||
int m = 0;
|
||||
if (e.creatif()) m |= F_CREATIF;
|
||||
@@ -33,6 +39,27 @@ public record TraceLigne(long tick, String joueur, String evenement,
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Masque complet d'un tick de mouvement : exemptions, état de déplacement et marques de grâce (téléportation,
|
||||
* recul, gel, selon les durées de la configuration).
|
||||
*/
|
||||
public static int masqueMouvement(EtatJoueur e, EtatMouvement m, GardeConfig cfg) {
|
||||
int f = masque(e);
|
||||
if (m != null) {
|
||||
if (m.sprint()) f |= F_SPRINT;
|
||||
if (m.furtif()) f |= F_FURTIF;
|
||||
if (m.ralentiParObjet()) f |= F_UTILISE_OBJET;
|
||||
if (m.milieuSpecial()) f |= F_MILIEU;
|
||||
if (m.frictionAnormale()) f |= F_FRICTION;
|
||||
f |= (Math.min(15, Math.max(0, m.amplificateurVitesse())) << DECALAGE_VITESSE);
|
||||
f |= (Math.min(15, Math.max(0, m.amplificateurLenteur())) << DECALAGE_LENTEUR);
|
||||
}
|
||||
if (e.ticksDepuisTeleport() <= cfg.graceTeleportTicks) f |= F_TELEPORT;
|
||||
if (e.ticksDepuisRecul() <= cfg.graceReculTicks) f |= F_RECUL;
|
||||
if (e.ticksDepuisGel() <= cfg.graceGelTicks) f |= F_GEL;
|
||||
return f;
|
||||
}
|
||||
|
||||
public String json() {
|
||||
StringBuilder sb = new StringBuilder(160);
|
||||
sb.append("{\"t\":").append(tick)
|
||||
|
||||
Référencer dans un nouveau ticket
Bloquer un utilisateur