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.game.GameContext;
|
||||
import thb.jeanluc.adventure.io.text.RoomView;
|
||||
import thb.jeanluc.adventure.model.Direction;
|
||||
import thb.jeanluc.adventure.model.Npc;
|
||||
import thb.jeanluc.adventure.model.Room;
|
||||
import thb.jeanluc.adventure.model.item.Item;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Prints a full description of the room the player is currently in:
|
||||
* name, description, visible items, visible NPCs, and available exits.
|
||||
* Usage: {@code look}.
|
||||
* Describes the current room by emitting a {@link RoomView}; the active
|
||||
* frontend decides how to render it. Usage: {@code look}.
|
||||
*/
|
||||
public class LookCommand implements Command {
|
||||
|
||||
@Override
|
||||
public void execute(GameContext ctx, List<String> args) {
|
||||
Room room = ctx.getPlayer().getCurrentRoom();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("== ").append(room.getName()).append(" ==\n");
|
||||
sb.append(room.getDescription().stripTrailing());
|
||||
|
||||
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());
|
||||
List<String> items = room.getItems().values().stream().map(Item::getName).toList();
|
||||
List<String> npcs = room.getNpcs().values().stream().map(Npc::getName).toList();
|
||||
List<String> exits = room.getExits().keySet().stream().map(Direction::getLabel).toList();
|
||||
ctx.getIo().showRoom(new RoomView(room.getName(), room.getDescription(), items, npcs, exits));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user