Changed action cards logic. x2
This commit is contained in:
parent
3592547afe
commit
28ac188218
@ -29,8 +29,6 @@ public class DatabaseController {
|
||||
private final TextDataValService textDataValService;
|
||||
private final DisasterService disasterService;
|
||||
private final SynergyService synergyService;
|
||||
private final ActionScriptsService actionService;
|
||||
private final ActionRequestService actionRequestService;
|
||||
private final GameThemeService themeService;
|
||||
|
||||
private void saveGender(Map<String, String> params) {
|
||||
@ -207,24 +205,6 @@ public class DatabaseController {
|
||||
disasterService.updateDisaster(id, t1.getId(), t2.getId(), themeId);
|
||||
}
|
||||
|
||||
private void saveAction(Map<String, String> params) {
|
||||
String scriptBody = params.get("action_body_text");
|
||||
String name_text = new String(HashUtils.decodeHexString(params.get("action_name_text")));
|
||||
TextDataVal t1 = textDataValService.save(new TextDataVal(name_text));
|
||||
|
||||
String desc_text = new String(HashUtils.decodeHexString(params.get("action_desc_text")));
|
||||
TextDataVal t2 = textDataValService.save(new TextDataVal(desc_text));
|
||||
|
||||
actionService.saveScript(new ActionScript(t1.getId(), t2.getId(), scriptBody));
|
||||
}
|
||||
|
||||
private void saveActionRequest(Map<String, String> params) {
|
||||
String scriptBody = params.get("action_body_text");
|
||||
String name_text = new String(HashUtils.decodeHexString(params.get("action_name_text")));
|
||||
String desc_text = new String(HashUtils.decodeHexString(params.get("action_desc_text")));
|
||||
actionRequestService.saveScript(new ActionScriptRequest(name_text, desc_text, scriptBody));
|
||||
}
|
||||
|
||||
@PostMapping("/api/remove_synergy")
|
||||
public String remove_synergy(@RequestParam Map<String, String> params) {
|
||||
long id = Long.parseLong(params.get("synergy_id"));
|
||||
@ -293,33 +273,12 @@ public class DatabaseController {
|
||||
case "prof" -> saveWork(params);
|
||||
case "heal" -> saveHealth(params);
|
||||
case "hobb" -> saveHobby(params);
|
||||
case "actions" -> saveAction(params);
|
||||
case "themes" -> saveTheme(params);
|
||||
default -> saveDiss(params);
|
||||
}
|
||||
return "ok";
|
||||
}
|
||||
|
||||
@PostMapping("/api/accept_script_request")
|
||||
public String accept_script_request(@RequestParam Map<String, String> params) {
|
||||
long entry_id = Long.parseLong(params.get("entry_id"));
|
||||
ActionScriptRequest req = actionRequestService.getActionScriptById(entry_id);
|
||||
String scriptBody = req.getScriptBody();
|
||||
String name_text = req.getTextName();
|
||||
TextDataVal t1 = textDataValService.save(new TextDataVal(name_text));
|
||||
String desc_text = req.getTextDesc();
|
||||
TextDataVal t2 = textDataValService.save(new TextDataVal(desc_text));
|
||||
actionService.saveScript(new ActionScript(t1.getId(), t2.getId(), scriptBody));
|
||||
actionRequestService.removeById(entry_id);
|
||||
return "ok";
|
||||
}
|
||||
|
||||
@PostMapping("/public/api/add_entry_request")
|
||||
public String add_entry_request(@RequestParam Map<String, String> params) {
|
||||
saveActionRequest(params);
|
||||
return "ok";
|
||||
}
|
||||
|
||||
@PostMapping("/api/remove_entry")
|
||||
public String remove_entry(@RequestParam Map<String, String> params) {
|
||||
String section = params.get("section");
|
||||
@ -331,8 +290,6 @@ public class DatabaseController {
|
||||
case "lugg" -> { luggageService.removeById(entry_id); synergyService.removeByEntityId(entry_id, SectionType.LUGGAGE); }
|
||||
case "heal" -> { healthService.removeById(entry_id); synergyService.removeByEntityId(entry_id, SectionType.HEALTH); }
|
||||
case "prof" -> { workService.removeById(entry_id); synergyService.removeByEntityId(entry_id, SectionType.WORK); }
|
||||
case "actions" -> actionService.removeById(entry_id);
|
||||
case "script_request" -> actionRequestService.removeById(entry_id);
|
||||
case "themes" -> themeService.removeById(entry_id);
|
||||
default -> disasterService.removeById(entry_id);
|
||||
}
|
||||
@ -355,7 +312,6 @@ public class DatabaseController {
|
||||
case "prof" -> mapper.writeValueAsString(workService.getAllWorks());
|
||||
case "heal" -> mapper.writeValueAsString(healthService.getAllHealth());
|
||||
case "lugg" -> mapper.writeValueAsString(luggageService.getAllLuggages());
|
||||
case "actions" -> mapper.writeValueAsString(actionService.getAllActionScripts());
|
||||
case "themes" -> mapper.writeValueAsString(themeService.getAllGameThemes());
|
||||
default -> mapper.writeValueAsString(disasterService.getAllDisasters());
|
||||
};
|
||||
@ -376,7 +332,6 @@ public class DatabaseController {
|
||||
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));
|
||||
case "themes" -> mapper.writeValueAsString(themeService.getThemeById(l));
|
||||
default -> mapper.writeValueAsString(disasterService.getDisasterById(l));
|
||||
};
|
||||
|
@ -30,7 +30,6 @@ public class PanelController {
|
||||
private final WorkService workService;
|
||||
private final TextDataValService textDataValService;
|
||||
private final DisasterService disasterService;
|
||||
private final ActionScriptsServiceImpl scriptsService;
|
||||
private final ActionRequestServiceImpl actionRequestService;
|
||||
private final GameThemeServiceImpl gameThemeService;
|
||||
|
||||
@ -136,36 +135,6 @@ public class PanelController {
|
||||
return cards;
|
||||
}
|
||||
|
||||
private List<Card> actionsToCards() {
|
||||
List<ActionScript> scripts = scriptsService.getAllActionScripts();
|
||||
List<Card> cards = new ArrayList<>();
|
||||
for( ActionScript b : scripts ) {
|
||||
Card card = new Card();
|
||||
card.setId(b.getId());
|
||||
card.setTitle(textDataValService.getTextDataValById(b.getTextNameId()).getText());
|
||||
card.setBody(Arrays.asList("Script body hidden."));
|
||||
cards.add(card);
|
||||
}
|
||||
cards.sort(Comparator.comparing(Card::getId));
|
||||
Collections.reverse(cards);
|
||||
return cards;
|
||||
}
|
||||
|
||||
private List<Card> requestsToCards() {
|
||||
List<ActionScriptRequest> scriptRequests = actionRequestService.getAllActionScripts();
|
||||
List<Card> cards = new ArrayList<>();
|
||||
for( ActionScriptRequest b : scriptRequests ) {
|
||||
Card card = new Card();
|
||||
card.setId(b.getId());
|
||||
card.setTitle(b.getTextName());
|
||||
card.setBody(new ArrayList<>(Arrays.asList(b.getScriptBody().split("\n"))));
|
||||
cards.add(card);
|
||||
}
|
||||
cards.sort(Comparator.comparing(Card::getId));
|
||||
Collections.reverse(cards);
|
||||
return cards;
|
||||
}
|
||||
|
||||
private List<Card> themesToCards() {
|
||||
List<GameTheme> themeList = gameThemeService.getAllGameThemes();
|
||||
List<Card> cards = new ArrayList<>();
|
||||
@ -218,12 +187,6 @@ public class PanelController {
|
||||
case "stats":
|
||||
// !
|
||||
break;
|
||||
case "actions":
|
||||
model.addAttribute("cards", is_mobile ? actionsToCards() : toPairs(actionsToCards()) );
|
||||
break;
|
||||
case "script_request":
|
||||
model.addAttribute("cards", is_mobile ? requestsToCards() : toPairs(requestsToCards()) );
|
||||
break;
|
||||
case "themes":
|
||||
model.addAttribute("cards", is_mobile ? themesToCards() : toPairs(themesToCards()));
|
||||
break;
|
||||
|
@ -1,150 +0,0 @@
|
||||
package com.alterdekim.javabot.util;
|
||||
|
||||
import com.alterdekim.javabot.bot.InfoSections;
|
||||
import com.alterdekim.javabot.bot.Player;
|
||||
import com.alterdekim.javabot.entities.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
public class LuaDeserializer {
|
||||
public static List<Player> deserializePlayers(LuaValue val) {
|
||||
LuaTable table = val.checktable();
|
||||
return Arrays.stream(table.keys())
|
||||
.map(key -> deserializePlayer(table.get(key).checktable()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static <T> T deserialize(LuaTable table, Class<T> obj) {
|
||||
try {
|
||||
List<String> keys = Arrays.stream(table.keys())
|
||||
.map(LuaValue::checkjstring)
|
||||
.collect(Collectors.toList());
|
||||
T result = obj.getDeclaredConstructor().newInstance();
|
||||
Arrays.stream(obj.getDeclaredFields())
|
||||
.filter(f -> keys.contains(f.getName()))
|
||||
.forEach(f -> {
|
||||
try {
|
||||
f.setAccessible(true);
|
||||
f.set(result, checkObject(f, table.get(f.getName())));
|
||||
} catch (IllegalAccessException e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
});
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Object checkObject(Field f, LuaValue val) {
|
||||
return switch(f.getType().getCanonicalName()) {
|
||||
case "java.lang.Boolean" -> Boolean.valueOf(val.checkboolean());
|
||||
case "java.lang.Long" -> Long.valueOf(val.checklong());
|
||||
case "java.lang.Float" -> Float.valueOf((float) val.checkdouble());
|
||||
case "java.lang.Double" -> Double.valueOf(val.checkdouble());
|
||||
case "java.lang.Integer" -> Integer.valueOf(val.checkint());
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
private static Player deserializePlayer(LuaTable table) {
|
||||
int age = table.get("age").checkint();
|
||||
Player p = new Player(table.get("telegramId").checklong(), table.get("firstName").checkjstring());
|
||||
p.setAge(age);
|
||||
p.setIsAnswered(table.get("isAnswered").checkboolean());
|
||||
p.setIsVoted(table.get("isVoted").checkboolean());
|
||||
p.setScriptMessageId(table.get("scriptMessageId").checkint());
|
||||
|
||||
p.setGender(deserialize(table.get("gender").checktable(), Bio.class));
|
||||
p.setHealth(deserialize(table.get("health").checktable(), Health.class));
|
||||
p.setHobby(deserialize(table.get("hobby").checktable(), Hobby.class));
|
||||
p.setWork(deserialize(table.get("work").checktable(), Work.class));
|
||||
p.setLuggage(deserialize(table.get("luggage").checktable(), Luggage.class));
|
||||
p.setInfoSections(deserialize(table.get("infoSections").checktable(), InfoSections.class));
|
||||
|
||||
/*p.setGender(deserializeGender(table.get("gender").checktable()));
|
||||
p.setHealth(deserializeHealth(table.get("health").checktable()));
|
||||
p.setHobby(deserializeHobby(table.get("hobby").checktable()));
|
||||
p.setWork(deserializeWork(table.get("work").checktable()));
|
||||
p.setLuggage(deserializeLuggage(table.get("luggage").checktable()));
|
||||
|
||||
p.setInfoSections(deserializeInfoSections(table.get("infoSections").checktable()));*/
|
||||
return p;
|
||||
}
|
||||
|
||||
/*
|
||||
private static InfoSections deserializeInfoSections(LuaTable table) {
|
||||
InfoSections infoSections = new InfoSections();
|
||||
infoSections.setIsGenderShowed(table.get("isGenderShowed").checkboolean());
|
||||
infoSections.setIsAgeShowed(table.get("isAgeShowed").checkboolean());
|
||||
infoSections.setIsWorkShowed(table.get("isWorkShowed").checkboolean());
|
||||
infoSections.setIsLuggageShowed(table.get("isLuggageShowed").checkboolean());
|
||||
infoSections.setIsHobbyShowed(table.get("isHobbyShowed").checkboolean());
|
||||
infoSections.setIsHealthShowed(table.get("isHealthShowed").checkboolean());
|
||||
return infoSections;
|
||||
}
|
||||
|
||||
private static Hobby deserializeHobby(LuaTable table) {
|
||||
Hobby hobby = new Hobby();
|
||||
hobby.setId(table.get("id").checklong());
|
||||
hobby.setTextDescId(table.get("textDescId").checklong());
|
||||
hobby.setAsocial(table.get("asocial").tofloat());
|
||||
hobby.setPower(table.get("power").tofloat());
|
||||
hobby.setViolence(table.get("violence").tofloat());
|
||||
hobby.setFoodstuffs(table.get("foodstuffs").tofloat());
|
||||
return hobby;
|
||||
}
|
||||
|
||||
private static Work deserializeWork(LuaTable table) {
|
||||
Work work = new Work();
|
||||
work.setId(table.get("id").checklong());
|
||||
work.setTextDescId(table.get("textDescId").checklong());
|
||||
work.setTextNameId(table.get("textNameId").checklong());
|
||||
work.setAsocial(table.get("asocial").tofloat());
|
||||
work.setPower(table.get("power").tofloat());
|
||||
work.setViolence(table.get("violence").tofloat());
|
||||
work.setFoodstuffs(table.get("foodstuffs").tofloat());
|
||||
return work;
|
||||
}
|
||||
|
||||
private static Luggage deserializeLuggage(LuaTable table) {
|
||||
Luggage luggage = new Luggage();
|
||||
luggage.setId(table.get("id").checklong());
|
||||
luggage.setTextDescId(table.get("textDescId").checklong());
|
||||
luggage.setTextNameId(table.get("textNameId").checklong());
|
||||
luggage.setAsocial(table.get("asocial").tofloat());
|
||||
luggage.setPower(table.get("power").tofloat());
|
||||
luggage.setViolence(table.get("violence").tofloat());
|
||||
luggage.setFoodstuffs(table.get("foodstuffs").tofloat());
|
||||
luggage.setGarbage(table.get("garbage").checkboolean());
|
||||
return luggage;
|
||||
}
|
||||
|
||||
private static Health deserializeHealth(LuaTable table) {
|
||||
Health health = new Health();
|
||||
health.setId(table.get("id").checklong());
|
||||
health.setHealth_index(table.get("health_index").tofloat());
|
||||
health.setIsChildfree(table.get("isChildfree").checkboolean());
|
||||
health.setTextDescId(table.get("textDescId").checklong());
|
||||
health.setTextNameId(table.get("textNameId").checklong());
|
||||
return health;
|
||||
}
|
||||
|
||||
private static Bio deserializeGender(LuaTable table) {
|
||||
Bio bio = new Bio();
|
||||
bio.setId(table.get("id").checklong());
|
||||
bio.setGenderTextId(table.get("genderTextId").checklong());
|
||||
bio.setCanDie(table.get("canDie").checkboolean());
|
||||
bio.setIsMale(table.get("isMale").checkboolean());
|
||||
bio.setIsFemale(table.get("isFemale").checkboolean());
|
||||
return bio;
|
||||
} */
|
||||
}
|
@ -1,90 +0,0 @@
|
||||
package com.alterdekim.javabot.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
@Slf4j
|
||||
public class LuaSerializer {
|
||||
|
||||
public static LuaTable serializeObjectList(List<?> list) {
|
||||
LuaTable table = new LuaTable();
|
||||
IntStream.range(0, list.size()).forEach(i -> table.set(i, serializeObject(list.get(i))));
|
||||
return table;
|
||||
}
|
||||
|
||||
public static LuaValue serializeObject(Object o) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
getPrivateFields(o.getClass())
|
||||
.forEach(f -> {
|
||||
try {
|
||||
f.setAccessible(true);
|
||||
String type_name = ((Class) f.getType()).getName();
|
||||
String name = f.getName();
|
||||
switch (type_name) {
|
||||
case "java.lang.Long", "java.lang.Integer",
|
||||
"java.lang.Float", "java.lang.Double",
|
||||
"java.lang.Boolean", "java.lang.String":
|
||||
map.put(name, f.get(o));
|
||||
break;
|
||||
case "long":
|
||||
map.put(name, f.getLong(o));
|
||||
break;
|
||||
case "int":
|
||||
map.put(name, f.getInt(o));
|
||||
break;
|
||||
case "float":
|
||||
map.put(name, f.getFloat(o));
|
||||
break;
|
||||
case "double":
|
||||
map.put(name, f.getDouble(o));
|
||||
break;
|
||||
case "boolean":
|
||||
map.put(name, f.getBoolean(o));
|
||||
break;
|
||||
case "java.util.List":
|
||||
break;
|
||||
default:
|
||||
map.put(name, serializeObject(f.get(o)));
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
//log.error(e.getMessage(), e);
|
||||
}
|
||||
});
|
||||
return convert(map);
|
||||
}
|
||||
|
||||
private static LuaValue convert(Map<String, Object> map) {
|
||||
LuaTable luaTable = new LuaTable();
|
||||
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
Object value = entry.getValue();
|
||||
|
||||
// Convert Java object to LuaValue (handle different data types)
|
||||
LuaValue luaValue = CoerceJavaToLua.coerce(value);
|
||||
luaTable.set(key, luaValue);
|
||||
}
|
||||
return luaTable;
|
||||
}
|
||||
|
||||
private static List<Field> getPrivateFields(Class<?> theClass) {
|
||||
List<Field> privateFields = new ArrayList<Field>();
|
||||
Field[] fields = theClass.getDeclaredFields();
|
||||
for(Field field : fields){
|
||||
if(Modifier.isPrivate(field.getModifiers())) {
|
||||
privateFields.add(field);
|
||||
}
|
||||
}
|
||||
return privateFields;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user