Skip to content

Commit

Permalink
feat(discord): Implement party size
Browse files Browse the repository at this point in the history
- fix: can't hide start timestamp
  • Loading branch information
iHDeveloper committed Jan 7, 2021
1 parent 1a3f7f7 commit e02c616
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ public final class DiscordRPCBuffer {
private String details;
private String state;
private OffsetDateTime startTimestamp;
private int partySize;
private int partyMax;
private boolean changed;

public DiscordRPCBuffer() {
reset();
}

/**
* Resets the buffer data
*/
Expand All @@ -24,6 +30,8 @@ public synchronized void reset() {
this.state = null;
this.startTimestamp = null;
this.changed = true;
this.partySize = -1;
this.partyMax = -1;
}

/**
Expand Down Expand Up @@ -82,6 +90,44 @@ public synchronized OffsetDateTime getStartTimestamp() {
return startTimestamp;
}

/**
* Sets the current party size
*
* @param partySize The current party size
*/
public synchronized void setPartySize(int partySize) {
this.partySize = partySize;
this.changed = true;
}

/**
* Returns the current party size
*
* @return The party size
*/
public synchronized int getPartySize() {
return partySize;
}

/**
* Sets the maximum number of the players in the party
*
* @param partyMax The number of the players
*/
public synchronized void setPartyMax(int partyMax) {
this.partyMax = partyMax;
this.changed = true;
}

/**
* Returns the maximum number of players in the party
*
* @return The maximum number of players in the party
*/
public synchronized int getPartyMax() {
return partyMax;
}

/**
* Check if the buffer has changed
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@ public static void setStartTimestamp(OffsetDateTime timestamp) {
getInstance().thread.getBuffer().setStartTimestamp(timestamp);
}

/**
* Sets the party information on the buffer
*
* @param size The number of the players in the party
* @param max The maximum number of the players in the party
*/
public static void setPartyInfo(int size, int max) {
DiscordRPCBuffer buffer = getInstance().thread.getBuffer();
buffer.setPartySize(size);
buffer.setPartyMax(max);
}

/**
* Resets the party information on the buffer
*/
public static void resetPartyInfo() {
setPartyInfo(-1, -1);
}

private static DiscordRPCSubSystem getInstance() {
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.subsystem.discordrpc;

import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.entitySystem.event.ReceiveEvent;
import org.terasology.entitySystem.systems.BaseComponentSystem;
import org.terasology.entitySystem.systems.RegisterMode;
import org.terasology.entitySystem.systems.RegisterSystem;
import org.terasology.game.Game;
import org.terasology.logic.afk.AfkEvent;
import org.terasology.logic.delay.DelayManager;
import org.terasology.logic.delay.PeriodicActionTriggeredEvent;
import org.terasology.logic.players.LocalPlayer;
import org.terasology.logic.players.event.LocalPlayerInitializedEvent;
import org.terasology.network.ClientComponent;
import org.terasology.network.NetworkMode;
import org.terasology.network.NetworkSystem;
import org.terasology.registry.In;
Expand All @@ -24,6 +29,8 @@
*/
@RegisterSystem(RegisterMode.CLIENT)
public final class DiscordRPCSystem extends BaseComponentSystem {
private static final String UPDATE_PARTY_SIZE_ID = "discord-rpc:party-size";
private static final long UPDATE_PARTY_SIZE_PERIOD = 25L * 1000L;

@In
private Game game;
Expand All @@ -34,30 +41,53 @@ public final class DiscordRPCSystem extends BaseComponentSystem {
@In
private NetworkSystem networkSystem;

@In
private EntityManager entityManager;

@In
private DelayManager delayManager;

private int onlinePlayers;

@Override
public void initialise() {
onlinePlayers = 1;

DiscordRPCSubSystem.discover();
}

@Override
public void preBegin() {
DiscordRPCSubSystem.setGameplayName("Custom");
DiscordRPCSubSystem.setState(getPartyState());
DiscordRPCSubSystem.setState(null);
DiscordRPCSubSystem.setStartTimestamp(null);
}

@Override
public void postBegin() {
DiscordRPCSubSystem.setStartTimestamp(OffsetDateTime.now());
setPartyState();
}

@Override
public void shutdown() {
if (delayManager.hasPeriodicAction(player.getClientEntity(), UPDATE_PARTY_SIZE_ID)) {
delayManager.cancelPeriodicAction(player.getClientEntity(), UPDATE_PARTY_SIZE_ID);
}

DiscordRPCSubSystem.reset();
DiscordRPCSubSystem.setState("In Main Menu");
DiscordRPCSubSystem.setStartTimestamp(null);
}

@ReceiveEvent
public void onPlayerInitialized(LocalPlayerInitializedEvent event, EntityRef player) {
/* Adds the periodic action when the player is hosting or playing online to update party size */
if (networkSystem.getMode() != NetworkMode.NONE) {
delayManager.addPeriodicAction(player, UPDATE_PARTY_SIZE_ID, 0, UPDATE_PARTY_SIZE_PERIOD);
}
}

@ReceiveEvent
public void onAfk(AfkEvent event, EntityRef entityRef) {
if (isServer() && player.getClientEntity().equals(entityRef)) {
Expand All @@ -66,22 +96,38 @@ public void onAfk(AfkEvent event, EntityRef entityRef) {

if (event.isAfk()) {
DiscordRPCSubSystem.setState("Idle");
DiscordRPCSubSystem.setStartTimestamp(null);
DiscordRPCSubSystem.resetPartyInfo();
} else {
DiscordRPCSubSystem.setState(getPartyState());
DiscordRPCSubSystem.setStartTimestamp(OffsetDateTime.now());
setPartyState();
}
}

public String getPartyState() {
NetworkMode networkMode = networkSystem.getMode();
@ReceiveEvent
public void onPeriodicTrigger(PeriodicActionTriggeredEvent event, EntityRef entity) {
if (event.getActionId().equals(UPDATE_PARTY_SIZE_ID)) {
onlinePlayers = 0;
entityManager.getEntitiesWith(ClientComponent.class).forEach(ignored -> onlinePlayers++);
DiscordRPCSubSystem.setPartyInfo(onlinePlayers, 99);
}
}

private void setPartyState() {
final NetworkMode networkMode = networkSystem.getMode();

String mode = "Playing Online";
if (networkMode == NetworkMode.DEDICATED_SERVER) {
mode = "Hosting";
} else if (networkMode == NetworkMode.NONE) {
mode = "Playing Solo";
DiscordRPCSubSystem.setPartyInfo(1, 1);
}

DiscordRPCSubSystem.setState(mode);
if (networkMode != NetworkMode.NONE) {

/* The player is playing online or hosting a game */
DiscordRPCSubSystem.setPartyInfo(onlinePlayers, 99);
}
return mode;
}

private boolean isServer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,25 @@ public synchronized DiscordRPCBuffer getBuffer() {
}

private RichPresence build() {
return new RichPresence.Builder()
.setLargeImage(DISCORD_APP_DEFAULT_IMAGE)
.setDetails(buffer.getDetails())
.setState(buffer.getState())
.setStartTimestamp(buffer.getStartTimestamp())
.build();
RichPresence.Builder builder = new RichPresence.Builder()
.setLargeImage(DISCORD_APP_DEFAULT_IMAGE);

if (buffer.getDetails() != null)
builder.setDetails(buffer.getDetails());

if (buffer.getState() != null)
builder.setState(buffer.getState());

if (buffer.getStartTimestamp() != null)
builder.setStartTimestamp(buffer.getStartTimestamp());

int partySize = buffer.getPartySize();
int partyMax = buffer.getPartyMax();
if (partySize > 0 && partyMax > 0) {
builder.setParty("null", partySize, partyMax);
}

return builder.build();
}

private void reset(boolean resetConnection) {
Expand Down

0 comments on commit e02c616

Please sign in to comment.