refactor(command): LookCommand emits semantic RoomView
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -2,49 +2,27 @@ package thb.jeanluc.adventure.command.impl;
|
|||||||
|
|
||||||
import thb.jeanluc.adventure.command.Command;
|
import thb.jeanluc.adventure.command.Command;
|
||||||
import thb.jeanluc.adventure.game.GameContext;
|
import thb.jeanluc.adventure.game.GameContext;
|
||||||
|
import thb.jeanluc.adventure.io.text.RoomView;
|
||||||
import thb.jeanluc.adventure.model.Direction;
|
import thb.jeanluc.adventure.model.Direction;
|
||||||
import thb.jeanluc.adventure.model.Npc;
|
import thb.jeanluc.adventure.model.Npc;
|
||||||
import thb.jeanluc.adventure.model.Room;
|
import thb.jeanluc.adventure.model.Room;
|
||||||
import thb.jeanluc.adventure.model.item.Item;
|
import thb.jeanluc.adventure.model.item.Item;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prints a full description of the room the player is currently in:
|
* Describes the current room by emitting a {@link RoomView}; the active
|
||||||
* name, description, visible items, visible NPCs, and available exits.
|
* frontend decides how to render it. Usage: {@code look}.
|
||||||
* Usage: {@code look}.
|
|
||||||
*/
|
*/
|
||||||
public class LookCommand implements Command {
|
public class LookCommand implements Command {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(GameContext ctx, List<String> args) {
|
public void execute(GameContext ctx, List<String> args) {
|
||||||
Room room = ctx.getPlayer().getCurrentRoom();
|
Room room = ctx.getPlayer().getCurrentRoom();
|
||||||
StringBuilder sb = new StringBuilder();
|
List<String> items = room.getItems().values().stream().map(Item::getName).toList();
|
||||||
sb.append("== ").append(room.getName()).append(" ==\n");
|
List<String> npcs = room.getNpcs().values().stream().map(Npc::getName).toList();
|
||||||
sb.append(room.getDescription().stripTrailing());
|
List<String> exits = room.getExits().keySet().stream().map(Direction::getLabel).toList();
|
||||||
|
ctx.getIo().showRoom(new RoomView(room.getName(), room.getDescription(), items, npcs, exits));
|
||||||
if (!room.getItems().isEmpty()) {
|
|
||||||
String items = room.getItems().values().stream()
|
|
||||||
.map(Item::getName)
|
|
||||||
.collect(Collectors.joining(", "));
|
|
||||||
sb.append("\nYou see: ").append(items).append('.');
|
|
||||||
}
|
|
||||||
if (!room.getNpcs().isEmpty()) {
|
|
||||||
String npcs = room.getNpcs().values().stream()
|
|
||||||
.map(Npc::getName)
|
|
||||||
.collect(Collectors.joining(", "));
|
|
||||||
sb.append("\nHere is: ").append(npcs).append('.');
|
|
||||||
}
|
|
||||||
if (room.getExits().isEmpty()) {
|
|
||||||
sb.append("\nThere are no obvious exits.");
|
|
||||||
} else {
|
|
||||||
String exits = room.getExits().keySet().stream()
|
|
||||||
.map(Direction::getLabel)
|
|
||||||
.collect(Collectors.joining(", "));
|
|
||||||
sb.append("\nExits: ").append(exits).append('.');
|
|
||||||
}
|
|
||||||
ctx.getIo().write(sb.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user