Skip to content

Commit

Permalink
Merge pull request #68 from pehala/add_categories_to_track
Browse files Browse the repository at this point in the history
Add Categories into track files
  • Loading branch information
PhilippvK authored Nov 4, 2020
2 parents 22b5355 + 5a93f51 commit c03ce5d
Show file tree
Hide file tree
Showing 5,853 changed files with 17,518 additions and 33,841 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
25 changes: 1 addition & 24 deletions client/client.iml
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,8 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:3.5.10" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy:1.10.13" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy-agent:1.10.13" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:3.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-junit-jupiter:3.5.10" level="project" />
<orderEntry type="library" name="Maven: info.picocli:picocli:4.5.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:3.5.10" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy:1.10.13" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy-agent:1.10.13" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:3.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-junit-jupiter:3.5.10" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty:3.10.6.Final" level="project" />
<orderEntry type="library" name="Maven: info.picocli:picocli:4.5.1" level="project" />
<orderEntry type="library" name="Maven: info.picocli:picocli:4.5.2" level="project" />
<orderEntry type="module" module-name="shared" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
Expand Down
21 changes: 7 additions & 14 deletions client/src/main/java/org/moparforia/client/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public class Launcher implements Callable<Void> {
private int port;

@CommandLine.Option(names = {"--lang", "-l"},
description = "Sets language of the game, available values:\n ${COMPLETION-CANDIDATES}")
private Language lang = Language.EN_US;
description = "Sets language of the game, available values:\n ${COMPLETION-CANDIDATES}",
defaultValue = "en_us")
private Language lang;

@CommandLine.Option(names = {"--verbose", "-v"}, description = "Set if you want verbose information")
private static boolean verbose = false;
Expand All @@ -48,19 +49,11 @@ public static boolean isUsingCustomServer() {
return true;//instance.serverBox.isSelected();
}

