Skip to content

Instantly share code, notes, and snippets.

@NotArchon
Created July 31, 2022 03:44
Show Gist options
  • Save NotArchon/990593e1fb07476ce76097963579166a to your computer and use it in GitHub Desktop.
Save NotArchon/990593e1fb07476ce76097963579166a to your computer and use it in GitHub Desktop.
package io.ruin.model.world.messages.friends.chat;
import io.ruin.model.entity.player.Player;
import io.ruin.model.entity.player.friends.FriendsChat;
import io.ruin.model.world.WorldMessage;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JoinedFriendsChat extends WorldMessage {
public final int userId;
public final String displayName;
public final FriendsChat.Rank rank;
public final int channelId;
public JoinedFriendsChat(int userId, String displayName, FriendsChat.Rank rank, int channelId) {
this.userId = userId;
this.displayName = displayName;
this.rank = rank;
this.channelId = channelId;
}
@Override
public void received() {
FriendsChat.Channel fc;
if(fromWorldId() == thisWorldId()) {
fc = FriendsChat.CHANNELS.computeIfAbsent(channelId, i
-> new FriendsChat.Channel(channelId));
} else {
fc = FriendsChat.CHANNELS.get(channelId);
if(fc == null || fc.getUsers().isEmpty())
return;
/* basically there are people in this world in the channel that was joined */
List<Player> online = onlinePlayers();
int estimatedResponseSize = Math.min(fc.getUsers().size(), online.size());
Map<Integer, Object[]> responseMap = new HashMap<>(estimatedResponseSize);
online.forEach(p -> {
FriendsChat.ChannelUser user = fc.getUser(p.getUserId());
if(user != null)
responseMap.put(p.getUserId(), new Object[]{user.getName(), user.getRank()});
});
if(!responseMap.isEmpty()) {
UpdateWorldChannel updateWorldChannel = new UpdateWorldChannel(channelId, responseMap);
updateWorldChannel.sendToWorld(fromWorldId());
}
}
fc.addUser(userId, displayName, rank, fromWorldId());
}
private static final class UpdateWorldChannel extends WorldMessage {
public final int channelId;
public final Map<Integer, Object[]> map;
private UpdateWorldChannel(int channelId, Map<Integer, Object[]> map) {
this.channelId = channelId;
this.map = map;
}
@Override
public void received() {
FriendsChat.Channel fc = FriendsChat.CHANNELS.computeIfAbsent(channelId, i -> new FriendsChat.Channel(channelId));
map.forEach((userId, info) -> {
String name = (String) info[0];
FriendsChat.Rank rank = (FriendsChat.Rank) info[1];
fc.addUser(userId, name, rank, fromWorldId());
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment