From 71dd81cdd24729efbf66ccc71b21eb82c83f2571 Mon Sep 17 00:00:00 2001 From: TheSaminator Date: Tue, 12 Jul 2022 11:54:50 -0400 Subject: [PATCH] Fixes --- .../starshipfights/data/admiralty/admirals.kt | 26 +- .../data/space/star_clusters.kt | 299 ------------------ 2 files changed, 9 insertions(+), 316 deletions(-) diff --git a/src/jvmMain/kotlin/net/starshipfights/data/admiralty/admirals.kt b/src/jvmMain/kotlin/net/starshipfights/data/admiralty/admirals.kt index b05d878..a8ad174 100644 --- a/src/jvmMain/kotlin/net/starshipfights/data/admiralty/admirals.kt +++ b/src/jvmMain/kotlin/net/starshipfights/data/admiralty/admirals.kt @@ -1,23 +1,15 @@ package net.starshipfights.data.admiralty -import com.github.jershell.kbson.NonEncodeNull -import kotlinx.coroutines.async -import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.map -import kotlinx.coroutines.flow.mapNotNull import kotlinx.coroutines.flow.toList import kotlinx.serialization.Contextual import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import net.starshipfights.campaign.CampaignMenuAdmiral -import net.starshipfights.campaign.CampaignMenuAdmiralStatus -import net.starshipfights.campaign.StarClusterMenuData import net.starshipfights.data.DataDocument import net.starshipfights.data.DocumentTable import net.starshipfights.data.Id import net.starshipfights.data.auth.User import net.starshipfights.data.invoke -import net.starshipfights.data.space.StarCluster import net.starshipfights.game.* import org.bson.conversions.Bson import org.litote.kmongo.* @@ -40,9 +32,9 @@ data class Admiral( val isOnline: Boolean = false, - @NonEncodeNull - val inCluster: Id? = null, - val invitedToClusters: Set> = emptySet(), + //@NonEncodeNull + //val inCluster: Id? = null, + //val invitedToClusters: Set> = emptySet(), ) : DataDocument { val rank: AdmiralRank get() = AdmiralRank.fromAcumen(acumen) @@ -52,9 +44,9 @@ data class Admiral( companion object Table : DocumentTable by DocumentTable.create({ index(Admiral::owningUser) - index(Admiral::inCluster) - index(Admiral::invitedToClusters) - uniqueIf(Admiral::inCluster.exists(), Admiral::owningUser, Admiral::inCluster) + //index(Admiral::inCluster) + //index(Admiral::invitedToClusters) + //uniqueIf(Admiral::inCluster.exists(), Admiral::owningUser, Admiral::inCluster) }) } @@ -136,7 +128,7 @@ data class ShipMemorial( }) } -suspend fun getAllInGameAdmiralsForBattle(user: User) = Admiral.filter(and(Admiral::owningUser eq user.id, Admiral::inCluster eq null, Admiral::isOnline eq false)).map { admiral -> +suspend fun getAllInGameAdmiralsForBattle(user: User) = Admiral.filter(and(Admiral::owningUser eq user.id/*, Admiral::inCluster eq null*/, Admiral::isOnline eq false)).map { admiral -> InGameAdmiral( admiral.id.reinterpret(), InGameUser(user.id.reinterpret(), user.profileName), @@ -162,7 +154,7 @@ suspend fun getInGameAdmiral(admiralId: Id) = Admiral.get(admiral getInGameAdmiral(admiral) } -suspend fun getInGameAdmiralsInCluster(clusterId: Id) = coroutineScope { +/*suspend fun getInGameAdmiralsInCluster(clusterId: Id) = coroutineScope { Admiral.filter(Admiral::inCluster eq clusterId).map { admiral -> async { User.get(admiral.owningUser)?.let { user -> @@ -237,7 +229,7 @@ suspend fun getCampaignMenuAdmirals(user: User) = Admiral.filter(and(Admiral::ow } ) ) -}.toList() +}.toList()*/ suspend fun getAdmiralsShips(admiralId: Id): Map, Ship> { val now = Instant.now() diff --git a/src/jvmMain/kotlin/net/starshipfights/data/space/star_clusters.kt b/src/jvmMain/kotlin/net/starshipfights/data/space/star_clusters.kt index 363513a..c62547a 100644 --- a/src/jvmMain/kotlin/net/starshipfights/data/space/star_clusters.kt +++ b/src/jvmMain/kotlin/net/starshipfights/data/space/star_clusters.kt @@ -1,300 +1 @@ package net.starshipfights.data.space - -import kotlinx.coroutines.async -import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.flow.map -import kotlinx.coroutines.flow.mapNotNull -import kotlinx.coroutines.flow.singleOrNull -import kotlinx.coroutines.flow.toList -import kotlinx.coroutines.launch -import kotlinx.serialization.Serializable -import net.starshipfights.campaign.* -import net.starshipfights.data.DataDocument -import net.starshipfights.data.DocumentTable -import net.starshipfights.data.Id -import net.starshipfights.data.admiralty.Admiral -import net.starshipfights.data.admiralty.ShipInDrydock -import net.starshipfights.data.admiralty.getInGameAdmiral -import net.starshipfights.data.invoke -import net.starshipfights.game.FactionFlavor -import net.starshipfights.game.Position -import net.starshipfights.game.Ship -import org.litote.kmongo.* -import java.time.Instant - -@Serializable -data class StarCluster( - override val id: Id, - val host: Id, - - val background: StarClusterBackground, - val lanes: Set -) : DataDocument { - companion object Table : DocumentTable by DocumentTable.create({ - unique(StarCluster::host) - }) -} - -@Serializable -data class ClusterStarSystem( - override val id: Id, - val clusterId: Id, - - val name: String, - val holder: FactionFlavor?, - - val position: Position, - val radius: Double, -) : DataDocument { - companion object Table : DocumentTable by DocumentTable.create({ - index(ClusterStarSystem::clusterId) - }) -} - -@Serializable -data class ClusterCelestialObject( - override val id: Id, - val starSystemId: Id, - - val celestialObject: CelestialObject -) : DataDocument { - companion object Table : DocumentTable by DocumentTable.create({ - index(ClusterCelestialObject::starSystemId) - }) -} - -@Serializable -data class ClusterFleetPresence( - override val id: Id, - val starSystemId: Id, - - val fleetPresence: FleetPresenceData -) : DataDocument { - companion object Table : DocumentTable by DocumentTable.create({ - index(ClusterFleetPresence::starSystemId) - - val admiralIdProp = ClusterFleetPresence::fleetPresence / FleetPresenceData.Player::admiralId - uniqueIf(admiralIdProp.exists(), admiralIdProp) - }) -} - -@Serializable -sealed class FleetPresenceData { - @Serializable - data class NPC( - val name: String, - val ships: Map, Ship>, - val admiral: FleetPresenceAdmiral.NPC - ) : FleetPresenceData() - - @Serializable - data class Player( - val admiralId: Id - ) : FleetPresenceData() -} - -suspend fun getFleetPresenceOf(admiralId: Id): ClusterFleetPresence? { - return ClusterFleetPresence.filter( - (ClusterFleetPresence::fleetPresence / FleetPresenceData.Player::admiralId) eq admiralId - ).singleOrNull() -} - -suspend fun FleetPresenceData.resolve(): FleetPresence? { - val now = Instant.now() - return when (this) { - is FleetPresenceData.NPC -> FleetPresence( - name = name, - ships = ships, - admiral = admiral - ) - is FleetPresenceData.Player -> { - val (admiral, ships) = coroutineScope { - val admiralAsync = async { Admiral.get(admiralId) } - val shipsAsync = async { - ShipInDrydock.filter( - and(ShipInDrydock::owningAdmiral eq admiralId, ShipInDrydock::readyAt lte now) - ).toList() - } - - admiralAsync.await() to shipsAsync.await() - } - - admiral ?: return null - - FleetPresence( - name = "Fleet of ${admiral.fullName}", - ships = ships.associate { inDrydock -> - inDrydock.id.reinterpret() to inDrydock.shipData - }, - admiral = FleetPresenceAdmiral.Player(admiral.id.reinterpret()) - ) - } - } -} - -suspend fun deleteCluster(clusterViewId: Id) { - val clusterId = clusterViewId.reinterpret() - - coroutineScope { - launch { StarCluster.del(clusterId) } - launch { - ClusterStarSystem.filter(ClusterStarSystem::clusterId eq clusterId).collect { cSystem -> - launch { - ClusterCelestialObject.remove(ClusterCelestialObject::starSystemId eq cSystem.id) - } - launch { - ClusterFleetPresence.remove(ClusterFleetPresence::starSystemId eq cSystem.id) - } - launch { - ClusterStarSystem.del(cSystem.id) - } - } - } - launch { - Admiral.update(Admiral::inCluster eq clusterId, unset(Admiral::inCluster)) - } - launch { - Admiral.update(Admiral::invitedToClusters contains clusterId, pull(Admiral::invitedToClusters, clusterId)) - } - } -} - -suspend fun createCluster(clusterView: StarClusterView, forHost: Id): Id { - val cluster = StarCluster( - id = Id(), - host = forHost, - background = clusterView.background, - lanes = clusterView.lanes - ) - - coroutineScope { - launch { StarCluster.put(cluster) } - launch { - for ((systemId, system) in clusterView.systems) { - val clusterSystem = ClusterStarSystem( - id = systemId.reinterpret(), - clusterId = cluster.id, - name = system.name, - holder = system.holder, - position = system.position, - radius = system.radius - ) - - launch { ClusterStarSystem.put(clusterSystem) } - launch { - ClusterCelestialObject.put( - system.bodies.map { (bodyId, body) -> - ClusterCelestialObject( - id = bodyId.reinterpret(), - starSystemId = clusterSystem.id, - celestialObject = body - ) - } - ) - } - launch { - ClusterFleetPresence.put( - system.fleets.map { (fleetId, fleet) -> - ClusterFleetPresence( - id = fleetId.reinterpret(), - starSystemId = clusterSystem.id, - fleetPresence = when (val admiral = fleet.admiral) { - is FleetPresenceAdmiral.NPC -> FleetPresenceData.NPC( - name = fleet.name, - ships = fleet.ships, - admiral = admiral - ) - is FleetPresenceAdmiral.Player -> FleetPresenceData.Player( - admiral.id.reinterpret() - ) - } - ) - } - ) - } - } - } - } - - return cluster.id.reinterpret() -} - -suspend fun viewCluster(clusterViewId: Id): StarClusterView? { - val clusterId = clusterViewId.reinterpret() - - return coroutineScope { - val clusterAsync = async { StarCluster.get(clusterId) } - val systemsAsync = async { - ClusterStarSystem.filter(ClusterStarSystem::clusterId eq clusterId).map { cSystem -> - async { - val bodiesAsync = async { - val bodies = ClusterCelestialObject.filter(ClusterCelestialObject::starSystemId eq cSystem.id).toList() - - bodies.associate { cBody -> - cBody.id.reinterpret() to cBody.celestialObject - } - } - val fleetsAsync = async { - ClusterFleetPresence.filter(ClusterFleetPresence::starSystemId eq cSystem.id).map { fleet -> - async { - fleet.fleetPresence.resolve()?.let { fleet.id.reinterpret() to it } - } - }.mapNotNull { it.await() }.toList().toMap() - } - - cSystem.id.reinterpret() to StarSystem( - cSystem.name, - cSystem.holder, - fleetsAsync.await(), - cSystem.position, - cSystem.radius, - bodiesAsync.await() - ) - } - }.map { it.await() }.toList().toMap() - } - - val cluster = clusterAsync.await() ?: return@coroutineScope null - val systems = systemsAsync.await() - - StarClusterView( - background = cluster.background, - systems = systems, - lanes = cluster.lanes - ) - } -} - -suspend fun viewAdmiralsInCluster(clusterViewId: Id): List { - val cluster = StarCluster.get(clusterViewId.reinterpret()) ?: return emptyList() - - val (host, members, invitees) = coroutineScope { - val hostAsync = async { - listOfNotNull(getInGameAdmiral(cluster.host.reinterpret())) - } - - val membersAsync = async { - Admiral.filter(and(Admiral::inCluster eq clusterViewId.reinterpret(), Admiral::id ne cluster.host)) - .map { async { getInGameAdmiral(it) } } - .mapNotNull { it.await() } - .toList() - } - - val inviteesAsync = async { - Admiral.filter(Admiral::invitedToClusters contains clusterViewId.reinterpret()) - .map { async { getInGameAdmiral(it) } } - .mapNotNull { it.await() } - .toList() - } - - Triple(hostAsync.await(), membersAsync.await(), inviteesAsync.await()) - } - - return host.map { - CampaignAdmiral(it, CampaignAdmiralStatus.HOST) - } + members.map { - CampaignAdmiral(it, CampaignAdmiralStatus.MEMBER) - } + invitees.map { - CampaignAdmiral(it, CampaignAdmiralStatus.INVITED) - } -} -- 2.25.1