Skip to content

Commit

Permalink
step 1: minimize timecomplexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Mindgamesnl committed Jan 30, 2024
1 parent adf4055 commit 124faa5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.craftmend.openaudiomc.generic.networking.interfaces.NetworkingService;
import com.craftmend.openaudiomc.generic.utils.data.Filter;
import com.craftmend.openaudiomc.spigot.modules.voicechat.filters.PeerFilter;
import com.craftmend.openaudiomc.spigot.modules.voicechat.utils.CombinationChecker;
import lombok.Setter;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -76,6 +77,8 @@ public void run() {
.filter((c) -> c.getRtcSessionManager().isReady())
.toArray(ClientConnection[]::new);

CombinationChecker combinationChecker = new CombinationChecker();

for (ClientConnection client : allClients) {

// am I valid? no? do nothing.
Expand All @@ -92,19 +95,21 @@ public void run() {
// clear the applicable players if i'm disabled myself
if (!client.getRtcSessionManager().getBlockReasons().isEmpty()) applicableClients.clear();

// remove moderators, if I'm not moderating myself
if (!client.getSession().isModerating()) {
applicableClients.removeIf(other -> other.getSession().isModerating());
}

// find players that we don't have yet
applicableClients
.stream()
.filter(peer -> {
if (combinationChecker.hasChecked(client.getUser().getUniqueId(), peer.getUser().getUniqueId())) return false;
return client.getSession().isModerating() && other.getSession().isModerating();
})
.filter(peer -> !client.getRtcSessionManager().getCurrentProximityPeers().contains(peer.getOwner().getUniqueId()))
.filter(peer -> !peer.getSession().isResetVc()) // they are already resetting, give it a sec
.filter(peer -> (client.isModerating() || !peer.isModerating())) // ignore moderators

.forEach(peer -> {
// register check
combinationChecker.registerCheck(client.getUser().getUniqueId(), peer.getUser().getUniqueId());

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.craftmend.openaudiomc.spigot.modules.voicechat.utils;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class CombinationChecker {

/**
* This is a small utility class that helps with the combination of two UUIDs.
* We use it to cut down on iterations during proximity checks, where we want to
* register AA-AA-AA>BB-BB-BB and exempt BB-BB-BB>AA-AA-AA.
* This implementation has some room for error, because the hashcode method of UUID
* does not guarantee a unique hash for each UUID, but it's good enough for our use case.
* https://stackoverflow.com/questions/24876188/how-big-is-the-chance-to-get-a-java-uuid-randomuuid-collision
* If this ever happens then well, guess some minecraft has worse odds than winning the lottery but we'll have to fix it
*/

private Set<Integer> checkedCombinations = new HashSet<>();

public boolean contains(UUID player1, UUID player2) {
int hashCode = getCombinedHashCode(player1, player2);
return checkedCombinations.contains(hashCode);
}

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

private int getCombinedHashCode(UUID player1, UUID player2) {
return player1.hashCode() ^ player2.hashCode();
}

}

0 comments on commit 124faa5

Please sign in to comment.