Skip to content

Commit

Permalink
Word Format
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamesuta committed Nov 1, 2020
1 parent 9d92322 commit a98e946
Show file tree
Hide file tree
Showing 6 changed files with 1,712 additions and 1,669 deletions.
24 changes: 20 additions & 4 deletions src/main/java/net/teamfruit/sushida/Sushida.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.teamfruit.sushida;

import com.google.common.collect.ImmutableMap;
import net.teamfruit.sushida.belowname.BelowNameManager;
import net.teamfruit.sushida.data.ConversionTableLoader;
import net.teamfruit.sushida.data.Word;
Expand All @@ -10,9 +11,13 @@
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

Expand All @@ -36,11 +41,22 @@ public void onEnable() {
String hash = getConfig().getString("resourcepack.hash");
resourcePack = new ResourcePack(url, hash);

saveResource("wordset/word.yml", false);
File folder = new File(getDataFolder(), "wordset");
Map<String, Word> wordset = Arrays.stream(Optional.ofNullable(folder.listFiles()).orElseGet(() -> new File[0]))
.collect(Collectors.toMap(e -> StringUtils.substringBeforeLast(e.getName(), ".yml"),
e -> Word.load(getResource("wordset/" + e.getName()))));
if (!new File(folder, "word.yml").exists())
saveResource("wordset/word.yml", false);
Map<String, Word> wordset = Arrays.stream(Optional.ofNullable(folder.listFiles())
.orElseGet(() -> new File[0]))
.collect(Collectors.toMap(
e -> StringUtils.substringBeforeLast(e.getName(), ".yml"),
e -> {
try (InputStream in = new FileInputStream(e)) {
return Word.load(in);
} catch (Exception ex) {
Sushida.logger.log(Level.SEVERE, String.format("Failed to load word [%s]", e.getName()), ex);
}
return new Word(ImmutableMap.of());
}
));
logic = new GameLogic.GameLogicBuilder()
.romaji(ConversionTableLoader.createFromStream(getResource("romaji.csv")))
.word(wordset)
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/net/teamfruit/sushida/data/Word.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
package net.teamfruit.sushida.data;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public class Word {
public final ImmutableMap<String, Map<String, String>> mappings;
public final ImmutableMap<String, ImmutableList<ImmutableList<Map.Entry<String, String>>>> mappings;

public Word(ImmutableMap<String, Map<String, String>> mappings) {
public Word(ImmutableMap<String, ImmutableList<ImmutableList<Map.Entry<String, String>>>> mappings) {
this.mappings = mappings;
}

@SuppressWarnings("unchecked")
public static Word load(InputStream input) {
try {
Yaml cfg = new Yaml(new CustomClassLoaderConstructor(Word.class.getClassLoader()));
WordData data = cfg.loadAs(new InputStreamReader(input, Charsets.UTF_8), WordData.class);
return new Word(ImmutableMap.copyOf(data.word));
return new Word(data.word.entrySet().stream()
.collect(ImmutableMap.toImmutableMap(
Map.Entry::getKey,
e -> e.getValue().stream()
.map(f -> ((f instanceof List)
? ((List<Map<String, String>>) f).stream()
: Stream.of((Map<String, String>) f)
)
.map(g -> g.entrySet().stream().findFirst().get())
.collect(ImmutableList.toImmutableList())
)
.collect(ImmutableList.toImmutableList())
))
);
} catch (Exception e) {
throw new RuntimeException("Word load error", e);
}
}

public static class WordData {
public Map<String, Map<String, String>> word;
public Map<String, List<Object>> word;
}
}
9 changes: 6 additions & 3 deletions src/main/java/net/teamfruit/sushida/mode/TimeAttackMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.CharMatcher;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.teamfruit.sushida.player.Group;
Expand Down Expand Up @@ -174,7 +175,7 @@ public Iterator<Consumer<StateContainer>> getResultMessageTasks() {
public ImmutableList<Map.Entry<String, String>> getWords(Group group) {
int setting = getSetting(SettingCount);
List<Integer> splits = CustomCollectors.splitInt(setting, group.getWord().mappings.size());
List<Map.Entry<String, Map<String, String>>> wordRequiredListByLevel = group.getWord().mappings.entrySet().stream()
List<Map.Entry<String, ImmutableList<ImmutableList<Map.Entry<String, String>>>>> wordRequiredListByLevel = group.getWord().mappings.entrySet().stream()
.sorted((a, b) -> {
int ai = NumberUtils.toInt(CharMatcher.inRange('0', '9').retainFrom(a.getKey()), -1);
int bi = NumberUtils.toInt(CharMatcher.inRange('0', '9').retainFrom(b.getKey()), -1);
Expand All @@ -183,9 +184,11 @@ public ImmutableList<Map.Entry<String, String>> getWords(Group group) {
.collect(Collectors.toList());
return IntStream.range(0, splits.size())
.mapToObj(i ->
wordRequiredListByLevel.get(i).getValue().entrySet().stream()
wordRequiredListByLevel.get(i).getValue().stream()
.collect(CustomCollectors.toRandomPickList(splits.get(i)))
).flatMap(Collection::stream)
)
.flatMap(Collection::stream)
.flatMap(Collection::stream)
.collect(ImmutableList.toImmutableList());
}
}
8 changes: 5 additions & 3 deletions src/main/java/net/teamfruit/sushida/mode/TimeLimitMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.CharMatcher;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.teamfruit.sushida.player.Group;
Expand All @@ -17,7 +18,7 @@
import java.util.stream.Collectors;

public class TimeLimitMode implements GameMode {
public static final GameSettingType SettingTime = new GameSettingType("time", "制限時間", "ゲームの制限時間", 60, Arrays.asList(60, 90, 120));
public static final GameSettingType SettingTime = new GameSettingType("time", "制限時間", "ゲームの制限時間", 120, Arrays.asList(60, 90, 120));
public static final GameSettingType SettingLevel = new GameSettingType("level", "コース", "1:お手軽コース(2~7文字), 2:おすすめコース(5~10文字), 3:高級コース(9~14文字以上), 0:すべて", 0, Arrays.asList(0, 1, 2, 3));

private final Map<GameSettingType, Integer> settings = new HashMap<>();
Expand Down Expand Up @@ -180,7 +181,7 @@ public Iterator<Consumer<StateContainer>> getResultMessageTasks() {
@Override
public ImmutableList<Map.Entry<String, String>> getWords(Group group) {
int level = getSetting(SettingLevel);
List<Map.Entry<String, Map<String, String>>> wordRequiredListByLevel = group.getWord().mappings.entrySet().stream()
List<Map.Entry<String, ImmutableList<ImmutableList<Map.Entry<String, String>>>>> wordRequiredListByLevel = group.getWord().mappings.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toList());
switch (level) {
Expand Down Expand Up @@ -216,8 +217,9 @@ public ImmutableList<Map.Entry<String, String>> getWords(Group group) {
}
}
return wordRequiredListByLevel.stream()
.flatMap(e -> e.getValue().entrySet().stream())
.flatMap(e -> e.getValue().stream())
.collect(CustomCollectors.toShuffledList()).stream()
.flatMap(e -> e.stream())
.collect(ImmutableList.toImmutableList());
}
}
7 changes: 6 additions & 1 deletion src/main/java/net/teamfruit/sushida/player/PlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import net.teamfruit.sushida.player.state.TitleState;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerResourcePackStatusEvent;
import org.bukkit.scoreboard.Scoreboard;

public class PlayerData {
public Player player;
private StateContainer session;
private Group group;

private boolean resourceLoaded;
public BelowNameManager.NameTagReference entity;

public PlayerData(Player player) {
Expand Down Expand Up @@ -88,7 +90,10 @@ public void create() {
session.apply(StateContainer.supply(TitleState::new));

// リソースパック
Sushida.resourcePack.apply(player);
if (!resourceLoaded || !player.hasResourcePack()) {
Sushida.resourcePack.apply(player);
resourceLoaded = true;
}

if (!getGroup().getMembers().isEmpty())
joinScoreboard(getGroup().getGroupScoreboard());
Expand Down
Loading

0 comments on commit a98e946

Please sign in to comment.