Refactoring stuff x2

This commit is contained in:
Michael Wain 2024-07-04 20:27:12 +03:00
parent d442b1e159
commit cc55257cd4
10 changed files with 162 additions and 246 deletions

View File

@ -1,31 +0,0 @@
package com.alterdekim.javabot.bot;
import com.alterdekim.javabot.Constants;
import com.alterdekim.javabot.util.HashUtils;
public enum BioButtons {
HOBBY(Constants.HOBBY_BTN),
WORK(Constants.WORK_BTN),
HEALTH(Constants.HEALTH_BTN),
AGE(Constants.AGE_BTN),
GENDER(Constants.GENDER_BTN),
LUGGAGE(Constants.LUGG_BTN),
UNKNOWN("");
private final String name;
BioButtons(String name) {
this.name = name;
}
public String getHash() {
return HashUtils.getCRC32(this.name.getBytes());
}
public static BioButtons fromHash( String hash ) {
for( BioButtons b : values() ) {
if( b.getHash().equals(hash) ) return b;
}
return UNKNOWN;
}
}

View File

@ -0,0 +1,12 @@
package com.alterdekim.javabot.bot;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
@ToString
@Getter
@AllArgsConstructor
public class InfoSection {
private SectionType type;
}

View File

@ -1,26 +1,30 @@
package com.alterdekim.javabot.bot;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.ArrayList;
import java.util.List;
@ToString
@Getter
@Setter
public class InfoSections {
private Boolean isGenderShowed;
private Boolean isAgeShowed;
private Boolean isWorkShowed;
private Boolean isLuggageShowed;
private Boolean isHobbyShowed;
private Boolean isHealthShowed;
private final List<InfoSection> sections;
public InfoSections() {
this.isAgeShowed = false;
this.isLuggageShowed = false;
this.isGenderShowed = false;
this.isWorkShowed = false;
this.isHealthShowed = false;
this.isHobbyShowed = false;
this.sections = new ArrayList<>();
}
public boolean isShowed(SectionType type) {
return this.sections.stream().anyMatch(p -> p.getType() == type);
}
public void pushShowedState(SectionType type) {
if(this.isShowed(type)) return;
this.sections.add(new InfoSection(type));
}
public void clear() {
this.sections.clear();
}
}

View File

