From 4f1446e3a5902066499779d767aea9bbe740e1c1 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sat, 21 Jul 2018 17:57:44 +0200 Subject: [PATCH] Ajout dans StringUtil et RandomUtil --- .../pandacube/java/external_tools/Main.java | 121 ++++++++++++++++++ .../fr/pandacube/java/util/RandomUtil.java | 9 ++ .../fr/pandacube/java/util/StringUtil.java | 10 ++ 3 files changed, 140 insertions(+) create mode 100644 src/main/java/fr/pandacube/java/external_tools/Main.java diff --git a/src/main/java/fr/pandacube/java/external_tools/Main.java b/src/main/java/fr/pandacube/java/external_tools/Main.java new file mode 100644 index 0000000..ea3e0bc --- /dev/null +++ b/src/main/java/fr/pandacube/java/external_tools/Main.java @@ -0,0 +1,121 @@ +package fr.pandacube.java.external_tools; + +import java.io.FileReader; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +public class Main { + + + static class Row { + int rowid; + String date, user, uuid; + int wid, x, y, z, type, amount, action; + boolean rolled_back; + } + + + + static class Chest { + final int wid, x, y, z, hashCode; + List sources = new ArrayList<>(); + int actualCount = 0; + boolean alreadyRolledBack = false; + public Chest(int w, int wx, int wy, int wz) { + wid = w; + x = wx; + y = wy; + z = wz; + hashCode = Objects.hash(wid, x, y, z); + } + + @Override + public boolean equals(Object o) { + if (o == null || !(o instanceof Chest)) + return false; + Chest c = (Chest) o; + return wid == c.wid && x == c.x && y == c.y && z == c.z; + } + + @Override + public int hashCode() { + return hashCode; + } + + @Override + public String toString() { + return "world=" + wid + " - /tppos " + x + " " + y + " " + z + " - rolledBack=" + alreadyRolledBack + " - amount=" + actualCount; + } + + public String toCSVString() { + return wid + "," + x + "," + y + "," + z + "," + alreadyRolledBack + "," + actualCount; + } + } + + + + + public static void main(String args[]) throws Exception { + + /*CSVParser parser = new CSVParser(new FileReader("co_survie_container.csv"), CSVFormat.DEFAULT); + + + List rows = StreamSupport.stream(parser.spliterator(), false).map(r -> { + Row ret = new Row(); + ret.rowid = Integer.parseInt(r.get(0)); + ret.date = r.get(1); + ret.user = r.get(2); + ret.uuid = r.get(3); + ret.wid = Integer.parseInt(r.get(4)); + ret.x = Integer.parseInt(r.get(5)); + ret.y = Integer.parseInt(r.get(6)); + ret.z = Integer.parseInt(r.get(7)); + ret.type = Integer.parseInt(r.get(8)); + ret.amount = Integer.parseInt(r.get(9)); + ret.action = Integer.parseInt(r.get(10)); + ret.rolled_back = r.get(11).equals("1"); + return ret; + }).collect(Collectors.toList()); + + rows.sort(Comparator.comparingInt(r -> r.rowid)); + + List chests = new ArrayList<>(); + + + + for (Row row : rows) { + Chest c = new Chest(row.wid, row.x, row.y, row.z); + int idx = chests.indexOf(c); + c = (idx >= 0) ? chests.get(idx) : c; + if (idx < 0) { + chests.add(c); + idx = chests.size() - 1; + } + + c.sources.add(row); + + if (row.rolled_back) + c.alreadyRolledBack = true; + + if (!c.alreadyRolledBack) { + c.actualCount += (row.action == 1) ? row.amount : -row.amount; + if (c.actualCount == 0) { + chests.remove(idx); + } + } + + } + + + + chests.sort(Comparator.comparingInt(c -> c.actualCount)); + + chests.forEach(c -> System.out.println(c.toCSVString())); + */ + + } +} \ No newline at end of file diff --git a/src/main/java/fr/pandacube/java/util/RandomUtil.java b/src/main/java/fr/pandacube/java/util/RandomUtil.java index cc0bd1d..a53d1bb 100644 --- a/src/main/java/fr/pandacube/java/util/RandomUtil.java +++ b/src/main/java/fr/pandacube/java/util/RandomUtil.java @@ -1,5 +1,6 @@ package fr.pandacube.java.util; +import java.util.List; import java.util.Random; public class RandomUtil { @@ -13,5 +14,13 @@ public class RandomUtil { public static double nextDoubleBetween(double minInclu, double maxExclu) { return rand.nextDouble() * (maxExclu - minInclu) + minInclu; } + + public static T arrayElement(T[] arr) { + return arr[rand.nextInt(arr.length)]; + } + + public static T listElement(List arr) { + return arr.get(rand.nextInt(arr.size())); + } } diff --git a/src/main/java/fr/pandacube/java/util/StringUtil.java b/src/main/java/fr/pandacube/java/util/StringUtil.java index 665c56b..c599f0b 100644 --- a/src/main/java/fr/pandacube/java/util/StringUtil.java +++ b/src/main/java/fr/pandacube/java/util/StringUtil.java @@ -1,5 +1,7 @@ package fr.pandacube.java.util; +import java.util.List; + public class StringUtil { public static String formatDouble(double d) { if (d == (long) d) @@ -19,4 +21,12 @@ public class StringUtil { if (c == c_match) count++; return count; } + + + + + public static String joinGrammatically(CharSequence sep1, CharSequence sepFinal, List strings) { + int size = strings == null ? 0 : strings.size(); + return size == 0 ? "" : size == 1 ? strings.get(0) : String.join(sep1, strings.subList(0, --size)) + sepFinal + strings.get(size); + } }