diff --git a/pom.xml b/pom.xml
index 0b776af..7c9b963 100644
--- a/pom.xml
+++ b/pom.xml
@@ -85,11 +85,6 @@
telegrambots-abilities
6.7.0
-
- org.luaj
- luaj-jse
- 3.0.1
-
org.apache.commons
commons-math3
diff --git a/src/main/java/com/alterdekim/javabot/Constants.java b/src/main/java/com/alterdekim/javabot/Constants.java
index fac4d61..5335f9b 100644
--- a/src/main/java/com/alterdekim/javabot/Constants.java
+++ b/src/main/java/com/alterdekim/javabot/Constants.java
@@ -53,4 +53,9 @@ public interface Constants {
String INFO_MESSAGE = "Вот открытая информация о живых игроках\n";
String CANT_SEND_NOT_DAY = "Нельзя использовать эту команду во время перерыва.";
+ String BUNKER_STATS = "В вашем бункере следующая ситуация:\n%s";
+ String RANDOM_HIV = "Затмение венеры (рандомный вич)";
+ String RANDOM_HIV_ENABLED = "Венера взошла для игрока %s";
+ String CHANGE_WORKS = "Обменяться профессиями";
+ String CHANGE_WORKS_TRIGGERED = "Вы обменялись профессиями с игроком %s";
}
\ No newline at end of file
diff --git a/src/main/java/com/alterdekim/javabot/bot/Player.java b/src/main/java/com/alterdekim/javabot/bot/Player.java
index c1cfaa2..7a98651 100644
--- a/src/main/java/com/alterdekim/javabot/bot/Player.java
+++ b/src/main/java/com/alterdekim/javabot/bot/Player.java
@@ -1,5 +1,6 @@
package com.alterdekim.javabot.bot;
+import com.alterdekim.javabot.bot.cards.ActionCard;
import com.alterdekim.javabot.entities.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
@@ -25,7 +26,7 @@ public class Player {
private String firstName;
private InfoSections infoSections;
private Boolean isVoted = false;
- private List scripts;
+ private List> scripts;
private Integer scriptMessageId;
public Player(Long telegramId, String name) {
diff --git a/src/main/java/com/alterdekim/javabot/bot/cards/ActionCard.java b/src/main/java/com/alterdekim/javabot/bot/cards/ActionCard.java
new file mode 100644
index 0000000..769d07e
--- /dev/null
+++ b/src/main/java/com/alterdekim/javabot/bot/cards/ActionCard.java
@@ -0,0 +1,18 @@
+package com.alterdekim.javabot.bot.cards;
+
+import com.alterdekim.javabot.bot.Player;
+import com.alterdekim.javabot.components.BunkerBot;
+import lombok.AllArgsConstructor;
+import lombok.NoArgsConstructor;
+
+
+@AllArgsConstructor
+@NoArgsConstructor
+public abstract class ActionCard {
+ BunkerBot bot;
+ Player activator;
+
+ public abstract void execute();
+
+ public abstract String getName();
+}
diff --git a/src/main/java/com/alterdekim/javabot/bot/cards/ChangeWorksCard.java b/src/main/java/com/alterdekim/javabot/bot/cards/ChangeWorksCard.java
new file mode 100644
index 0000000..887da97
--- /dev/null
+++ b/src/main/java/com/alterdekim/javabot/bot/cards/ChangeWorksCard.java
@@ -0,0 +1,23 @@
+package com.alterdekim.javabot.bot.cards;
+
+import com.alterdekim.javabot.Constants;
+import com.alterdekim.javabot.bot.Player;
+import com.alterdekim.javabot.entities.Work;
+import com.alterdekim.javabot.util.BotUtils;
+import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
+
+public class ChangeWorksCard extends ActionCard {
+ @Override
+ public void execute() {
+ Player p = (Player) BotUtils.getRandomFromList(this.bot.players, this.bot.random);
+ Work w = this.activator.getWork();
+ this.activator.setWork(p.getWork());
+ p.setWork(w);
+ this.bot.sendApi(new SendMessage(this.activator.getTelegramId()+"", String.format(Constants.CHANGE_WORKS_TRIGGERED, p.getFirstName())));
+ }
+
+ @Override
+ public String getName() {
+ return Constants.CHANGE_WORKS;
+ }
+}
diff --git a/src/main/java/com/alterdekim/javabot/bot/cards/RandomHIVCard.java b/src/main/java/com/alterdekim/javabot/bot/cards/RandomHIVCard.java
new file mode 100644
index 0000000..e7268e5
--- /dev/null
+++ b/src/main/java/com/alterdekim/javabot/bot/cards/RandomHIVCard.java
@@ -0,0 +1,22 @@
+package com.alterdekim.javabot.bot.cards;
+
+import com.alterdekim.javabot.Constants;
+import com.alterdekim.javabot.bot.Player;
+import com.alterdekim.javabot.util.BotUtils;
+import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
+
+public class RandomHIVCard extends ActionCard {
+ private static final long HIV_ID = 31;
+
+ @Override
+ public void execute() {
+ Player p = (Player) BotUtils.getRandomFromList(this.bot.players, this.bot.random);
+ p.setHealth(this.bot.healthService.getHealthById(HIV_ID));
+ this.bot.sendApi(new SendMessage(this.activator.getTelegramId()+"", String.format(Constants.RANDOM_HIV_ENABLED, p.getFirstName())));
+ }
+
+ @Override
+ public String getName() {
+ return Constants.RANDOM_HIV;
+ }
+}
diff --git a/src/main/java/com/alterdekim/javabot/components/BunkerBot.java b/src/main/java/com/alterdekim/javabot/components/BunkerBot.java
index b3302ff..2f33611 100644
--- a/src/main/java/com/alterdekim/javabot/components/BunkerBot.java
+++ b/src/main/java/com/alterdekim/javabot/components/BunkerBot.java
@@ -4,14 +4,14 @@ import com.alterdekim.javabot.bot.*;
import com.alterdekim.javabot.Commands;
import com.alterdekim.javabot.Constants;
import com.alterdekim.javabot.TelegramConfig;
+import com.alterdekim.javabot.bot.cards.ActionCard;
+import com.alterdekim.javabot.bot.cards.ChangeWorksCard;
+import com.alterdekim.javabot.bot.cards.RandomHIVCard;
import com.alterdekim.javabot.entities.*;
import com.alterdekim.javabot.service.*;
import com.alterdekim.javabot.util.*;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
-import org.luaj.vm2.Globals;
-import org.luaj.vm2.LuaValue;
-import org.luaj.vm2.lib.jse.JsePlatform;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
@@ -44,19 +44,19 @@ public class BunkerBot extends TelegramLongPollingBot {
private double last_p = -1;
- private List players;
+ public List players;
private final BioService bioService;
- private final HealthService healthService;
+ public final HealthService healthService;
private final HobbyService hobbyService;
private final LuggageService luggageService;
private final WorkService workService;
private final TextDataValService textDataValService;
private final DisasterService disasterService;
private final SynergyService synergyService;
- private final ActionScriptsServiceImpl scriptsService;
+ private List> actionCards;
- private final RandomComponent random;
+ public final RandomComponent random;
private DayNightFields dayNightFields;
@@ -72,7 +72,6 @@ public class BunkerBot extends TelegramLongPollingBot {
TextDataValService textDataValService,
DisasterService disasterService,
SynergyService synergyService,
- ActionScriptsServiceImpl scriptsService,
RandomComponent randomComponent) {
this.telegramConfig = telegramConfig;
this.players = new ArrayList<>();
@@ -85,7 +84,7 @@ public class BunkerBot extends TelegramLongPollingBot {
this.textDataValService = textDataValService;
this.disasterService = disasterService;
this.synergyService = synergyService;
- this.scriptsService = scriptsService;
+ this.actionCards = List.of(RandomHIVCard.class, ChangeWorksCard.class);
this.random = randomComponent;
this.dayNightFields = new DayNightFields();
this.linkedQueue = new ConcurrentLinkedQueue<>();
@@ -180,7 +179,7 @@ public class BunkerBot extends TelegramLongPollingBot {
private void startGame() {
if( gameState != GameState.JOINING )
return;
- if(players.size() < 2) { // TODO: change to 2
+ if(players.size() < 1) { // TODO: change to 2
sendApi(new SendMessage(groupId, Constants.PLAYERS_LESS_THAN_ZERO));
return;
}
@@ -188,12 +187,12 @@ public class BunkerBot extends TelegramLongPollingBot {
this.gameState = GameState.STARTED;
Disaster d = (Disaster) BotUtils.getRandomFromList(disasterService.getAllDisasters(), random);
sendApi(new SendMessage(groupId, getStringById(d.getDescTextId())));
+ //sendApi(new SendMessage(groupId, String.format(Constants.BUNKER_STATS, )));
List bios = bioService.getAllBios();
List works = workService.getAllWorks();
List luggs = luggageService.getAllLuggages();
List hobbies = hobbyService.getAllHobbies();
List healths = healthService.getAllHealth();
- List scripts = scriptsService.getAllActionScripts();
for( int i = 0; i < players.size(); i++ ) {
players.get(i).setAge(random.nextInt(57)+18);
players.get(i).setGender((Bio) BotUtils.getRandomFromList(bios, random));
@@ -201,13 +200,12 @@ public class BunkerBot extends TelegramLongPollingBot {
players.get(i).setLuggage((Luggage) BotUtils.getRandomFromList(luggs, random));
players.get(i).setHobby((Hobby) BotUtils.getRandomFromList(hobbies, random));
players.get(i).setHealth((Health) BotUtils.getRandomFromList(healths, random));
- if( (random.nextInt(100) >= 45 || (i == (players.size()-1) && isNoOneHasScripts())) && !scripts.isEmpty() ) {
- ActionScript asc = (ActionScript) BotUtils.getRandomFromList(scripts, random);
- //ActionScript asc = scripts.get(scripts.size()-1);
- scripts.removeIf(p -> p.getId().longValue() == asc.getId().longValue());
+ if( (random.nextInt(100) >= 45 || (i == (players.size()-1) && isNoOneHasScripts())) && !this.actionCards.isEmpty() ) {
+ Class extends ActionCard> asc = (Class extends ActionCard>) BotUtils.getRandomFromList(this.actionCards, random);
+ this.actionCards.removeIf(p -> p.getCanonicalName().equals(asc.getCanonicalName()));
players.get(i).setScripts(Collections.singletonList(asc));
} else {
- players.get(i).setScripts(new ArrayList<>());
+ players.get(i).setScripts(Collections.emptyList());
}
}
doNight();
@@ -226,7 +224,7 @@ public class BunkerBot extends TelegramLongPollingBot {
sendApi(sendMessage);
if( p.getScripts().isEmpty() ) continue;
sendMessage = new SendMessage(p.getTelegramId()+"", Constants.SCRIPT_MESSAGE);
- sendMessage.setReplyMarkup(BotUtils.getScriptKeyboard(p.getScripts(), textDataValService));
+ sendMessage.setReplyMarkup(BotUtils.getScriptKeyboard(p.getScripts()));
try {
setScriptMessageId(p, sendApiMethod(sendMessage).getMessageId());
} catch (Exception e) {
@@ -247,8 +245,16 @@ public class BunkerBot extends TelegramLongPollingBot {
}
private void processNightScriptButton(Player p, CallbackQuery callbackQuery, String data) {
- ActionScript script = p.getScripts().stream()
- .filter(s -> HashUtils.getCRC32(textDataValService.getTextDataValById(s.getTextNameId()).getText().getBytes()).equals(data))
+ Class extends ActionCard> script = p.getScripts().stream()
+ .filter(s -> {
+ try {
+ return HashUtils.getCRC32(s.getDeclaredConstructor().newInstance().getName().getBytes()).equals(data);
+ } catch (Exception e) {
+ log.error(e.getMessage());
+ }
+ return false;
+ }
+ )
.findFirst()
.orElse(null);
if( script == null ) return;
@@ -256,24 +262,11 @@ public class BunkerBot extends TelegramLongPollingBot {
sendApi(new SendMessage(groupId, String.format(Constants.PRESSED_SCRIPT_NIGHT, callbackQuery.getFrom().getFirstName())));
sendApi(new SendMessage(callbackQuery.getMessage().getChatId()+"", Constants.THANK_YOU));
p.setScripts(new ArrayList<>());
- executeLuaScript(script, p);
- }
-
- private void executeLuaScript(ActionScript script, Player p) {
- Globals globals = JsePlatform.standardGlobals();
- globals.set("players", LuaSerializer.serializeObjectList(players));
- globals.set("player", LuaSerializer.serializeObject(p));
- globals.set("genders", LuaSerializer.serializeObjectList(bioService.getAllBios()));
- globals.set("hobbies", LuaSerializer.serializeObjectList(hobbyService.getAllHobbies()));
- globals.set("healths", LuaSerializer.serializeObjectList(healthService.getAllHealth()));
- globals.set("luggages", LuaSerializer.serializeObjectList(luggageService.getAllLuggages()));
- globals.set("works", LuaSerializer.serializeObjectList(workService.getAllWorks()));
- LuaValue chunk = globals.load(script.getScriptBody());
- chunk.call();
- this.players = LuaDeserializer.deserializePlayers(globals.get("players"))
- .stream()
- .peek(p1 -> p1.setScripts(getPlayerById(p1.getTelegramId()).getScripts()))
- .collect(Collectors.toList());
+ try {
+ script.getDeclaredConstructor(BunkerBot.class, Player.class).newInstance(this, p).execute();
+ } catch (Exception e) {
+ log.error(e.getMessage());
+ }
}
private void processNightButton(CallbackQuery callbackQuery) {
@@ -333,7 +326,7 @@ public class BunkerBot extends TelegramLongPollingBot {
}
}
- private void sendApi(BotApiMethod extends Serializable> method) {
+ public void sendApi(BotApiMethod extends Serializable> method) {
this.linkedQueue.add(method);
}
diff --git a/src/main/java/com/alterdekim/javabot/entities/ActionScript.java b/src/main/java/com/alterdekim/javabot/entities/ActionScript.java
deleted file mode 100644
index 301d44a..0000000
--- a/src/main/java/com/alterdekim/javabot/entities/ActionScript.java
+++ /dev/null
@@ -1,32 +0,0 @@
-package com.alterdekim.javabot.entities;
-
-import jakarta.persistence.*;
-import lombok.*;
-
-@ToString
-@Getter
-@Setter
-@NoArgsConstructor
-@AllArgsConstructor
-@Entity
-@Table(name = "action_scripts")
-public class ActionScript {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Long id;
-
- @Column(nullable = false)
- private Long textNameId;
-
- @Column(nullable = false)
- private Long textDescId;
-
- @Column(nullable = false)
- private String scriptBody;
-
- public ActionScript(Long textNameId, Long textDescId, String scriptBody) {
- this.textNameId = textNameId;
- this.textDescId = textDescId;
- this.scriptBody = scriptBody;
- }
-}
diff --git a/src/main/java/com/alterdekim/javabot/entities/ActionScriptRequest.java b/src/main/java/com/alterdekim/javabot/entities/ActionScriptRequest.java
deleted file mode 100644
index 25d5182..0000000
--- a/src/main/java/com/alterdekim/javabot/entities/ActionScriptRequest.java
+++ /dev/null
@@ -1,34 +0,0 @@
-package com.alterdekim.javabot.entities;
-
-
-import jakarta.persistence.*;
-import lombok.*;
-
-@ToString
-@Getter
-@Setter
-@NoArgsConstructor
-@AllArgsConstructor
-@Entity
-@Table(name = "action_scripts_req")
-public class ActionScriptRequest {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Long id;
-
- @Column(nullable = false)
- private String textName;
-
- @Column(nullable = false)
- private String textDesc;
-
- @Column(nullable = false)
- private String scriptBody;
-
- public ActionScriptRequest(String textName, String textDesc, String scriptBody) {
- this.textName = textName;
- this.textDesc = textDesc;
- this.scriptBody = scriptBody;
- }
-}
diff --git a/src/main/java/com/alterdekim/javabot/repository/ActionScriptRequestsRepository.java b/src/main/java/com/alterdekim/javabot/repository/ActionScriptRequestsRepository.java
deleted file mode 100644
index 3e60a28..0000000
--- a/src/main/java/com/alterdekim/javabot/repository/ActionScriptRequestsRepository.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package com.alterdekim.javabot.repository;
-
-import com.alterdekim.javabot.entities.ActionScript;
-import com.alterdekim.javabot.entities.ActionScriptRequest;
-import org.springframework.data.jpa.repository.JpaRepository;
-
-import java.util.List;
-import java.util.Optional;
-
-public interface ActionScriptRequestsRepository extends JpaRepository {
- Optional findById(Long id);
-
- List findAll();
-}
diff --git a/src/main/java/com/alterdekim/javabot/repository/ActionScriptsRepository.java b/src/main/java/com/alterdekim/javabot/repository/ActionScriptsRepository.java
deleted file mode 100644
index e4c4a31..0000000
--- a/src/main/java/com/alterdekim/javabot/repository/ActionScriptsRepository.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.alterdekim.javabot.repository;
-
-import com.alterdekim.javabot.entities.ActionScript;
-import org.springframework.data.jpa.repository.JpaRepository;
-
-import java.util.List;
-import java.util.Optional;
-
-public interface ActionScriptsRepository extends JpaRepository {
- Optional findById(Long id);
-
- List findAll();
-}
diff --git a/src/main/java/com/alterdekim/javabot/service/ActionRequestService.java b/src/main/java/com/alterdekim/javabot/service/ActionRequestService.java
deleted file mode 100644
index b079fea..0000000
--- a/src/main/java/com/alterdekim/javabot/service/ActionRequestService.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.alterdekim.javabot.service;
-
-import com.alterdekim.javabot.entities.ActionScript;
-import com.alterdekim.javabot.entities.ActionScriptRequest;
-
-import java.util.List;
-
-public interface ActionRequestService {
- List getAllActionScripts();
- ActionScriptRequest getActionScriptById(long scriptId);
- void removeById(long scriptId);
- void saveScript(ActionScriptRequest script);
-}
diff --git a/src/main/java/com/alterdekim/javabot/service/ActionScriptsService.java b/src/main/java/com/alterdekim/javabot/service/ActionScriptsService.java
deleted file mode 100644
index 554df80..0000000
--- a/src/main/java/com/alterdekim/javabot/service/ActionScriptsService.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.alterdekim.javabot.service;
-
-import com.alterdekim.javabot.entities.ActionScript;
-import com.alterdekim.javabot.entities.Luggage;
-
-import java.util.List;
-
-public interface ActionScriptsService {
- List getAllActionScripts();
- ActionScript getActionScriptById(long scriptId);
- void removeById(long scriptId);
- void saveScript(ActionScript script);
-}
diff --git a/src/main/java/com/alterdekim/javabot/service/ActionScriptsServiceImpl.java b/src/main/java/com/alterdekim/javabot/service/ActionScriptsServiceImpl.java
deleted file mode 100644
index 6a63f6a..0000000
--- a/src/main/java/com/alterdekim/javabot/service/ActionScriptsServiceImpl.java
+++ /dev/null
@@ -1,35 +0,0 @@
-package com.alterdekim.javabot.service;
-
-import com.alterdekim.javabot.entities.ActionScript;
-import com.alterdekim.javabot.repository.ActionScriptsRepository;
-import lombok.AllArgsConstructor;
-import org.springframework.stereotype.Service;
-
-import java.util.List;
-
-@Service
-@AllArgsConstructor
-public class ActionScriptsServiceImpl implements ActionScriptsService {
-
- private final ActionScriptsRepository actionScriptsRepository;
-
- @Override
- public List getAllActionScripts() {
- return actionScriptsRepository.findAll();
- }
-
- @Override
- public ActionScript getActionScriptById(long scriptId) {
- return actionScriptsRepository.findById(scriptId).orElse(null);
- }
-
- @Override
- public void removeById(long scriptId) {
- actionScriptsRepository.deleteById(scriptId);
- }
-
- @Override
- public void saveScript(ActionScript script) {
- actionScriptsRepository.save(script);
- }
-}
diff --git a/src/main/java/com/alterdekim/javabot/util/BotUtils.java b/src/main/java/com/alterdekim/javabot/util/BotUtils.java
index d387efc..4d27d33 100644
--- a/src/main/java/com/alterdekim/javabot/util/BotUtils.java
+++ b/src/main/java/com/alterdekim/javabot/util/BotUtils.java
@@ -3,16 +3,17 @@ package com.alterdekim.javabot.util;
import com.alterdekim.javabot.Constants;
import com.alterdekim.javabot.bot.InfoSections;
import com.alterdekim.javabot.bot.SectionType;
+import com.alterdekim.javabot.bot.cards.ActionCard;
import com.alterdekim.javabot.components.RandomComponent;
-import com.alterdekim.javabot.entities.ActionScript;
-import com.alterdekim.javabot.service.TextDataValService;
-import com.alterdekim.javabot.service.TextDataValServiceImpl;
+import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
+import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.stream.Collectors;
+@Slf4j
public class BotUtils {
public static InlineKeyboardMarkup getJoinKeyboard() {
InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
@@ -77,13 +78,18 @@ public class BotUtils {
return inlineKeyboardMarkup;
}
- public static InlineKeyboardMarkup getScriptKeyboard(List scripts, TextDataValService textDataValService) {
+ public static InlineKeyboardMarkup getScriptKeyboard(List> scripts) {
InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
inlineKeyboardMarkup.setKeyboard(scripts.stream()
.map(s -> {
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton();
- inlineKeyboardButton.setText(textDataValService.getTextDataValById(s.getTextNameId()).getText());
- inlineKeyboardButton.setCallbackData(HashUtils.getCRC32(textDataValService.getTextDataValById(s.getTextNameId()).getText().getBytes()));
+ try {
+ inlineKeyboardButton.setText(s.getDeclaredConstructor().newInstance().getName());
+ inlineKeyboardButton.setCallbackData(HashUtils.getCRC32(s.getDeclaredConstructor().newInstance().getName().getBytes()));
+ } catch (NoSuchMethodException | InstantiationException | IllegalAccessException |
+ InvocationTargetException e) {
+ log.error(e.getMessage());
+ }
return Collections.singletonList(inlineKeyboardButton);
}).collect(Collectors.toList()));
return inlineKeyboardMarkup;
diff --git a/src/main/resources/templates/panel.html b/src/main/resources/templates/panel.html
index 8f39706..3a04da7 100644
--- a/src/main/resources/templates/panel.html
+++ b/src/main/resources/templates/panel.html
@@ -42,12 +42,12 @@
-
+
+