Skip to content

Commit

Permalink
Refactor + excempt global peers
Browse files Browse the repository at this point in the history
  • Loading branch information
Mindgamesnl committed Jan 31, 2024
1 parent 9c3f28d commit fb8f518
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public void registerAlias(String aliasName, String value) {

@Override
public void setProximityFilter(Filter<ClientConnection, Player> filter) {
OpenAudioMc.getService(SpigotVoiceChatService.class).getProximityTicker().setFilter(filter);
OpenAudioMc.getService(SpigotVoiceChatService.class).getPeerTicker().setFilter(filter);
}

@Override
public void addProximityFilter(Filter<ClientConnection, Player> filter) {
OpenAudioMc.getService(SpigotVoiceChatService.class).getProximityTicker().addFilter(filter);
OpenAudioMc.getService(SpigotVoiceChatService.class).getPeerTicker().addFilter(filter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public RtcSessionManager(ClientConnection clientConnection) {
this.clientConnection.onDisconnect(() -> {
// go over all other clients, check if we might have a relations ship and break up if thats the case
currentProximityPeers.clear();
currentGlobalPeers.clear();
this.isMicrophoneEnabled = false;
makePeersDrop();
locationUpdateQueue.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.craftmend.openaudiomc.spigot.modules.voicechat.filters.PeerFilter;
import com.craftmend.openaudiomc.spigot.modules.voicechat.filters.impl.GamemodeFilter;
import com.craftmend.openaudiomc.spigot.modules.voicechat.filters.impl.TeamFilter;
import com.craftmend.openaudiomc.spigot.modules.voicechat.tasks.PlayerProximityTicker;
import com.craftmend.openaudiomc.spigot.modules.voicechat.tasks.PlayerPeerTicker;
import com.craftmend.openaudiomc.spigot.modules.voicechat.tasks.PlayerVicinityMessageTask;
import com.craftmend.openaudiomc.spigot.modules.voicechat.tasks.TickVoicePacketQueue;
import lombok.AllArgsConstructor;
Expand All @@ -40,7 +40,7 @@ public class SpigotVoiceChatService extends Service {
private NetworkingService networkingService;

@Getter
private PlayerProximityTicker proximityTicker;
private PlayerPeerTicker peerTicker;
private boolean firstRun = true;
private int broadcastTickLoop = 0;

Expand All @@ -63,8 +63,8 @@ public void onEnable() {
int maxDistance = StorageKey.SETTINGS_VC_RADIUS.getInt();

// tick every second
proximityTicker = new PlayerProximityTicker(maxDistance, new PeerFilter());
taskService.scheduleAsyncRepeatingTask(proximityTicker, 20, 20);
peerTicker = new PlayerPeerTicker(maxDistance, new PeerFilter());
taskService.scheduleAsyncRepeatingTask(peerTicker, 20, 20);
taskService.scheduleAsyncRepeatingTask(new TickVoicePacketQueue(), 3, 3);
}
firstRun = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class PlayerProximityTicker implements Runnable {
public class PlayerPeerTicker implements Runnable {

@Setter
private Filter<ClientConnection, Player> filter;
Expand Down Expand Up @@ -52,7 +52,7 @@ public class PlayerProximityTicker implements Runnable {
* Here be dragons.
*/

public PlayerProximityTicker(int maxDistance, PeerFilter peerFilter) {
public PlayerPeerTicker(int maxDistance, PeerFilter peerFilter) {
this.filter = peerFilter;
this.filter.updateProperty("d", maxDistance);

Expand Down Expand Up @@ -96,7 +96,13 @@ public void run() {
.filter((c) -> !c.getSession().isResetVc()) // don't check players that are resetting
.filter((c) -> c.getOwner().getUniqueId() != client.getOwner().getUniqueId()) // don't check yourself
.filter((c) -> !combinationChecker.contains(client.getUser().getUniqueId(), c.getUser().getUniqueId())) // don't check combinations that failed
.filter((c) -> c.isModerating() == client.isModerating()); // only allow equal moderation states
.filter(c -> !client.getRtcSessionManager().getCurrentGlobalPeers().contains(c.getOwner().getUniqueId())) // exempt global peers
.filter((c) -> c.isModerating() == client.isModerating()) // only allow equal moderation states

// mark checked, prior to filtering, because if someone isn't
// applicable, then they should still be marked as checked to prevent
// future checks
.peek((c) -> combinationChecker.markChecked(client.getUser().getUniqueId(), c.getUser().getUniqueId()));

// execute API filters
applicableClients = filter.wrap(
Expand All @@ -107,9 +113,6 @@ public void run() {

// find players that we don't have yet
applicableClients.forEach(peer -> {
// register check
combinationChecker.mark(client.getUser().getUniqueId(), peer.getUser().getUniqueId());

// am I moderating compared to this peer?
boolean isModerating = client.isModerating() && !peer.isModerating();

Expand All @@ -132,6 +135,7 @@ public void run() {
.stream()
.filter(p -> p != client.getOwner().getUniqueId())
.filter(uuid -> (client.getSession().isResetVc() || applicableClients.stream().noneMatch(apc -> apc.getOwner().getUniqueId() == uuid)))
.filter(uuid -> !client.getRtcSessionManager().getCurrentGlobalPeers().contains(uuid))
.collect(Collectors.toSet())) {

// unsubscribe these
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public boolean contains(UUID player1, UUID player2) {
return checkedCombinations.contains(hashCode);
}

public void mark(UUID player1, UUID player2) {
public void markChecked(UUID player1, UUID player2) {
int hashCode = getCombinedHashCode(player1, player2);
checkedCombinations.add(hashCode);
}
Expand Down

0 comments on commit fb8f518

Please sign in to comment.