Skip to content

Commit

Permalink
fix(map): remove connections when pasting signatures removed signatur…
Browse files Browse the repository at this point in the history
…es linked to connections
  • Loading branch information
updraft0 committed Jun 15, 2024
1 parent 5731c9f commit 9b6c5f5
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 35 deletions.
17 changes: 14 additions & 3 deletions db/src/main/scala/org/updraft0/controltower/db/query/map.scala
Original file line number Diff line number Diff line change
Expand Up @@ -323,20 +323,26 @@ object map:
.delete
)

def deleteMapWormholeConnection(id: ConnectionId, byCharacterId: CharacterId): DbOperation[Long] =
def deleteMapWormholeConnection(mapId: MapId, id: ConnectionId, byCharacterId: CharacterId): DbOperation[Long] =
ctx.run(
mapWormholeConnection
.filter(_.id == lift(id))
.filter(_.mapId == lift(mapId))
.update(_.isDeleted -> lift(true), _.updatedByCharacterId -> lift(byCharacterId), _.updatedAt -> unixepoch)
)

def deleteMapWormholeConnections(ids: Chunk[ConnectionId], byCharacterId: CharacterId): DbOperation[Long] =
def deleteMapWormholeConnections(
mapId: MapId,
ids: Chunk[ConnectionId],
byCharacterId: CharacterId
): DbOperation[Long] =
ctx
.run(
quote(
liftQuery(ids).foreach(id =>
mapWormholeConnection
.filter(_.id == id)
.filter(_.mapId == lift(mapId))
.update(
_.isDeleted -> lift(true),
_.updatedByCharacterId -> lift(byCharacterId),
Expand All @@ -347,12 +353,17 @@ object map:
)
.map(_.sum)

def deleteSignaturesWithConnectionIds(ids: Chunk[ConnectionId], byCharacterId: CharacterId): DbOperation[Long] =
def deleteSignaturesWithConnectionIds(
mapId: MapId,
ids: Chunk[ConnectionId],
byCharacterId: CharacterId
): DbOperation[Long] =
ctx
.run(
quote(
liftQuery(ids).foreach(id =>
mapSystemSignature
.filter(_.mapId == lift(mapId))
.filter(_.wormholeConnectionId.exists(_ == id))
.update(
_.isDeleted -> lift(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ case class MapWormholeConnectionRank(

case class MapWormholeConnectionWithSigs(
connection: model.MapWormholeConnection,
jumps: Array[model.MapWormholeConnectionJump],
jumps: Chunk[model.MapWormholeConnectionJump],
fromSignature: Option[model.MapSystemSignature],
toSignature: Option[model.MapSystemSignature]
)

case class MapSystemWithAll(
sys: model.MapSystem,
display: Option[model.SystemDisplayData],
structures: Array[model.MapSystemStructure],
notes: Array[model.MapSystemNote],
signatures: Array[model.MapSystemSignature],
connections: Array[model.MapWormholeConnection]
structures: Chunk[model.MapSystemStructure],
notes: Chunk[model.MapSystemNote],
signatures: Chunk[model.MapSystemSignature],
connections: Chunk[model.MapWormholeConnection]
)

/** Queries for map information
Expand Down Expand Up @@ -381,7 +381,14 @@ object MapQueries:
)
}).map(
_.map((mss, dis, structures, notes, signatures, connections) =>
MapSystemWithAll(mss, dis, structures.value, notes.value, signatures.value, connections.value)
MapSystemWithAll(
sys = mss,
display = dis,
structures = Chunk.fromArray(structures.value),
notes = Chunk.fromArray(notes.value),
signatures = Chunk.fromArray(signatures.value),
connections = Chunk.fromArray(connections.value)
)
)
)

Expand Down Expand Up @@ -440,7 +447,7 @@ object MapQueries:
connection = whc,
fromSignature = fromSig,
toSignature = toSig,
jumps = jumps.value
jumps = Chunk.fromArray(jumps.value)
)
)
)
Expand All @@ -465,7 +472,7 @@ object MapQueries:
connection = whc,
fromSignature = fromSig,
toSignature = toSig,
jumps = jumps.value
jumps = Chunk.fromArray(jumps.value)
)
)
)
Expand All @@ -490,7 +497,7 @@ object MapQueries:
connection = whc,
fromSignature = fromSig,
toSignature = toSig,
jumps = jumps.value
jumps = Chunk.fromArray(jumps.value)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import org.updraft0.controltower.db.model.{
displayType
}
import org.updraft0.controltower.db.{model, query}
import org.updraft0.controltower.server.Log
import org.updraft0.controltower.server.db.*
import org.updraft0.controltower.server.*
import org.updraft0.controltower.server.tracking.{CharacterLocationState, LocationTracker, LocationUpdate}
import org.updraft0.minireactive.*
import zio.*
Expand Down Expand Up @@ -120,7 +120,7 @@ private[map] case class MapState(
Some(
prev.copy(
display = None,
connections = Array.empty,
connections = Chunk.empty,
signatures = prev.signatures.filterNot(_.wormholeConnectionId.exists(removedConnectionIds.contains))
)
)
Expand Down Expand Up @@ -491,7 +491,7 @@ object MapEntity extends ReactiveEntity[MapEnv, MapId, MapState, Identified[MapR
) =
for
// TODO: remove connection id from signature too!
_ <- query.map.deleteMapWormholeConnection(removeConn.connectionId, sessionId.characterId)
_ <- query.map.deleteMapWormholeConnection(mapId, removeConn.connectionId, sessionId.characterId)
whcOpt <- MapQueries
.getWormholeConnectionsWithSigs(mapId, Some(removeConn.connectionId), includeDeleted = true)
.map(_.headOption)
Expand Down Expand Up @@ -572,8 +572,30 @@ object MapEntity extends ReactiveEntity[MapEnv, MapId, MapState, Identified[MapR
now: Instant,
uss: MapRequest.UpdateSystemSignatures
) =
// TODO: signature updates cannot currently change connection ids so only a single system needs to be reloaded
// when replacing all and the signatures being removed have connection ids, those connections need to be removed
// this is very similar to removeSystemSignatures()
val updateSigIds = uss.scanned.map(_.signatureId).toSet
val removedSignatures =
Option
.when(uss.replaceAll)(
state.getSystem(uss.systemId).map(_.signatures.filterNot(mss => updateSigIds.contains(mss.signatureId)))
)
.flatten
.getOrElse(Chunk.empty)
val removedConnectionIds = removedSignatures.flatMap(_.wormholeConnectionId.toChunk)
// gather all system ids those connections affect
val systemIdsToRefresh =
removedConnectionIds
.map(state.connections)
.toSet
.flatMap(whc => Set(whc.connection.toSystemId, whc.connection.fromSystemId))
for
// delete the connections if any were found
_ <- query.map.deleteMapWormholeConnections(mapId, removedConnectionIds, sessionId.characterId)
// delete any signatures that map to those connection ids
_ <- query.map.deleteSignaturesWithConnectionIds(mapId, removedConnectionIds, sessionId.characterId)
deletedConnections <- query.map.getWormholeConnections(mapId, removedConnectionIds, isDeleted = true)
// delete signatures when replacing all
_ <- query.map.deleteMapSystemSignaturesAll(mapId, uss.systemId, now, sessionId.characterId).when(uss.replaceAll)
mapSystemId = (mapId, uss.systemId)
mapSystem = state.getSystem(uss.systemId).get
Expand All @@ -582,7 +604,15 @@ object MapEntity extends ReactiveEntity[MapEnv, MapId, MapState, Identified[MapR
.map(lookupExisting(mapSystem, _))
.map((prevOpt, newSig) => toModelSignature(now, sessionId, mapSystemId, prevOpt, newSig))
)(query.map.upsertMapSystemSignature)
resp <- reloadSystemSnapshot(mapId, uss.systemId)(state)
// if some connections were found, will reload multiple systems
resp <-
if (systemIdsToRefresh.nonEmpty)
combineMany(
state,
Chunk(removeConnections(deletedConnections)) ++
systemIdsToRefresh.map(sId => reloadSystemSnapshot(mapId, sId))
)
else reloadSystemSnapshot(mapId, uss.systemId)(state)
yield resp

private def removeSystemAndConnection(
Expand All @@ -597,9 +627,9 @@ object MapEntity extends ReactiveEntity[MapEnv, MapId, MapState, Identified[MapR
Chunk.from(connections.valuesIterator.map(_.connection.fromSystemId).filter(_ != rs.systemId))
for
// mark connections as removed
_ <- query.map.deleteMapWormholeConnections(connectionIds, sessionId.characterId)
_ <- query.map.deleteMapWormholeConnections(mapId, connectionIds, sessionId.characterId)
// remove signatures that have those connections
_ <- query.map.deleteSignaturesWithConnectionIds(connectionIds, sessionId.characterId)
_ <- query.map.deleteSignaturesWithConnectionIds(mapId, connectionIds, sessionId.characterId)
// remove the display of the system
_ <- query.map.deleteMapSystemDisplay(mapId, rs.systemId)
// recompute all the connection ranks
Expand Down Expand Up @@ -657,9 +687,9 @@ object MapEntity extends ReactiveEntity[MapEnv, MapId, MapState, Identified[MapR
.flatMap(whc => Set(whc.connection.toSystemId, whc.connection.fromSystemId))
)
// delete the connections if any were found
_ <- query.map.deleteMapWormholeConnections(Chunk.fromIterable(connectionIds), sessionId.characterId)
_ <- query.map.deleteMapWormholeConnections(mapId, Chunk.fromIterable(connectionIds), sessionId.characterId)
// delete any signatures that map to those connection ids
_ <- query.map.deleteSignaturesWithConnectionIds(Chunk.fromIterable(connectionIds), sessionId.characterId)
_ <- query.map.deleteSignaturesWithConnectionIds(mapId, Chunk.fromIterable(connectionIds), sessionId.characterId)
deletedConnections <- query.map.getWormholeConnections(mapId, Chunk.fromIterable(connectionIds), isDeleted = true)
// delete the signatures
_ <- rss.signatures match
Expand Down Expand Up @@ -1006,24 +1036,22 @@ private inline def broadcastMany(values: MapResponse*): Chunk[Identified[MapResp
private inline def withState[A](state: MapState)(f: MapState => A): A = f(state)

private inline def updateConnectionById(
arr: Array[MapWormholeConnection],
arr: Chunk[MapWormholeConnection],
whc: MapWormholeConnection
): Array[MapWormholeConnection] =
): Chunk[MapWormholeConnection] =
arr.indexWhere(_.id == whc.id) match
case -1 => arr.appended(whc)
case idx =>
arr.update(idx, whc)
arr
case -1 => arr.appended(whc)
case idx => arr.updated(idx, whc)

private inline def removeConnectionById(
arr: Array[MapWormholeConnection],
arr: Chunk[MapWormholeConnection],
whc: MapWormholeConnection
) =
arr.indexWhere(_.id == whc.id) match
case -1 => arr
case idx => arr.patch(idx, Nil, 1)

private inline def removeSignatureById(arr: Array[MapSystemSignature], idOpt: Option[SigId]) =
private inline def removeSignatureById(arr: Chunk[MapSystemSignature], idOpt: Option[SigId]) =
idOpt
.map(sigId =>
arr.indexWhere(_.signatureId == sigId) match
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,10 @@ private def toProtoSystemSnapshot(value: MapSystemWithAll): protocol.MapSystemSn
protocol.MapSystemSnapshot(
system = toProtoSystem(value.sys, value.display),
display = value.display.map(toProtoDisplay),
structures = value.structures.map(toProtoStructure),
notes = value.notes.map(toProtoNote),
signatures = value.signatures.map(toProtoSignature),
connections = value.connections.map(toProtoConnection)
structures = value.structures.map(toProtoStructure).toArray,
notes = value.notes.map(toProtoNote).toArray,
signatures = value.signatures.map(toProtoSignature).toArray,
connections = value.connections.map(toProtoConnection).toArray
)

private def toProtoSystem(value: model.MapSystem, displayData: Option[model.SystemDisplayData]): protocol.MapSystem =
Expand Down Expand Up @@ -589,7 +589,7 @@ private def toProtoConnectionWithSigs(
): protocol.MapWormholeConnectionWithSigs =
protocol.MapWormholeConnectionWithSigs(
connection = toProtoConnection(value.connection),
jumps = value.jumps.map(toProtoConnectionJump),
jumps = value.jumps.map(toProtoConnectionJump).toArray,
fromSignature = value.fromSignature.flatMap(toProtoSignatureWormhole(_)),
toSignature = value.toSignature.flatMap(toProtoSignatureWormhole(_)),
rank = toProtoConnectionRank(rank)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.updraft0.controltower.server

import zio.Chunk

extension [A](v: Option[A])
inline def toChunk: Chunk[A] = v match
case None => Chunk.empty[A]
case Some(a) => Chunk.single(a)
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ object MapReactiveSpec extends ZIOSpecDefault:
private def testMapLayer: ZLayer[Scope, Throwable, MapEnv] = ZLayer.make[MapEnv](
TempDb.empty,
TestLocationTracker.empty,
TestPermissionTracker.empty,
TestPermissionTracker.empty
// uncomment to enable debug logging in tests
// consoleLogger(ConsoleLoggerConfig.apply(LogFormat.colored, LogFilter.LogLevelByNameConfig(LogLevel.Debug))),
// Slf4jBridge.init(LogFilter.acceptAll)
Expand Down

0 comments on commit 9b6c5f5

Please sign in to comment.