Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.8.1 #74

Merged
merged 16 commits into from
Aug 9, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class RedisConnection extends MessengerConnection implements IMessengerConnection {
private final Vector<RedisSubscriber> subscribers = new Vector<>();
private final Map<PacketIdentification, List<PacketListener<? extends Packet.Wrapper>>> listeners = new HashMap<>();
private final RedisPublisher publisher;
private final RedisClient.Builder clientBuilder;
private boolean isAlive = false;
private final AtomicBoolean alive = new AtomicBoolean(false);
private ExecutorService executorService;
private final FailService failService;
private final AESCryptor cryptor;
Expand All @@ -37,7 +38,7 @@ public RedisConnection(RedisClient.Builder clientBuilder, AESCryptor cryptor) {
}

protected void subscribe(IMessageCacheService<?> cache, PluginLogger logger, Packet.Node senderUUID) {
if(!this.isAlive) return;
if(!this.alive.get()) return;

this.executorService.submit(() -> {
try {
Expand All @@ -62,24 +63,28 @@ protected void subscribe(IMessageCacheService<?> cache, PluginLogger logger, Pac
}

public void startListening(IMessageCacheService<?> cache, PluginLogger logger, Packet.Node senderUUID) {
if(this.isAlive) throw new IllegalStateException("The RedisService is already running! You can't start it again! Shut it down with `.kill()` first and then try again!");
if(this.alive.get()) throw new IllegalStateException("The RedisService is already running! You can't start it again! Shut it down with `.kill()` first and then try again!");
this.executorService = Executors.newFixedThreadPool(2);

this.isAlive = true;
this.alive.set(true);

this.subscribe(cache, logger, senderUUID);
}

@Override
public void kill() {
this.isAlive = false;
this.alive.set(false);
this.failService.kill();

for (Iterator<RedisSubscriber> iterator = this.subscribers.elements().asIterator(); iterator.hasNext(); ) {
RedisSubscriber subscriber = iterator.next();
subscriber.shutdown();
}

try {
this.publisher.kill();
} catch (Exception ignore) {}

try {
this.executorService.shutdown();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import group.aelysium.rustyconnector.core.lib.crypt.AESCryptor;
import group.aelysium.rustyconnector.toolkit.core.packet.Packet;
import group.aelysium.rustyconnector.toolkit.core.serviceable.interfaces.Service;
import io.lettuce.core.RedisChannelHandler;
import io.lettuce.core.RedisConnectionStateAdapter;
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
import io.lettuce.core.pubsub.api.async.RedisPubSubAsyncCommands;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class RedisPublisher {
public class RedisPublisher implements Service {
private final RedisClient client;
private StatefulRedisPubSubConnection<String, String> connection;
private final AESCryptor cryptor;
private final AtomicBoolean killed = new AtomicBoolean(false);
protected RedisPublisher(RedisClient client, AESCryptor cryptor) {
this.client = client;
this.client.addListener(new RedisPublisherListener());
Expand All @@ -35,6 +38,8 @@ public void shutdown() {
* @throws IllegalStateException If you attempt to send a received RedisMessage.
*/
public void publish(Packet packet) {
if(killed.get()) return;

String signedPacket;
try {
signedPacket = this.cryptor.encrypt(packet.toString());
Expand All @@ -50,6 +55,11 @@ public void publish(Packet packet) {
async.publish(this.client.dataChannel(), signedPacket);
}

public void kill() {
this.killed.set(true);
this.connection.close();
}

static class RedisPublisherListener extends RedisConnectionStateAdapter {
@Override
public void onRedisExceptionCaught(RedisChannelHandler<?, ?> connection, Throwable cause) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public Session session() {
new MCLoaderMatchPlayer(
player_uuid,
entryObject.get("username").getAsString(),
entryObject.get("schema").getAsString(),
DefaultRankResolver.New().resolve(entryObject.get("rank").getAsJsonObject())
entryObject.get("rank_schema").getAsString(),
DefaultRankResolver.New().resolve(entryObject.get("rank_schema").getAsJsonObject())
)
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ protected DefaultRankResolver() {}

@Override
public IPlayerRank resolve(JsonObject object) throws IllegalStateException {
String schema = object.get("schema").getAsString();
String schema = object.get("rank_schema").getAsString();

if(schema.equals(RandomizedPlayerRank.schema()))
return RandomizedPlayerRank.New();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public String schemaName() {
@Override
public JsonObject toJSON() {
JsonObject object = new JsonObject();
object.add("schema", new JsonPrimitive(this.schemaName()));
object.add("rank_schema", new JsonPrimitive(this.schemaName()));
object.add("elo", new JsonPrimitive(this.elo));
return object;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public String schemaName() {
@Override
public JsonObject toJSON() {
JsonObject object = new JsonObject();
object.add("schema", new JsonPrimitive(this.schemaName()));
object.add("rank_schema", new JsonPrimitive(this.schemaName()));
object.add("mu", new JsonPrimitive(this.mu));
object.add("sigma", new JsonPrimitive(this.sigma));
return object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public String schemaName() {
@Override
public JsonObject toJSON() {
JsonObject object = new JsonObject();
object.add("schema", new JsonPrimitive(this.schemaName()));
object.add("rank_schema", new JsonPrimitive(this.schemaName()));
return object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public String schemaName() {
@Override
public JsonObject toJSON() {
JsonObject object = new JsonObject();
object.add("schema", new JsonPrimitive(this.schemaName()));
object.add("rank_schema", new JsonPrimitive(this.schemaName()));
object.add("wins", new JsonPrimitive(this.wins));
object.add("losses", new JsonPrimitive(this.losses));
return object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public String schemaName() {
@Override
public JsonObject toJSON() {
JsonObject object = new JsonObject();
object.add("schema", new JsonPrimitive(this.schemaName()));
object.add("rank_schema", new JsonPrimitive(this.schemaName()));
object.add("wins", new JsonPrimitive(this.wins));
object.add("losses", new JsonPrimitive(this.losses));
object.add("ties", new JsonPrimitive(this.ties));
Expand Down
2 changes: 1 addition & 1 deletion plugin/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ org.gradle.jvmargs = -Xmx4G
java_version = 17

# Plugin Properties
plugin_version = 0.8.0
plugin_version = 0.8.1
maven_group = group.aelysium.rustyconnector
archives_base_name = rustyconnector
config_version = 6
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package group.aelysium.rustyconnector.toolkit.velocity.matchmaking;

import group.aelysium.rustyconnector.toolkit.core.matchmaking.IPlayerRank;
import group.aelysium.rustyconnector.toolkit.velocity.load_balancing.ISortable;
import group.aelysium.rustyconnector.toolkit.velocity.player.IPlayer;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package group.aelysium.rustyconnector.toolkit.velocity.matchmaking;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public interface IVelocityPlayerRank extends group.aelysium.rustyconnector.toolkit.core.matchmaking.IPlayerRank {
/**
* Returns the computer used to compute new ranks.
*/
IComputor computor();
@NotNull IComputer computer();

interface IComputor {
interface IComputer {
/**
* Compute the new rank of all the winners and losers.
* @param winners The winners.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ class Send {
} catch (NoSuchElementException e) {
logger.send(ProxyLang.RC_SEND_NO_FAMILY.build(familyName));
} catch (Exception e) {
e.printStackTrace();
logger.send(Component.text("There was an issue using that command! "+e.getMessage(), NamedTextColor.RED));
}
return Command.SINGLE_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,7 @@ protected void register() throws IllegalStateException, NoOutputException {

this.maxMembers = IYAML.getValue(this.data, "max-members", Integer.class);

try {
this.friendsOnly = IYAML.getValue(this.data, "friends-only", Boolean.class);
if(this.friendsOnly)
Tinder.get().services().friends().orElseThrow();
} catch (Exception ignore) {
Tinder.get().logger().send(ProxyLang.BOXED_MESSAGE_COLORED.build("[friends-only] in `party.yml` is set to true. But the friends module isn't enabled! Ignoring...", NamedTextColor.YELLOW));
this.friendsOnly = false;
}
this.friendsOnly = IYAML.getValue(this.data, "friends-only", Boolean.class);
this.localOnly = IYAML.getValue(this.data, "local-only", Boolean.class);

this.partyLeader_onlyLeaderCanInvite = IYAML.getValue(this.data, "party-leader.only-leader-can-invite", Boolean.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public Request connect(IPlayer player) {
Request request = new Request(player, result);

if(!this.validateLoadBalancer()) {
result.complete(ConnectionResult.failed(Component.text("There are no servers for you to connect to!")));
result.complete(ConnectionResult.failed(Component.text("There are no available servers to connect you to! Try again later.")));
return request;
}
if(!this.whitelisted(player)) {
Expand All @@ -130,14 +130,16 @@ public Request connect(IPlayer player) {
for (int attempt = 1; attempt <= attemptsLeft; attempt++) {
IMCLoader server = this.loadBalancer.current().orElse(null);
if(server == null) {
result.complete(ConnectionResult.failed(Component.text("There are no servers for you to connect to!")));
result.complete(ConnectionResult.failed(Component.text("There are no available servers to connect you to! Try again later.")));
return request;
}

serverResponse = Optional.of(server.connect(player));
this.loadBalancer.forceIterate();
}

if(!result.isDone()) result.complete(ConnectionResult.failed(Component.text("There are no available servers to connect you to! Try again later.")));

return serverResponse.orElse(request);
}
}
Expand All @@ -153,7 +155,7 @@ public Request connect(IPlayer player) {
Request request = new Request(player, result);

if(!this.validateLoadBalancer()) {
result.complete(ConnectionResult.failed(Component.text("There are no servers for you to connect to!")));
result.complete(ConnectionResult.failed(Component.text("There are no available servers to connect you to! Try again later.")));
return request;
}
if(!this.whitelisted(player)) {
Expand All @@ -162,7 +164,7 @@ public Request connect(IPlayer player) {
}
IMCLoader server = this.loadBalancer.current().orElse(null);
if(server == null) {
result.complete(ConnectionResult.failed(Component.text("There are no server to connect to. Try again later.")));
result.complete(ConnectionResult.failed(Component.text("There are no available servers to connect you to! Try again later.")));
return request;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import group.aelysium.rustyconnector.toolkit.core.matchmaking.IPlayerRank;
import group.aelysium.rustyconnector.toolkit.velocity.matchmaking.IVelocityPlayerRank;
import group.aelysium.rustyconnector.toolkit.velocity.player.IPlayer;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

Expand All @@ -12,21 +13,21 @@ public class MatchPlayer implements IMatchPlayer {
private final IVelocityPlayerRank rank;
private final String gameId;

public MatchPlayer(IPlayer player, IVelocityPlayerRank rank, String gameId) {
public MatchPlayer(@NotNull IPlayer player, @NotNull IVelocityPlayerRank rank, @NotNull String gameId) {
this.player = player;
this.rank = rank;
this.gameId = gameId;
}

public IPlayer player() {
public @NotNull IPlayer player() {
return this.player;
}

public IVelocityPlayerRank gameRank() {
public @NotNull IVelocityPlayerRank gameRank() {
return this.rank;
}

public String gameId() {
public @NotNull String gameId() {
return this.gameId;
}

Expand Down
Loading
Loading