LuaDeserializer

This commit is contained in:
Michael Wain 2024-03-13 19:07:37 +03:00
parent 3bebf92599
commit efd2acf642
3 changed files with 126 additions and 4 deletions

View File

@ -6,10 +6,7 @@ import com.alterdekim.javabot.Constants;
import com.alterdekim.javabot.TelegramConfig; import com.alterdekim.javabot.TelegramConfig;
import com.alterdekim.javabot.entities.*; import com.alterdekim.javabot.entities.*;
import com.alterdekim.javabot.service.*; import com.alterdekim.javabot.service.*;
import com.alterdekim.javabot.util.BotUtils; import com.alterdekim.javabot.util.*;
import com.alterdekim.javabot.util.GameState;
import com.alterdekim.javabot.util.HashUtils;
import com.alterdekim.javabot.util.LuaSerializer;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.luaj.vm2.Globals; import org.luaj.vm2.Globals;
@ -261,6 +258,7 @@ public class BunkerBot extends TelegramLongPollingBot {
globals.set("player", LuaSerializer.serializeObject(p)); globals.set("player", LuaSerializer.serializeObject(p));
LuaValue chunk = globals.load(script.getScriptBody()); LuaValue chunk = globals.load(script.getScriptBody());
chunk.call(); chunk.call();
players = LuaDeserializer.deserializePlayers(globals.get("players"));
} }
private void processNightButton(CallbackQuery callbackQuery) { private void processNightButton(CallbackQuery callbackQuery) {

View File

@ -0,0 +1,110 @@
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 org.springframework.security.core.parameters.P;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@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 Player deserializePlayer(LuaTable table) {
int age = table.get("age").checkint();
Player p = new Player(table.get("telegramId").checklong(), table.get("name").checkjstring());
p.setAge(age);
p.setIsAnswered(table.get("isAnswered").checkboolean());
p.setFirstName(table.get("firstName").checkjstring());
p.setIsVoted(table.get("isVoted").checkboolean());
p.setScriptMessageId(table.get("scriptMessageId").checkint());
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;
}
}

View File

@ -44,6 +44,18 @@ public class LuaSerializer {
case "java.lang.Integer": case "java.lang.Integer":
map.put(name, ((Integer) f.get(o)).intValue()); map.put(name, ((Integer) f.get(o)).intValue());
break; break;
case "float":
map.put(name, f.getFloat(o));
break;
case "java.lang.Float":
map.put(name, ((Float) f.get(o)).floatValue());
break;
case "double":
map.put(name, f.getDouble(o));
break;
case "java.lang.Double":
map.put(name, ((Double) f.get(o)).doubleValue());
break;
case "boolean": case "boolean":
map.put(name, f.getBoolean(o)); map.put(name, f.getBoolean(o));
break; break;
@ -53,6 +65,8 @@ public class LuaSerializer {
case "java.lang.String": case "java.lang.String":
map.put(name, (String) f.get(o)); map.put(name, (String) f.get(o));
break; break;
case "java.util.List":
break;
default: default:
map.put(name, serializeObject(f.get(o))); map.put(name, serializeObject(f.get(o)));
break; break;