Skip to content

Commit 72a78d6

Browse files
committed
fixxes
1 parent e5e4113 commit 72a78d6

7 files changed

Lines changed: 56 additions & 32 deletions

File tree

src/main/kotlin/dev/robothanzo/werewolf/game/steps/DayStep.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import dev.robothanzo.werewolf.game.GameStep
77
import dev.robothanzo.werewolf.service.GameSessionService
88
import dev.robothanzo.werewolf.service.GameStateService
99
import dev.robothanzo.werewolf.service.SpeechService
10+
import dev.robothanzo.werewolf.utils.CmdUtils
1011
import org.springframework.context.annotation.Lazy
1112
import org.springframework.stereotype.Component
1213

@@ -25,11 +26,14 @@ class DayStep(
2526
speechService.setAllMute(session.guildId, true) // Ensure silence for announcement
2627
session.courtTextChannel?.sendMessage("# **:sunny: 天亮了**")?.queue()
2728
session.courtVoiceChannel?.play(Audio.Resource.MORNING) {
28-
gameSessionService.withLockedSession(session.guildId) { lockedSession ->
29-
if (lockedSession.currentState == id) {
30-
service.nextStep(lockedSession)
29+
// Audio is ~7s, add 3s delay to make total ~10s
30+
CmdUtils.schedule({
31+
gameSessionService.withLockedSession(session.guildId) { lockedSession ->
32+
if (lockedSession.currentState == id) {
33+
service.nextStep(lockedSession)
34+
}
3135
}
32-
}
36+
}, 3000L)
3337
}
3438
}
3539

@@ -38,7 +42,7 @@ class DayStep(
3842
}
3943

4044
override fun getDurationSeconds(session: Session): Int {
41-
return 41 // 40s for discussion + 1s buffer before next phase starts
45+
return 10
4246
}
4347

4448
override fun handleInput(session: Session, input: Map<String, Any>): Map<String, Any> {

src/main/kotlin/dev/robothanzo/werewolf/game/steps/JudgeDecisionStep.kt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ class JudgeDecisionStep : GameStep {
5656
when (action) {
5757
"end_game_confirm" -> {
5858
performEndGame(session)
59+
60+
// Finalize state change to SETUP so UI updates
61+
session.stateData.pendingNextStep = null
62+
session.stateData.gameEndReason = null
63+
WerewolfApplication.gameStateService.startStep(session, "SETUP")
64+
5965
return mapOf("success" to true)
6066
}
6167

@@ -106,26 +112,26 @@ class JudgeDecisionStep : GameStep {
106112
vc.upsertPermissionOverride(everyone).grant(Permission.VOICE_SPEAK).queue()
107113
}
108114

109-
// 2. Grant Court/Off-court permissions (Read/Write) to everyone
110-
// "開放法院、場外的發言和閱讀權限給所有人" -> Everyone can read/speak in Court Text and Spectator Text?
111-
// Usually "Off-court" means Spectator channel.
115+
// 2. Grant permissions (Read/Write) to everyone
112116

113-
val publicRole = guild.publicRole
117+
courtText?.upsertPermissionOverride(guild.publicRole)
118+
?.grant(Permission.VIEW_CHANNEL, Permission.MESSAGE_SEND)
119+
?.queue()
114120

115-
courtText?.upsertPermissionOverride(publicRole)
121+
session.spectatorRole?.let { courtText?.upsertPermissionOverride(it) }
116122
?.grant(Permission.VIEW_CHANNEL, Permission.MESSAGE_SEND)
117123
?.queue()
118124

119-
spectatorText?.upsertPermissionOverride(publicRole)
125+
spectatorText?.upsertPermissionOverride(guild.publicRole)
120126
?.grant(Permission.VIEW_CHANNEL, Permission.MESSAGE_SEND)
121127
?.queue()
122128

123129
// 3. "開放死人旁觀語音發言權限" -> Dead players (Spectator Role) can speak.
124130
// Already covered by granting @everyone VOICE_SPEAK in court voice?
125131
// Just to be sure, let's explicitly grant it to Spectator Role if exists
126-
val spectatorRole = session.spectatorRole
127-
if (spectatorRole != null && courtVoice != null) {
128-
courtVoice.upsertPermissionOverride(spectatorRole).grant(Permission.VOICE_SPEAK).queue()
132+
val specRole = session.spectatorRole
133+
if (specRole != null && courtVoice != null) {
134+
courtVoice.upsertPermissionOverride(specRole).grant(Permission.VOICE_SPEAK).queue()
129135
}
130136
}
131137
}

src/main/kotlin/dev/robothanzo/werewolf/listeners/GuildJoinListener.kt

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import net.dv8tion.jda.api.entities.channel.concrete.TextChannel
99
import net.dv8tion.jda.api.events.guild.GuildJoinEvent
1010
import net.dv8tion.jda.api.events.guild.GuildLeaveEvent
1111
import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent
12+
import net.dv8tion.jda.api.events.session.ReadyEvent
1213
import net.dv8tion.jda.api.hooks.ListenerAdapter
1314
import org.slf4j.LoggerFactory
1415
import org.springframework.stereotype.Component
@@ -19,6 +20,19 @@ class GuildJoinListener(
1920
) : ListenerAdapter() {
2021
private val log = LoggerFactory.getLogger(GuildJoinListener::class.java)
2122

23+
override fun onReady(event: ReadyEvent) {
24+
log.info("JDA Ready, syncing {} guilds with sessions...", event.jda.guilds.size)
25+
val allSessions = gameSessionService.getAllSessions()
26+
val joinedGuildIds = event.jda.guilds.map { it.idLong }.toSet()
27+
28+
allSessions.forEach { session ->
29+
if (!joinedGuildIds.contains(session.guildId)) {
30+
log.info("Deleting stale session for guild {} (not in joined guilds)", session.guildId)
31+
cleanupGuild(session.guildId)
32+
}
33+
}
34+
}
35+
2236
override fun onGuildJoin(event: GuildJoinEvent) {
2337
log.info("Bot joined guild: {} ({})", event.guild.name, event.guild.id)
2438
checkAndSetup(event.guild)
@@ -32,8 +46,14 @@ class GuildJoinListener(
3246

3347
override fun onGuildLeave(event: GuildLeaveEvent) {
3448
log.info("Bot left guild: {}", event.guild.id)
35-
gameSessionService.deleteSession(event.guild.idLong)
36-
WerewolfApplication.speechService.interruptSession(event.guild.idLong)
49+
cleanupGuild(event.guild.idLong)
50+
}
51+
52+
private fun cleanupGuild(guildId: Long) {
53+
gameSessionService.deleteSession(guildId)
54+
WerewolfApplication.speechService.interruptSession(guildId)
55+
WerewolfApplication.policeService.interrupt(guildId)
56+
WerewolfApplication.expelService.endExpelPoll(guildId)
3757
}
3858

3959
private fun checkAndSetup(guild: Guild) {

src/main/kotlin/dev/robothanzo/werewolf/service/impl/ExpelServiceImpl.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class ExpelServiceImpl : ExpelService {
3636

3737
override fun endExpelPoll(guildId: Long) {
3838
sessions.remove(guildId)
39-
// remove any running poll instances as well
4039
polls.remove(guildId)
4140
log.info("Ended expel poll for guild {}", guildId)
4241
}

src/main/kotlin/dev/robothanzo/werewolf/service/impl/PoliceServiceImpl.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,11 @@ class PoliceServiceImpl(
326326
}
327327

328328
override fun interrupt(guildId: Long, triggerCallback: Boolean) {
329-
val policeSession = sessions.remove(guildId)
330-
if (policeSession != null) {
331-
cancelPollTasks(policeSession)
332-
policeSession.session.courtTextChannel?.sendMessage("警長投票已終止")?.queue()
329+
sessions.remove(guildId)?.let {
330+
cancelPollTasks(it)
331+
it.session.courtTextChannel?.sendMessage("警長投票已終止")?.queue()
333332
if (triggerCallback) {
334-
policeSession.finishedCallback?.invoke()
333+
it.finishedCallback?.invoke()
335334
}
336335
}
337336
}

src/main/kotlin/dev/robothanzo/werewolf/service/impl/SpeechServiceImpl.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -347,19 +347,15 @@ class SpeechServiceImpl(
347347
}
348348

349349
override fun interruptSession(guildId: Long, triggerCallback: Boolean) {
350-
val speechSession = speechSessions[guildId]
351-
if (speechSession != null) {
352-
val guild = WerewolfApplication.jda.getGuildById(guildId)
353-
if (guild != null) {
354-
val session = gameSessionService.getSession(guildId).getOrNull()
355-
session?.courtTextChannel?.sendMessage("法官已強制終止發言流程")?.queue()
350+
speechSessions.remove(guildId)?.let {
351+
gameSessionService.getSession(guildId).getOrNull()?.let {
352+
it.courtTextChannel?.sendMessage("法官已強制終止發言流程")?.queue()
356353
}
357354

358-
speechSession.order.clear()
359-
stopCurrentSpeaker(speechSession)
360-
speechSessions.remove(guildId)
355+
it.order.clear()
356+
stopCurrentSpeaker(it)
361357
if (triggerCallback) {
362-
speechSession.finishedCallback?.invoke()
358+
it.finishedCallback?.invoke()
363359
}
364360
}
365361
}
-297 KB
Binary file not shown.

0 commit comments

Comments
 (0)