Rename and clean up MapleMatchCheckerCoordinator

This commit is contained in:
P0nk
2021-09-09 21:29:45 +02:00
parent 024cf0cac8
commit c02efc5ca1
4 changed files with 86 additions and 88 deletions

View File

@@ -30,25 +30,24 @@ import java.util.Map.Entry;
import java.util.concurrent.Semaphore;
/**
*
* @author Ronan
*/
public class MapleMatchCheckerCoordinator {
public class MatchCheckerCoordinator {
private final Map<Integer, MapleMatchCheckingElement> matchEntries = new HashMap<>();
private final Set<Integer> pooledCids = new HashSet<>();
private final Semaphore semaphorePool = new Semaphore(7);
private class MapleMatchCheckingEntry {
private class MatchCheckingEntry {
private boolean accepted;
private int cid;
private MapleMatchCheckingEntry(int cid) {
private final int cid;
private MatchCheckingEntry(int cid) {
this.cid = cid;
this.accepted = false;
}
private boolean setAccept() {
if (!this.accepted) {
this.accepted = true;
@@ -57,25 +56,25 @@ public class MapleMatchCheckerCoordinator {
return false;
}
}
private boolean getAccept() {
return this.accepted;
}
}
private class MapleMatchCheckingElement {
private int leaderCid;
private int world;
private MatchCheckerType matchType;
private AbstractMatchCheckerListener listener;
private Map<Integer, MapleMatchCheckingEntry> confirmingMembers = new HashMap<>();
private final int leaderCid;
private final int world;
private final MatchCheckerType matchType;
private final AbstractMatchCheckerListener listener;
private final Map<Integer, MatchCheckingEntry> confirmingMembers = new HashMap<>();
private int confirmCount;
private boolean active = true;
private String message;
private final String message;
private MapleMatchCheckingElement(MatchCheckerType matchType, int leaderCid, int world, AbstractMatchCheckerListener leaderListener, Set<Integer> matchPlayers, String message) {
this.leaderCid = leaderCid;
this.world = world;
@@ -83,59 +82,57 @@ public class MapleMatchCheckerCoordinator {
this.confirmCount = 0;
this.message = message;
this.matchType = matchType;
for (Integer cid : matchPlayers) {
MapleMatchCheckingEntry mmcEntry = new MapleMatchCheckingEntry(cid);
MatchCheckingEntry mmcEntry = new MatchCheckingEntry(cid);
confirmingMembers.put(cid, mmcEntry);
}
}
private boolean acceptEntry(int cid) {
MapleMatchCheckingEntry mmcEntry = confirmingMembers.get(cid);
MatchCheckingEntry mmcEntry = confirmingMembers.get(cid);
if (mmcEntry != null) {
if (mmcEntry.setAccept()) {
this.confirmCount++;
if (this.confirmCount == this.confirmingMembers.size()) {
return true;
}
return this.confirmCount == this.confirmingMembers.size();
}
}
return false;
}
private boolean isMatchActive() {
return active;
}
private void setMatchActive(boolean a) {
active = a;
}
private Set<Integer> getMatchPlayers() {
return confirmingMembers.keySet();
}
private Set<Integer> getAcceptedMatchPlayers() {
Set<Integer> s = new HashSet<>();
for (Entry<Integer, MapleMatchCheckingEntry> e : confirmingMembers.entrySet()) {
for (Entry<Integer, MatchCheckingEntry> e : confirmingMembers.entrySet()) {
if (e.getValue().getAccept()) {
s.add(e.getKey());
}
}
return s;
}
private Set<Character> getMatchCharacters() {
Set<Character> players = new HashSet<>();
World wserv = Server.getInstance().getWorld(world);
if (wserv != null) {
PlayerStorage ps = wserv.getPlayerStorage();
for (Integer cid : getMatchPlayers()) {
Character chr = ps.getCharacterById(cid);
if (chr != null) {
@@ -143,25 +140,25 @@ public class MapleMatchCheckerCoordinator {
}
}
}
return players;
}
private void dispatchMatchCreated() {
Set<Character> nonLeaderMatchPlayers = getMatchCharacters();
Character leader = null;
for (Character chr : nonLeaderMatchPlayers) {
if (chr.getId() == leaderCid) {
leader = chr;
break;
}
}
nonLeaderMatchPlayers.remove(leader);
listener.onMatchCreated(leader, nonLeaderMatchPlayers, message);
}
private void dispatchMatchResult(boolean accept) {
if (accept) {
listener.onMatchAccepted(leaderCid, getMatchCharacters(), message);
@@ -169,29 +166,29 @@ public class MapleMatchCheckerCoordinator {
listener.onMatchDeclined(leaderCid, getMatchCharacters(), message);
}
}
private void dispatchMatchDismissed() {
listener.onMatchDismissed(leaderCid, getMatchCharacters(), message);
}
}
private void unpoolMatchPlayer(Integer cid) {
unpoolMatchPlayers(Collections.singleton(cid));
}
private void unpoolMatchPlayers(Set<Integer> matchPlayers) {
for (Integer cid : matchPlayers) {
pooledCids.remove(cid);
}
}
private boolean poolMatchPlayer(Integer cid) {
return poolMatchPlayers(Collections.singleton(cid));
}
private boolean poolMatchPlayers(Set<Integer> matchPlayers) {
Set<Integer> pooledPlayers = new HashSet<>();
for (Integer cid : matchPlayers) {
if (!pooledCids.add(cid)) {
unpoolMatchPlayers(pooledPlayers);
@@ -200,24 +197,24 @@ public class MapleMatchCheckerCoordinator {
pooledPlayers.add(cid);
}
}
return true;
}
private boolean isMatchingAvailable(Set<Integer> matchPlayers) {
for (Integer cid : matchPlayers) {
if (matchEntries.containsKey(cid)) {
return false;
}
}
return true;
}
private void reenablePlayerMatching(Set<Integer> matchPlayers) {
for (Integer cid : matchPlayers) {
MapleMatchCheckingElement mmce = matchEntries.get(cid);
if (mmce != null) {
synchronized (mmce) {
if (!mmce.isMatchActive()) {
@@ -227,7 +224,7 @@ public class MapleMatchCheckerCoordinator {
}
}
}
public int getMatchConfirmationLeaderid(int cid) {
MapleMatchCheckingElement mmce = matchEntries.get(cid);
if (mmce != null) {
@@ -236,7 +233,7 @@ public class MapleMatchCheckerCoordinator {
return -1;
}
}
public MatchCheckerType getMatchConfirmationType(int cid) {
MapleMatchCheckingElement mmce = matchEntries.get(cid);
if (mmce != null) {
@@ -245,7 +242,7 @@ public class MapleMatchCheckerCoordinator {
return null;
}
}
public boolean isMatchConfirmationActive(int cid) {
MapleMatchCheckingElement mmce = matchEntries.get(cid);
if (mmce != null) {
@@ -254,18 +251,18 @@ public class MapleMatchCheckerCoordinator {
return false;
}
}
private MapleMatchCheckingElement createMatchConfirmationInternal(MatchCheckerType matchType, int world, int leaderCid, AbstractMatchCheckerListener leaderListener, Set<Integer> players, String message) {
MapleMatchCheckingElement mmce = new MapleMatchCheckingElement(matchType, leaderCid, world, leaderListener, players, message);
for (Integer cid : players) {
matchEntries.put(cid, mmce);
}
acceptMatchElement(mmce, leaderCid);
return mmce;
}
public boolean createMatchConfirmation(MatchCheckerType matchType, int world, int leaderCid, Set<Integer> players, String message) {
MapleMatchCheckingElement mmce = null;
try {
@@ -289,7 +286,7 @@ public class MapleMatchCheckerCoordinator {
} catch (InterruptedException ie) {
ie.printStackTrace();
}
if (mmce != null) {
mmce.dispatchMatchCreated();
return true;
@@ -297,15 +294,16 @@ public class MapleMatchCheckerCoordinator {
return false;
}
}
private void disposeMatchElement(MapleMatchCheckingElement mmce) {
Set<Integer> matchPlayers = mmce.getMatchPlayers(); // thanks Ai for noticing players getting match-stuck on certain cases
while (!poolMatchPlayers(matchPlayers)) {
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {}
} catch (InterruptedException ie) {
}
}
try {
for (Integer cid : matchPlayers) {
matchEntries.remove(cid);
@@ -314,30 +312,30 @@ public class MapleMatchCheckerCoordinator {
unpoolMatchPlayers(matchPlayers);
}
}
private boolean acceptMatchElement(MapleMatchCheckingElement mmce, int cid) {
if (mmce.acceptEntry(cid)) {
unpoolMatchPlayer(cid);
disposeMatchElement(mmce);
return true;
} else {
return false;
}
}
private void denyMatchElement(MapleMatchCheckingElement mmce, int cid) {
unpoolMatchPlayer(cid);
disposeMatchElement(mmce);
}
private void dismissMatchElement(MapleMatchCheckingElement mmce, int cid) {
mmce.setMatchActive(false);
unpoolMatchPlayer(cid);
disposeMatchElement(mmce);
}
public boolean answerMatchConfirmation(int cid, boolean accept) {
MapleMatchCheckingElement mmce = null;
try {
@@ -347,7 +345,7 @@ public class MapleMatchCheckerCoordinator {
if (poolMatchPlayer(cid)) {
try {
mmce = matchEntries.get(cid);
if (mmce != null) {
synchronized (mmce) {
if (!mmce.isMatchActive()) { // thanks Alex (Alex-0000) for noticing that exploiters could stall on match checking
@@ -358,7 +356,7 @@ public class MapleMatchCheckerCoordinator {
if (!acceptMatchElement(mmce, cid)) {
mmce = null;
}
break; // thanks Rohenn for noticing loop scenario here
} else {
denyMatchElement(mmce, cid);
@@ -378,14 +376,14 @@ public class MapleMatchCheckerCoordinator {
} catch (InterruptedException ie) {
ie.printStackTrace();
}
if (mmce != null) {
mmce.dispatchMatchResult(accept);
}
return false;
}
public boolean dismissMatchConfirmation(int cid) {
MapleMatchCheckingElement mmce = null;
try {
@@ -416,7 +414,7 @@ public class MapleMatchCheckerCoordinator {
} catch (InterruptedException ie) {
ie.printStackTrace();
}
if (mmce != null) {
mmce.dispatchMatchDismissed();
return true;
@@ -424,5 +422,5 @@ public class MapleMatchCheckerCoordinator {
return false;
}
}
}

View File

@@ -30,7 +30,7 @@ import net.server.Server;
import net.server.audit.locks.MonitoredLockType;
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
import net.server.channel.Channel;
import net.server.coordinator.matchchecker.MapleMatchCheckerCoordinator;
import net.server.coordinator.matchchecker.MatchCheckerCoordinator;
import net.server.coordinator.world.MapleInviteCoordinator;
import net.server.coordinator.world.MapleInviteCoordinator.InviteType;
import net.server.coordinator.world.MapleInviteCoordinator.MapleInviteResult;
@@ -752,7 +752,7 @@ public class MapleGuild {
Set<Character> guildMembers = new HashSet<>();
guildMembers.add(guildLeader);
MapleMatchCheckerCoordinator mmce = guildLeader.getWorldServer().getMatchCheckerCoordinator();
MatchCheckerCoordinator mmce = guildLeader.getWorldServer().getMatchCheckerCoordinator();
for (Character chr : guildLeader.getMap().getAllPlayers()) {
if (chr.getParty() == null && chr.getGuild() == null && mmce.getMatchConfirmationLeaderid(chr.getId()) == -1) {
guildMembers.add(chr);

View File

@@ -28,7 +28,7 @@ import net.server.audit.LockCollector;
import net.server.audit.locks.MonitoredLockType;
import net.server.audit.locks.MonitoredReentrantLock;
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
import net.server.coordinator.matchchecker.MapleMatchCheckerCoordinator;
import net.server.coordinator.matchchecker.MatchCheckerCoordinator;
import net.server.coordinator.matchchecker.MatchCheckerListenerFactory.MatchCheckerType;
import scripting.event.EventInstanceManager;
import server.maps.MapleDoor;
@@ -436,7 +436,7 @@ public class MapleParty {
player.setParty(null);
MapleMatchCheckerCoordinator mmce = c.getWorldServer().getMatchCheckerCoordinator();
MatchCheckerCoordinator mmce = c.getWorldServer().getMatchCheckerCoordinator();
if (mmce.getMatchConfirmationLeaderid(player.getId()) == player.getId() && mmce.getMatchConfirmationType(player.getId()) == MatchCheckerType.GUILD_CREATION) {
mmce.dismissMatchConfirmation(player.getId());
}

View File

@@ -39,7 +39,7 @@ import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
import net.server.audit.locks.factory.MonitoredWriteLockFactory;
import net.server.channel.Channel;
import net.server.channel.CharacterIdChannelPair;
import net.server.coordinator.matchchecker.MapleMatchCheckerCoordinator;
import net.server.coordinator.matchchecker.MatchCheckerCoordinator;
import net.server.coordinator.partysearch.MaplePartySearchCoordinator;
import net.server.coordinator.world.MapleInviteCoordinator;
import net.server.coordinator.world.MapleInviteCoordinator.InviteResult;
@@ -88,7 +88,7 @@ public class World {
private Map<Integer, MapleGuildSummary> gsStore = new HashMap<>();
private PlayerStorage players = new PlayerStorage();
private ServicesManager services = new ServicesManager(WorldServices.SAVE_CHARACTER);
private MapleMatchCheckerCoordinator matchChecker = new MapleMatchCheckerCoordinator();
private MatchCheckerCoordinator matchChecker = new MatchCheckerCoordinator();
private MaplePartySearchCoordinator partySearch = new MaplePartySearchCoordinator();
private final MonitoredReentrantReadWriteLock chnLock = new MonitoredReentrantReadWriteLock(MonitoredLockType.WORLD_CHANNELS, true);
@@ -513,7 +513,7 @@ public class World {
return players;
}
public MapleMatchCheckerCoordinator getMatchCheckerCoordinator() {
public MatchCheckerCoordinator getMatchCheckerCoordinator() {
return matchChecker;
}