From 90d1f211fa5f3b6fe6f472606c86b3ffd4355730 Mon Sep 17 00:00:00 2001 From: Bobcat00 Date: Mon, 13 Jan 2020 11:03:19 +0100 Subject: [PATCH] Add `ready` state to sign templates. Adds support for a new `ready` state in the template engine for arena signs. A sign is in the `ready` state when there are players in the lobby and all of them have readied up. This is only relevant in arenas with start delay timers, as the arena session automatically starts when all players are ready otherwise. The new state is completely optional, even when no base state is provided, and it inherits from the `joining` state when not defined. Closes #593 --- .../MobArena/signs/LoadsTemplateStore.java | 3 ++ .../MobArena/signs/RendersTemplate.java | 3 ++ .../garbagemule/MobArena/signs/Template.java | 19 ++++++++- .../MobArena/signs/RendersTemplateTest.java | 41 +++++++++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/garbagemule/MobArena/signs/LoadsTemplateStore.java b/src/main/java/com/garbagemule/MobArena/signs/LoadsTemplateStore.java index b2c0c9a..bb7b13d 100644 --- a/src/main/java/com/garbagemule/MobArena/signs/LoadsTemplateStore.java +++ b/src/main/java/com/garbagemule/MobArena/signs/LoadsTemplateStore.java @@ -72,6 +72,7 @@ class LoadsTemplateStore { private boolean hasStateSuffix(String id) { return id.endsWith("-idle") || id.endsWith("-joining") + || id.endsWith("-ready") || id.endsWith("-running"); } @@ -79,12 +80,14 @@ class LoadsTemplateStore { String[] base = getLines(yaml, id); String[] idle = getLines(yaml, id + "-idle"); String[] joining = getLines(yaml, id + "-joining"); + String[] ready = getLines(yaml, id + "-ready"); String[] running = getLines(yaml, id + "-running"); return new Template.Builder(id) .withBase(base) .withIdle(idle) .withJoining(joining) + .withReady(ready) .withRunning(running) .build(); } diff --git a/src/main/java/com/garbagemule/MobArena/signs/RendersTemplate.java b/src/main/java/com/garbagemule/MobArena/signs/RendersTemplate.java index 0ff040f..50a51f8 100644 --- a/src/main/java/com/garbagemule/MobArena/signs/RendersTemplate.java +++ b/src/main/java/com/garbagemule/MobArena/signs/RendersTemplate.java @@ -37,6 +37,9 @@ class RendersTemplate { return template.running; } if (arena.getPlayersInLobby().size() > 0) { + if (arena.getNonreadyPlayers().size() == 0) { + return template.ready; + } return template.joining; } return template.idle; diff --git a/src/main/java/com/garbagemule/MobArena/signs/Template.java b/src/main/java/com/garbagemule/MobArena/signs/Template.java index 3d88767..f540c46 100644 --- a/src/main/java/com/garbagemule/MobArena/signs/Template.java +++ b/src/main/java/com/garbagemule/MobArena/signs/Template.java @@ -4,11 +4,13 @@ class Template { final String[] idle; final String[] joining; + final String[] ready; final String[] running; - private Template(String[] idle, String[] joining, String[] running) { + private Template(String[] idle, String[] joining, String[] ready, String[] running) { this.idle = idle; this.joining = joining; + this.ready = ready; this.running = running; } @@ -18,6 +20,7 @@ class Template { private String[] base; private String[] idle; private String[] joining; + private String[] ready; private String[] running; Builder(String id) { @@ -39,12 +42,21 @@ class Template { return this; } + Builder withReady(String[] lines) { + this.ready = lines; + return this; + } + Builder withRunning(String[] lines) { this.running = lines; return this; } Template build() { + // If the base template has not been defined, there must be + // templates for the idle, joining, and running states. + // A template for the ready state is optional; If not defined, + // it will inherit from the joining template. if (base == null) { if (idle == null) { missing("idle"); @@ -62,10 +74,13 @@ class Template { if (joining == null) { joining = base; } + if (ready == null) { + ready = joining; + } if (running == null) { running = base; } - return new Template(idle, joining, running); + return new Template(idle, joining, ready, running); } private void missing(String state) { diff --git a/src/test/java/com/garbagemule/MobArena/signs/RendersTemplateTest.java b/src/test/java/com/garbagemule/MobArena/signs/RendersTemplateTest.java index 5a15b2c..788427b 100644 --- a/src/test/java/com/garbagemule/MobArena/signs/RendersTemplateTest.java +++ b/src/test/java/com/garbagemule/MobArena/signs/RendersTemplateTest.java @@ -95,6 +95,47 @@ public class RendersTemplateTest { assertThat(result, equalTo(joining)); } + @Test + public void readyOverridesLobbyIfPlayersReady() { + Player ready = mock(Player.class); + Arena arena = mock(Arena.class); + when(arena.configName()).thenReturn("castle"); + when(arena.isRunning()).thenReturn(false); + when(arena.getPlayersInLobby()).thenReturn(Collections.singleton(ready)); + when(arena.getNonreadyPlayers()).thenReturn(Collections.emptyList()); + when(arena.getReadyPlayersInLobby()).thenReturn(Collections.singleton(ready)); + String[] readyTemplate = {"we", "are", "all", "ready"}; + Template template = new Template.Builder("template") + .withBase(new String[]{"this", "is", "the", "base"}) + .withJoining(new String[]{"joining", "template", "is", "here"}) + .withReady(readyTemplate) + .build(); + + String[] result = subject.render(template, arena); + + assertThat(result, equalTo(readyTemplate)); + } + + @Test + public void readyPlayersReturnsJoiningIfNotDefined() { + Player ready = mock(Player.class); + Arena arena = mock(Arena.class); + when(arena.configName()).thenReturn("castle"); + when(arena.isRunning()).thenReturn(false); + when(arena.getPlayersInLobby()).thenReturn(Collections.singleton(ready)); + when(arena.getNonreadyPlayers()).thenReturn(Collections.emptyList()); + when(arena.getReadyPlayersInLobby()).thenReturn(Collections.singleton(ready)); + String[] joining = {"we", "in", "da", "lobby"}; + Template template = new Template.Builder("template") + .withBase(new String[]{"this", "is", "the", "base"}) + .withJoining(joining) + .build(); + + String[] result = subject.render(template, arena); + + assertThat(result, equalTo(joining)); + } + @Test public void rendersReadyListEntries() { Player ready = mock(Player.class);