Skip to content

Commit

Permalink
fix(signatures),chore: fix cached values causing wrong signature ids …
Browse files Browse the repository at this point in the history
…to be updated, also refactor to use SystemId opaque type in backend
  • Loading branch information
updraft0 committed Jun 22, 2024
1 parent 6e703f2 commit f49a139
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 100 deletions.
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
java graalvm-community-21.0.2
java graalvm-community-22.0.1
nodejs 18.18.1
sbt 1.9.9
sbt 1.10.0
yarn 1.22.19
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import org.updraft0.controltower.constant.*
import java.time.Instant
import scala.CanEqual

type SystemId = Long // TODO move to opaque

enum ChainNamingStrategy extends Enum[ChainNamingStrategy] derives CanEqual:
case Manual

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.updraft0.controltower.db.model

import org.updraft0.controltower.constant.WormholeClass
import org.updraft0.controltower.constant.{SystemId, WormholeClass}

import java.time.Instant

case class Constellation(id: Long, name: String, regionId: Long, regionName: String)
Expand All @@ -14,14 +15,14 @@ case class NpcStation(
operationId: Long,
planetId: Long,
moonId: Option[Long],
systemId: Long
systemId: SystemId
)
case class SolarSystemAsteroidBelt(id: Long, planetId: Long, systemId: Long)
case class SolarSystemPlanet(id: Long, systemId: Long, idx: Long, typeId: Long)
case class SolarSystemMoon(id: Long, planetId: Long, systemId: Long, idx: Long)
case class SolarSystemAsteroidBelt(id: Long, planetId: Long, systemId: SystemId)
case class SolarSystemPlanet(id: Long, systemId: SystemId, idx: Long, typeId: Long)
case class SolarSystemMoon(id: Long, planetId: Long, systemId: SystemId, idx: Long)
case class SolarSystemStar(id: Long, type_id: Long)
case class SolarSystem(
id: Long, // TODO: use opaque type
id: SystemId,
starId: Option[Long],
starTypeId: Option[Long],
name: String,
Expand All @@ -46,7 +47,7 @@ case class Stargate(id: Long, systemId: SystemId, toStargateId: Long)
// from map

case class SystemStaticWormhole(
systemId: Long,
systemId: SystemId,
staticTypeId: Long,
validFrom: Option[Instant] = None,
validUntil: Option[Instant] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ object auth:
given MappedEncoding[Long, ConnectionId] = MappedEncoding(ConnectionId.apply)
given MappedEncoding[ConnectionId, Long] = MappedEncoding(identity)

given MappedEncoding[Long, SystemId] = MappedEncoding(SystemId.apply)
given MappedEncoding[SystemId, Long] = MappedEncoding(identity)

given MappedEncoding[String, SigId] = MappedEncoding(SigId.apply)
given MappedEncoding[SigId, String] = MappedEncoding(identity)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.updraft0.controltower.db.query

import io.getquill.*
import org.updraft0.controltower.constant.{SystemId as _, *}
import org.updraft0.controltower.constant.*
import org.updraft0.controltower.db.model.*
import zio.{ZIO, Chunk}

Expand Down Expand Up @@ -106,15 +106,15 @@ object map:
)
)

def getWormholeSystemNames: DbOperation[Map[String, Long]] =
def getWormholeSystemNames: DbOperation[Map[String, SystemId]] =
ctx
.run(quote(sde.schema.solarSystem.map(ss => ss.name -> ss.id)))
.map(_.filter((n, _) => n.startsWith("J") || n == "Thera" || n == "Turnur").toMap)

def getWormholeTypeNames: DbOperation[List[(String, Long)]] =
ctx.run(quote { sde.schema.itemType.filter(_.groupId == lift(WormholeGroupId)).map(it => it.name -> it.id) })

