Skip to content

Commit

Permalink
Reload the webpanel data when reloading typewriter
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Nov 13, 2024
1 parent 0861b0e commit 0a34cf8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
29 changes: 29 additions & 0 deletions app/lib/models/communicator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ class SocketNotifier extends StateNotifier<Socket?> {
"stagingState",
(data) => ref.read(communicatorProvider).handleStagingState(data),
)
..on(
"updatePages",
(data) => ref.read(communicatorProvider).handleUpdatePages(data),
)
..on(
"updateExtensions",
(data) => ref.read(communicatorProvider).handleUpdateExtensions(data),
)
..on(
"createPage",
(data) => ref.read(communicatorProvider).handleCreatePage(data),
Expand Down Expand Up @@ -476,6 +484,27 @@ class Communicator {
ref.read(stagingStateProvider.notifier).state = state;
}

void handleUpdatePages(dynamic data) {
final rawPages = data as String;
final jsonPages = jsonDecode(rawPages) as List;

// ignore: unnecessary_lambdas
final pages = jsonPages.map((p) => Page.fromJson(p)).toList();
ref.read(bookProvider.notifier).book =
ref.read(bookProvider).copyWith(pages: pages);
}

void handleUpdateExtensions(dynamic data) {
final rawExtensions = data as String;
final jsonExtensions = jsonDecode(rawExtensions) as List;

// ignore: unnecessary_lambdas
final extensions =
jsonExtensions.map((e) => Extension.fromJson(e)).toList();
ref.read(bookProvider.notifier).book =
ref.read(bookProvider).copyWith(extensions: extensions);
}

void handleCreatePage(dynamic data) {
final json = jsonDecode(data as String) as Map<String, dynamic>;
final page = Page.fromJson(json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class TypewriterPaperPlugin : KotlinPlugin(), KoinComponent {
get<TypewriterCore>().load()

get<StagingManager>().loadState()
get<CommunicationHandler>().load()
get<RoadNetworkManager>().load()
get<InteractionHandler>().load()
get<EntryListeners>().load()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.corundumstudio.socketio.Configuration
import com.corundumstudio.socketio.HandshakeData
import com.corundumstudio.socketio.SocketIOClient
import com.corundumstudio.socketio.SocketIOServer
import com.google.gson.JsonArray
import lirand.api.extensions.events.listen
import lirand.api.extensions.server.server
import com.typewritermc.engine.paper.entry.StagingManager
Expand All @@ -13,6 +14,7 @@ import com.typewritermc.engine.paper.plugin
import com.typewritermc.engine.paper.utils.config
import com.typewritermc.engine.paper.utils.logErrorIfNull
import com.typewritermc.engine.paper.utils.optionalConfig
import com.typewritermc.loader.ExtensionLoader
import org.bukkit.entity.Player
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
Expand All @@ -26,6 +28,8 @@ class CommunicationHandler : KoinComponent {
private val writers: Writers by inject()
private val stagingManager: StagingManager by inject()
private val panelHost: PanelHost by inject()
private val extensionLoader: ExtensionLoader by inject()
private val extensionJson by extensionLoader::extensionJson

private val hostName: String by config(
"hostname",
Expand Down Expand Up @@ -133,6 +137,18 @@ class CommunicationHandler : KoinComponent {
}
}

fun load() {
if (!enabled) return
server?.broadcastOperations?.sendEvent("stagingState", stagingManager.stagingState.name.lowercase())

val array = JsonArray()
stagingManager.pages.forEach { (_, page) ->
array.add(page)
}
server?.broadcastOperations?.sendEvent("updatePages", array.toString())
server?.broadcastOperations?.sendEvent("updateExtensions", extensionJson.toString())
}

private fun authenticate(data: HandshakeData): Boolean {
if (auth == "none") return true
if (auth == "session") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class DelayedActionEntry(
@Help("The duration before the next triggers are fired.")
private val duration: Var<Duration> = ConstVar(Duration.ZERO),
) : CustomTriggeringActionEntry {

override fun execute(player: Player) {
DISPATCHERS_ASYNC.launch {
delay(duration.get(player).toMillis())
Expand Down

0 comments on commit 0a34cf8

Please sign in to comment.