Skip to content

Commit

Permalink
🕹️ Game Managment (#10)
Browse files Browse the repository at this point in the history
* 🕹️ Added Game and GameManger

* 🕹️ canJoin gets multiple Players

* 🕹️ added Game Registering

* 🕹️ added joinGame

* 🕹️ fixed games array, added createGame and simplified joinGame

* 🕹️ cleanup and joining works :D

* 🕹️ added abstract onJoin

* 🕹️ added instances, uuid and eventNode

* 🌎 Made Game only have one instance

* 🕹️ made onLeave and playerGame Map

* 🕹️ added unregistering for player in GameManger

* 🕹️ there was a typo whole time?
  • Loading branch information
DasLixou authored Apr 26, 2022
1 parent da4caff commit 017cd29
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/kotlin/cc/lixou/stracciatella/Stracciatella.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cc.lixou.stracciatella

import cc.lixou.stracciatella.game.GameManager
import net.minestom.server.MinecraftServer
import net.minestom.server.coordinate.Pos
import net.minestom.server.event.player.PlayerDisconnectEvent
import net.minestom.server.event.player.PlayerLoginEvent
import net.minestom.server.instance.block.Block

Expand All @@ -19,6 +21,9 @@ class Stracciatella {
}

val eventHandler = MinecraftServer.getGlobalEventHandler()
eventHandler.addListener(PlayerDisconnectEvent::class.java) { event ->
GameManager.unregisterPlayer(event.player)
}
eventHandler.addListener(PlayerLoginEvent::class.java) { event ->
val player = event.player
event.setSpawningInstance(instanceContainer)
Expand Down
48 changes: 48 additions & 0 deletions src/main/kotlin/cc/lixou/stracciatella/game/Game.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cc.lixou.stracciatella.game

import net.minestom.server.entity.Player
import net.minestom.server.event.EventFilter
import net.minestom.server.event.EventNode
import net.minestom.server.event.trait.PlayerEvent
import net.minestom.server.instance.Instance
import world.cepi.kstom.Manager
import java.util.*

abstract class Game {

protected val players = ArrayList<Player>()
protected lateinit var instance: Instance
protected val uuid: UUID = UUID.randomUUID()
protected val eventNode =
EventNode.type("${this.javaClass.simpleName}-${uuid}", EventFilter.INSTANCE) { event, instance ->
if (event is PlayerEvent) {
return@type players.contains(event.player)
} else {
return@type this.instance.uniqueId.equals(instance)
}
}

init {
Manager.globalEvent.addChild(eventNode)
}

/**
* @param players the players that should join
* @return false if game is full, then creates new instance
*/
abstract fun canJoin(players: Array<Player>): Boolean

abstract fun onJoin(player: Player)
abstract fun onLeave(player: Player)

fun addPlayers(players: Array<Player>) {
this.players.addAll(players)
players.forEach { onJoin(it) }
}

fun removePlayer(player: Player) {
this.players.remove(player)
onLeave(player)
}

}
44 changes: 44 additions & 0 deletions src/main/kotlin/cc/lixou/stracciatella/game/GameManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cc.lixou.stracciatella.game

import net.minestom.server.entity.Player
import java.util.concurrent.ConcurrentHashMap
import kotlin.reflect.KClass
import kotlin.reflect.full.primaryConstructor

object GameManager {

private val playerGame: ConcurrentHashMap<Player, Game> = ConcurrentHashMap()
private val games: ConcurrentHashMap<KClass<out Game>, ArrayList<Game>> = ConcurrentHashMap()

fun unregisterPlayer(player: Player) {
playerGame[player]?.removePlayer(player)
playerGame.remove(player)
}

inline fun <reified T : Game> joinGame(players: Array<Player>) = joinGame(T::class, players)

fun <T : Game> joinGame(clazz: KClass<out T>, players: Array<Player>) {
val game: Game = games[clazz]?.find { it.canJoin(players) }
?: createGame(clazz)
players.forEach {
playerGame[it]?.removePlayer(it)
playerGame[it] = game
}
game.addPlayers(players)
}

private fun <T : Game> createGame(clazz: KClass<out T>): Game {
val game: T = clazz.primaryConstructor?.call()
?: throw java.lang.IllegalArgumentException("Game ${clazz.simpleName} has wrong constructor as a Game")
games[clazz]?.add(game)
return game
}

inline fun <reified T : Game> registerGame() = registerGame(T::class)

fun <T : Game> registerGame(clazz: KClass<out T>) {
if (games.containsKey(clazz)) return
games[clazz] = ArrayList()
}

}

0 comments on commit 017cd29

Please sign in to comment.