|
| 1 | +package ru.base.game.server.dto.chess; |
| 2 | + |
| 3 | +import com.google.common.io.CharStreams; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.io.InputStreamReader; |
| 8 | +import java.io.Reader; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Optional; |
| 15 | +import java.util.regex.Matcher; |
| 16 | +import java.util.regex.Pattern; |
| 17 | + |
| 18 | +public interface PortableGameNotation { |
| 19 | + |
| 20 | + enum Kind { |
| 21 | + KING, ROOK, BISHOP, KNIGHT, PAWN |
| 22 | + } |
| 23 | + |
| 24 | + enum Color { |
| 25 | + WHITE, BLACK |
| 26 | + } |
| 27 | + |
| 28 | + interface Element { |
| 29 | + } |
| 30 | + |
| 31 | + sealed interface Metadata extends Element { |
| 32 | + record Value(String name, String text) implements Metadata { |
| 33 | + |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + sealed interface Move extends Element { |
| 38 | + enum Type { |
| 39 | + SIMPLE, CASTLING |
| 40 | + } |
| 41 | + |
| 42 | + record Line(int index, Step white, Step black) implements Move { |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + record Step(Kind kind, |
| 47 | + Color color, |
| 48 | + Optional<Position> from, |
| 49 | + Optional<Position> to, |
| 50 | + boolean capturing, |
| 51 | + boolean checking, |
| 52 | + Type type) { |
| 53 | + |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + record Position(int x, int y) { |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + final class Default implements PortableGameNotation { |
| 62 | + private static final Pattern GRU = Pattern.compile( |
| 63 | + "(\\[(\\w+)\\s+\\\"(.*)\\\"\\]|\\d+\\.((([BQKNR]?([a-h1-8]?)x?[a-h][1-8]\\+?)|O-O|O-O-O)\\s(\\{(.*)\\}\\s)?){2})"); |
| 64 | + private final Map<String, Metadata> metadata = new HashMap<>(); |
| 65 | + private final List<Move> moves = new ArrayList<>(); |
| 66 | + |
| 67 | + private Default(Reader reader) throws IOException { |
| 68 | + this(CharStreams.toString(reader)); |
| 69 | + } |
| 70 | + |
| 71 | + private Default(String text) throws IOException { |
| 72 | + parse(text); |
| 73 | + } |
| 74 | + |
| 75 | + public Default(InputStream stream) throws IOException { |
| 76 | + this(new InputStreamReader(stream, StandardCharsets.UTF_8)); |
| 77 | + } |
| 78 | + |
| 79 | + private void parse(String text) { |
| 80 | + metadata.clear(); |
| 81 | + moves.clear(); |
| 82 | + Matcher matcher = GRU.matcher(text); |
| 83 | + matcher.results().forEach(result -> { |
| 84 | + int grouped = result.groupCount(); |
| 85 | + if (grouped > 0) { |
| 86 | + String value = result.group(4); |
| 87 | + if (value == null) { |
| 88 | + metadata.put(result.group(2), new Metadata.Value(result.group(2), result.group(3))); |
| 89 | + } else { |
| 90 | + System.out.println("Found group '" + grouped + "'"); |
| 91 | + for (int i = 0; i < grouped; i++) { |
| 92 | + System.out.printf("%d. %s%n", i, result.group(i)); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments