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
This commit is contained in:
Bobcat00 2020-01-13 11:03:19 +01:00 committed by Andreas Troelsen
parent 5f7be69f0f
commit 90d1f211fa
4 changed files with 64 additions and 2 deletions

View File

@ -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();
}

View File

@ -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;

View File

@ -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) {

View File

@ -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);