Skip to content

Commit

Permalink
notify party members on join and leave
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Oct 2, 2023
1 parent 7a6874e commit a451258
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,12 @@ void errorCommandCommandExecution(
@Message("command.party.disband.description")
Component partyDisbandDesc();

@Message("party.player_joined")
void playerJoinedParty(Audience audience, Component partyName, Component displayName);

@Message("party.player_left")
void playerLeftParty(Audience audience, Component partyName, Component displayName);

@Message("deletemessage.prefix")
Component deleteMessagePrefix();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
import java.util.stream.Collectors;
import net.draycia.carbon.api.CarbonServer;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.Party;
import net.draycia.carbon.common.messaging.MessagingManager;
import net.draycia.carbon.common.messaging.packets.PacketFactory;
Expand All @@ -58,6 +60,7 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
private final Injector injector;
private final Provider<MessagingManager> messagingManager;
private final PacketFactory packetFactory;
private final CarbonServer server;
private final ReentrantLock cacheLock;
private final Map<UUID, CompletableFuture<CarbonPlayerCommon>> cache;
private final AsyncCache<UUID, Party> partyCache;
Expand All @@ -67,7 +70,8 @@ protected CachingUserManager(
final ProfileResolver profileResolver,
final Injector injector,
final Provider<MessagingManager> messagingManager,
final PacketFactory packetFactory
final PacketFactory packetFactory,
final CarbonServer server
) {
this.logger = logger;
this.executor = Executors.newSingleThreadExecutor(ConcurrentUtil.carbonThreadFactory(logger, this.getClass().getSimpleName()));
Expand All @@ -78,6 +82,7 @@ protected CachingUserManager(
this.injector = injector;
this.messagingManager = messagingManager;
this.packetFactory = packetFactory;
this.server = server;
this.cacheLock = new ReentrantLock();
this.cache = new HashMap<>();
}
Expand Down Expand Up @@ -253,21 +258,33 @@ public final void disbandParty(final UUID id) {

@Override
public void partyChangeMessageReceived(final PartyChangePacket pkt) {
final @Nullable CompletableFuture<Party> future = this.partyCache.getIfPresent(pkt.partyId());
if (future != null) {
future.thenAccept(party -> {
final PartyImpl impl = (PartyImpl) party;
pkt.changes().forEach((id, type) -> {
switch (type) {
case ADD -> impl.addMemberRaw(id);
case REMOVE -> impl.removeMemberRaw(id);
}
});
}).whenComplete(($, thr) -> {
if (thr != null) {
this.logger.warn("Exception handling party change packet {}", pkt, thr);
@Nullable CompletableFuture<@Nullable Party> future = this.partyCache.getIfPresent(pkt.partyId());
if (future == null) {
// we want to notify any online members even if the party isn't loaded locally yet
for (final CarbonPlayer player : this.server.players()) {
if (pkt.partyId().equals(((WrappedCarbonPlayer) player).partyId())) {
future = this.party(pkt.partyId());
}
});
}
}
if (future == null) {
return;
}
future.thenAccept(party -> {
if (party == null) {
return;
}
final PartyImpl impl = (PartyImpl) party;
pkt.changes().forEach((id, type) -> {
switch (type) {
case ADD -> impl.addMemberRaw(id);
case REMOVE -> impl.removeMemberRaw(id);
}
});
}).whenComplete(($, thr) -> {
if (thr != null) {
this.logger.warn("Exception handling party change packet {}", pkt, thr);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package net.draycia.carbon.common.users;

import com.google.common.base.Suppliers;
import com.google.inject.Inject;
import java.util.Map;
import java.util.Objects;
Expand All @@ -27,11 +28,14 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import net.draycia.carbon.api.CarbonServer;
import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.api.event.events.PartyJoinEvent;
import net.draycia.carbon.api.event.events.PartyLeaveEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.Party;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.apache.logging.log4j.Logger;
Expand All @@ -52,6 +56,7 @@ public final class PartyImpl implements Party {
private transient @MonotonicNonNull @Inject CarbonServer server;
private transient @MonotonicNonNull @Inject Logger logger;
private transient @MonotonicNonNull @Inject CarbonEventHandler events;
private transient @MonotonicNonNull @Inject CarbonMessages messages;
private transient volatile boolean disbanded = false;

private PartyImpl(
Expand Down Expand Up @@ -173,6 +178,8 @@ public Party party() {
return PartyImpl.this;
}
});

this.notifyJoin(id);
}

public void removeMemberRaw(final UUID id) {
Expand All @@ -190,6 +197,8 @@ public Party party() {
return PartyImpl.this;
}
});

this.notifyLeave(id);
}

public Map<UUID, ChangeType> pollChanges() {
Expand All @@ -212,6 +221,42 @@ public UUID id() {
return this.id;
}

private void notifyJoin(final UUID joined) {
this.notifyMembersChanged(joined, (p, party, member) -> {
this.messages.playerJoinedParty(member, party.name(), p.displayName());
});
}

private void notifyLeave(final UUID left) {
this.notifyMembersChanged(left, (p, party, member) -> {
this.messages.playerLeftParty(member, party.name(), p.displayName());
});
}

private void notifyMembersChanged(final UUID changed, final ChangeNotifier notify) {
final Supplier<CompletableFuture<? extends CarbonPlayer>> changedPlayer = Suppliers.memoize(() -> this.userManager.user(changed));
for (final CarbonPlayer player : this.server.players()) {
if (player.uuid().equals(changed)) {
continue;
}
final WrappedCarbonPlayer wrapped = (WrappedCarbonPlayer) player;
if (this.id().equals(wrapped.partyId())) {
changedPlayer.get().thenAccept(p -> {
notify.notify(p, this, wrapped);
}).whenComplete(($, thr) -> {
this.logger.warn("Exception notifying members of party change", thr);
});
}
}
}

@FunctionalInterface
private interface ChangeNotifier {

void notify(CarbonPlayer changed, Party party, CarbonPlayer member);

}

@Override
public String toString() {
return "PartyImpl[" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.UUID;
import java.util.function.Consumer;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.CarbonServer;
import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.common.config.ConfigManager;
Expand Down Expand Up @@ -83,14 +84,16 @@ private DatabaseUserManager(
final Injector injector,
final Provider<MessagingManager> messagingManager,
final PacketFactory packetFactory,
final ChannelRegistry channelRegistry
final ChannelRegistry channelRegistry,
final CarbonServer server
) {
super(
logger,
profileResolver,
injector,
messagingManager,
packetFactory
packetFactory,
server
);
this.jdbi = jdbi;
this.dataSource = dataSource;
Expand Down Expand Up @@ -274,6 +277,7 @@ public static final class Factory {
private final Injector injector;
private final Provider<MessagingManager> messagingManager;
private final PacketFactory packetFactory;
private final CarbonServer server;

@Inject
private Factory(
Expand All @@ -283,7 +287,8 @@ private Factory(
final ProfileResolver profileResolver,
final Injector injector,
final Provider<MessagingManager> messagingManager,
final PacketFactory packetFactory
final PacketFactory packetFactory,
final CarbonServer server
) {
this.channelRegistry = channelRegistry;
this.configManager = configManager;
Expand All @@ -292,6 +297,7 @@ private Factory(
this.injector = injector;
this.messagingManager = messagingManager;
this.packetFactory = packetFactory;
this.server = server;
}

public DatabaseUserManager create(final String migrationsLocation, final Consumer<Jdbi> configureJdbi) {
Expand Down Expand Up @@ -352,7 +358,8 @@ public DatabaseUserManager create(final String migrationsLocation, final Consume
this.injector,
this.messagingManager,
this.packetFactory,
this.channelRegistry
this.channelRegistry,
this.server
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.nio.file.Path;
import java.util.Map;
import java.util.UUID;
import net.draycia.carbon.api.CarbonServer;
import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.common.DataDirectory;
Expand Down Expand Up @@ -69,14 +70,16 @@ public JSONUserManager(
final UUIDSerializerGson uuidSerializer,
final Provider<MessagingManager> messagingManager,
final PacketFactory packetFactory,
final CarbonChannelRegistry channelRegistry
final CarbonChannelRegistry channelRegistry,
final CarbonServer server
) throws IOException {
super(
logger,
profileResolver,
injector,
messagingManager,
packetFactory
packetFactory,
server
);
this.userDirectory = dataDirectory.resolve("users");
this.partyDirectory = dataDirectory.resolve("party");
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/resources/locale/messages-en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ command.party.accept.description=Accept party invites.
command.party.leave.description=Leave your current party.
command.party.already_in_party=<display_name><red> is already in your party.
command.party.disband.description=Disband your current party.
party.player_joined=<display_name><green> joined your party.
party.player_left=<display_name><green> left your party.
config.reload.failed=<red>Config failed to reload
config.reload.success=<green>Config reloaded successfully
error.command.argument_parsing=<red>Invalid command argument: <gray><throwable_message>
Expand Down

0 comments on commit a451258

Please sign in to comment.