From 0686ab99c5c02b215b4dff2c2e5fdbbe618cfc6d Mon Sep 17 00:00:00 2001 From: Samy Avrillon Date: Fri, 1 Nov 2024 23:23:15 +0100 Subject: [PATCH] Added link with website and tampermonkey script --- .../greposimu/GrepoSimuApplication.java | 17 +++ .../controller/SchedulerController.java | 67 +++++++++ .../controller/SimulatorController.java | 1 - .../simulator/objective/TownObjective.java | 4 + .../bernard/greposimu/source/JSONSourcer.java | 4 +- src/main/resources/application.properties | 3 +- .../{greposource.js => static/userscript.js} | 128 +++++++++++------- .../greposimu/GrepoSimuApplicationTests.java | 11 +- 8 files changed, 182 insertions(+), 53 deletions(-) create mode 100644 src/main/java/com/bernard/greposimu/controller/SchedulerController.java rename src/main/resources/{greposource.js => static/userscript.js} (62%) diff --git a/src/main/java/com/bernard/greposimu/GrepoSimuApplication.java b/src/main/java/com/bernard/greposimu/GrepoSimuApplication.java index d6f304d..daa7575 100644 --- a/src/main/java/com/bernard/greposimu/GrepoSimuApplication.java +++ b/src/main/java/com/bernard/greposimu/GrepoSimuApplication.java @@ -1,19 +1,36 @@ package com.bernard.greposimu; +import java.io.File; import java.io.IOException; +import java.util.Map; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.bernard.greposimu.model.game.GameConfig; +import com.bernard.greposimu.model.game.GrepoYaml; +import com.bernard.greposimu.model.simulator.objective.TownObjective; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature; @SpringBootApplication public class GrepoSimuApplication { public static GameConfig GREPOLIS_GC; + public static Map OBJECTIVES; public static void main(String[] args) throws IOException { GREPOLIS_GC = GrepoSimu.makeGameData(); + File objectiveFile = new File("/home/mysaa/Documents/Projets/eclipse-workspace/GrepoSimu/src/test/resources/objectives.yml"); + + ObjectMapper om = new ObjectMapper(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER)); + om.registerModule(new GrepoYaml(GREPOLIS_GC)); + + JavaType objType = om.getTypeFactory().constructParametricType(Map.class, String.class,TownObjective.class); + OBJECTIVES = om.readValue(objectiveFile, objType); + SpringApplication.run(GrepoSimuApplication.class, args); } diff --git a/src/main/java/com/bernard/greposimu/controller/SchedulerController.java b/src/main/java/com/bernard/greposimu/controller/SchedulerController.java new file mode 100644 index 0000000..233239f --- /dev/null +++ b/src/main/java/com/bernard/greposimu/controller/SchedulerController.java @@ -0,0 +1,67 @@ +package com.bernard.greposimu.controller; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; +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.SimulatorData; +import com.bernard.greposimu.model.simulator.objective.TownObjective; +import com.bernard.greposimu.source.JSONSourcer; + +@Controller +public class SchedulerController { + + Map registered = new HashMap<>(); + + @CrossOrigin + @PostMapping(value = "/registerScheduler", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.TEXT_PLAIN_VALUE) + //@GetMapping(value = "/registerScheduler") + public @ResponseBody String register(Model model,@RequestBody String data) { + //System.out.println(data.toString()); + GameConfig gc = GrepoSimuApplication.GREPOLIS_GC; + SimulatorData sd = JSONSourcer.makeSimulationData(data.toString(),gc); + + UUID uuid = UUID.randomUUID(); + registered.put(uuid, sd); + + return uuid.toString(); + } + + @GetMapping(value = "/scheduler/{uuid}") + public String simulator(Model model, @PathVariable("uuid") String uuidStr) { + UUID uuid = UUID.fromString(uuidStr); + + GameConfig gc = GrepoSimuApplication.GREPOLIS_GC; + SimulatorData sd = registered.get(uuid); + + TownObjective defObjective = GrepoSimuApplication.OBJECTIVES.get("defTerTown"); + defObjective.setGc(gc); + + StringBuilder out = new StringBuilder(); + for(Ville v : sd.getVilles().values()) { + out.append("

==== Ville "+v.getNom()+" ====

\n"); + out.append(defObjective.getDifferences(v).stream().map(Command::toString).sorted().collect(Collectors.joining("
\n"))); + } + + model.addAttribute("content",out.toString()); + return "debug"; + } + +} diff --git a/src/main/java/com/bernard/greposimu/controller/SimulatorController.java b/src/main/java/com/bernard/greposimu/controller/SimulatorController.java index 8d57d9b..c4a627d 100644 --- a/src/main/java/com/bernard/greposimu/controller/SimulatorController.java +++ b/src/main/java/com/bernard/greposimu/controller/SimulatorController.java @@ -48,7 +48,6 @@ public class SimulatorController { return "simulator"; } - @PostMapping("/simulate") @GetMapping("/simulate") public String simulate(@ModelAttribute SimulatorParams params, Model model) throws IOException { if(params == null) diff --git a/src/main/java/com/bernard/greposimu/model/simulator/objective/TownObjective.java b/src/main/java/com/bernard/greposimu/model/simulator/objective/TownObjective.java index e2ed9cf..ceab138 100644 --- a/src/main/java/com/bernard/greposimu/model/simulator/objective/TownObjective.java +++ b/src/main/java/com/bernard/greposimu/model/simulator/objective/TownObjective.java @@ -123,6 +123,10 @@ public class TownObjective { return god; } + public void setGc(GameConfig gc) { + this.gc = gc; + } + @Override public String toString() { return "TownObjective [gc=" + gc + ", buildings=" + buildings + ", unitsProportions=" + unitsProportions diff --git a/src/main/java/com/bernard/greposimu/source/JSONSourcer.java b/src/main/java/com/bernard/greposimu/source/JSONSourcer.java index 3efd350..f135e0a 100644 --- a/src/main/java/com/bernard/greposimu/source/JSONSourcer.java +++ b/src/main/java/com/bernard/greposimu/source/JSONSourcer.java @@ -61,10 +61,10 @@ public class JSONSourcer { return new UnitResources(res.wood, res.stone, res.iron,god,res.favor); } - public static final SimulatorData makeSimulationData(File file, GameConfig gc) { + public static final SimulatorData makeSimulationData(String json, GameConfig gc) { ObjectMapper om = new ObjectMapper(); try { - SourcedData sd = om.readValue(file, SourcedData.class); + SourcedData sd = om.readValue(json, SourcedData.class); Map villes = new HashMap(); for(String idS : sd.towns.keySet()) { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index c601b78..4ab4d5c 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,4 @@ spring.application.name=GrepoSimu spring.devtools.restart.pollInterval=10s -spring.mvc.favicon.enabled=false \ No newline at end of file +spring.mvc.favicon.enabled=false +spring.web.resources.static-locations=classpath:/static/ \ No newline at end of file diff --git a/src/main/resources/greposource.js b/src/main/resources/static/userscript.js similarity index 62% rename from src/main/resources/greposource.js rename to src/main/resources/static/userscript.js index e88be46..7d70c85 100644 --- a/src/main/resources/greposource.js +++ b/src/main/resources/static/userscript.js @@ -1,3 +1,17 @@ +// ==UserScript== +// @name GrepoSimu +// @namespace greposimu +// @version 1.0 +// @author Mysaa Java +// @homepage https://greposimu.bernard.com.de +// @updateURL https://greposimu.bernard.com.de/userscript.js +// @downloadURL https://greposimu.bernard.com.de/userscript.js +// @description This script sends information on the town to greposimu +// @include https://*.grepolis.com/game/* +// @exclude view-source://* +// @icon https://greposimu.bernard.com.de/favicon.ico +// ==/UserScript== + function downloadObjectAsJson(exportObj, exportName){ var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj,null,2)); var downloadAnchorNode = document.createElement('a'); @@ -43,16 +57,16 @@ function makeObject() { orderz = ITowns.towns[townid].buildingOrders() out[townid].buildingOrders = [] - for(var i = 0; i