Skip to content

Commit

Permalink
feat: Add wave management and unit management commands
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Oct 14, 2023
1 parent 4a2e5a6 commit ad5e2e4
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,20 @@ fun main() {
val application = ImperiumDiscord()

application.instances.createSingletons()
application.register(BridgeListener::class)
application.register(PingCommand::class)
application.register(ServerCommand::class)
application.register(ReportListener::class)
application.register(MapCommand::class)
application.register(SchematicCommand::class)
application.register(VerifyCommand::class)
application.register(ModerationCommand::class)
application.register(PunishmentListener::class)
for (listener in
listOf(
BridgeListener::class,
PingCommand::class,
ServerCommand::class,
ReportListener::class,
MapCommand::class,
SchematicCommand::class,
VerifyCommand::class,
ModerationCommand::class,
PunishmentListener::class,
)) {
application.register(listener)
}

val commands = application.instances.get<CommandRegistry>("slash")
val buttons = application.instances.get<CommandRegistry>("button")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import com.xpdustry.imperium.mindustry.chat.ChatMessageListener
import com.xpdustry.imperium.mindustry.chat.ChatTranslatorListener
import com.xpdustry.imperium.mindustry.command.HelpCommand
import com.xpdustry.imperium.mindustry.config.ConventionListener
import com.xpdustry.imperium.mindustry.game.KillCommand
import com.xpdustry.imperium.mindustry.game.WaveCommand
import com.xpdustry.imperium.mindustry.history.HistoryCommand
import com.xpdustry.imperium.mindustry.security.AdminRequestListener
import com.xpdustry.imperium.mindustry.security.GatekeeperListener
Expand Down Expand Up @@ -73,7 +75,8 @@ class ImperiumPlugin : AbstractMindustryPlugin() {
DistributorProvider.get().globalLocalizationSource.addLocalizationSource(source)

application.instances.createSingletons()
listOf(
for (listener in
listOf(
ConventionListener::class,
GatekeeperListener::class,
ChatTranslatorListener::class,
Expand All @@ -92,8 +95,11 @@ class ImperiumPlugin : AbstractMindustryPlugin() {
RockTheVoteCommand::class,
CoreBlockListener::class,
HelpCommand::class,
)
.forEach { application.register(it) }
WaveCommand::class,
KillCommand::class,
)) {
application.register(listener)
}
application.init()

val registry = application.instances.get<CommandRegistry>()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Imperium, the software collection powering the Xpdustry network.
* Copyright (C) 2023 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.imperium.mindustry.game

import com.xpdustry.imperium.common.application.ImperiumApplication
import com.xpdustry.imperium.common.command.Command
import com.xpdustry.imperium.common.inject.InstanceManager
import com.xpdustry.imperium.common.inject.get
import com.xpdustry.imperium.mindustry.command.SimpleVoteManager
import com.xpdustry.imperium.mindustry.command.VoteManager
import com.xpdustry.imperium.mindustry.misc.runMindustryThread
import fr.xpdustry.distributor.api.command.sender.CommandSender
import kotlin.time.Duration.Companion.seconds
import mindustry.gen.Call
import mindustry.gen.Groups

class KillCommand(instances: InstanceManager) : ImperiumApplication.Listener {
private val voteKillAll =
SimpleVoteManager<Unit>(
plugin = instances.get(),
duration = 20.seconds,
finished = { session ->
if (session.status != VoteManager.Session.Status.SUCCESS) {
Call.sendMessage("[scarlet]Vote to kill all units failed.")
return@SimpleVoteManager
}
runMindustryThread {
Groups.unit.toList().forEach { unit ->
if (!unit.isPlayer) Call.unitDespawn(unit)
}
}
})

/*
@Command(["kill"], permission = Permission.MODERATOR)
private suspend fun onKillCommand(sender: CommandSender) {
if (sender.player.unit() == Nulls.unit || sender.player.unit().dead) {
sender.sendMessage("Mate, you're already dead.")
} else {
runMindustryThread {
sender.sendMessage("[scarlet]KYS.")
sender.player.unit().kill()
}
}
}
*/

@Command(["ku"])
private fun onKillUnitsCommand(sender: CommandSender) {
if (voteKillAll.session != null) {
sender.sendMessage("There is already a vote to kill all units.")
return
}
voteKillAll.start(sender.player, true, Unit)
}

@Command(["ku", "y"])
private fun onKillUnitsYesCommand(sender: CommandSender) {
val session =
voteKillAll.session ?: return sender.sendMessage("There is no vote to kill all units.")
if (session.getVote(sender.player) != null)
return sender.sendMessage("You have already voted.")
session.setVote(sender.player, true)
Call.sendMessage("[green]${sender.player.name} voted yes to kill all units.")
}

@Command(["ku", "n"])
private fun onKillUnitsNoCommand(sender: CommandSender) {
val session =
voteKillAll.session ?: return sender.sendMessage("There is no vote to kill all units.")
if (session.getVote(sender.player) != null)
return sender.sendMessage("You have already voted.")
session.setVote(sender.player, false)
Call.sendMessage("[green]${sender.player.name} voted no to kill all units.")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Imperium, the software collection powering the Xpdustry network.
* Copyright (C) 2023 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.imperium.mindustry.game

import com.xpdustry.imperium.common.application.ImperiumApplication
import com.xpdustry.imperium.common.command.Command
import com.xpdustry.imperium.common.command.Permission
import com.xpdustry.imperium.common.command.annotation.Min
import com.xpdustry.imperium.common.inject.InstanceManager
import com.xpdustry.imperium.common.inject.get
import com.xpdustry.imperium.mindustry.command.SimpleVoteManager
import com.xpdustry.imperium.mindustry.command.VoteManager
import com.xpdustry.imperium.mindustry.command.annotation.ClientSide
import com.xpdustry.imperium.mindustry.misc.runMindustryThread
import com.xpdustry.imperium.mindustry.ui.View
import com.xpdustry.imperium.mindustry.ui.menu.MenuInterface
import com.xpdustry.imperium.mindustry.ui.menu.MenuOption
import fr.xpdustry.distributor.api.command.sender.CommandSender
import java.time.Duration
import kotlin.time.Duration.Companion.seconds
import mindustry.Vars
import mindustry.gen.Call

class WaveCommand(instances: InstanceManager) : ImperiumApplication.Listener {
private val voteNewWave =
SimpleVoteManager<Int>(
plugin = instances.get(),
duration = 45.seconds,
finished = {
if (it.status != VoteManager.Session.Status.SUCCESS) {
Call.sendMessage("[scarlet]Vote to skip the wave failed.")
return@SimpleVoteManager
}
runMindustryThread {
Vars.state.wave += it.target
Vars.state.wavetime = Vars.state.rules.waveSpacing
Call.sendMessage("[green]Skipped ${it.target} wave(s).")
}
})

private val waveSkipInterface =
MenuInterface.create(instances.get()).apply {
addTransformer { _, pane ->
pane.title = "Skip Wave"
pane.content = "How many waves do you want to skip?"
pane.options.addRow(
listOf(3, 5, 10, 15).map { skip ->
MenuOption("[orange]$skip") { view ->
view.close()
if (voteNewWave.session != null)
return@MenuOption view.viewer.sendMessage(
"A vote is already running.")
val session = voteNewWave.start(view.viewer, true, skip)
Call.sendMessage(
"[green]${view.viewer.name} started a vote to skip the $skip wave(s). ${session.required} are required.")
}
})
pane.options.addRow(MenuOption("[lightgray]Cancel", View::closeAll))
}
}

@Command(["wave", "set", "time"], Permission.MODERATOR)
@ClientSide
private fun onWaveSetTime(sender: CommandSender, time: Duration) {
Vars.state.wavetime = time.seconds.toFloat() * 60F
sender.sendMessage("Set wave time to $time")
}

@Command(["wave", "set", "counter"], Permission.MODERATOR)
@ClientSide
private fun onWaveSetCounter(sender: CommandSender, wave: Int) {
Vars.state.wave = wave
Vars.state.wavetime = Vars.state.rules.waveSpacing
sender.sendMessage("Set wave to counter $wave")
}

@Command(["wave", "run"], Permission.MODERATOR)
@ClientSide
private fun onWaveRun(sender: CommandSender, @Min(0) waves: Int = 0) {
repeat(waves) { Vars.logic.runWave() }
sender.sendMessage("Skipped $waves wave(s).")
}

@Command(["wave", "skip"])
@ClientSide
private fun onWaveSkip(sender: CommandSender) {
waveSkipInterface.open(sender.player)
}

@Command(["ws", "y"])
@ClientSide
private fun onWaveSkipYes(sender: CommandSender) {
val session = voteNewWave.session ?: return sender.sendMessage("No vote is running.")
if (session.getVote(sender.player) != null) return sender.sendMessage("You already voted.")
Call.sendMessage(
"[green]${sender.player.name} voted to skip the wave. (${session.votes + 1}/${session.required})")
session.setVote(sender.player, true)
}

@Command(["ws", "n"])
@ClientSide
private fun onWaveSkipNo(sender: CommandSender) {
val session = voteNewWave.session ?: return sender.sendMessage("No vote is running.")
if (session.getVote(sender.player) != null) return sender.sendMessage("You already voted.")
Call.sendMessage(
"[green]${sender.player.name} voted to not skip the wave. (${session.votes - 1}/${session.required})")
session.setVote(sender.player, false)
}
}

0 comments on commit ad5e2e4

Please sign in to comment.