Added command metadata

This commit is contained in:
Samy Avrillon 2024-11-02 00:39:17 +01:00
parent 0686ab99c5
commit 0bcc1e3591
Signed by: Mysaa
GPG Key ID: 0220AC4A3D6A328B
16 changed files with 143 additions and 34 deletions

View File

@ -8,19 +8,18 @@ import java.util.stream.Collectors;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import com.bernard.greposimu.GrepoSimuApplication; import com.bernard.greposimu.GrepoSimuApplication;
import com.bernard.greposimu.model.game.GameConfig; import com.bernard.greposimu.model.game.GameConfig;
import com.bernard.greposimu.model.simulator.Ville; import com.bernard.greposimu.model.simulator.Ville;
import com.bernard.greposimu.model.simulator.command.Command; import com.bernard.greposimu.model.simulator.command.Command;
import com.bernard.greposimu.model.simulator.command.TownCommand;
import com.bernard.greposimu.model.simulator.SimulatorData; import com.bernard.greposimu.model.simulator.SimulatorData;
import com.bernard.greposimu.model.simulator.objective.TownObjective; import com.bernard.greposimu.model.simulator.objective.TownObjective;
import com.bernard.greposimu.source.JSONSourcer; import com.bernard.greposimu.source.JSONSourcer;
@ -57,10 +56,20 @@ public class SchedulerController {
StringBuilder out = new StringBuilder(); StringBuilder out = new StringBuilder();
for(Ville v : sd.getVilles().values()) { for(Ville v : sd.getVilles().values()) {
out.append("<h3>==== Ville "+v.getNom()+" ====</h3>\n"); out.append("<h3>==== Ville "+v.getNom()+" ====</h3>\n");
out.append("<ul>");
for(Command c : defObjective.getDifferences(v)){
out.append("<li>");
out.append(c.toString());
out.append("===>");
out.append(c.timeNeeded(sd));
if(c instanceof TownCommand)
out.append("///"+((TownCommand)c).neededResources(sd).toString());
out.append("</li>\n");
}
out.append(defObjective.getDifferences(v).stream().map(Command::toString).sorted().collect(Collectors.joining("<br/>\n"))); out.append(defObjective.getDifferences(v).stream().map(Command::toString).sorted().collect(Collectors.joining("<br/>\n")));
out.append("</ul>");
} }
model.addAttribute("raw",out.toString());
model.addAttribute("content",out.toString());
return "debug"; return "debug";
} }

View File

@ -11,7 +11,6 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import com.bernard.greposimu.GrepoSimuApplication; import com.bernard.greposimu.GrepoSimuApplication;

View File

@ -7,7 +7,6 @@ import com.bernard.greposimu.model.DefContext;
import com.bernard.greposimu.model.FightStats; import com.bernard.greposimu.model.FightStats;
import com.bernard.greposimu.model.OffContext; import com.bernard.greposimu.model.OffContext;
import com.bernard.greposimu.model.game.GameConfig; import com.bernard.greposimu.model.game.GameConfig;
import com.bernard.greposimu.model.game.researches.Research;
import com.bernard.greposimu.model.game.units.FightType; import com.bernard.greposimu.model.game.units.FightType;
import com.bernard.greposimu.model.game.units.NavalUnit; import com.bernard.greposimu.model.game.units.NavalUnit;
import com.bernard.greposimu.model.game.units.TerrestrialUnit; import com.bernard.greposimu.model.game.units.TerrestrialUnit;

View File

@ -23,5 +23,14 @@ public class Resources {
public int getIron() { public int getIron() {
return iron; return iron;
} }
public Resources prod(double p){
return new Resources((int)p*wood, (int)p*stone, (int)p*iron);
}
@Override
public String toString() {
return "["+this.wood+";"+this.stone+";"+this.iron+"]";
}
} }

View File

@ -62,5 +62,4 @@ public class Research implements Identified{
return researchPoints; return researchPoints;
} }
} }

View File