@ -1,6 +1,5 @@
package com.alterdekim.javabot.bot;
import com.alterdekim.javabot.entities.ColumnType;
import com.alterdekim.javabot.entities.Synergy;
import com.alterdekim.javabot.util.Clamp;
@ -37,26 +36,15 @@ public class LiveFormula {
return Clamp.clamp(i * 1.2d, 0, 1);
}
private static Boolean entity(List<Player> players, ColumnType ct, Long eid) {
Boolean fb = false;
switch (ct) {
case Bio:
fb = LiveFormula.searchForBio(players, eid);
break;
case Work:
fb = LiveFormula.searchForWork(players, eid);
break;
case Hobby:
fb = LiveFormula.searchForHobby(players, eid);
break;
case Health:
fb = LiveFormula.searchForHealth(players, eid);
break;
case Luggage:
fb = LiveFormula.searchForLuggage(players, eid);
break;
}
return fb;
private static Boolean entity(List<Player> players, SectionType ct, Long eid) {
return switch (ct) {
case GENDER -> LiveFormula.searchForBio(players, eid);
case WORK -> LiveFormula.searchForWork(players, eid);
case HOBBY -> LiveFormula.searchForHobby(players, eid);
case HEALTH -> LiveFormula.searchForHealth(players, eid);
case LUGGAGE -> LiveFormula.searchForLuggage(players, eid);
default -> false;
};
}
private static Boolean searchForBio(List<Player> players, Long id) {

View File

@ -0,0 +1,33 @@
package com.alterdekim.javabot.bot;
import com.alterdekim.javabot.Constants;
import com.alterdekim.javabot.util.HashUtils;
public enum SectionType {
GENDER(Constants.GENDER_BTN, true), // Also called "bio" in some cases
HEALTH(Constants.HEALTH_BTN, true),
HOBBY(Constants.HOBBY_BTN, true),
LUGGAGE(Constants.LUGG_BTN, true),
WORK(Constants.WORK_BTN, true),
AGE(Constants.AGE_BTN, false),
UNKNOWN("", false);
private final String name;
private final Boolean dbAppearance;
SectionType(String name, Boolean dbAppearance) {
this.name = name;
this.dbAppearance = dbAppearance;
}
public String getHash() {
return HashUtils.getCRC32(this.name.getBytes());
}
public static SectionType fromHash( String hash ) {
for( SectionType b : values() ) {
if( b.getHash().equals(hash) ) return b;
}
return UNKNOWN;
}
}

View File

@ -235,11 +235,10 @@ public class BunkerBot extends TelegramLongPollingBot {
}
private void setScriptMessageId(Player p, Integer messageId) {
IntStream.range(0, players.size()).forEach(i -> {
if( players.get(i).getTelegramId().longValue() == p.getTelegramId().longValue() ) {
players.get(i).setScriptMessageId(messageId);
}
});
IntStream.range(0, players.size())
.boxed()
.filter(i -> players.get(i).getTelegramId().longValue() == p.getTelegramId().longValue())
.forEach(i -> players.get(i).setScriptMessageId(messageId));
}
private String getStringById(Long id) {
@ -281,46 +280,42 @@ public class BunkerBot extends TelegramLongPollingBot {
!getPlayerById(callbackQuery.getFrom().getId()).getIsAnswered() ) {
Player p = getPlayerById(callbackQuery.getFrom().getId());
InfoSections ins = p.getInfoSections();
switch(BioButtons.fromHash(callbackQuery.getData())) {
SectionType curSection = SectionType.fromHash(callbackQuery.getData());
switch(curSection) {
case GENDER:
dayNightFields.appendMessage(String.format(Constants.GENDER_MESAGE, callbackQuery.getFrom().getFirstName(), getStringById(p.getGender().getGenderTextId()),
p.getGender().getCanDie() ? Constants.TRUE : Constants.FALSE,
p.getGender().getIsMale() ? Constants.TRUE : Constants.FALSE,
p.getGender().getIsFemale() ? Constants.TRUE : Constants.FALSE) + "\n");
ins.setIsGenderShowed(true);
break;
case HEALTH:
dayNightFields.appendMessage(String.format(Constants.HEALTH_MESSAGE, callbackQuery.getFrom().getFirstName(), getStringById(p.getHealth().getTextNameId()),
getStringById(p.getHealth().getTextDescId()),
(int) (p.getHealth().getHealth_index()*100f),
p.getHealth().getIsChildfree() ? Constants.TRUE : Constants.FALSE) + "\n");
ins.setIsHealthShowed(true);
break;
case AGE:
dayNightFields.appendMessage(String.format(Constants.AGE_MESSAGE, callbackQuery.getFrom().getFirstName(), p.getAge()) + "\n");
ins.setIsAgeShowed(true);
break;
case HOBBY:
dayNightFields.appendMessage(String.format(Constants.HOBBY_MESSAGE, callbackQuery.getFrom().getFirstName(),
getStringById(p.getHobby().getTextDescId())) + "\n");
ins.setIsHobbyShowed(true);
break;
case LUGGAGE:
dayNightFields.appendMessage(String.format(Constants.LUGG_MESSAGE, callbackQuery.getFrom().getFirstName(),
getStringById(p.getLuggage().getTextNameId()),
getStringById(p.getLuggage().getTextDescId())) + "\n");
ins.setIsLuggageShowed(true);
break;
case WORK:
dayNightFields.appendMessage(String.format(Constants.WORK_MESSAGE, callbackQuery.getFrom().getFirstName(),
getStringById(p.getWork().getTextNameId()),
getStringById(p.getWork().getTextDescId())) + "\n");
ins.setIsWorkShowed(true);
break;
default:
processNightScriptButton(p, callbackQuery, callbackQuery.getData());
return;
}
ins.pushShowedState(curSection);
setIsAnswered(callbackQuery.getFrom().getId());
updateInfoSections(p, ins);
sendApi(new SendMessage(callbackQuery.getMessage().getChatId()+"", Constants.THANK_YOU));
@ -404,41 +399,28 @@ public class BunkerBot extends TelegramLongPollingBot {
message.append(p.getFirstName());
message.append(":\n");
InfoSections s = p.getInfoSections();
if(s.getIsGenderShowed()) {
message.append(String.format(Constants.GENDER_MESAGE, p.getFirstName(), getStringById(p.getGender().getGenderTextId()),
p.getGender().getCanDie() ? Constants.TRUE : Constants.FALSE,
p.getGender().getIsMale() ? Constants.TRUE : Constants.FALSE,
p.getGender().getIsFemale() ? Constants.TRUE : Constants.FALSE));
s.getSections().forEach(s1 -> {
switch (s1.getType()) {
case GENDER -> message.append(String.format(Constants.GENDER_MESAGE, p.getFirstName(), getStringById(p.getGender().getGenderTextId()),
p.getGender().getCanDie() ? Constants.TRUE : Constants.FALSE,
p.getGender().getIsMale() ? Constants.TRUE : Constants.FALSE,
p.getGender().getIsFemale() ? Constants.TRUE : Constants.FALSE));
case AGE -> message.append(String.format(Constants.AGE_MESSAGE, p.getFirstName(), p.getAge()));
case LUGGAGE -> message.append(String.format(Constants.LUGG_MESSAGE, p.getFirstName(),
getStringById(p.getLuggage().getTextNameId()),
getStringById(p.getLuggage().getTextDescId())));
case HEALTH -> message.append(String.format(Constants.HEALTH_MESSAGE, p.getFirstName(), getStringById(p.getHealth().getTextNameId()),
getStringById(p.getHealth().getTextDescId()),
(int) (p.getHealth().getHealth_index()*100f),
p.getHealth().getIsChildfree() ? Constants.TRUE : Constants.FALSE));
case WORK -> message.append(String.format(Constants.WORK_MESSAGE, p.getFirstName(),
getStringById(p.getWork().getTextNameId()),
getStringById(p.getWork().getTextDescId())));
case HOBBY -> message.append(String.format(Constants.HOBBY_MESSAGE, p.getFirstName(),
getStringById(p.getHobby().getTextDescId())));
}
message.append("\n");
}
if(s.getIsAgeShowed()) {
message.append(String.format(Constants.AGE_MESSAGE, p.getFirstName(), p.getAge()));
message.append("\n");
}
if(s.getIsLuggageShowed()) {
message.append(String.format(Constants.LUGG_MESSAGE, p.getFirstName(),
getStringById(p.getLuggage().getTextNameId()),
getStringById(p.getLuggage().getTextDescId())));
message.append("\n");
}
if(s.getIsHealthShowed()) {
message.append(String.format(Constants.HEALTH_MESSAGE, p.getFirstName(), getStringById(p.getHealth().getTextNameId()),
getStringById(p.getHealth().getTextDescId()),
(int) (p.getHealth().getHealth_index()*100f),
p.getHealth().getIsChildfree() ? Constants.TRUE : Constants.FALSE));
message.append("\n");
}
if(s.getIsWorkShowed()) {
message.append(String.format(Constants.WORK_MESSAGE, p.getFirstName(),
getStringById(p.getWork().getTextNameId()),
getStringById(p.getWork().getTextDescId())));
message.append("\n");
}
if(s.getIsHobbyShowed()) {
message.append(String.format(Constants.HOBBY_MESSAGE, p.getFirstName(),
getStringById(p.getHobby().getTextDescId())));
message.append("\n");
}
});
message.append("\n");
}
sendApi(new SendMessage(groupId, message.toString()));
@ -539,6 +521,7 @@ public class BunkerBot extends TelegramLongPollingBot {
String chatId = update.getMessage().getChatId()+"";
// TODO: state-based refactoring (reduce IF count)
if( ( update.getMessage().getText().equals(Commands.SET_GROUP + "@" + getBotUsername()) ||
update.getMessage().getText().equals(Commands.SET_GROUP)) &&
update.getMessage().getFrom().getId().equals(getMasterId()) && gameState == GameState.NONE) {
@ -548,6 +531,7 @@ public class BunkerBot extends TelegramLongPollingBot {
if( !chatId.equals(groupId) ) return;
// TODO: state-based refactoring (reduce IF count)
if ( (update.getMessage().getText().equals(Commands.START_GAME + "@" + getBotUsername()) ||
update.getMessage().getText().equals(Commands.START_GAME) ) &&
gameState == GameState.NONE) {

View File

@ -1,5 +1,6 @@
package com.alterdekim.javabot.controller;
import com.alterdekim.javabot.bot.SectionType;
import com.alterdekim.javabot.dto.SynergyResult;
import com.alterdekim.javabot.entities.*;
import com.alterdekim.javabot.service.*;
@ -115,7 +116,7 @@ public class DatabaseController {
@PostMapping("/api/remove_synergy")
public String remove_synergy(@RequestParam Map<String, String> params) {
Long id = Long.parseLong(params.get("synergy_id"));
long id = Long.parseLong(params.get("synergy_id"));
synergyService.removeById(id);
return "ok";
}
@ -123,9 +124,9 @@ public class DatabaseController {
@PostMapping("/api/add_synergy")
public String add_synergy(@RequestParam Map<String, String> params) {
Long feid = Long.parseLong(params.get("first_entity_id"));
ColumnType fetype = ColumnType.values()[Integer.parseInt(params.get("first_entity_type"))];
SectionType fetype = SectionType.values()[Integer.parseInt(params.get("first_entity_type"))];
Long seid = Long.parseLong(params.get("second_entity_id"));
ColumnType setype = ColumnType.values()[Integer.parseInt(params.get("second_entity_type"))];
SectionType setype = SectionType.values()[Integer.parseInt(params.get("second_entity_type"))];
Float probability = Float.parseFloat(params.get("probability"));
synergyService.saveSynergy(new Synergy(feid, fetype, seid, setype, probability));
@ -139,24 +140,14 @@ public class DatabaseController {
String section = params.get("entity_type");
ObjectMapper mapper = new ObjectMapper();
try {
List<Synergy> synergyList = new ArrayList<>();
switch (section) {
case "agge":
synergyList = bioService.getSynergies(id);
break;
case "lugg":
synergyList = luggageService.getSynergies(id);
break;
case "prof":
synergyList = workService.getSynergies(id);
break;
case "heal":
synergyList = healthService.getSynergies(id);
break;
case "hobb":
synergyList = hobbyService.getSynergies(id);
break;
}
List<Synergy> synergyList = switch (section) {
case "agge" -> bioService.getSynergies(id);
case "lugg" -> luggageService.getSynergies(id);
case "prof" -> workService.getSynergies(id);
case "heal" -> healthService.getSynergies(id);
case "hobb" -> hobbyService.getSynergies(id);
default -> new ArrayList<>();
};
List<SynergyResult> results = new ArrayList<>();
for( Synergy s : synergyList ) {
String textFirst = getText(s.getFirstType(), s.getFirstEntityId());
@ -170,20 +161,15 @@ public class DatabaseController {
return "ok";
}
private String getText(ColumnType type, Long feid) {
switch (type) {
case Bio:
return textDataValService.getTextDataValById(bioService.getBioById(feid).getGenderTextId()).getText();
case Health:
return textDataValService.getTextDataValById(healthService.getHealthById(feid).getTextNameId()).getText();
case Hobby:
return textDataValService.getTextDataValById(hobbyService.getHobbyById(feid).getTextDescId()).getText();
case Luggage:
return textDataValService.getTextDataValById(luggageService.getLuggageById(feid).getTextNameId()).getText();
case Work:
return textDataValService.getTextDataValById(workService.getWorkById(feid).getTextNameId()).getText();
}
return "-";
private String getText(SectionType type, Long feid) {
return switch (type) {
case GENDER -> textDataValService.getTextDataValById(bioService.getBioById(feid).getGenderTextId()).getText();
case HEALTH -> textDataValService.getTextDataValById(healthService.getHealthById(feid).getTextNameId()).getText();
case HOBBY -> textDataValService.getTextDataValById(hobbyService.getHobbyById(feid).getTextDescId()).getText();
case LUGGAGE -> textDataValService.getTextDataValById(luggageService.getLuggageById(feid).getTextNameId()).getText();
case WORK -> textDataValService.getTextDataValById(workService.getWorkById(feid).getTextNameId()).getText();
default -> "";
};
}
@PostMapping("/api/add_entry")
@ -191,30 +177,13 @@ public class DatabaseController {
/* additional data, disasters */
String section = params.get("section");
switch (section) {
case "agge":
saveGender(params);
break;
case "lugg":
saveLuggage(params);
break;
case "prof":
saveWork(params);
break;
case "heal":
saveHealth(params);
break;
case "hobb":
saveHobby(params);
break;
case "diss":
saveDiss(params);
break;
case "actions":
saveAction(params);
break;
default:
saveDiss(params);
break;
case "agge" -> saveGender(params);
case "lugg" -> saveLuggage(params);
case "prof" -> saveWork(params);
case "heal" -> saveHealth(params);
case "hobb" -> saveHobby(params);
case "actions" -> saveAction(params);
default -> saveDiss(params);
}
return "ok";
}
@ -224,30 +193,13 @@ public class DatabaseController {
String section = params.get("section");
long entry_id = Long.parseLong(params.get("entry_id"));
switch (section) {
case "agge":
bioService.removeById(entry_id);
break;
case "hobb":
hobbyService.removeById(entry_id);
break;
case "lugg":
luggageService.removeById(entry_id);
break;
case "heal":
healthService.removeById(entry_id);
break;
case "prof":
workService.removeById(entry_id);
break;
case "diss":
disasterService.removeById(entry_id);
break;
case "actions":
actionService.removeById(entry_id);
break;
default:
disasterService.removeById(entry_id);
break;
case "agge" -> bioService.removeById(entry_id);
case "hobb" -> hobbyService.removeById(entry_id);
case "lugg" -> luggageService.removeById(entry_id);
case "heal" -> healthService.removeById(entry_id);
case "prof" -> workService.removeById(entry_id);
case "actions" -> actionService.removeById(entry_id);
default -> disasterService.removeById(entry_id);
}
return "ok";
}
@ -262,24 +214,15 @@ public class DatabaseController {
public String getEntries(@RequestParam Map<String, String> params) {
ObjectMapper mapper = new ObjectMapper();
try {
switch (params.get("section")) {
case "agge":
return mapper.writeValueAsString(bioService.getAllBios());
case "hobb":
return mapper.writeValueAsString(hobbyService.getAllHobbies());
case "prof":
return mapper.writeValueAsString(workService.getAllWorks());
case "heal":
return mapper.writeValueAsString(healthService.getAllHealth());
case "lugg":
return mapper.writeValueAsString(luggageService.getAllLuggages());
case "diss":
return mapper.writeValueAsString(disasterService.getAllDisasters());
case "actions":
return mapper.writeValueAsString(actionService.getAllActionScripts());
default:
return mapper.writeValueAsString(disasterService.getAllDisasters());
}
return switch (params.get("section")) {
case "agge" -> mapper.writeValueAsString(bioService.getAllBios());
case "hobb" -> mapper.writeValueAsString(hobbyService.getAllHobbies());
case "prof" -> mapper.writeValueAsString(workService.getAllWorks());
case "heal" -> mapper.writeValueAsString(healthService.getAllHealth());
case "lugg" -> mapper.writeValueAsString(luggageService.getAllLuggages());
case "actions" -> mapper.writeValueAsString(actionService.getAllActionScripts());
default -> mapper.writeValueAsString(disasterService.getAllDisasters());
};
} catch (JacksonException e) {
log.error(e.getMessage());
}
@ -291,24 +234,15 @@ public class DatabaseController {
ObjectMapper mapper = new ObjectMapper();
long l = Long.parseLong(params.get("entry_id"));
try {
switch (params.get("section")) {
case "agge":
return mapper.writeValueAsString(bioService.getBioById(l));
case "hobb":
return mapper.writeValueAsString(hobbyService.getHobbyById(l));
case "prof":
return mapper.writeValueAsString(workService.getWorkById(l));
case "heal":
return mapper.writeValueAsString(healthService.getHealthById(l));
case "lugg":
return mapper.writeValueAsString(luggageService.getLuggageById(l));
case "diss":
return mapper.writeValueAsString(disasterService.getDisasterById(l));
case "actions":
return mapper.writeValueAsString(actionService.getActionScriptById(l));
default:
return mapper.writeValueAsString(disasterService.getDisasterById(l));
}
return switch (params.get("section")) {
case "agge" -> mapper.writeValueAsString(bioService.getBioById(l));
case "hobb" -> mapper.writeValueAsString(hobbyService.getHobbyById(l));
case "prof" -> mapper.writeValueAsString(workService.getWorkById(l));
case "heal" -> mapper.writeValueAsString(healthService.getHealthById(l));
case "lugg" -> mapper.writeValueAsString(luggageService.getLuggageById(l));
case "actions" -> mapper.writeValueAsString(actionService.getActionScriptById(l));
default -> mapper.writeValueAsString(disasterService.getDisasterById(l));
};
} catch (JacksonException e) {
log.error(e.getMessage());
}

View File

@ -1,6 +1,6 @@
package com.alterdekim.javabot.dto;
import com.alterdekim.javabot.entities.ColumnType;
import com.alterdekim.javabot.bot.SectionType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -12,7 +12,7 @@ public class SynergyResult {
private Long id;
private String firstEntityText;
private String secondEntityText;
private ColumnType firstType;
private ColumnType secondType;
private SectionType firstType;
private SectionType secondType;
private Float probabilityValue;
}

View File

@ -1,9 +0,0 @@
package com.alterdekim.javabot.entities;
public enum ColumnType {
Bio,
Health,
Hobby,
Luggage,
Work
}

View File

@ -1,5 +1,6 @@
package com.alterdekim.javabot.entities;
import com.alterdekim.javabot.bot.SectionType;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
@ -21,18 +22,18 @@ public class Synergy {
private Long firstEntityId;
@Enumerated(EnumType.ORDINAL)
private ColumnType firstType;
private SectionType firstType;
@Column(nullable = false)
private Long secondEntityId;
@Enumerated(EnumType.ORDINAL)
private ColumnType secondType;
private SectionType secondType;
@Column(nullable = false)
private Float probabilityValue;
public Synergy(Long firstEntityId, ColumnType firstType, Long secondEntityId, ColumnType secondType, Float probabilityValue) {
public Synergy(Long firstEntityId, SectionType firstType, Long secondEntityId, SectionType secondType, Float probabilityValue) {
this.firstEntityId = firstEntityId;
this.firstType = firstType;
this.secondEntityId = secondEntityId;