public static void main(String... args) throws Exception {
public static void main(String... args) {
Launcher launcher = new Launcher();
try {
CommandLine.ParseResult parseResult = new CommandLine(launcher)
.setCaseInsensitiveEnumValuesAllowed(true)
.parseArgs(args);
if (!CommandLine.printHelpIfRequested(parseResult)) {
launcher.call();
}
} catch (CommandLine.ParameterException ex) { // command line arguments could not be parsed
System.err.println(ex.getMessage());
ex.getCommandLine().usage(System.err);
}
new CommandLine(launcher)
.setCaseInsensitiveEnumValuesAllowed(true)
.execute(args);
}

public boolean showSettingDialog(JFrame frame, String server, int port) throws ParseException {
Expand Down
24 changes: 15 additions & 9 deletions client/src/test/java/org/moparforia/client/LauncherCLITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,25 @@ class LauncherCLITest {
private StringWriter stdErr;

@BeforeEach
void setUp() throws ParseException, IOException {
void setUp() throws Exception {
// Mock game
launcher = spy(new Launcher());
lenient().doReturn(mock(Game.class)).when(launcher).launchGame(any(JFrame.class), anyString(), anyInt(), any(), anyBoolean());
launcher = mock(Launcher.class, withSettings()
.lenient()
.withoutAnnotations());

// Mock creating JFrame
lenient().doReturn(mock(JFrame.class)).when(launcher).createFrame();
// Use real methods
doCallRealMethod().when(launcher).call();
doCallRealMethod().when(launcher).setPort(anyInt());
doCallRealMethod().when(launcher).setHostname(anyString());

// Mock settings dialog
lenient().doAnswer((invocaton) -> {
doReturn(mock(JFrame.class)).when(launcher).createFrame();
doAnswer((invocaton) -> {
launcher.setPort(invocaton.getArgument(2));
launcher.setHostname(invocaton.getArgument(1));
return true;
}).when(launcher).showSettingDialog(any(JFrame.class), anyString(), anyInt());


cmd = new CommandLine(launcher).setCaseInsensitiveEnumValuesAllowed(true);

stdOut = new StringWriter();
Expand Down Expand Up @@ -117,10 +121,12 @@ void testOnlyHostname() {
@Test
void testDefaultValues() {
assertEquals(0, cmd.execute());
verify(launcher).launchGame(any(),
verify(launcher).launchGame(
any(),
eq(Launcher.DEFAULT_SERVER),
eq(Launcher.DEFAULT_PORT),
eq(Launcher.Language.EN_US),
eq(false));
eq(false)
);
}
}
41 changes: 41 additions & 0 deletions doc/FileFormat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Track File Format
Tracks use special file format where each line is in a format of `<char> <content>` where `char` is character specifying type of the line and content is the actual data.

## V1 File Format
This format is now outdated and does not contain `TrackCategory`.
To parse this file format you can use, now deprecated, `TrackFileParser`.

```
V 1
A {AUTHOR OF TRACK}
N {NAME OF TRACK}
T {DATA}
I {NUMBER OF PLAYERS TO COMPLETE},{NUMBER OF STROKES},{BEST NUMBER OF STROKES},{NUMBER OF PEOPLE THAT GOT BEST STROKE}
B {FIRST BEST PAR PLAYER},{UNIX TIMESTAMP OF FIRST BEST PAR}
L {LAST BEST PAR PLAYER},{UNIX TIMESTAMP OF LAST BEST PAR}
R {RATING: 0},{RATING: 1},{RATING: 2},{RATING: 3},{RATING: 4},{RATING: 5},{RATING: 6},{RATING: 7},{RATING: 8},{RATING: 9},{RATING: 10}
```
### Directory structure
Since this format does not have `TrackCategory` specified in the actual file it
determines it based on the directory it is located in.
That means all `modern` tracks needs to be in `tracks/modern` directory and so on.
If you want track in multiple categories you need to duplicate the file and put it in multiple directories.

## V2+ File Format
New format that has `TrackCategory` specified inside the file. It can be parsed with `VersionedTrackFileParser`
```
V >=2
A {AUTHOR OF TRACK}
N {NAME OF TRACK}
T {DATA}
C {CATEGORY_ID}, {CATEGORY_Id}, ...
I {NUMBER OF PLAYERS TO COMPLETE},{NUMBER OF STROKES},{BEST NUMBER OF STROKES},{NUMBER OF PEOPLE THAT GOT BEST STROKE}
B {FIRST BEST PAR PLAYER},{UNIX TIMESTAMP OF FIRST BEST PAR}000
L {LAST BEST PAR PLAYER},{UNIX TIMESTAMP OF LAST BEST PAR}000
R {RATING: 0},{RATING: 1},{RATING: 2},{RATING: 3},{RATING: 4},{RATING: 5},{RATING: 6},{RATING: 7},{RATING: 8},{RATING: 9},{RATING: 10}
```

### Directory Structure
All tracks are located in `tracks/tracks` directory and tracksets are in `tracks/sets`


15 changes: 10 additions & 5 deletions editor/src/main/java/org/moparforia/editor/TrackEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.moparforia.shared.tracks.Track;
import org.moparforia.shared.tracks.TrackCategory;
import org.moparforia.shared.tracks.filesystem.FileSystemTrackManager;
import org.moparforia.shared.tracks.filesystem.TrackFileParser;
import org.moparforia.shared.tracks.parsers.VersionedTrackFileParser;

import javax.swing.*;
import javax.swing.border.TitledBorder;
Expand All @@ -20,13 +20,13 @@
import java.io.*;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Scanner;

/**
* @author Johan Ljungberg
* @author fc
*/
public class TrackEditor extends JFrame implements IEditor {
private static final VersionedTrackFileParser parser = new VersionedTrackFileParser();

private SpriteManager spriteManager;
private MapCanvas mapCanvas;
Expand Down Expand Up @@ -189,9 +189,9 @@ private void generateButtons() {
togglePencil.setSelected(true);
}

public static final Track loadTrack(Path path) throws IOException {
public static Track loadTrack(Path path) throws IOException {

return TrackFileParser.parseTrack(path, TrackCategory.UNKNOWN);
return parser.parseTrack(path);
}

private void menuNewActionPerformed(ActionEvent e) {
Expand All @@ -206,7 +206,12 @@ private void menuOpenActionPerformed(ActionEvent e) {
File f = chooser.getSelectedFile();
Track currentTrack = loadTrack(f.toPath());
Map m = new MapDecompressor().decompress(currentTrack.getMap());
mapCanvas.updateProperties(currentTrack.getName(), currentTrack.getCategory().getId());
TrackCategory category = TrackCategory.UNKNOWN;
if (!currentTrack.getCategories().isEmpty()) {
// Oneliner to get one random category from loaded set, editor cannot work wiht multiple categories yet
category = currentTrack.getCategories().stream().reduce((t, u) -> t).orElse(TrackCategory.UNKNOWN);
}
mapCanvas.updateProperties(currentTrack.getName(), category.getId());
mapCanvas.setMap(m);
}
} catch (Exception exp) {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.mainClass>test.mainClass</project.mainClass>
<picocli.version>4.5.1</picocli.version>
<picocli.version>4.5.2</picocli.version>
</properties>
<build>
<pluginManagement>
Expand Down
2 changes: 1 addition & 1 deletion server/server.iml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: io.netty:netty:3.10.6.Final" level="project" />
<orderEntry type="module" module-name="shared" />
<orderEntry type="library" name="Maven: info.picocli:picocli:4.5.1" level="project" />
<orderEntry type="library" name="Maven: info.picocli:picocli:4.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.6.2" level="project" />
Expand Down
23 changes: 23 additions & 0 deletions server/src/main/java/org/moparforia/server/Converter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.moparforia.server;

import org.moparforia.shared.tracks.parsers.TrackConverter;
import picocli.CommandLine;

import java.nio.file.Path;
import java.util.concurrent.Callable;

@CommandLine.Command(name = "convert",
mixinStandardHelpOptions = true,
description = "Converts all tracks in the folder into v2 track file versions",
version = "1.0")
public class Converter implements Callable<Integer> {

@CommandLine.Parameters
private Path rootDir;

@Override
public Integer call() throws Exception {
TrackConverter.convertTracks(rootDir);
return 0;
}
}
47 changes: 25 additions & 22 deletions server/src/main/java/org/moparforia/server/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,41 @@
description = "Starts Minigolf Server",
name = "server",
mixinStandardHelpOptions = true,
versionProvider = ManifestVersionProvider.class
versionProvider = ManifestVersionProvider.class,
subcommands = {
Converter.class
}
)
public class Launcher implements Callable<Void> {
public class Launcher implements Callable<Integer> {

public static final String DEFAULT_HOST = "0.0.0.0";
public static final int DEFAULT_PORT = 4242;

@CommandLine.Option(names = {"--hostname", "-ip"},
description = "Sets server hostname")
private String host = DEFAULT_HOST;

@CommandLine.Option(names = {"--port", "-p"},
description = "Sets server port")
private int port = DEFAULT_PORT;
public static final String DEFAULT_PORT = "4242";

@CommandLine.Option(
names = {"--hostname", "-ip"},
description = "Sets server hostname",
defaultValue = DEFAULT_HOST
)
private String host;

@CommandLine.Option(
names = {"--port", "-p"},
description = "Sets server port",
defaultValue = DEFAULT_PORT
)
private int port;

public static void main(String... args) {
Launcher launcher = new Launcher();
try {
CommandLine.ParseResult parseResult = new CommandLine(launcher).parseArgs(args);
if (!CommandLine.printHelpIfRequested(parseResult)) {
launcher.call();
}
} catch (CommandLine.ParameterException ex) { // command line arguments could not be parsed
System.err.println(ex.getMessage());
ex.getCommandLine().usage(System.err);
}
new CommandLine(launcher)
.setCaseInsensitiveEnumValuesAllowed(true)
.execute(args);
}

@Override
public Void call() {
public Integer call() {
getServer(host, port).start();
return null;
return 0;
}

public Server getServer(String host, int port) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import org.moparforia.shared.Tools;
import org.moparforia.shared.tracks.Track;
import org.moparforia.shared.tracks.TrackManager;
import org.moparforia.shared.tracks.filesystem.FileSystemTrackManager;
import org.moparforia.shared.tracks.filesystem.FileSystemStatsManager;
import org.moparforia.shared.tracks.filesystem.FileSystemTrackManager;
import org.moparforia.shared.tracks.stats.StatsManager;
import org.moparforia.shared.tracks.stats.TrackStats;

Expand Down Expand Up @@ -116,16 +116,17 @@ protected void reset() {
tracks = initTracks();
}


public void startGame() {
writeAll(new Packet(PacketType.DATA, Tools.tabularize("game", "start")));
StringBuilder buff = new StringBuilder();
for (int i = 0; i < getPlayers().size(); i++) {
buff.append("t");
}
playStatus = buff.toString().replace("t", "f");
TrackStats track = statsManager.getStats(tracks.get(0));
TrackStats track = statsManager.getStats(tracks.get(0));
writeAll(new Packet(PacketType.DATA, Tools.tabularize("game", "resetvoteskip")));
writeAll(new Packet(PacketType.DATA, Tools.tabularize("game", "starttrack", buff.toString(), gameId, track)));
writeAll(new Packet(PacketType.DATA, Tools.tabularize("game", "starttrack", buff.toString(), gameId, track.networkSerialize())));
writeAll(new Packet(PacketType.DATA, Tools.tabularize("game", "startturn", 0)));
}

Expand Down Expand Up @@ -210,7 +211,7 @@ protected void nextTrack() {
}
playStatus = buff.toString().replace("t", "f");
writeAll(new Packet(PacketType.DATA, Tools.tabularize("game", "resetvoteskip")));
writeAll(new Packet(PacketType.DATA, Tools.tabularize("game", "starttrack", buff.toString(), gameId, track)));
writeAll(new Packet(PacketType.DATA, Tools.tabularize("game", "starttrack", buff.toString(), gameId, track.networkSerialize())));
writeAll(new Packet(PacketType.DATA, Tools.tabularize("game", "startturn", getFirstPlayer())));
} else {
endGame();
Expand Down
Loading

0 comments on commit c03ce5d

Please sign in to comment.