Skip to content

Commit

Permalink
add kill/assist streaks
Browse files Browse the repository at this point in the history
  • Loading branch information
matsu1213 committed Jul 6, 2024
1 parent 2f20791 commit a4ead53
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 3 deletions.
54 changes: 54 additions & 0 deletions src/main/java/net/azisaba/lgwneo/config/LeonGunWarNeoConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import net.azisaba.lgwneo.sql.MySQLConnectionData;
import org.bukkit.configuration.file.FileConfiguration;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Getter
@RequiredArgsConstructor
public class LeonGunWarNeoConfig {
Expand All @@ -18,6 +22,12 @@ public class LeonGunWarNeoConfig {

private String serverName;

private Map<Integer, Map.Entry<List<String>, List<String>>> streaks;
private Map<Integer, Map.Entry<List<String>, List<String>>> killLevels;
private String removed;

private Map<Integer, Map.Entry<List<String>, List<String>>> assistLevels;

/**
* Configを読み込みます
*
Expand Down Expand Up @@ -54,6 +64,50 @@ public LeonGunWarNeoConfig load() {
mySQLHostname, mySQLPort, mySQLUsername, mySQLPassword, mySQLDatabase);

serverName = conf.getString("server-name");

streaks = new HashMap<>();
conf.getConfigurationSection("streaks").getValues(false).keySet().stream()
.map(Integer::valueOf)
.collect(
Collectors.toMap(
Function.identity(),
count -> {
List<String> messages = conf.getStringList("streaks." + count + ".messages");
List<String> commands = conf.getStringList("streaks." + count + ".commands");
return new AbstractMap.SimpleEntry<>(messages, commands);
}))
.forEach(streaks::put);
streaks = Collections.unmodifiableMap(streaks);

killLevels = new HashMap<>();
conf.getConfigurationSection("levels").getValues(false).keySet().stream()
.map(Integer::valueOf)
.collect(
Collectors.toMap(
Function.identity(),
count -> {
List<String> messages = conf.getStringList("killLevels." + count + ".messages");
List<String> commands = conf.getStringList("killLevels." + count + ".commands");
return new AbstractMap.SimpleEntry<>(messages, commands);
}))
.forEach(killLevels::put);
killLevels = Collections.unmodifiableMap(killLevels);

removed = conf.getString("removed");

assistLevels = new HashMap<>();
conf.getConfigurationSection("levels").getValues(false).keySet().stream()
.map(Integer::valueOf)
.collect(
Collectors.toMap(
Function.identity(),
count -> {
List<String> messages = conf.getStringList("assistLevels." + count + ".messages");
List<String> commands = conf.getStringList("assistLevels." + count + ".commands");
return new AbstractMap.SimpleEntry<>(messages, commands);
}))
.forEach(assistLevels::put);
assistLevels = Collections.unmodifiableMap(assistLevels);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void onKill(PlayerDeathEvent e) {
// キル数とデス数をカウントする
if (match.getKillDeathAssistCounter() != null) {
match.getKillDeathAssistCounter().addKills(killer.getUniqueId());
match.getKillDeathAssistCounter().addDeaths(e.getEntity().getUniqueId());
match.getKillDeathAssistCounter().addDeaths(e.getEntity().getUniqueId(), killer.getUniqueId());
}
}

Expand Down
55 changes: 55 additions & 0 deletions src/main/java/net/azisaba/lgwneo/match/AssistStreaks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package net.azisaba.lgwneo.match;

import lombok.RequiredArgsConstructor;
import net.azisaba.lgwneo.LeonGunWarNeo;
import net.azisaba.lgwneo.match.mode.Match;
import net.azisaba.lgwneo.util.Chat;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

@RequiredArgsConstructor
public class AssistStreaks {

private final LeonGunWarNeo plugin;
private final Map<UUID, AtomicInteger> streaksMap = new HashMap<>();

public void removedBy(UUID uuid) {
streaksMap.remove(uuid);
}

public AtomicInteger get(UUID uuid) {
streaksMap.putIfAbsent(uuid, new AtomicInteger(0));
return streaksMap.get(uuid);
}

public void add(UUID uuid) {
// カウントを追加
int streaks = get(uuid).incrementAndGet();
Player player = Bukkit.getPlayer(uuid);

// 報酬を付与
plugin.getLeonGunWarNeoConfig().getAssistLevels().entrySet().stream()
.filter(entry -> streaks % entry.getKey() == 0)
.map(Map.Entry::getValue)
.map(Map.Entry::getValue)
.flatMap(List::stream)
.map(command -> Chat.f(command, player.getName()))
.forEach(command -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command));
Match match = plugin.getMatchOrganizer().getMatchFromPlayer(player);
// アシストストリークをお知らせ
plugin.getLeonGunWarNeoConfig().getAssistLevels().entrySet().stream()
.filter(entry -> streaks % entry.getKey() == 0)
.map(Map.Entry::getValue)
.map(Map.Entry::getKey)
.flatMap(List::stream)
.map(message -> Chat.f(message, LeonGunWarNeo.getChatPrefix(), player.getDisplayName()))
.forEach(match::broadcastMessage);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.RequiredArgsConstructor;
import net.azisaba.lgwneo.match.mode.Match;

/**
* プレイヤーの1試合でのキル数デス数アシスト数を記録するクラス
Expand All @@ -14,6 +15,8 @@
@RequiredArgsConstructor
public class KillDeathAssistCounter {

private final Match match;

private final HashMap<UUID, AtomicInteger> kills = new HashMap<>();
private final HashMap<UUID, AtomicInteger> deaths = new HashMap<>();
private final HashMap<UUID, AtomicInteger> assists = new HashMap<>();
Expand All @@ -27,6 +30,7 @@ public class KillDeathAssistCounter {
*/
public void addKills(UUID uuid) {
kills.computeIfAbsent(uuid, k -> new AtomicInteger()).incrementAndGet();
match.getKillStreaks().add(uuid);
}

/**
Expand Down Expand Up @@ -62,8 +66,10 @@ public void addKills(UUID uuid, UUID victim) {
*
* @param uuid デス数を追加するプレイヤーのUUID
*/
public void addDeaths(UUID uuid) {
public void addDeaths(UUID uuid, UUID killer) {
deaths.computeIfAbsent(uuid, k -> new AtomicInteger()).incrementAndGet();
match.getKillStreaks().removedBy(uuid, killer);
match.getAssistStreaks().removedBy(uuid);
}

/**
Expand All @@ -73,6 +79,7 @@ public void addDeaths(UUID uuid) {
*/
public void addAssists(UUID uuid) {
assists.computeIfAbsent(uuid, k -> new AtomicInteger()).incrementAndGet();
match.getAssistStreaks().add(uuid);
}

/**
Expand Down
104 changes: 104 additions & 0 deletions src/main/java/net/azisaba/lgwneo/match/KillStreaks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package net.azisaba.lgwneo.match;

import lombok.RequiredArgsConstructor;
import net.azisaba.lgwneo.LeonGunWarNeo;
import net.azisaba.lgwneo.match.mode.LeaderDeathMatch;
import net.azisaba.lgwneo.match.mode.Match;
import net.azisaba.lgwneo.util.Chat;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

@RequiredArgsConstructor
public class KillStreaks {

private final LeonGunWarNeo plugin;

private final Map<UUID, AtomicInteger> streaksMap = new HashMap<>();

public void removedBy(UUID uuid, UUID killerU) {
Player player = Bukkit.getPlayer(uuid);
Player killer = Bukkit.getPlayer(killerU);
int streaks = get(uuid).get();
int minStreaks =
plugin.getLeonGunWarNeoConfig().getStreaks().entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.map(Map.Entry::getKey)
.findFirst()
.orElse(-1);

if (killer != null && streaks >= minStreaks) {
Match match = plugin.getMatchOrganizer().getMatchFromPlayer(uuid);
match.broadcastMessage(
Chat.f(
plugin.getLeonGunWarNeoConfig().getRemoved(),
LeonGunWarNeo.getChatPrefix(),
killer.getDisplayName(),
player.getDisplayName()
)
);
}

streaksMap.remove(player.getUniqueId());
}

public AtomicInteger get(UUID uuid) {
streaksMap.putIfAbsent(uuid, new AtomicInteger(0));
return streaksMap.get(uuid);
}

private void giveRewards(int streaks, UUID uuid) {
Player player = Bukkit.getPlayer(uuid);
plugin.getLeonGunWarNeoConfig().getStreaks().entrySet().stream()
.filter(entry -> streaks == entry.getKey())
.map(Map.Entry::getValue)
.map(Map.Entry::getValue)
.flatMap(List::stream)
.map(command -> Chat.f(command, player.getName()))
.forEach(command -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command));
plugin.getLeonGunWarNeoConfig().getKillLevels().entrySet().stream()
.filter(entry -> streaks % entry.getKey() == 0)
.map(Map.Entry::getValue)
.map(Map.Entry::getValue)
.flatMap(List::stream)
.map(command -> Chat.f(command, player.getName()))
.forEach(command -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command));
}

public void add(UUID uuid) {
// カウントを追加
int streaks = get(uuid).incrementAndGet();
Match match = plugin.getMatchOrganizer().getMatchFromPlayer(uuid);
Player player = Bukkit.getPlayer(uuid);
// 報酬を付与
giveRewards(streaks, uuid);
if (match instanceof LeaderDeathMatch) {
if (((LeaderDeathMatch)match).getTeamLeaderMap().containsValue(uuid)) {
giveRewards(streaks, uuid);
player.sendMessage(Chat.f("{0}&7あなたはリーダーなので &e2倍 &7の報酬を受け取りました!", LeonGunWarNeo.getChatPrefix()));
}
}

// キルストリークをお知らせ
plugin.getLeonGunWarNeoConfig().getStreaks().entrySet().stream()
.filter(entry -> streaks == entry.getKey())
.map(Map.Entry::getValue)
.map(Map.Entry::getKey)
.flatMap(List::stream)
.map(message -> Chat.f(message, LeonGunWarNeo.getChatPrefix(), player.getDisplayName()))
.forEach(match::broadcastMessage);
plugin.getLeonGunWarNeoConfig().getKillLevels().entrySet().stream()
.filter(entry -> streaks % entry.getKey() == 0)
.map(Map.Entry::getValue)
.map(Map.Entry::getKey)
.flatMap(List::stream)
.map(message -> Chat.f(message, LeonGunWarNeo.getChatPrefix(), player.getDisplayName()))
.forEach(match::broadcastMessage);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import lombok.Setter;
import net.azisaba.lgwneo.LeonGunWarNeo;
import net.azisaba.lgwneo.animation.MatchResultAnimation;
import net.azisaba.lgwneo.match.AssistStreaks;
import net.azisaba.lgwneo.match.KillDeathAssistCounter;
import net.azisaba.lgwneo.match.KillStreaks;
import net.azisaba.lgwneo.match.component.MatchResult;
import net.azisaba.lgwneo.match.component.MatchStatus;
import net.azisaba.lgwneo.match.component.MatchTeam;
Expand Down Expand Up @@ -72,6 +74,7 @@ public class LeaderDeathMatch implements Match {
private final HashMap<MatchTeam, Set<Player>> teamPlayerMap = new HashMap<>();
private final HashMap<MatchTeam, Set<UUID>> offlineTeamPlayerMap = new HashMap<>();
private final Set<Party> queueingParties = new HashSet<>();
@Getter
private final HashMap<MatchTeam, UUID> teamLeaderMap = new HashMap<>();

@Getter
Expand All @@ -80,7 +83,7 @@ public class LeaderDeathMatch implements Match {
private final FlexibleCountdownTask matchCountdownTask = new FlexibleCountdownTask(600);

@Getter
private final KillDeathAssistCounter killDeathAssistCounter = new KillDeathAssistCounter();
private final KillDeathAssistCounter killDeathAssistCounter = new KillDeathAssistCounter(this);

@Getter
private final HashMap<MatchTeam, AtomicInteger> scoreMap = new HashMap<>();
Expand All @@ -99,6 +102,11 @@ public class LeaderDeathMatch implements Match {

private final HashMap<MatchTeam, ItemStack> chestPlateMap = new HashMap<>();

@Getter
private final KillStreaks killStreaks = new KillStreaks(plugin);
@Getter
private final AssistStreaks assistStreaks = new AssistStreaks(plugin);

@Override
public Map<String, Object> getMatchInformationAsMap() {
HashMap<String, Object> data = new HashMap<>();
Expand Down Expand Up @@ -367,6 +375,11 @@ public Location getRespawnLocationFor(Player player) {
return null;
}

@Override
public void broadcastMessage(String message){
this.getParticipatePlayers().forEach(Player::sendMessage);
}

/**
* 試合でのプレイヤーのチームを取得する
*
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/azisaba/lgwneo/match/mode/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

import net.azisaba.lgwneo.match.AssistStreaks;
import net.azisaba.lgwneo.match.KillDeathAssistCounter;
import net.azisaba.lgwneo.match.KillStreaks;
import net.azisaba.lgwneo.match.component.MatchStatus;
import net.azisaba.lgwneo.party.Party;
import net.azisaba.lgwneo.world.map.MatchMap;
Expand Down Expand Up @@ -128,4 +131,9 @@ public interface Match {
* @return 試合で使用しているKillDeathAssistCounterインスタンス
*/
KillDeathAssistCounter getKillDeathAssistCounter();

void broadcastMessage(String message);

KillStreaks getKillStreaks();
AssistStreaks getAssistStreaks();
}

0 comments on commit a4ead53

Please sign in to comment.