feat: Mayor and taking items from room

This commit is contained in:
jzitnik-dev 2024-12-31 14:22:10 +01:00
parent 910808eb6e
commit c47df0d609
Signed by: jzitnik
GPG Key ID: C577A802A6AF4EF3
20 changed files with 270 additions and 38 deletions

View File

@ -1,5 +1,6 @@
package cz.jzitnik.chronos.controllers;
import cz.jzitnik.chronos.entities.Item;
import cz.jzitnik.chronos.entities.Message;
import cz.jzitnik.chronos.entities.MessageType;
import cz.jzitnik.chronos.entities.Room;
@ -8,11 +9,14 @@ import cz.jzitnik.chronos.payload.responses.UnifiedResponse;
import cz.jzitnik.chronos.repository.PlayerRepository;
import cz.jzitnik.chronos.repository.RoomRepository;
import cz.jzitnik.chronos.services.GameService;
import cz.jzitnik.chronos.services.ItemService;
import cz.jzitnik.chronos.utils.anotations.CheckUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@CrossOrigin(origins = "*", maxAge = 3600)
@ -28,9 +32,12 @@ public class RoomController {
@Autowired
private GameService gameService;
@Autowired
private ItemService itemService;
@GetMapping
@CheckUser
public ResponseEntity<UnifiedResponse<List<Room>, Error>> getAllRooms(@RequestParam("playerKey") String playerKey) {
public ResponseEntity<UnifiedResponse<List<Room>, Error>> getAllRooms(@RequestParam String playerKey) {
var player = playerRepository.findByPlayerKey(playerKey).get();
return ResponseEntity.ok(
@ -40,7 +47,7 @@ public class RoomController {
@GetMapping("/room")
@CheckUser
public ResponseEntity<UnifiedResponse<Room, Error>> getRoom(@RequestParam("playerKey") String playerKey, @RequestParam("roomId") Long roomId) {
public ResponseEntity<UnifiedResponse<Room, Error>> getRoom(@RequestParam String playerKey, @RequestParam Long roomId) {
var player = playerRepository.findByPlayerKey(playerKey).get();
var roomOptional = roomRepository.findById(roomId);
@ -64,7 +71,7 @@ public class RoomController {
@GetMapping("/current_room")
@CheckUser
public ResponseEntity<UnifiedResponse<Room, Error>> getCurrentRoom(@RequestParam("playerKey") String playerKey) {
public ResponseEntity<UnifiedResponse<Room, Error>> getCurrentRoom(@RequestParam String playerKey) {
var player = playerRepository.findByPlayerKey(playerKey).get();
return ResponseEntity.ok(
@ -74,7 +81,7 @@ public class RoomController {
@PostMapping("/move")
@CheckUser
public ResponseEntity<UnifiedResponse<Object, Error>> moveToRoom(@RequestParam("playerKey") String playerKey, @RequestParam("roomId") Long roomId) {
public ResponseEntity<UnifiedResponse<Object, Error>> moveToRoom(@RequestParam String playerKey, @RequestParam Long roomId) {
var player = playerRepository.findByPlayerKey(playerKey).get();
var roomOptional = roomRepository.findById(roomId);
@ -83,21 +90,50 @@ public class RoomController {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(UnifiedResponse.failure(null));
}
var fromThisGame = gameService.isFromThisGame(player.getGame(), roomOptional.get());
var room = roomOptional.get();
var fromThisGame = gameService.isFromThisGame(player.getGame(), room);
if (!fromThisGame) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(UnifiedResponse.failure(new Error("Invalid roomId")));
}
player.setCurrentRoom(roomOptional.get());
player.setCurrentRoom(room);
// Send message to chat
var message = new Message(player, String.valueOf(roomId), MessageType.MOVE_TO_ROOM);
player.getMessages().add(message);
playerRepository.save(player);
return ResponseEntity.ok(UnifiedResponse.success(null));
}
@PostMapping("/take_items")
@CheckUser
public ResponseEntity<UnifiedResponse<List<Item>, Error>> takeItems(@RequestParam String playerKey) {
var player = playerRepository.findByPlayerKey(playerKey).get();
var room = player.getCurrentRoom();
if (room.getItems().isEmpty()) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(UnifiedResponse.failure(new Error("No items in this room!")));
}
var items = room.getItems();
var availableSlots = player.getInventorySize() - player.getInventory().size();
System.out.println(availableSlots);
var playerGotItems = new ArrayList<Item>();
for (var i = 0; i <= Math.min(items.size(), availableSlots) - 1; i++) {
var item = items.get(i);
item.setRoom(null);
item.setOwner(player);
itemService.addItem(player, item);
playerGotItems.add(item);
}
return ResponseEntity.ok(UnifiedResponse.success(playerGotItems));
}
}

View File

@ -39,4 +39,9 @@ public class Item {
this.itemType = itemType;
this.owner = owner;
}
public Item(ItemType itemType, Room room) {
this.itemType = itemType;
this.room = room;
}
}

View File

@ -4,7 +4,8 @@ import cz.jzitnik.chronos.payload.errors.ItemNotUsableException;
public enum ItemType {
KEY_FRAGMENT,
LUCK_POTION;
LUCK_POTION,
GOLDEN_WATCH;
public void useItem(Player player) throws ItemNotUsableException {
switch (this) {

View File

@ -9,6 +9,7 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
@ -66,4 +67,18 @@ public class Player {
public void addItem(Item item) {
inventory.add(item);
}
@JsonIgnore
public int getInventorySize() {
try {
Field field = this.getClass().getDeclaredField("inventory");
if (field.isAnnotationPresent(Size.class)) {
Size size = field.getAnnotation(Size.class);
return size.max();
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return Integer.MAX_VALUE;
}
}

View File

@ -5,4 +5,5 @@ public enum Interaction {
Cashier,
Librarian,
Innkeeper,
Mayor
}

View File

@ -3,6 +3,7 @@ package cz.jzitnik.chronos.interactions;
import cz.jzitnik.chronos.entities.Player;
import cz.jzitnik.chronos.entities.Character;
import cz.jzitnik.chronos.interactions.list.Hangman;
import cz.jzitnik.chronos.interactions.list.Mayor;
import cz.jzitnik.chronos.interactions.list.RockPaperScissors;
import cz.jzitnik.chronos.interactions.list.TicTacToe;
import cz.jzitnik.chronos.interactions.list.wordle.Wordle;
@ -20,6 +21,8 @@ public class InteractionService {
private Wordle wordle;
@Autowired
private TicTacToe ticTacToe;
@Autowired
private Mayor mayor;
@FunctionalInterface
public interface Function3<T, U, V, W> {
@ -32,6 +35,7 @@ public class InteractionService {
case Cashier -> hangman::play;
case Librarian -> wordle::play;
case Innkeeper -> ticTacToe::play;
case Mayor -> mayor::play;
};
}
}

View File

@ -0,0 +1,46 @@
package cz.jzitnik.chronos.interactions.list;
import cz.jzitnik.chronos.entities.Character;
import cz.jzitnik.chronos.entities.Item;
import cz.jzitnik.chronos.entities.ItemType;
import cz.jzitnik.chronos.entities.Player;
import cz.jzitnik.chronos.interactions.InteractionPlayer;
import cz.jzitnik.chronos.payload.responses.InteractionResponse;
import cz.jzitnik.chronos.repository.CharacterRepository;
import cz.jzitnik.chronos.services.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
@Service
public class Mayor implements InteractionPlayer {
@Autowired
private ItemService itemService;
@Autowired
private CharacterRepository characterRepository;
@Override
public InteractionResponse play(Player player, Character character, String data) {
var playerItems = player.getInventory();
var golderWatches = playerItems.stream().filter(item -> item.getItemType().equals(ItemType.GOLDEN_WATCH)).toList();
if (golderWatches.isEmpty()) {
return new InteractionResponse(false, "Golden watch is not present in your inventory!", new ArrayList<>());
}
var golderWatch = golderWatches.getFirst();
playerItems.remove(golderWatch);
var item = new Item(ItemType.KEY_FRAGMENT, player);
itemService.addItem(player, item); // itemRepository.save is ran by this function so we don't need to save the player when we removed the item from his inv
character.setInteractedWith(true);
characterRepository.save(character);
var items = new ArrayList<Item>();
items.add(item);
return new InteractionResponse(false, "Díky moc za mé ztracené zlaté hodinky... Doplnit dialog!!!", items);
}
}

View File

@ -14,7 +14,7 @@ public class InitGameService {
// Outside
var outside = new Room(
"Outside", // Name
"Outside", // Náměstí
game
);
var outside_characters = new ArrayList<Character>();
@ -109,12 +109,42 @@ public class InitGameService {
inn_characters.add(innkeeper);
inn.setCharacters(inn_characters);
// Dům starosty
var mayor_house = new Room(
"Dům starosty",
game
);
var mayor_house_characters = new ArrayList<Character>();
var mayor = new Character(
"Starosta",
mayor_house,
"Ahoj já jsem starosta. Dej mi moje ztracené zlaté hodinky a dám ti fragment klíče."
);
mayor.setInteraction(Interaction.Mayor);
mayor.setInteractionData(new cz.jzitnik.chronos.entities.Interaction(
"",
"Zlaté hodinky už mám. Děkuji za nalezení.",
mayor
));
mayor_house_characters.add(mayor);
mayor_house.setCharacters(mayor_house_characters);
// Zarostlá zahrada
var garden = new Room(
"Zarostlá zahrada",
game
);
// Zlaté hodinky pro starostu
garden.getItems().add(new Item(ItemType.GOLDEN_WATCH, garden));
rooms.add(outside);
rooms.add(stodola);
rooms.add(shop);
rooms.add(library);
rooms.add(inn);
rooms.add(mayor_house);
rooms.add(garden);
game.setRooms(rooms);

View File

@ -47,7 +47,7 @@ public class Main {
}
}
Cli.info("Prosím počkejte...");
Cli.info("Prosím počkejte než hra začne...");
chronos.waitForStart();
Cli.info("Hra začala!");
System.out.println("\n\n");

View File

@ -71,6 +71,11 @@ public interface ApiService {
@Query("roomId") Long roomId
);
@POST("game/rooms/take_items")
Call<UnifiedResponse<List<Item>, Error>> takeItemsFromRoom(
@Query("playerKey") String playerKey
);
@POST("game/characters/take_items")
Call<UnifiedResponse<TakeItemsResponse, Error>> takeItems(
@Query("playerKey") String playerKey,

View File

@ -4,5 +4,6 @@ public enum Interaction {
Farmer,
Cashier,
Librarian,
Innkeeper
Innkeeper,
Mayor
}

View File

@ -5,7 +5,8 @@ import java.util.Set;
public enum ItemType {
KEY_FRAGMENT,
LUCK_POTION;
LUCK_POTION,
GOLDEN_WATCH;
private static final Set<ItemType> usableItems = new HashSet<>();
@ -22,6 +23,7 @@ public enum ItemType {
return switch (this) {
case LUCK_POTION -> "Lektvar štěstí";
case KEY_FRAGMENT -> "Fragment klíče";
case GOLDEN_WATCH -> "Zlaté hodinky";
};
}
}

View File

@ -19,7 +19,7 @@ public class Room {
@Override
public String toString() {
if (name.equals("Outside")) {
return "Venek";
return "Náměstí";
}
return name;

View File

@ -17,6 +17,7 @@ import java.net.ConnectException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@ -209,14 +210,33 @@ public class Chronos {
public void visit(Room room) throws IOException {
System.out.println("\n");
if (room.getName().equals("Outside")) {
Cli.info("Nyní jste venku");
Cli.info("Nyní jste na náměstí.");
} else {
Cli.info("Nyní se nacházíte v místnosti " + room.getName());
Cli.info("Nyní se nacházíte v " + Cli.Colors.BLUE + room.getName() + Cli.Colors.RESET + ".");
}
var items = room.getItems();
if (!items.isEmpty()) {
Cli.info("V místnosti se nachází: " + String.join(", ", items.stream().map(Item::toString).toList()));
var options = new String[] {"Vzít si itemy", "Nebrat si itemy"};
var selected = Cli.selectOptionIndex(Arrays.asList(options));
if (selected == 0) {
var response = apiService.takeItemsFromRoom(localData.getUserSecret()).execute();
var itemsGot = response.body().getData().get();
Cli.gotItems(itemsGot);
if (itemsGot.size() < items.size()) {
Cli.info("Některé itemy jste nemohl vzít, protože váš inventář je plný.");
}
}
}
System.out.println();
var characters = room.getCharacters();
if (characters.isEmpty()) {
Cli.type("V místnosti je prázno... Ani jedna duše.");
Cli.type("V místnosti se nenachází jediná duše...");
} else {
talk(characters);
}

View File

@ -1,12 +1,11 @@
package cz.jzitnik.game.interactions;
import cz.jzitnik.api.ApiService;
import cz.jzitnik.api.requests.InteractionRequest;
import cz.jzitnik.api.types.Character;
import cz.jzitnik.game.interactions.list.*;
import cz.jzitnik.utils.Cli;
import java.io.IOException;
import java.util.Arrays;
public class Interactions {
public static void runInteraction(Character character, ApiService apiService, String playerKey) throws IOException {
@ -18,24 +17,14 @@ public class Interactions {
return;
}
if (!interactionData.getStartText().isBlank()) {
Cli.type(character, interactionData.getStartText());
}
switch (character.getInteraction()) {
case Farmer -> {
var options = new String[]{"Kámen", "Nůžky", "Papír"};
var selected = Cli.selectOptionIndex(Arrays.stream(options).toList());
var requestData = new InteractionRequest(String.valueOf(selected), character.getId());
var res = apiService.interact(playerKey, requestData).execute();
var interactionResponse = res.body().getData().get();
Cli.type(character, interactionResponse.getResponseText());
if (interactionResponse.isSuccess()) {
Cli.gotItems(interactionResponse.getItems());
}
RockPaperScissors rockPaperScissors = new RockPaperScissors(character, apiService, playerKey);
rockPaperScissors.play();
}
case Cashier -> {
Hangman hangman = new Hangman(character, apiService, playerKey);
@ -49,6 +38,10 @@ public class Interactions {
TicTacToe ticTacToe = new TicTacToe(character, apiService, playerKey);
ticTacToe.play();
}
case Mayor -> {
Mayor mayor = new Mayor(character, apiService, playerKey);
mayor.play();
}
}
}
}

View File

@ -1,4 +1,4 @@
package cz.jzitnik.game.interactions;
package cz.jzitnik.game.interactions.list;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.jzitnik.api.ApiService;

View File

@ -0,0 +1,40 @@
package cz.jzitnik.game.interactions.list;
import cz.jzitnik.api.ApiService;
import cz.jzitnik.api.requests.InteractionRequest;
import cz.jzitnik.api.types.Character;
import cz.jzitnik.api.types.ItemType;
import cz.jzitnik.utils.Cli;
import lombok.AllArgsConstructor;
import java.io.IOException;
import java.util.Arrays;
@AllArgsConstructor
public class Mayor {
private Character character;
private ApiService apiService;
private String playerKey;
public void play() throws IOException {
var response = apiService.getMe(playerKey).execute();
var me = response.body().getData().get();
if (!me.getInventory().stream().anyMatch(item -> item.getItemType().equals(ItemType.GOLDEN_WATCH))) {
// Player does not have golden watch
return;
}
var options = new String[]{"Dát starostovi zlaté hodinky", "Nedát starostovi zlaté hodinky"};
var selected = Cli.selectOptionIndex(Arrays.stream(options).toList());
if (selected == 0) {
var res = apiService.interact(playerKey, new InteractionRequest("", character.getId())).execute();
var data = res.body().getData().get();
Cli.type(character, data.getResponseText());
Cli.gotItems(data.getItems());
}
}
}

View File

@ -0,0 +1,34 @@
package cz.jzitnik.game.interactions.list;
import cz.jzitnik.api.ApiService;
import cz.jzitnik.api.requests.InteractionRequest;
import cz.jzitnik.api.types.Character;
import cz.jzitnik.utils.Cli;
import lombok.AllArgsConstructor;
import java.io.IOException;
import java.util.Arrays;
@AllArgsConstructor
public class RockPaperScissors {
private Character character;
private ApiService apiService;
private String playerKey;
public void play() throws IOException {
var options = new String[]{"Kámen", "Nůžky", "Papír"};
var selected = Cli.selectOptionIndex(Arrays.stream(options).toList());
var requestData = new InteractionRequest(String.valueOf(selected), character.getId());
var res = apiService.interact(playerKey, requestData).execute();
var interactionResponse = res.body().getData().get();
Cli.type(character, interactionResponse.getResponseText());
if (interactionResponse.isSuccess()) {
Cli.gotItems(interactionResponse.getItems());
}
}
}

View File

@ -1,4 +1,4 @@
package cz.jzitnik.game.interactions;
package cz.jzitnik.game.interactions.list;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.jzitnik.api.ApiService;

View File

@ -1,4 +1,4 @@
package cz.jzitnik.game.interactions;
package cz.jzitnik.game.interactions.list;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.jzitnik.api.ApiService;
@ -12,7 +12,6 @@ import lombok.Getter;
import java.io.Console;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
@AllArgsConstructor
public class Wordle {