def getMapSystem(mapId: MapId, systemId: Long): DbOperation[Option[MapSystem]] =
def getMapSystem(mapId: MapId, systemId: SystemId): DbOperation[Option[MapSystem]] =
ctx
.run(quote(mapSystem.filter(ms => ms.mapId == lift(mapId) && ms.systemId == lift(systemId))))
.map(_.headOption)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.sql.SQLException
import javax.sql.DataSource

object sde:
import auth.given
import schema.*
import ctx.*
import zio.json.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.updraft0.controltower.sdeloader

import io.getquill.JsonValue
import org.updraft0.controltower.constant.{WormholeClass, WormholeClasses}
import org.updraft0.controltower.constant.{SystemId, WormholeClass, WormholeClasses}
import org.updraft0.controltower.db.model.{SignatureGroup, SignatureInGroup, SystemStaticWormhole, Wormhole}
import org.updraft0.controltower.db.query.map
import zio.*
Expand Down Expand Up @@ -59,7 +59,7 @@ private def toWormhole(id: Long, name: String, attributes: JsonValue[Map[String,
)

private def readSystemStatics(
systemsByName: Map[String, Long],
systemsByName: Map[String, SystemId],
wormholeTypes: Map[String, Long]
): ZStream[Any, Throwable, SystemStaticWormhole] =
ZStream
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.updraft0.controltower.sdeloader

import org.updraft0.controltower.constant.SystemId
import org.updraft0.controltower.db.model.*
import org.updraft0.controltower.db.query
import org.updraft0.controltower.sde
Expand Down Expand Up @@ -152,7 +153,7 @@ private def toSolarSystem(
s: ExportedData.SolarSystem
) =
SolarSystem(
id = s.id,
id = SystemId(s.id),
name = names(s.id).name,
starId = s.star.map(_.id),
starTypeId = s.star.map(_.typeId),
Expand All @@ -172,12 +173,14 @@ private def toSolarSystem(
regional = s.regional
)

private def toPlanet(s: ExportedData.SolarSystem, p: sde.Planet) = SolarSystemPlanet(p.id, s.id, p.index, p.typeId)
private def toPlanet(s: ExportedData.SolarSystem, p: sde.Planet) =
SolarSystemPlanet(p.id, SystemId(s.id), p.index, p.typeId)
private def toPlanetMoon(s: ExportedData.SolarSystem, p: sde.Planet, m: sde.PlanetMoon, idx: Int) =
SolarSystemMoon(m.id, p.id, s.id, idx)
SolarSystemMoon(m.id, p.id, SystemId(s.id), idx)
private def toAsteroidBelt(s: ExportedData.SolarSystem, p: sde.Planet, ab: sde.PlanetAsteroidBelt) =
SolarSystemAsteroidBelt(ab.id, p.id, s.id)
private def toStargate(s: ExportedData.SolarSystem, sg: sde.Stargate) = Stargate(sg.id, s.id, sg.destinationId)
SolarSystemAsteroidBelt(ab.id, p.id, SystemId(s.id))
private def toStargate(s: ExportedData.SolarSystem, sg: sde.Stargate) =
Stargate(sg.id, SystemId(s.id), sg.destinationId)
private def toFaction(f: sde.Faction): Faction =
Faction(
id = f.id,
Expand Down Expand Up @@ -206,7 +209,7 @@ private def toNpcStation(
operationId = ns.operationId,
planetId = p.id,
moonId = m.map(_.id),
systemId = s.id
systemId = SystemId(s.id)
)

private def toNpcCorporation(c: sde.NpcCorporation): NpcCorporation =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ enum WormholeConnectionType derives CanEqual:
case Known(typeId: Long)

enum MapSystemSignature(
val systemId: SystemId,
val id: SigId,
val createdAt: Instant,
val createdByCharacterId: CharacterId,
Expand All @@ -157,12 +158,14 @@ enum MapSystemSignature(
val signatureGroup: SignatureGroup
) derives CanEqual:
case Unknown(
override val systemId: SystemId,
override val id: SigId,
override val createdAt: Instant,
override val createdByCharacterId: CharacterId,
override val updatedAt: Instant,
override val updatedByCharacterId: CharacterId
) extends MapSystemSignature(
systemId,
id,
createdAt,
createdByCharacterId,
Expand All @@ -171,6 +174,7 @@ enum MapSystemSignature(
SignatureGroup.Unknown
)
case Wormhole(
override val systemId: SystemId,
override val id: SigId,
eolAt: Option[Instant],
connectionType: WormholeConnectionType,
Expand All @@ -182,6 +186,7 @@ enum MapSystemSignature(
override val updatedAt: Instant,
override val updatedByCharacterId: CharacterId
) extends MapSystemSignature(
systemId,
id,
createdAt,
createdByCharacterId,
Expand All @@ -191,6 +196,7 @@ enum MapSystemSignature(
)

case Site(
override val systemId: SystemId,
override val id: SigId,
group: SignatureGroup,
name: Option[String],
Expand All @@ -199,6 +205,7 @@ enum MapSystemSignature(
override val updatedAt: Instant,
override val updatedByCharacterId: CharacterId
) extends MapSystemSignature(
systemId,
id,
createdAt,
createdByCharacterId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ object MapQueries:
.mapOrFail: m =>
for
mapId <- m("mapId").as[MapId]
systemId <- m("systemId").as[model.SystemId]
systemId <- m("systemId").as[SystemId]
name <- m("name").as[String]
isDeleted <- m("isDeleted").as[Int].map(_ == 1)
ownerCorporationId <- m("ownerCorporationId").as[Option[CorporationId]]
Expand Down Expand Up @@ -110,7 +110,7 @@ object MapQueries:
for
id <- m("id").as[Long]
mapId <- m("mapId").as[MapId]
systemId <- m("systemId").as[model.SystemId]
systemId <- m("systemId").as[SystemId]
note <- m("note").as[String]
isDeleted <- m("isDeleted").as[Int].map(_ == 1)
createdAt <- m("createdAt").as[Long].map(Instant.ofEpochMilli)
Expand All @@ -133,7 +133,7 @@ object MapQueries:
.mapOrFail: m =>
for
mapId <- m("mapId").as[MapId]
systemId <- m("systemId").as[model.SystemId]
systemId <- m("systemId").as[SystemId]
signatureId <- m("signatureId").as[SigId]
isDeleted <- m("isDeleted").as[Int].map(_ == 1)
signatureGroup <- m("signatureGroup").as[model.SignatureGroup]
Expand Down Expand Up @@ -246,7 +246,7 @@ object MapQueries:
}).map(_.toMap)

@nowarn("msg=.*it is preferable to define both an encoder and a decoder.*")
def getMapSystemAll(mapId: MapId, systemId: Option[Long] = None): Result[List[MapSystemWithAll]] =
def getMapSystemAll(mapId: MapId, systemId: Option[SystemId] = None): Result[List[MapSystemWithAll]] =
run(quote {
(for
map <- mapModel.filter(_.id == lift(mapId))
Expand Down Expand Up @@ -454,7 +454,7 @@ object MapQueries:

def getWormholeConnectionsWithSigsBySystemId(
mapId: MapId,
systemId: model.SystemId
systemId: SystemId
): Result[List[MapWormholeConnectionWithSigs]] =
run(
quote(
Expand All @@ -479,7 +479,7 @@ object MapQueries:

def getWormholeConnectionsWithSigsBySystemIds(
mapId: MapId,
systemIds: Chunk[model.SystemId]
systemIds: Chunk[SystemId]
): Result[List[MapWormholeConnectionWithSigs]] =
run(
quote(
Expand Down Expand Up @@ -527,7 +527,7 @@ object MapQueries:

def getWormholeConnectionRanksForSystem(
mapId: MapId,
systemId: model.SystemId
systemId: SystemId
): Result[List[MapWormholeConnectionRank]] =
run(
quote(
Expand All @@ -544,8 +544,8 @@ object MapQueries:

def getWormholeConnectionRanksForSystems(
mapId: MapId,
systemId1: model.SystemId,
systemId2: model.SystemId
systemId1: SystemId,
systemId2: SystemId
): Result[List[MapWormholeConnectionRank]] =
run(
quote(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package org.updraft0.controltower.server.db

import io.getquill.*
import org.updraft0.controltower.constant
import org.updraft0.controltower.constant.{WormholeClasses, WormholeEffects}
import org.updraft0.controltower.constant.*
import org.updraft0.controltower.db.model
import org.updraft0.controltower.db.query.*
import org.updraft0.controltower.protocol
import org.updraft0.controltower.protocol.jsoncodec.given
import org.updraft0.controltower.protocol.{StationOperation, StationService}
import zio.*

case class StargateBothSides(
inGateId: Long,
outGateId: Long,
inSystemId: constant.SystemId,
outSystemId: constant.SystemId
inSystemId: SystemId,
outSystemId: SystemId
)
case class SolarSystemWithGates(sys: model.SolarSystem, gates: Array[StargateBothSides])

/** Queries for "reference" data (not map-dependent)
*/
object ReferenceQueries:
import ctx.{*, given}
import auth.given_MappedEncoding_Long_SystemId
import map.given
import map.schema.*
import sde.schema.*
Expand Down Expand Up @@ -192,7 +191,7 @@ object ReferenceQueries:
svc <- stationService.join(_.id == opSvc.serviceId)
yield (op.id, op.name, svc.id, svc.name))
.groupByMap(k => (k._1, k._2))(v =>
(v._1, v._2, jsonGroupArray[StationService](jsonObject2("id", v._3, "name", v._4)))
(v._1, v._2, jsonGroupArray[protocol.StationService](jsonObject2("id", v._3, "name", v._4)))
)
}).map(_.map(r => protocol.StationOperation(r._1.toInt, r._2, r._3.value)))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.updraft0.controltower.server.map

import org.updraft0.controltower.constant.{SystemId as _, *}
import org.updraft0.controltower.constant.*
import org.updraft0.controltower.db.model.{
MapSystemSignature,
MapWormholeConnection,
Expand All @@ -18,7 +18,6 @@ import java.time.Instant
import java.util.UUID

type MapEnv = javax.sql.DataSource & LocationTracker & MapPermissionTracker
type SystemId = Long // TODO opaque type
type ShipTypeId = Int

private[map] case class MapSolarSystem(
Expand Down Expand Up @@ -114,9 +113,9 @@ private[map] case class MapState(
)

def removeSystems(
removedSystemIds: Chunk[model.SystemId],
removedSystemIds: Chunk[SystemId],
removedConnectionIds: Chunk[ConnectionId],
refreshedSystemIds: Chunk[model.SystemId],
refreshedSystemIds: Chunk[SystemId],
connectionRanks: List[MapWormholeConnectionRank],
connectionsWithSigs: List[MapWormholeConnectionWithSigs]
): MapState =
Expand Down Expand Up @@ -896,7 +895,7 @@ object MapEntity extends ReactiveEntity[MapEnv, MapId, MapState, Identified[MapR
.filter((_, mls) => mls.locationInfo.isDefined && mls.online)
.transform: (_, mls) =>
val locationInfo = mls.locationInfo.get
locationInfo.system.value -> locationInfo.shipTypeId
locationInfo.system -> locationInfo.shipTypeId
)

private def isPotentialWormholeJump(state: MapState, fromSystemId: SystemId, toSystemId: SystemId) =
Expand Down Expand Up @@ -1112,7 +1111,7 @@ private def loadMapRef() =
whClass = WormholeClasses.ById(ss.sys.whClassId.get),
regionId = ss.sys.regionId,
constellationId = ss.sys.constellationId,
gates = ss.gates.map(sg => sg.outSystemId.value -> sg.inGateId).toMap
gates = ss.gates.map(sg => sg.outSystemId -> sg.inGateId).toMap
)
)
.toMap
Expand Down
Loading

0 comments on commit f49a139

Please sign in to comment.