@ -4,19 +4,22 @@ import java.util.Map;
public class SimulatorData { public class SimulatorData {
Map<String,Ville> villes; Map<Integer,Ville> villes;
Joueureuse joueureuse; Joueureuse joueureuse;
public SimulatorData(Map<String, Ville> villes, Joueureuse joueureuse) { public SimulatorData(Map<Integer, Ville> villes, Joueureuse joueureuse) {
super();
this.villes = villes; this.villes = villes;
this.joueureuse = joueureuse; this.joueureuse = joueureuse;
} }
public Map<String, Ville> getVilles() { public Map<Integer, Ville> getVilles() {
return villes; return villes;
} }
public Ville getVille(int id) {
return villes.get(id);
}
public Joueureuse getJoueureuse() { public Joueureuse getJoueureuse() {
return joueureuse; return joueureuse;

View File

@ -176,6 +176,10 @@ public class Ville {
return researches; return researches;
} }
public boolean hasResearch(Research r) {
return researches.contains(r);
}
public List<ResearchQueueItem> getResearchQueue() { public List<ResearchQueueItem> getResearchQueue() {
return researchQueue; return researchQueue;
} }

View File

@ -1,7 +1,9 @@
package com.bernard.greposimu.model.simulator.command; package com.bernard.greposimu.model.simulator.command;
import com.bernard.greposimu.model.game.Building; import com.bernard.greposimu.model.game.Building;
import com.bernard.greposimu.model.game.GameConfig;
import com.bernard.greposimu.model.game.Resources; import com.bernard.greposimu.model.game.Resources;
import com.bernard.greposimu.model.simulator.SimulatorData;
public class BuildCommand extends TownCommand { public class BuildCommand extends TownCommand {
@ -12,9 +14,8 @@ public class BuildCommand extends TownCommand {
boolean tearingDown; boolean tearingDown;
public BuildCommand(int town, Resources need, Building building, int level, boolean tearingDown) { public BuildCommand(GameConfig gc, int town, Building building, int level, boolean tearingDown) {
this.town = town; super(gc,town);
this.need = need;
this.building = building; this.building = building;
this.level = level; this.level = level;
this.tearingDown = tearingDown; this.tearingDown = tearingDown;
@ -27,5 +28,17 @@ public class BuildCommand extends TownCommand {
else else
return "[%d] Build %s from %d -> %d".formatted(this.town,this.building.name(),this.level-1,this.level); return "[%d] Build %s from %d -> %d".formatted(this.town,this.building.name(),this.level-1,this.level);
} }
@Override
public Resources neededResources(SimulatorData sd) {
// TODO Auto-generated method stub
return new Resources(1000, 1000, 1000);
}
@Override
public int timeNeeded(SimulatorData sd) {
// TODO Auto-generated method stub
return 3600;
}
} }

View File

@ -1,5 +1,16 @@
package com.bernard.greposimu.model.simulator.command; package com.bernard.greposimu.model.simulator.command;
import com.bernard.greposimu.model.game.GameConfig;
import com.bernard.greposimu.model.simulator.SimulatorData;
public abstract class Command { public abstract class Command {
GameConfig gc;
public Command(GameConfig gc) {
this.gc = gc;
}
public abstract int timeNeeded(SimulatorData sd);
} }

View File

@ -1,11 +1,15 @@
package com.bernard.greposimu.model.simulator.command; package com.bernard.greposimu.model.simulator.command;
import com.bernard.greposimu.model.game.GameConfig;
import com.bernard.greposimu.model.game.Resources;
import com.bernard.greposimu.model.simulator.SimulatorData;
public class HideStoreCommand extends TownCommand { public class HideStoreCommand extends TownCommand {
int amount; int amount;
public HideStoreCommand(int town, int amount) { public HideStoreCommand(GameConfig gc, int town, int amount) {
this.town = town; super(gc,town);
this.amount = amount; this.amount = amount;
} }
@ -13,5 +17,15 @@ public class HideStoreCommand extends TownCommand {
public String toString() { public String toString() {
return "[%d] Store %d iron in the hide".formatted(this.town,this.amount); return "[%d] Store %d iron in the hide".formatted(this.town,this.amount);
} }
@Override
public Resources neededResources(SimulatorData sd) {
return new Resources(0, 0, this.amount);
}
@Override
public int timeNeeded(SimulatorData sd) {
return 0;
}
} }

View File

@ -1,16 +1,19 @@
package com.bernard.greposimu.model.simulator.command; package com.bernard.greposimu.model.simulator.command;
import com.bernard.greposimu.model.game.Building;
import com.bernard.greposimu.model.game.GameConfig;
import com.bernard.greposimu.model.game.Resources; import com.bernard.greposimu.model.game.Resources;
import com.bernard.greposimu.model.game.units.Unit; import com.bernard.greposimu.model.game.units.Unit;
import com.bernard.greposimu.model.simulator.SimulatorData;
import com.bernard.greposimu.model.simulator.Ville;
public class RecruitCommand extends TownCommand{ public class RecruitCommand extends TownCommand{
Unit unit; Unit unit;
int count; int count;
public RecruitCommand(int town, Resources need, Unit unit, int count) { public RecruitCommand(GameConfig gc,int town, Unit unit, int count) {
this.town = town; super(gc,town);
this.need = need;
this.unit = unit; this.unit = unit;
this.count = count; this.count = count;
} }
@ -19,5 +22,30 @@ public class RecruitCommand extends TownCommand{
public String toString() { public String toString() {
return "[%d] Recruit %d %s".formatted(this.town,this.count, this.unit.getName()); return "[%d] Recruit %d %s".formatted(this.town,this.count, this.unit.getName());
} }
@Override
public Resources neededResources(SimulatorData sd) {
Resources base = this.unit.getBuildCost();
Ville v = sd.getVille(town);
double percentage = 1.0;
//TODO take heroes into account, generalize resarches
if(v.hasResearch(gc.getResearch("conscription")))
percentage -= .1;
return base.prod(percentage*count);
}
@Override
public int timeNeeded(SimulatorData sd) {
int base = this.unit.getBuildTime();
Ville v = sd.getVille(town);
double percentage = 1.0;
//TODO take heroes into account, generalize resarches
if(v.hasResearch(gc.getResearch("instructor")))
percentage -= .1;
//TODO move this code in gameconfig
//TODO use the real application computation (helpers/general_modifications.js:221)
percentage -= Math.pow(v.getBatiments().get(Building.BARRACKS) - 1, 1.1) / 100;
return (int) (base * percentage * count);
}
} }

View File

@ -1,15 +1,16 @@
package com.bernard.greposimu.model.simulator.command; package com.bernard.greposimu.model.simulator.command;
import com.bernard.greposimu.model.game.GameConfig;
import com.bernard.greposimu.model.game.Resources; import com.bernard.greposimu.model.game.Resources;
import com.bernard.greposimu.model.game.researches.Research; import com.bernard.greposimu.model.game.researches.Research;
import com.bernard.greposimu.model.simulator.SimulatorData;
public class ResearchCommand extends TownCommand { public class ResearchCommand extends TownCommand {
Research research; Research research;
boolean forget; boolean forget;
public ResearchCommand(int town, Resources need, Research research, boolean forget) { public ResearchCommand(GameConfig gc, int town, Resources need, Research research, boolean forget) {
this.town = town; super(gc,town);
this.need = need;
this.research = research; this.research = research;
this.forget = forget; this.forget = forget;
} }
@ -21,5 +22,19 @@ public class ResearchCommand extends TownCommand {
else else
return "[%d] Research %s".formatted(this.town,this.research.getName()); return "[%d] Research %s".formatted(this.town,this.research.getName());
} }
@Override
public Resources neededResources(SimulatorData sd) {
//TODO take into account other modifiers (heroes zB)
//TODO take forgetting into account
return research.getResources();
}
@Override
public int timeNeeded(SimulatorData sd) {
//TODO take into account other modifiers (heroes zB)
//TODO take forgetting into account
return research.getRequiredTime();
}
} }

View File

@ -1,11 +1,17 @@
package com.bernard.greposimu.model.simulator.command; package com.bernard.greposimu.model.simulator.command;
import com.bernard.greposimu.model.game.GameConfig;
import com.bernard.greposimu.model.game.Resources; import com.bernard.greposimu.model.game.Resources;
import com.bernard.greposimu.model.simulator.SimulatorData;
public abstract class TownCommand extends Command { public abstract class TownCommand extends Command {
int town; int town;
public TownCommand(GameConfig gc, int town) {
super(gc);
this.town = town;
}
Resources need; public abstract Resources neededResources(SimulatorData sd);
} }

View File

@ -72,10 +72,10 @@ public class TownObjective {
if(cur != obj) { if(cur != obj) {
if(obj>cur) if(obj>cur)
for(int i=cur+1;i<=obj;i++) for(int i=cur+1;i<=obj;i++)
commands.add(new BuildCommand(v.getId(), null, b, i, false)); commands.add(new BuildCommand(gc,v.getId(), b, i, false));
else else
for(int i=cur-1;i>=obj;i--) for(int i=cur-1;i>=obj;i--)
commands.add(new BuildCommand(v.getId(), null, b, i, true)); commands.add(new BuildCommand(gc,v.getId(), b, i, true));
} }
//TODO check queue //TODO check queue
} }
@ -84,9 +84,9 @@ public class TownObjective {
boolean cur = v.getResearches().contains(r); boolean cur = v.getResearches().contains(r);
boolean obj = this.researches.contains(r); boolean obj = this.researches.contains(r);
if(cur && !obj) if(cur && !obj)
commands.add(new ResearchCommand(v.getId(),null,r,true)); commands.add(new ResearchCommand(gc,v.getId(),null,r,true));
else if (!cur && obj) else if (!cur && obj)
commands.add(new ResearchCommand(v.getId(),null,r,false)); commands.add(new ResearchCommand(gc,v.getId(),null,r,false));
//TODO check queue //TODO check queue
} }
Troupes tot = Troupes.add(v.getTroupes(),v.getSoutiens().values().stream().reduce(Troupes::add).orElse(new Troupes(Map.of()))); Troupes tot = Troupes.add(v.getTroupes(),v.getSoutiens().values().stream().reduce(Troupes::add).orElse(new Troupes(Map.of())));
@ -94,12 +94,12 @@ public class TownObjective {
for(Unit u : gc.getUnits()) { for(Unit u : gc.getUnits()) {
int diff = target.getUnites().getOrDefault(u,0) - tot.getUnites().getOrDefault(u,0); int diff = target.getUnites().getOrDefault(u,0) - tot.getUnites().getOrDefault(u,0);
if(diff > 0) if(diff > 0)
commands.add(new RecruitCommand(v.getId(), null, u, diff)); commands.add(new RecruitCommand(gc,v.getId(), u, diff));
//TODO manage unit removal planned //TODO manage unit removal planned
} }
if(v.getPiecesStoquees() < hide) if(v.getPiecesStoquees() < hide)
commands.add(new HideStoreCommand(v.getId(), hide - v.getPiecesStoquees())); commands.add(new HideStoreCommand(gc,v.getId(), hide - v.getPiecesStoquees()));
return commands; return commands;
} }

View File

@ -10,7 +10,6 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.bernard.greposimu.GrepoSimu;
import com.bernard.greposimu.model.game.Building; import com.bernard.greposimu.model.game.Building;
import com.bernard.greposimu.model.game.GameConfig; import com.bernard.greposimu.model.game.GameConfig;
import com.bernard.greposimu.model.game.God; import com.bernard.greposimu.model.game.God;
@ -66,7 +65,7 @@ public class JSONSourcer {
try { try {
SourcedData sd = om.readValue(json, SourcedData.class); SourcedData sd = om.readValue(json, SourcedData.class);
Map<String,Ville> villes = new HashMap<String, Ville>(); Map<Integer,Ville> villes = new HashMap<>();
for(String idS : sd.towns.keySet()) { for(String idS : sd.towns.keySet()) {
Integer id = Integer.parseInt(idS); Integer id = Integer.parseInt(idS);
SourcedTown st = sd.getTowns().get(idS); SourcedTown st = sd.getTowns().get(idS);
@ -139,7 +138,7 @@ public class JSONSourcer {
} }
Set<SourcedCelebration> townCele = sd.celebrations.stream().filter(c -> c.getTown() == id).collect(Collectors.toSet()); Set<SourcedCelebration> townCele = sd.celebrations.stream().filter(c -> c.getTown() == id).collect(Collectors.toSet());
villes.put(st.name, new Ville( villes.put(id, new Ville(
id, id,
st.name, st.name,
buildings, buildings,

View File

@ -16,6 +16,7 @@ body {
</head> </head>
<body> <body>
<pre th:text="${content}"></pre> <pre th:text="${content}"></pre>
<main th:utext="${raw}"></main>
</body> </body>
</html> </html>