Refactoring stuff

This commit is contained in:
Michael Wain 2024-07-04 19:38:35 +03:00
parent da9fdadc12
commit d442b1e159
3 changed files with 67 additions and 28 deletions

View File

@ -114,8 +114,7 @@ public class BunkerBot extends TelegramLongPollingBot {
} }
private void joinGame(User user, Integer msgid) { private void joinGame(User user, Integer msgid) {
if( gameState != GameState.JOINING ) if( gameState != GameState.JOINING ) return;
return;
if( !hasPlayerWithId(user.getId()) ) { if( !hasPlayerWithId(user.getId()) ) {
if( canWrite(user) ) { if( canWrite(user) ) {
@ -205,7 +204,7 @@ public class BunkerBot extends TelegramLongPollingBot {
if( (random.nextInt(100) >= 60 || (i == (players.size()-1) && isNoOneHasScripts())) && !scripts.isEmpty() ) { if( (random.nextInt(100) >= 60 || (i == (players.size()-1) && isNoOneHasScripts())) && !scripts.isEmpty() ) {
ActionScript asc = (ActionScript) BotUtils.getRandomFromList(scripts, random); ActionScript asc = (ActionScript) BotUtils.getRandomFromList(scripts, random);
scripts.removeIf(p -> p.getId().longValue() == asc.getId().longValue()); scripts.removeIf(p -> p.getId().longValue() == asc.getId().longValue());
players.get(i).setScripts(Arrays.asList(asc)); players.get(i).setScripts(Collections.singletonList(asc));
} else { } else {
players.get(i).setScripts(new ArrayList<>()); players.get(i).setScripts(new ArrayList<>());
} }
@ -271,11 +270,10 @@ public class BunkerBot extends TelegramLongPollingBot {
globals.set("works", LuaSerializer.serializeObjectList(workService.getAllWorks())); globals.set("works", LuaSerializer.serializeObjectList(workService.getAllWorks()));
LuaValue chunk = globals.load(script.getScriptBody()); LuaValue chunk = globals.load(script.getScriptBody());
chunk.call(); chunk.call();
this.players = LuaDeserializer.deserializePlayers(globals.get("players")).stream() this.players = LuaDeserializer.deserializePlayers(globals.get("players"))
.map(p1 -> { .stream()
p1.setScripts(getPlayerById(p1.getTelegramId()).getScripts()); .peek(p1 -> p1.setScripts(getPlayerById(p1.getTelegramId()).getScripts()))
return p1; .collect(Collectors.toList());
}).collect(Collectors.toList());
} }
private void processNightButton(CallbackQuery callbackQuery) { private void processNightButton(CallbackQuery callbackQuery) {
@ -400,46 +398,54 @@ public class BunkerBot extends TelegramLongPollingBot {
sendApi(new SendMessage(groupId, Constants.CANT_SEND_NOT_DAY)); sendApi(new SendMessage(groupId, Constants.CANT_SEND_NOT_DAY));
return; return;
} }
String message = Constants.INFO_MESSAGE; StringBuilder message = new StringBuilder();
message.append(Constants.INFO_MESSAGE);
for( Player p : players ) { for( Player p : players ) {
message += p.getFirstName() + ":\n"; message.append(p.getFirstName());
message.append(":\n");
InfoSections s = p.getInfoSections(); InfoSections s = p.getInfoSections();
if(s.getIsGenderShowed()) { if(s.getIsGenderShowed()) {
message += String.format(Constants.GENDER_MESAGE, p.getFirstName(), getStringById(p.getGender().getGenderTextId()), message.append(String.format(Constants.GENDER_MESAGE, p.getFirstName(), getStringById(p.getGender().getGenderTextId()),
p.getGender().getCanDie() ? Constants.TRUE : Constants.FALSE, p.getGender().getCanDie() ? Constants.TRUE : Constants.FALSE,
p.getGender().getIsMale() ? Constants.TRUE : Constants.FALSE, p.getGender().getIsMale() ? Constants.TRUE : Constants.FALSE,
p.getGender().getIsFemale() ? Constants.TRUE : Constants.FALSE) + "\n"; p.getGender().getIsFemale() ? Constants.TRUE : Constants.FALSE));
message.append("\n");
} }
if(s.getIsAgeShowed()) { if(s.getIsAgeShowed()) {
message += String.format(Constants.AGE_MESSAGE, p.getFirstName(), p.getAge()) + "\n"; message.append(String.format(Constants.AGE_MESSAGE, p.getFirstName(), p.getAge()));
message.append("\n");
} }
if(s.getIsLuggageShowed()) { if(s.getIsLuggageShowed()) {
message += String.format(Constants.LUGG_MESSAGE, p.getFirstName(), message.append(String.format(Constants.LUGG_MESSAGE, p.getFirstName(),
getStringById(p.getLuggage().getTextNameId()), getStringById(p.getLuggage().getTextNameId()),
getStringById(p.getLuggage().getTextDescId())) + "\n"; getStringById(p.getLuggage().getTextDescId())));
message.append("\n");
} }
if(s.getIsHealthShowed()) { if(s.getIsHealthShowed()) {
message += String.format(Constants.HEALTH_MESSAGE, p.getFirstName(), getStringById(p.getHealth().getTextNameId()), message.append(String.format(Constants.HEALTH_MESSAGE, p.getFirstName(), getStringById(p.getHealth().getTextNameId()),
getStringById(p.getHealth().getTextDescId()), getStringById(p.getHealth().getTextDescId()),
(int) (p.getHealth().getHealth_index()*100f), (int) (p.getHealth().getHealth_index()*100f),
p.getHealth().getIsChildfree() ? Constants.TRUE : Constants.FALSE) + "\n"; p.getHealth().getIsChildfree() ? Constants.TRUE : Constants.FALSE));
message.append("\n");
} }
if(s.getIsWorkShowed()) { if(s.getIsWorkShowed()) {
message += String.format(Constants.WORK_MESSAGE, p.getFirstName(), message.append(String.format(Constants.WORK_MESSAGE, p.getFirstName(),
getStringById(p.getWork().getTextNameId()), getStringById(p.getWork().getTextNameId()),
getStringById(p.getWork().getTextDescId())) + "\n"; getStringById(p.getWork().getTextDescId())));
message.append("\n");
} }
if(s.getIsHobbyShowed()) { if(s.getIsHobbyShowed()) {
message += String.format(Constants.HOBBY_MESSAGE, p.getFirstName(), message.append(String.format(Constants.HOBBY_MESSAGE, p.getFirstName(),
getStringById(p.getHobby().getTextDescId())) + "\n"; getStringById(p.getHobby().getTextDescId())));
message.append("\n");
} }
message += "\n"; message.append("\n");
} }
sendApi(new SendMessage(groupId, message)); sendApi(new SendMessage(groupId, message.toString()));
} }
private void endVote() { private void endVote() {
Integer max = dayNightFields.getPoll().values().stream().max(Integer::compareTo).get(); Integer max = dayNightFields.getPoll().values().stream().max(Integer::compareTo).orElse(0);
long count = dayNightFields.getPoll().values().stream().filter(p -> p.equals(max)).count(); long count = dayNightFields.getPoll().values().stream().filter(p -> p.equals(max)).count();
SendMessage sendMessage = new SendMessage(groupId, Constants.ENDVOTE); SendMessage sendMessage = new SendMessage(groupId, Constants.ENDVOTE);
if( count > 1 ) { if( count > 1 ) {
@ -529,8 +535,7 @@ public class BunkerBot extends TelegramLongPollingBot {
return; return;
} }
if( !(update.hasMessage() && update.getMessage().hasText() && update.getMessage().isCommand()) ) if( !(update.hasMessage() && update.getMessage().hasText() && update.getMessage().isCommand()) ) return;
return;
String chatId = update.getMessage().getChatId()+""; String chatId = update.getMessage().getChatId()+"";
@ -541,8 +546,7 @@ public class BunkerBot extends TelegramLongPollingBot {
sendApi(new SendMessage(chatId, Constants.GROUP_SET)); sendApi(new SendMessage(chatId, Constants.GROUP_SET));
} }
if( !chatId.equals(groupId) ) if( !chatId.equals(groupId) ) return;
return;
if ( (update.getMessage().getText().equals(Commands.START_GAME + "@" + getBotUsername()) || if ( (update.getMessage().getText().equals(Commands.START_GAME + "@" + getBotUsername()) ||
update.getMessage().getText().equals(Commands.START_GAME) ) && update.getMessage().getText().equals(Commands.START_GAME) ) &&

View File

@ -0,0 +1,30 @@
package com.alterdekim.javabot.components;
import com.alterdekim.javabot.dto.UserDTO;
import com.alterdekim.javabot.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import java.util.UUID;
@Slf4j
@Component
public class StartupListener {
@Autowired
private UserService userService;
private static final String ADMIN_USERNAME = "admin";
@EventListener
public void onApplicationEvent(ContextRefreshedEvent event) {
if( userService.findByUsername(ADMIN_USERNAME) != null ) return;
String pwd = UUID.randomUUID().toString();
log.info("Your admin account password is: {}", pwd);
userService.saveUser(new UserDTO(ADMIN_USERNAME, pwd));
}
}

View File

@ -20,4 +20,9 @@ public class UserDTO {
private String invite_code; private String invite_code;
private String lang; private String lang;
public UserDTO(String username, String password) {
this.username = username;
this.password = password;
}
} }