feat: Started implementing cashier

This commit is contained in:
jzitnik-dev 2024-12-27 08:07:38 +01:00
parent 47e3a1dec5
commit 53b4e9090e
5 changed files with 121 additions and 72 deletions

View File

@ -1,5 +1,6 @@
package cz.jzitnik.chronos.interactions;
public enum Interaction {
Farmer
Farmer,
Cashier
}

View File

@ -0,0 +1,9 @@
package cz.jzitnik.chronos.interactions;
import cz.jzitnik.chronos.entities.Player;
import cz.jzitnik.chronos.entities.Character;
import cz.jzitnik.chronos.payload.responses.InteractionResponse;
public interface InteractionPlayer {
InteractionResponse play(Player player, Character character, String data);
}

View File

@ -1,29 +1,20 @@
package cz.jzitnik.chronos.interactions;
import cz.jzitnik.chronos.entities.Item;
import cz.jzitnik.chronos.entities.ItemType;
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.RockPaperScissors;
import cz.jzitnik.chronos.payload.responses.InteractionResponse;
import cz.jzitnik.chronos.repository.CharacterRepository;
import cz.jzitnik.chronos.repository.PlayerRepository;
import cz.jzitnik.chronos.services.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Random;
@Service
public class InteractionService {
@Autowired
private PlayerRepository playerRepository;
private RockPaperScissors rockPaperScissors;
@Autowired
private CharacterRepository characterRepository;
@Autowired
private ItemService itemService;
private Hangman hangman;
@FunctionalInterface
public interface Function3<T, U, V, W> {
@ -32,64 +23,8 @@ public class InteractionService {
public Function3<Player, Character, String, InteractionResponse> mapInteraction(Interaction interaction) {
return switch (interaction) {
case Farmer -> ((player, character, data) -> {
Random random = new Random();
boolean isLucky = player.isLuck();
int characterChoice = random.nextInt(3);
int userChoice = switch (data) {
case "0" -> 0; // Rock
case "1" -> 1; // Paper
case "2" -> 2; // Scissors
default -> throw new IllegalArgumentException("Invalid choice");
};
if (isLucky) {
// 50/50 chance
boolean playerWins = random.nextBoolean();
// Reset the luck potion after use
player.setLuck(false);
if (playerWins) {
var item = new Item(ItemType.KEY_FRAGMENT, player);
itemService.addItem(player, item);
character.setInteractedWith(true);
characterRepository.save(character);
var items = new ArrayList<Item>();
items.add(item);
return new InteractionResponse(true, "Vyhrál jsi, dostáváš ode mě jeden fragment klíče.", items);
} else {
character.setInteractedWith(true);
characterRepository.save(character);
return new InteractionResponse(false, "Prohrál jsi. Žádný fragment klíče ode mě už nedostaneš. Zkus to u někoho jiného.", new ArrayList<>());
}
} else {
// Standard rock-paper-scissors game logic
if (userChoice == characterChoice) {
return new InteractionResponse(false, "Remíza. Jestli chceš můžeš si se mnou zahrát ještě jednou.", new ArrayList<>());
} else if ((userChoice == 0 && characterChoice == 2) || (userChoice == 1 && characterChoice == 0) || (userChoice == 2 && characterChoice == 1)) {
var item = new Item(ItemType.KEY_FRAGMENT, player);
itemService.addItem(player, item);
character.setInteractedWith(true);
characterRepository.save(character);
var items = new ArrayList<Item>();
items.add(item);
return new InteractionResponse(true, "Vyhrál jsi, dostáváš ode mě jeden fragment klíče.", items);
} else {
character.setInteractedWith(true);
characterRepository.save(character);
return new InteractionResponse(false, "Prohrál jsi. Žádný fragment klíče ode mě už nedostaneš. Zkus to u někoho jiného.", new ArrayList<>());
}
}
});
case Farmer -> rockPaperScissors::play;
case Cashier -> hangman::play;
};
}
}

View File

@ -0,0 +1,16 @@
package cz.jzitnik.chronos.interactions.list;
import cz.jzitnik.chronos.entities.Character;
import cz.jzitnik.chronos.entities.Player;
import cz.jzitnik.chronos.interactions.InteractionPlayer;
import cz.jzitnik.chronos.payload.responses.InteractionResponse;
import org.springframework.stereotype.Service;
@Service
public class Hangman implements InteractionPlayer {
@Override
public InteractionResponse play(Player player, Character character, String data) {
return null;
}
}

View File

@ -0,0 +1,88 @@
package cz.jzitnik.chronos.interactions.list;
import cz.jzitnik.chronos.entities.Item;
import cz.jzitnik.chronos.entities.ItemType;
import cz.jzitnik.chronos.entities.Player;
import cz.jzitnik.chronos.entities.Character;
import cz.jzitnik.chronos.interactions.InteractionPlayer;
import cz.jzitnik.chronos.payload.responses.InteractionResponse;
import cz.jzitnik.chronos.repository.CharacterRepository;
import cz.jzitnik.chronos.repository.PlayerRepository;
import cz.jzitnik.chronos.services.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Random;
@Service
public class RockPaperScissors implements InteractionPlayer {
@Autowired
private PlayerRepository playerRepository;
@Autowired
private CharacterRepository characterRepository;
@Autowired
private ItemService itemService;
@Override
public InteractionResponse play(Player player, Character character, String data) {
Random random = new Random();
boolean isLucky = player.isLuck();
int characterChoice = random.nextInt(3);
int userChoice = switch (data) {
case "0" -> 0; // Rock
case "1" -> 1; // Paper
case "2" -> 2; // Scissors
default -> throw new IllegalArgumentException("Invalid choice");
};
if (isLucky) {
// 50/50 chance
boolean playerWins = random.nextBoolean();
// Reset the luck potion after use
player.setLuck(false);
if (playerWins) {
var item = new Item(ItemType.KEY_FRAGMENT, player);
itemService.addItem(player, item);
character.setInteractedWith(true);
characterRepository.save(character);
var items = new ArrayList<Item>();
items.add(item);
return new InteractionResponse(true, "Vyhrál jsi, dostáváš ode mě jeden fragment klíče.", items);
} else {
character.setInteractedWith(true);
characterRepository.save(character);
return new InteractionResponse(false, "Prohrál jsi. Žádný fragment klíče ode mě už nedostaneš. Zkus to u někoho jiného.", new ArrayList<>());
}
} else {
// Standard rock-paper-scissors game logic
if (userChoice == characterChoice) {
return new InteractionResponse(false, "Remíza. Jestli chceš můžeš si se mnou zahrát ještě jednou.", new ArrayList<>());
} else if ((userChoice == 0 && characterChoice == 2) || (userChoice == 1 && characterChoice == 0) || (userChoice == 2 && characterChoice == 1)) {
var item = new Item(ItemType.KEY_FRAGMENT, player);
itemService.addItem(player, item);
character.setInteractedWith(true);
characterRepository.save(character);
var items = new ArrayList<Item>();
items.add(item);
return new InteractionResponse(true, "Vyhrál jsi, dostáváš ode mě jeden fragment klíče.", items);
} else {
character.setInteractedWith(true);
characterRepository.save(character);
return new InteractionResponse(false, "Prohrál jsi. Žádný fragment klíče ode mě už nedostaneš. Zkus to u někoho jiného.", new ArrayList<>());
}
}
}
}