Skip to content

Commit

Permalink
feature: Game start command functions fully
Browse files Browse the repository at this point in the history
  • Loading branch information
utfunderscore committed Jun 1, 2024
1 parent a25bec1 commit 68f8a3a
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

public class SchematicArenaManager extends ArenaManager {

private static final int BUFFER_SIZE = 5;

private @Getter final WorldCreator worldCreator;
private @Getter final World world;
private static Logger logger = LoggerFactory.getLogger(SchematicArenaManager.class);
Expand Down Expand Up @@ -71,7 +73,7 @@ public SchematicArenaManager(JavaPlugin javaPlugin, MarkerScanner markerScanner,

for (ArenaMeta availableArena : availableArenas) {
ArrayDeque<Arena> arenas = new ArrayDeque<>();
for (int i = 0; i < 1; i++) {
for (int i = 0; i < BUFFER_SIZE; i++) {
Arena load;
try {
load = loadArena(availableArena);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import co.aikar.commands.PaperCommandManager;
import com.readutf.inari.core.arena.ArenaManager;
import com.readutf.inari.core.arena.marker.impl.TileEntityScanner;
import com.readutf.inari.core.arena.meta.ArenaMeta;
import com.readutf.inari.core.arena.selection.impl.WorldEditSelectionManager;
import com.readutf.inari.core.arena.stores.gridworld.GridArenaManager;
import com.readutf.inari.core.arena.stores.schematic.SchematicArenaManager;
Expand Down Expand Up @@ -55,13 +56,14 @@ public void onEnable() {
paperCommandManager.getCommandCompletions().registerCompletion("gameids", new GameCompletions.GameIdCompletion(gameManager));
paperCommandManager.getCommandCompletions().registerCompletion("gameplayers", new GameCompletions.GamePlayersCompletion(gameManager));
paperCommandManager.getCommandCompletions().registerCompletion("gametypes", c -> gameStarterManager.getGameStarters());
paperCommandManager.getCommandCompletions().registerCompletion("arena", c -> arenaManager.findAvailableArenas(arenaMeta -> true).stream().map(ArenaMeta::getName).toList());


Arrays.asList(
new ArenaCommands(this, worldEditSelectionManager, arenaManager),
new DevCommand(gameManager, arenaManager, gameEventManager),
// new DevCommand(gameManager, arenaManager, gameEventManager),
new EventDebugCommand(gameEventManager),
new GameCommand(gameStarterManager),
new GameCommand(arenaManager, gameStarterManager),
new GameMakerCommand(gameManager)
).forEach(paperCommandManager::registerCommand);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,61 @@
import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.Subcommand;
import com.readutf.inari.core.arena.ArenaManager;
import com.readutf.inari.core.event.GameEventManager;
import com.readutf.inari.core.game.GameManager;
import com.readutf.inari.core.game.team.Team;
import com.readutf.inari.core.arena.meta.ArenaMeta;
import com.readutf.inari.test.games.GameStarter;
import com.readutf.inari.test.games.GameStarterManager;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.lang.ArrayUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;

@CommandAlias("game")
@CommandAlias("game") @RequiredArgsConstructor
public class GameCommand extends BaseCommand {


private final ArenaManager arenaManager;
private final GameStarterManager gameStarterManager;

public GameCommand(GameStarterManager gameStarterManager) {
this.gameStarterManager = gameStarterManager;
}

@SneakyThrows
@Subcommand("start") @CommandCompletion("@games @players @players")
public void start(Player player, String gameName, String player1Name, String player2Name) {
OfflinePlayer player1 = Bukkit.getOfflinePlayer(player1Name);
OfflinePlayer player2 = Bukkit.getOfflinePlayer(player2Name);
@Subcommand("start") @CommandCompletion("@gametypes @arena @players")
public void start(Player player, String gameName, String arenaName, String... players) {

if(players.length % 2 != 0) {
player.sendMessage("Teams must be same size");
return;
}

if(players.length < 2) {
player.sendMessage("Not enough players");
return;
}

List<ArenaMeta> foundArena = arenaManager.findAvailableArenas(arenaMeta1 -> arenaMeta1.getName().equalsIgnoreCase(arenaName));
if(foundArena.isEmpty()) {
player.sendMessage("Arena not found");
return;
}

ArenaMeta arenaMeta = foundArena.get(0);

String[] team1 = (String[]) ArrayUtils.subarray(players, 0, players.length / 2);
String[] team2 = (String[]) ArrayUtils.subarray(players, players.length / 2, players.length);

List<UUID> team1Players = Arrays.stream(team1).map(s -> Bukkit.getOfflinePlayer(s).getUniqueId()).toList();
List<UUID> team2Players = Arrays.stream(team2).map(s -> Bukkit.getOfflinePlayer(s).getUniqueId()).toList();

GameStarter gameStarter = gameStarterManager.getStarter(gameName.toLowerCase());
if(gameStarter == null) {
player.sendMessage("Game not found");
return;
}

gameStarter.startGame(List.of(
Collections.singletonList(player1.getUniqueId()),
Collections.singletonList(player2.getUniqueId())
gameStarter.startGame(arenaMeta, List.of(
team1Players,
team2Players
));

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.readutf.inari.test.games;

import com.readutf.inari.core.arena.meta.ArenaMeta;
import com.readutf.inari.core.game.Game;
import com.readutf.inari.core.game.team.Team;

Expand All @@ -9,6 +10,6 @@

public interface GameStarter {

CompletableFuture<Game> startGame(List<List<UUID>> teams) throws Exception;
CompletableFuture<Game> startGame(ArenaMeta arenaMeta, List<List<UUID>> teams) throws Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class BedwarsStarter implements GameStarter {
);

@Override
public CompletableFuture<Game> startGame(List<List<UUID>> teams) throws Exception {
public CompletableFuture<Game> startGame(ArenaMeta arenaMeta, List<List<UUID>> teams) throws Exception {

List<Map.Entry<String, TeamColor>> teamColorsSet = new ArrayList<>(TEAM_COLORS.entrySet());

Expand All @@ -56,11 +56,8 @@ public CompletableFuture<Game> startGame(List<List<UUID>> teams) throws Exceptio
createdTeams.add(new BedwarsTeam(color.getKey(), color.getValue(), teams.get(i)));
}

List<ArenaMeta> availableArenas = arenaManager.findAvailableArenas(arenaMeta -> arenaMeta.getName().startsWith("bedwars"));
if (availableArenas.isEmpty()) throw new Exception("No available arenas");

CompletableFuture<Game> future = new CompletableFuture<>();
CompletableFuture<ActiveArena> load = arenaManager.load(availableArenas.get(ThreadLocalRandom.current().nextInt(availableArenas.size())));
CompletableFuture<ActiveArena> load = arenaManager.load(arenaMeta);
load.thenAccept(activeArena -> {
Game createdMatch = Game.builder(InariDemo.getInstance(), activeArena, eventManager, scoreboardManager, createdTeams,
(game, previousRound) -> new AwaitingPlayersStage(game, 2, 10),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,14 @@ public class MiniWallsStarter implements GameStarter {
);

@Override
public CompletableFuture<Game> startGame(List<List<UUID>> playerTeams) throws Exception {
public CompletableFuture<Game> startGame(ArenaMeta arenaMeta, List<List<UUID>> playerTeams) throws Exception {
Preconditions.checkArgument(playerTeams.size() == 2, "Miniwalls requires 2 playerTeams");

List<Team> teams = new ArrayList<>();
for (List<UUID> team : playerTeams) {
teams.add(new MiniWallsTeam("Team", teamColors.get(teams.size()), team));
}

List<ArenaMeta> availableArenas = arenaManager.findAvailableArenas(arenaMeta -> arenaMeta.getName().startsWith("miniwalls"));
if (availableArenas.isEmpty()) throw new Exception("No available arenas");

ArenaMeta arenaMeta = availableArenas.get(ThreadLocalRandom.current().nextInt(availableArenas.size()));

CompletableFuture<Game> gameReadyFuture = new CompletableFuture<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public AwaitingPlayersStage(Game game, int targetPlayers, int gameExpireTimeSeco
@Override
public void roundStart() {



startCountdown(game, gameExpireTimeSeconds, new CancellableTask<>() {
@Override
public void run(Integer integer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ public class SumoGameStarter implements GameStarter {
private final ScoreboardManager scoreboardManager;

@Override
public CompletableFuture<Game> startGame(List<List<UUID>> playerTeams) throws Exception {
public CompletableFuture<Game> startGame(ArenaMeta arenaMeta, List<List<UUID>> playerTeams) throws Exception {

logger.info("Starting Sumo Game");


List<ArenaMeta> availableArenas = arenaManager.findAvailableArenas(arenaMeta -> arenaMeta.getName().startsWith("sumo"));
if(availableArenas.isEmpty()) throw new Exception("No available arenas");

List<Team> teams = new ArrayList<>();
for (int i = 0; i < playerTeams.size(); i++) {
Expand All @@ -55,7 +53,7 @@ public CompletableFuture<Game> startGame(List<List<UUID>> playerTeams) throws Ex

logger.info("Finding arena.");

CompletableFuture<ActiveArena> arenaFuture = arenaManager.load(availableArenas.get(ThreadLocalRandom.current().nextInt(availableArenas.size())));
CompletableFuture<ActiveArena> arenaFuture = arenaManager.load(arenaMeta);

arenaFuture.thenAccept(activeArena -> {

Expand Down

0 comments on commit 68f8a3a

Please sign in to comment.