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.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bernard.greposimu.GrepoSimuApplication;
import com.bernard.greposimu.model.game.GameConfig;
import com.bernard.greposimu.model.simulator.Ville;
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.objective.TownObjective;
import com.bernard.greposimu.source.JSONSourcer;
@ -57,10 +56,20 @@ public class SchedulerController {
StringBuilder out = new StringBuilder();
for(Ville v : sd.getVilles().values()) {
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("</ul>");
}
model.addAttribute("content",out.toString());
model.addAttribute("raw",out.toString());
return "debug";
}

View File

@ -11,7 +11,6 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
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.OffContext;
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.NavalUnit;
import com.bernard.greposimu.model.game.units.TerrestrialUnit;

View File

@ -23,5 +23,14 @@ public class Resources {
public int getIron() {
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;
}
}

View File

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

View File

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

View File

@ -1,7 +1,9 @@
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.simulator.SimulatorData;
public class BuildCommand extends TownCommand {
@ -12,9 +14,8 @@ public class BuildCommand extends TownCommand {
boolean tearingDown;
public BuildCommand(int town, Resources need, Building building, int level, boolean tearingDown) {
this.town = town;
this.need = need;
public BuildCommand(GameConfig gc, int town, Building building, int level, boolean tearingDown) {
super(gc,town);
this.building = building;
this.level = level;
this.tearingDown = tearingDown;
@ -27,5 +28,17 @@ public class BuildCommand extends TownCommand {
else
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;
import com.bernard.greposimu.model.game.GameConfig;
import com.bernard.greposimu.model.simulator.SimulatorData;
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;
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 {
int amount;
public HideStoreCommand(int town, int amount) {
this.town = town;
public HideStoreCommand(GameConfig gc, int town, int amount) {
super(gc,town);
this.amount = amount;
}
@ -13,5 +17,15 @@ public class HideStoreCommand extends TownCommand {
public String toString() {
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;
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.units.Unit;
import com.bernard.greposimu.model.simulator.SimulatorData;
import com.bernard.greposimu.model.simulator.Ville;
public class RecruitCommand extends TownCommand{
Unit unit;
int count;
public RecruitCommand(int town, Resources need, Unit unit, int count) {
this.town = town;
this.need = need;
public RecruitCommand(GameConfig gc,int town, Unit unit, int count) {
super(gc,town);
this.unit = unit;
this.count = count;
}
@ -19,5 +22,30 @@ public class RecruitCommand extends TownCommand{
public String toString() {
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;
import com.bernard.greposimu.model.game.GameConfig;
import com.bernard.greposimu.model.game.Resources;
import com.bernard.greposimu.model.game.researches.Research;
import com.bernard.greposimu.model.simulator.SimulatorData;
public class ResearchCommand extends TownCommand {
Research research;
boolean forget;
public ResearchCommand(int town, Resources need, Research research, boolean forget) {
this.town = town;
this.need = need;
public ResearchCommand(GameConfig gc, int town, Resources need, Research research, boolean forget) {
super(gc,town);
this.research = research;
this.forget = forget;
}
@ -21,5 +22,19 @@ public class ResearchCommand extends TownCommand {
else
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;
import com.bernard.greposimu.model.game.GameConfig;
import com.bernard.greposimu.model.game.Resources;
import com.bernard.greposimu.model.simulator.SimulatorData;
public abstract class TownCommand extends Command {
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(obj>cur)
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
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
}
@ -84,9 +84,9 @@ public class TownObjective {
boolean cur = v.getResearches().contains(r);
boolean obj = this.researches.contains(r);
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)
commands.add(new ResearchCommand(v.getId(),null,r,false));
commands.add(new ResearchCommand(gc,v.getId(),null,r,false));
//TODO check queue
}
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()) {
int diff = target.getUnites().getOrDefault(u,0) - tot.getUnites().getOrDefault(u,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
}
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;
}

View File

@ -10,7 +10,6 @@ import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import com.bernard.greposimu.GrepoSimu;
import com.bernard.greposimu.model.game.Building;
import com.bernard.greposimu.model.game.GameConfig;
import com.bernard.greposimu.model.game.God;
@ -66,7 +65,7 @@ public class JSONSourcer {
try {
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()) {
Integer id = Integer.parseInt(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());
villes.put(st.name, new Ville(
villes.put(id, new Ville(
id,
st.name,
buildings,

View File

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