Mis a jour de la librairie de Yaml. C'est maintenant plus joli.
This commit is contained in:
parent
2fdaf82782
commit
11c91fe87e
@ -29,7 +29,7 @@ jar {
|
||||
|
||||
dependencies {
|
||||
|
||||
implementation 'com.amihaiemil.web:eo-yaml:5.1.3'
|
||||
implementation 'com.amihaiemil.web:eo-yaml:5.2.1'
|
||||
implementation 'com.formdev:flatlaf:0.37'
|
||||
|
||||
}
|
||||
|
||||
12
src/main/java/com/bernard/murder/FuncUtils.java
Normal file
12
src/main/java/com/bernard/murder/FuncUtils.java
Normal file
@ -0,0 +1,12 @@
|
||||
package com.bernard.murder;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class FuncUtils {
|
||||
|
||||
public static final <A,B,C> Function<A,C> compose(Function<B,C> f,Function<? super A,? extends B> g) {
|
||||
return f.compose(g);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -84,7 +84,7 @@ public class ParseUtils {
|
||||
}
|
||||
|
||||
|
||||
public static Set<String> mappingKeys(YamlMapping mapping) throws IOException{
|
||||
public static Set<String> mappingStringKeys(YamlMapping mapping) throws IOException{
|
||||
return mapping.keys().stream().map(n ->n.asScalar().value()).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
|
||||
@ -10,7 +10,6 @@ import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collector;
|
||||
|
||||
import com.amihaiemil.eoyaml.Node;
|
||||
import com.amihaiemil.eoyaml.Yaml;
|
||||
import com.amihaiemil.eoyaml.YamlMapping;
|
||||
import com.amihaiemil.eoyaml.YamlMappingBuilder;
|
||||
@ -21,10 +20,6 @@ import com.amihaiemil.eoyaml.YamlSequenceBuilder;
|
||||
|
||||
public class YamlUtils {
|
||||
|
||||
public static final <A,B,C> Function<A,C> compose(Function<B,C> f,Function<? super A,? extends B> g) {
|
||||
return f.compose(g);
|
||||
}
|
||||
|
||||
public static final YamlSequence listToSeq(List<YamlNode> nodes) {
|
||||
YamlSequenceBuilder ysb = Yaml.createYamlSequenceBuilder();
|
||||
for(YamlNode n : nodes)
|
||||
@ -53,26 +48,6 @@ public class YamlUtils {
|
||||
return ysb.build();
|
||||
}
|
||||
|
||||
public static final YamlSequence getSequence(YamlNode node) {
|
||||
if(node.type()==Node.SEQUENCE)return node.asSequence();
|
||||
if(node.type()==Node.SCALAR && node.asScalar().value().contentEquals("[]"))return Yaml.createYamlSequenceBuilder().build();
|
||||
throw new IllegalArgumentException("Le noeud n'est pas une séquence");
|
||||
}
|
||||
|
||||
public static final YamlMapping getMapping(YamlNode node) {
|
||||
if(node.type()==Node.MAPPING)return node.asMapping();
|
||||
if(node.type()==Node.SCALAR && node.asScalar().value().contentEquals("{}"))return Yaml.createYamlMappingBuilder().build();
|
||||
throw new IllegalArgumentException("Le noeud n'est pas une séquence");
|
||||
}
|
||||
|
||||
public static final boolean isMapping(YamlNode node) {
|
||||
return (node.type()==Node.MAPPING) || (node.type()==Node.SCALAR && node.asScalar().value().contentEquals("{}"));
|
||||
}
|
||||
|
||||
public static final boolean isSequence(YamlNode node) {
|
||||
return (node.type()==Node.SEQUENCE) || (node.type()==Node.SCALAR && node.asScalar().value().contentEquals("[]"));
|
||||
}
|
||||
|
||||
public static final YamlNode scalar(Object in) {
|
||||
return Yaml.createYamlScalarBuilder().addLine(in.toString()).buildPlainScalar();
|
||||
}
|
||||
|
||||
@ -1,16 +1,11 @@
|
||||
package com.bernard.murder.game;
|
||||
|
||||
import static com.bernard.murder.ParseUtils.mappingKeys;
|
||||
import static com.bernard.murder.ParseUtils.mappingStringKeys;
|
||||
import static com.bernard.murder.ParseUtils.parseTimeLength;
|
||||
import static com.bernard.murder.ParseUtils.sequenceStream;
|
||||
|
||||
import static com.bernard.murder.ParseUtils.watch;
|
||||
|
||||
import static com.bernard.murder.YamlUtils.getMapping;
|
||||
import static com.bernard.murder.YamlUtils.isMapping;
|
||||
import static com.bernard.murder.YamlUtils.getSequence;
|
||||
import static com.bernard.murder.YamlUtils.isSequence;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
@ -48,13 +43,13 @@ public class GameCreator {
|
||||
YamlInput input = Yaml.createYamlInput(toread);
|
||||
YamlMapping globalMap = input.readYamlMapping();
|
||||
|
||||
YamlMapping yjoueurs = getMapping(globalMap.value("joueurs"));
|
||||
YamlMapping yjoueurs = globalMap.yamlMapping("joueurs");
|
||||
|
||||
// Pour pouvoir créer les objets et les espaces personnels
|
||||
Set<String> playerNames = mappingKeys(yjoueurs).stream().map(n -> n.toString()).collect(Collectors.toSet());
|
||||
Set<String> playerNames = mappingStringKeys(yjoueurs).stream().map(n -> n.toString()).collect(Collectors.toSet());
|
||||
|
||||
|
||||
YamlMapping yactions = getMapping(globalMap.value("actions"));
|
||||
YamlMapping yactions = globalMap.yamlMapping("actions");
|
||||
Set<Action> actions = yactions.keys()
|
||||
.stream()
|
||||
.map(n -> new Action(n.asScalar().value(), parseTimeLength(yactions.string(n))))
|
||||
@ -62,35 +57,40 @@ public class GameCreator {
|
||||
Map<String,Set<Action>> persActions = playerNames.stream()
|
||||
.collect(Collectors.toMap(Function.identity(), s -> actions.stream().map(Action::clone).collect(Collectors.toSet())));
|
||||
|
||||
YamlSequence yinventory = getSequence(globalMap.value("inventaire"));
|
||||
YamlSequence yinventory = globalMap.yamlSequence("inventaire");
|
||||
|
||||
Set<String> objets = StreamSupport.stream(Spliterators.spliteratorUnknownSize(yinventory.iterator(),Spliterator.ORDERED),false)
|
||||
.map(n ->n.asScalar().value())
|
||||
.collect(Collectors.toSet());
|
||||
Map<String,Set<Objet>> persObjets = playerNames.stream()
|
||||
.collect(Collectors.toMap(Function.identity(),p -> objets.stream().map(o ->new Objet(String.format(o,p))).collect(Collectors.toSet())));
|
||||
|
||||
YamlSequence ystatus = getSequence(globalMap.value("status"));
|
||||
Map<String,Set<Objet>> persObjets =
|
||||
playerNames.stream()
|
||||
.collect(Collectors.toMap(
|
||||
Function.identity(),
|
||||
p -> objets.stream()
|
||||
.map(o ->new Objet(String.format(o,p)))
|
||||
.collect(Collectors.toSet())
|
||||
));
|
||||
YamlSequence ystatus = globalMap.yamlSequence("status");
|
||||
Set<Status> status = sequenceStream(ystatus).map(n -> new Status(n.asScalar().value())).collect(Collectors.toSet());
|
||||
|
||||
YamlMapping yespaces = getMapping(globalMap.value("espaces"));
|
||||
YamlMapping yespaces = globalMap.yamlMapping("espaces");
|
||||
|
||||
Map<String, Map<Objet, Integer>> objetsDansEspaces = yespaces.keys().stream().collect(Collectors.toMap(
|
||||
n -> n.asScalar().value(),
|
||||
n-> parseHiddenObjects(getSequence(yespaces.value(n)))
|
||||
n-> parseHiddenObjects(yespaces.yamlSequence(n))
|
||||
));
|
||||
Set<Piece> espaceObjets = yespaces.keys().stream()
|
||||
.map(n -> new Piece(n.asScalar().value(), objetsDansEspaces.get(n.asScalar().value())))
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
YamlMapping yespacesPersos = getMapping(globalMap.value("espacesPersos"));
|
||||
YamlMapping yespacesPersos = globalMap.yamlMapping("espacesPersos");
|
||||
|
||||
Map<String,Set<Piece>> persespacesPersos = playerNames.stream().collect(Collectors.toMap(
|
||||
Function.identity(),
|
||||
p -> yespacesPersos.keys().stream()
|
||||
.map(e -> new Piece(
|
||||
String.format(e.asScalar().value(), p),
|
||||
parseHiddenObjects(getSequence(yespacesPersos.value(e)),p)
|
||||
parseHiddenObjects(yespacesPersos.yamlSequence(e),p)
|
||||
))
|
||||
.collect(Collectors.toSet())
|
||||
));
|
||||
@ -99,32 +99,33 @@ public class GameCreator {
|
||||
for(YamlNode pn : yjoueurs.keys()) {
|
||||
String pname = pn.asScalar().value();
|
||||
persActions.get(pname).addAll(
|
||||
getMapping(getMapping(yjoueurs.value(pn)).value("actions")).keys()
|
||||
yjoueurs.yamlMapping(pn).yamlMapping("actions").keys()
|
||||
.stream()
|
||||
.map(n -> new Action(n.asScalar().value(), parseTimeLength(getMapping(getMapping(yjoueurs.value(pn)).value("actions")).string(n))))
|
||||
.map(n -> new Action(n.asScalar().value(), parseTimeLength(yjoueurs.yamlMapping(pn).yamlMapping("actions").string(n))))
|
||||
.collect(Collectors.toSet())
|
||||
);
|
||||
persObjets.get(pname).addAll(
|
||||
StreamSupport.stream(Spliterators.spliteratorUnknownSize(getSequence(getMapping(yjoueurs.value(pn)).value("inventaire")).iterator(),Spliterator.ORDERED),false)
|
||||
.map(n ->n.asScalar().value())
|
||||
.map(o ->new Objet(o))
|
||||
StreamSupport.stream(Spliterators.spliteratorUnknownSize(yjoueurs.yamlMapping(pn).yamlSequence("inventaire").iterator(),Spliterator.ORDERED),false)
|
||||
.map(n -> n.asScalar().value())
|
||||
.map(o -> new Objet(o))
|
||||
.collect(Collectors.toSet())
|
||||
);
|
||||
if(isMapping(getMapping(yjoueurs.value(pn)).value("espacePerso")))
|
||||
|
||||
if(yjoueurs.yamlMapping(pn).value("espacePerso").type()==Node.MAPPING)
|
||||
// Plusieurs espaces
|
||||
getMapping(getMapping(yjoueurs.value(pn)).value("espacePerso")).keys().forEach(n ->
|
||||
yjoueurs.yamlMapping(pn).yamlMapping("espacePerso").keys().forEach(n ->
|
||||
persespacesPersos.get(pname)
|
||||
.stream()
|
||||
.filter(p -> p.getNom().contentEquals(n.asScalar().value()))
|
||||
.findAny()
|
||||
.orElseGet(() -> new Piece(n.asScalar().value()))
|
||||
.insertObjects(parseHiddenObjects(getSequence(getMapping(getMapping(yjoueurs.value(pn)).value("espacePerso")).value(n))))
|
||||
.insertObjects(parseHiddenObjects(yjoueurs.yamlMapping(pn).yamlMapping("espacePerso").yamlSequence(n)))
|
||||
|
||||
);
|
||||
else
|
||||
((persespacesPersos.get(pname).isEmpty())?
|
||||
Stream.of(new Piece(String.format("Espace de %s",pname))):persespacesPersos.get(pname).stream())
|
||||
.forEach(p -> p.insertObjects(parseHiddenObjects(getSequence(getMapping(yjoueurs.value(pn)).value("espacePerso")))));
|
||||
.forEach(p -> p.insertObjects(parseHiddenObjects(yjoueurs.yamlMapping(pn).yamlSequence("espacePerso"))));
|
||||
|
||||
|
||||
}
|
||||
@ -137,8 +138,8 @@ public class GameCreator {
|
||||
persActions.get(p),
|
||||
status.stream().filter(st -> sequenceStream(ystatus)
|
||||
.filter(n -> n instanceof YamlMapping)
|
||||
.filter(n -> getMapping(n).string(st.getName())!=null)
|
||||
.filter(n -> getMapping(getMapping(n).value(st.getName())).string("onStart").contentEquals("true"))
|
||||
.filter(n -> n.asMapping().string(st.getName())!=null)
|
||||
.filter(n -> (n.asMapping().yamlMapping(st.getName())).string("onStart").contentEquals("true"))
|
||||
.findAny().isPresent())
|
||||
.collect(Collectors.toSet()),
|
||||
persespacesPersos.get(p)
|
||||
@ -203,50 +204,50 @@ public class GameCreator {
|
||||
YamlInput input = Yaml.createYamlInput(f);
|
||||
YamlMapping globalMap = input.readYamlMapping();
|
||||
|
||||
YamlSequence ystatus = getSequence(globalMap.value("status"));
|
||||
YamlSequence ystatus = globalMap.yamlSequence("status");
|
||||
Set<Status> status = sequenceStream(ystatus).map(n -> new Status(n.asScalar().value())).collect(Collectors.toSet());
|
||||
|
||||
|
||||
YamlMapping yespaces = getMapping(globalMap.value("pieces"));
|
||||
YamlMapping yespaces = globalMap.yamlMapping("pieces");
|
||||
Map<String, Map<Objet, Integer>> objetsDansEspaces = yespaces.keys().stream().collect(Collectors.toMap(
|
||||
n -> watch(watch(n).asScalar().value()),
|
||||
n-> parseHiddenObjects(getSequence(yespaces.value(n)))
|
||||
n-> parseHiddenObjects(yespaces.yamlSequence(n))
|
||||
));
|
||||
Set<Piece> espaceObjets = yespaces.keys().stream()
|
||||
.map(n -> new Piece(n.asScalar().value(), objetsDansEspaces.get(n.asScalar().value())))
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
|
||||
YamlMapping yjoueurs = getMapping(globalMap.value("personnages"));
|
||||
YamlMapping yjoueurs = globalMap.yamlMapping("personnages");
|
||||
|
||||
Set<Personnage> personnages = new HashSet<Personnage>();
|
||||
for(YamlNode pn : yjoueurs.keys()) {
|
||||
String pname = pn.asScalar().value();
|
||||
|
||||
Set<Action> actions = getMapping(getMapping(yjoueurs.value(pn)).value("actions")).keys()
|
||||
Set<Action> actions = yjoueurs.yamlMapping(pn).yamlMapping("actions").keys()
|
||||
.stream()
|
||||
.map(n -> new Action(n.asScalar().value(), Long.parseLong(getMapping(getMapping(getMapping(yjoueurs.value(pn)).value("actions")).value(n)).string("basetime")),
|
||||
Long.parseLong(getMapping(getMapping(getMapping(yjoueurs.value(pn)).value("actions")).value(n)).string("triggerTime"))))
|
||||
.map(n -> new Action(n.asScalar().value(), Long.parseLong(yjoueurs.yamlMapping(pn).yamlMapping("actions").yamlMapping(n).string("basetime")),
|
||||
Long.parseLong(yjoueurs.yamlMapping(pn).yamlMapping("actions").yamlMapping(n).string("triggerTime"))))
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
Set<Objet> inventaire =
|
||||
StreamSupport.stream(Spliterators.spliteratorUnknownSize(getSequence(getMapping(yjoueurs.value(pn)).value("inventaire")).iterator(),Spliterator.ORDERED),false)
|
||||
StreamSupport.stream(Spliterators.spliteratorUnknownSize(yjoueurs.yamlMapping(pn).yamlSequence("inventaire").iterator(),Spliterator.ORDERED),false)
|
||||
.map(n ->n.asScalar().value())
|
||||
.map(o ->new Objet(o))
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
Set<Piece> espacesPerso = getMapping(getMapping(yjoueurs.value(pn)).value("espacePerso")).keys().stream().map(n ->
|
||||
new Piece(n.asScalar().value(), parseHiddenObjects(getSequence(getMapping(getMapping(yjoueurs.value(pn)).value("espacePerso")).value(n))))).collect(Collectors.toSet());
|
||||
Set<Piece> espacesPerso = yjoueurs.yamlMapping(pn).yamlMapping("espacePerso").keys().stream().map(n ->
|
||||
new Piece(n.asScalar().value(), parseHiddenObjects(yjoueurs.yamlMapping(pn).yamlMapping("espacePerso").yamlSequence(n)))).collect(Collectors.toSet());
|
||||
|
||||
Set<Status> persoStatus = status.stream().filter(
|
||||
s -> !isSequence(getMapping(yjoueurs.value(pn)).value("status"))?false:getSequence(getMapping(yjoueurs.value(pn)).value("status")).values().stream().anyMatch(n -> n.asScalar().value().equals(s.getName()))
|
||||
s -> (yjoueurs.yamlMapping(pn).value("status").type()!=Node.SEQUENCE)?false:yjoueurs.yamlMapping(pn).yamlSequence("status").values().stream().anyMatch(n -> n.asScalar().value().equals(s.getName()))
|
||||
).collect(Collectors.toSet());
|
||||
|
||||
|
||||
personnages.add(new Personnage(pname, inventaire, actions, persoStatus, espacesPerso));
|
||||
}
|
||||
|
||||
YamlMapping minelsMap = getMapping(globalMap.value("minels"));
|
||||
YamlMapping minelsMap = globalMap.yamlMapping("minels");
|
||||
|
||||
return new QuicksavedPartie(new Partie(personnages, status, espaceObjets),minelsMap);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user