-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9fed3c
commit 107489c
Showing
11 changed files
with
167 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package gay.j10a1n15.sillygames.rpc | ||
|
||
data class RpcInfo( | ||
var firstLine: String, | ||
var secondLine: String, | ||
|
||
var start: Long, | ||
var end: Long? = null, | ||
|
||
var thumbnail: String? = null, | ||
var icon: String? = null, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package gay.j10a1n15.sillygames.rpc | ||
|
||
import com.google.gson.JsonObject | ||
import com.jagrosh.discordipc.IPCClient | ||
import com.jagrosh.discordipc.IPCListener | ||
import com.jagrosh.discordipc.entities.RichPresence | ||
import com.jagrosh.discordipc.entities.pipe.PipeStatus | ||
import gay.j10a1n15.sillygames.games.GameManager | ||
import gay.j10a1n15.sillygames.utils.Scheduling | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.ScheduledFuture | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
private const val CLIENT_ID = 1287128195124039715L | ||
|
||
object RpcManager : IPCListener { | ||
|
||
private var client: IPCClient? = null | ||
private var lastInfo: RpcInfo? = null | ||
private var scheduler: ScheduledFuture<*>? = null | ||
private var started: Boolean = false | ||
|
||
fun start() { | ||
if (started) return | ||
CompletableFuture.runAsync { | ||
client = IPCClient(CLIENT_ID) | ||
client?.setListener(this) | ||
client?.connect() | ||
} | ||
} | ||
|
||
fun stop() { | ||
if (client?.status != PipeStatus.CONNECTED) return | ||
CompletableFuture.runAsync { | ||
close() | ||
} | ||
} | ||
|
||
override fun onReady(client: IPCClient?) { | ||
scheduler = Scheduling.schedule(Duration.ZERO, 1.seconds) { | ||
val info = (GameManager.game as? RpcProvider)?.getRpcInfo() | ||
if (info != lastInfo) { | ||
lastInfo = info?.copy() | ||
updatePresence() | ||
} | ||
} | ||
} | ||
|
||
override fun onClose(client: IPCClient?, json: JsonObject?) = close() | ||
override fun onDisconnect(client: IPCClient?, t: Throwable?) = close() | ||
|
||
private fun close() { | ||
scheduler?.cancel(true) | ||
scheduler = null | ||
started = false | ||
client = null | ||
} | ||
|
||
private fun updatePresence() { | ||
val client = client ?: return | ||
if (lastInfo == null) { | ||
client.sendRichPresence(null) | ||
} else { | ||
val info = lastInfo!! | ||
client.sendRichPresence(RichPresence.Builder().apply { | ||
setDetails(info.firstLine) | ||
setState(info.secondLine) | ||
setStartTimestamp(info.start) | ||
info.end?.let { setEndTimestamp(it) } | ||
info.thumbnail?.let { setLargeImage(it) } | ||
info.icon?.let { setSmallImage(it) } | ||
}.build()) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package gay.j10a1n15.sillygames.rpc | ||
|
||
interface RpcProvider { | ||
|
||
fun getRpcInfo(): RpcInfo? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/gay/j10a1n15/sillygames/utils/Scheduling.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package gay.j10a1n15.sillygames.utils | ||
|
||
import java.util.concurrent.Executors | ||
import java.util.concurrent.ScheduledExecutorService | ||
import java.util.concurrent.ScheduledFuture | ||
import java.util.concurrent.TimeUnit | ||
import java.util.concurrent.atomic.AtomicInteger | ||
import kotlin.time.Duration | ||
import kotlin.time.DurationUnit | ||
|
||
object Scheduling { | ||
|
||
private val counter = AtomicInteger(0) | ||
private val scheduler: ScheduledExecutorService = Executors.newScheduledThreadPool(10) { target: Runnable? -> | ||
Thread(target, "Scheduling-Thread-${counter.getAndIncrement()}") | ||
} | ||
|
||
fun schedule(time: Duration, runnable: () -> Unit): ScheduledFuture<*> = scheduler.schedule( | ||
runnable, | ||
time.toLong(DurationUnit.MILLISECONDS), | ||
TimeUnit.MILLISECONDS, | ||
) | ||
|
||
fun schedule(initalDelay: Duration, delay: Duration, runnable: () -> Unit): ScheduledFuture<*> = scheduler.scheduleAtFixedRate( | ||
runnable, | ||
initalDelay.toLong(DurationUnit.MILLISECONDS), | ||
delay.toLong(DurationUnit.MILLISECONDS), TimeUnit.MILLISECONDS, | ||
) | ||
} |