Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement waiting phase #45

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@

public class BukkitMessageSender {

public static List<String> getOnlinePlayersNames() {
return Bukkit.getOnlinePlayers().stream().map(p -> p.getName()).toList();
}

public static void sendChatMessage(PrisonEscapePlayer player, String message) {
Player bukkitPlayer = Bukkit.getPlayer(player.getName());
sendChatMessage(player.getName(), message);
}

public static void sendChatMessage(String playerName, String message) {
Player bukkitPlayer = Bukkit.getPlayer(playerName);
if (bukkitPlayer == null || !bukkitPlayer.isOnline()) {
return;
}
Expand All @@ -19,7 +27,11 @@ public static void sendChatMessage(PrisonEscapePlayer player, String message) {
}

public static void sendChatMessage(PrisonEscapePlayer player, String[] message) {
Player bukkitPlayer = Bukkit.getPlayer(player.getName());
sendChatMessage(player.getName(), message);
}

public static void sendChatMessage(String playerName, String[] message) {
Player bukkitPlayer = Bukkit.getPlayer(playerName);
if (bukkitPlayer == null || !bukkitPlayer.isOnline()) {
return;
}
Expand All @@ -28,9 +40,13 @@ public static void sendChatMessage(PrisonEscapePlayer player, String[] message)
}

public static void sendChatMessage(PrisonEscapePlayer player, List<String> message) {
sendChatMessage(player.getName(), message);
}

public static void sendChatMessage(String playerName, List<String> message) {
String[] messageArray = message.toArray(new String[0]);

sendChatMessage(player, messageArray);
sendChatMessage(playerName, messageArray);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Collections;
import java.util.List;

import net.tiagofar78.prisonescape.bukkit.BukkitMessageSender;
import net.tiagofar78.prisonescape.bukkit.BukkitScheduler;
import net.tiagofar78.prisonescape.bukkit.BukkitTeleporter;
import net.tiagofar78.prisonescape.game.phases.Finished;
Expand All @@ -13,6 +14,7 @@
import net.tiagofar78.prisonescape.game.prisonbuilding.PrisonEscapeLocation;
import net.tiagofar78.prisonescape.managers.ConfigManager;
import net.tiagofar78.prisonescape.managers.GameManager;
import net.tiagofar78.prisonescape.managers.MessageLanguageManager;

public class PrisonEscapeGame {

Expand Down Expand Up @@ -139,6 +141,43 @@ private void startWaitingPhase() {

// TODO add delay to wait for players to join
// TODO add warnings so players know the game is about to start

ConfigManager config = ConfigManager.getInstance();

runWaitingPhaseScheduler(config.getWaitingPhaseDuration(), true);
}

private void runWaitingPhaseScheduler(int remainingSeconds, boolean isFirst) {
ConfigManager config = ConfigManager.getInstance();

BukkitScheduler.runSchedulerLater(new Runnable() {

@Override
public void run() {
if (remainingSeconds == 0) {
return;
}

if (remainingSeconds % config.getDelayBetweenAnnouncements() == 0) {
List<String> playersNames = BukkitMessageSender.getOnlinePlayersNames();
for (String playerName : playersNames) {
String playerLanguage = MessageLanguageManager.getPlayerLanguage(playerName);
MessageLanguageManager messages = MessageLanguageManager.getInstance(playerLanguage);

List<String> announcement = messages
.getGameStartingAnnouncementMessage(remainingSeconds, _playersOnLobby.size());
BukkitMessageSender.sendChatMessage(playerName, announcement);
}
}

int fullLobbyWaitDuration = config.getFullLobbyWaitDuration();
if (_playersOnLobby.size() == config.getMaxPlayers() && remainingSeconds > fullLobbyWaitDuration) {
runWaitingPhaseScheduler(fullLobbyWaitDuration, false);
}

runWaitingPhaseScheduler(remainingSeconds - 1, false);
}
}, isFirst ? 0 : 1 * TICKS_PER_SECOND);
}

private void startOngoingPhase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ public int getMaxPlayers() {
return 0; // TODO
}

public int getWaitingPhaseDuration() {
return 0; // TODO
}

public int getFullLobbyWaitDuration() {
return 0; // TODO
}

public int getDelayBetweenAnnouncements() {
return 0; // TODO
}

public String getWorldName() {
return null; // TODO
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.tiagofar78.prisonescape.managers;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

Expand Down Expand Up @@ -48,6 +49,8 @@ public static String getPlayerLanguage(String playerName) {
private String _successfullyLeftGameMessage;
private String _successfullyForceStoppedGameMessage;

private List<String> _gameStartingAnnouncementMessage;

// ########################################
// # Errors #
// ########################################
Expand Down Expand Up @@ -102,6 +105,19 @@ public String getSuccessfullyForceStoppedGameMessage() {
return _successfullyForceStoppedGameMessage;
}

public List<String> getGameStartingAnnouncementMessage(int remainingTime, int playersOnLobby) {
List<String> message = new ArrayList<>(_gameStartingAnnouncementMessage);

for (int i = 0; i < message.size(); i++) {

message.set(i, message.get(i)
.replace("{TIME}", Integer.toString(remainingTime)
.replace("{PLAYERS}", Integer.toString(playersOnLobby))));
}

return message;
}

// ########################################
// # Errors #
// ########################################
Expand Down