package ar.com.companeros.panel; import java.util.Set; /** Lista cerrada de operaciones del panel; nunca acepta comandos libres. */ public final class PanelPolicy { private PanelPolicy() { } public enum Action { STATUS(false), SENSITIVITY(false), JOIN_ALL(true), JOIN_BOT(true), PAUSE_BOT(true), AUTONOMOUS(true), POPULATION(true), INTENSITY(true), SOUND_VOLUME(true), METEOR(true), INFECT(true), ADVANCE(true), STAGE(true), SPAWN(true), LORE(true), PURSUIT(true), SKIN(true); public final boolean admin; Action(boolean admin) { this.admin = admin; } } public static String command(Action action, String value, boolean administrator) { if (action.admin && !administrator) throw new IllegalArgumentException("Necesitás permiso de administrador."); if (value == null || value.length() > 128 || value.indexOf('\n') >= 0 || value.indexOf('\r') >= 0) throw new IllegalArgumentException("Valor inválido."); return switch (action) { case STATUS -> { empty(value); yield ""; } case SENSITIVITY -> { if (!Set.of("true", "false").contains(value)) throw new IllegalArgumentException("Modo seguro inválido."); yield "sensitivity mode " + (value.equals("true") ? "enabled" : "disabled"); } case JOIN_ALL -> { empty(value); yield "raps join bots"; } case JOIN_BOT -> "raps join bot " + name(value); case PAUSE_BOT -> "raps pausar " + name(value); case AUTONOMOUS -> "raps autonomo " + name(value); case POPULATION -> { int number = integer(value, 2, 10); if (!Set.of(2, 4, 6, 10).contains(number)) throw new IllegalArgumentException("Población: 2, 4, 6 o 10."); yield "raps fase " + number; } case INTENSITY -> "raps intensity " + integer(value, 0, 100); case SOUND_VOLUME -> { integer(value, 0, 300); yield ""; } case METEOR -> { empty(value); yield "raps mimicer meteorito"; } case INFECT -> "raps mimicer infectar" + (value.isEmpty() ? "" : " " + name(value)); case ADVANCE -> { empty(value); yield "raps mimicer avanzar"; } case STAGE -> "raps mimicer etapa " + integer(value, 0, 4); case SPAWN -> { empty(value); yield "raps mimicer spawn"; } case LORE -> { empty(value); yield "raps mimicer lore"; } case PURSUIT -> { empty(value); yield "raps mimicer persecucion"; } case SKIN -> { if (!Set.of("Steve", "host").contains(value)) throw new IllegalArgumentException("Aspecto inválido."); yield "raps mimicer skin " + value; } }; } private static String name(String value) { if (!value.matches("[A-Za-z0-9_]{1,16}")) throw new IllegalArgumentException("Nombre de bot inválido."); return value; } public static int integer(String value, int min, int max) { if (!value.matches("[0-9]{1,3}")) throw new IllegalArgumentException("Ingresá un número entero."); int number = Integer.parseInt(value); if (number < min || number > max) throw new IllegalArgumentException("Valor permitido: " + min + " a " + max + "."); return number; } private static void empty(String value) { if (!value.isEmpty()) throw new IllegalArgumentException("La acción no admite parámetros."); } }