Skip to content

Commit

Permalink
reworking the sdk as a cannon
Browse files Browse the repository at this point in the history
  • Loading branch information
robertfmurdock committed Jul 14, 2023
1 parent 7479905 commit 1e87b8c
Show file tree
Hide file tree
Showing 85 changed files with 522 additions and 471 deletions.
3 changes: 3 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions cli/src/main/kotlin/com/zegreatrob/coupling/cli/party/List.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.zegreatrob.coupling.cli.Auth0Environment
import com.zegreatrob.coupling.cli.getAccessToken
import com.zegreatrob.coupling.model.Record
import com.zegreatrob.coupling.model.party.PartyDetails
import com.zegreatrob.coupling.sdk.KtorCouplingSdk
import com.zegreatrob.coupling.sdk.couplingSdk
import com.zegreatrob.coupling.sdk.defaultClient
import com.zegreatrob.coupling.sdk.gql.graphQuery
import kotlinx.coroutines.runBlocking
Expand All @@ -26,12 +26,12 @@ class List : CliktCommand() {
return
}

val sdk = KtorCouplingSdk(
val sdk = couplingSdk(
{ accessToken },
defaultClient(environment.audienceHost() to ""),
)
runBlocking {
sdk.perform(graphQuery { partyList() })
sdk.fire(graphQuery { partyList() })
?.partyList
?.map(Record<PartyDetails>::data)
?.joinToString("\n") { "Party: id = ${it.id.value}, name = ${it.name}" }
Expand Down
5 changes: 2 additions & 3 deletions client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import com.fasterxml.jackson.databind.ObjectMapper
import com.zegreatrob.coupling.plugins.NodeExec
import com.zegreatrob.coupling.plugins.setup
Expand Down Expand Up @@ -188,8 +187,8 @@ tasks {
.withType(ReleaseVersion::class.java)
.named("release")
.configure {
finalizedBy(uploadToS3)
}
finalizedBy(uploadToS3)
}

val additionalResources by registering(Copy::class) {
outputs.cacheIf { true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import web.cssom.ident
import web.cssom.s

val prepareToSpinButtonClassName = ClassName("prepare-to-spin")

external interface PrepareToSpinButtonProps : Props {
var party: PartyDetails
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.zegreatrob.minreact.nfc
import emotion.react.css
import react.Props
import react.dom.html.ReactHTML.div
import web.authn.AuthenticatorTransport.Companion.nfc
import web.cssom.Display
import web.cssom.TextAlign
import web.cssom.WhiteSpace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class StubDispatcher {
?.responseFunc
?.invoke(result)
}

fun <C, R> wrappedSendResult(result: R) {
dispatchList.filterIsInstance<DispatchedFunc<ActionWrapper<C>, R>>()
.firstOrNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ package com.zegreatrob.coupling.client

import com.benasher44.uuid.Uuid
import com.zegreatrob.coupling.action.TraceIdProvider
import com.zegreatrob.coupling.client.memory.ClientPartyPlayerQueryDispatcher
import com.zegreatrob.coupling.client.memory.ClientStatisticsQueryDispatcher
import com.zegreatrob.coupling.client.party.NewPartyCommandDispatcher
import com.zegreatrob.coupling.sdk.CouplingSdk
import com.zegreatrob.coupling.sdk.CouplingSdkDispatcher
import com.zegreatrob.testmints.action.ActionCannon

class CommandDispatcher(override val traceId: Uuid, override val sdk: CouplingSdk) :
TraceIdProvider,
NewPartyCommandDispatcher,
ClientPartyPlayerQueryDispatcher,
ClientStatisticsQueryDispatcher,
CouplingSdk by sdk
class CommandDispatcher(override val traceId: Uuid, val sdk: ActionCannon<CouplingSdkDispatcher>) : TraceIdProvider
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package com.zegreatrob.coupling.client

import com.zegreatrob.coupling.action.LoggingActionPipe
import com.zegreatrob.coupling.action.TraceIdProvider
import com.zegreatrob.coupling.client.components.DispatchFunc
import com.zegreatrob.react.dataloader.DataLoaderTools
import com.zegreatrob.testmints.action.ActionCannon
import com.zegreatrob.testmints.action.async.SuspendAction

class DecoratedDispatchFunc<D : TraceIdProvider>(
val dispatcherFunc: () -> D,
class DecoratedDispatchFunc<D>(
val cannonFunc: () -> ActionCannon<D>,
private val tools: DataLoaderTools,
) : DispatchFunc<D> {

private val dispatcher get() = dispatcherFunc()

override fun <C : SuspendAction<D, R>, R> invoke(commandFunc: () -> C, response: (R) -> Unit) = fun() {
val cannon = cannonFunc()
val command = commandFunc()
tools.performAsyncWork(
{ command.execute(dispatcher) },
{ cannon.fire(command) },
{ handler: Throwable -> throw handler },
response,
)
Expand All @@ -28,7 +25,7 @@ class DecoratedDispatchFunc<D : TraceIdProvider>(
fireCommand: suspend ActionCannon<D>.(C) -> R,
response: (R) -> Unit,
): () -> Unit = fun() {
val cannon = ActionCannon(dispatcher, LoggingActionPipe(dispatcher.traceId))
val cannon = cannonFunc()
val command = commandFunc()
tools.performAsyncWork(
{ fireCommand(cannon, command) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,23 @@ val AddToSlackButton by nfc<AddToSlackButtonProps> { props ->
}
viewBox = "0 0 122.8 122.8"
path {
d = "M25.8 77.6c0 7.1-5.8 12.9-12.9 12.9S0 84.7 0 77.6s5.8-12.9 12.9-12.9h12.9v12.9zm6.5 0c0-7.1 5.8-12.9 12.9-12.9s12.9 5.8 12.9 12.9v32.3c0 7.1-5.8 12.9-12.9 12.9s-12.9-5.8-12.9-12.9V77.6z"
d =
"M25.8 77.6c0 7.1-5.8 12.9-12.9 12.9S0 84.7 0 77.6s5.8-12.9 12.9-12.9h12.9v12.9zm6.5 0c0-7.1 5.8-12.9 12.9-12.9s12.9 5.8 12.9 12.9v32.3c0 7.1-5.8 12.9-12.9 12.9s-12.9-5.8-12.9-12.9V77.6z"
fill = "#e01e5a"
}
path {
d = "M45.2 25.8c-7.1 0-12.9-5.8-12.9-12.9S38.1 0 45.2 0s12.9 5.8 12.9 12.9v12.9H45.2zm0 6.5c7.1 0 12.9 5.8 12.9 12.9s-5.8 12.9-12.9 12.9H12.9C5.8 58.1 0 52.3 0 45.2s5.8-12.9 12.9-12.9h32.3z"
d =
"M45.2 25.8c-7.1 0-12.9-5.8-12.9-12.9S38.1 0 45.2 0s12.9 5.8 12.9 12.9v12.9H45.2zm0 6.5c7.1 0 12.9 5.8 12.9 12.9s-5.8 12.9-12.9 12.9H12.9C5.8 58.1 0 52.3 0 45.2s5.8-12.9 12.9-12.9h32.3z"
fill = "#36c5f0"
}
path {
d = "M97 45.2c0-7.1 5.8-12.9 12.9-12.9s12.9 5.8 12.9 12.9-5.8 12.9-12.9 12.9H97V45.2zm-6.5 0c0 7.1-5.8 12.9-12.9 12.9s-12.9-5.8-12.9-12.9V12.9C64.7 5.8 70.5 0 77.6 0s12.9 5.8 12.9 12.9v32.3z"
d =
"M97 45.2c0-7.1 5.8-12.9 12.9-12.9s12.9 5.8 12.9 12.9-5.8 12.9-12.9 12.9H97V45.2zm-6.5 0c0 7.1-5.8 12.9-12.9 12.9s-12.9-5.8-12.9-12.9V12.9C64.7 5.8 70.5 0 77.6 0s12.9 5.8 12.9 12.9v32.3z"
fill = "#2eb67d"
}
path {
d = "M77.6 97c7.1 0 12.9 5.8 12.9 12.9s-5.8 12.9-12.9 12.9-12.9-5.8-12.9-12.9V97h12.9zm0-6.5c-7.1 0-12.9-5.8-12.9-12.9s5.8-12.9 12.9-12.9h32.3c7.1 0 12.9 5.8 12.9 12.9s-5.8 12.9-12.9 12.9H77.6z"
d =
"M77.6 97c7.1 0 12.9 5.8 12.9 12.9s-5.8 12.9-12.9 12.9-12.9-5.8-12.9-12.9V97h12.9zm0-6.5c-7.1 0-12.9-5.8-12.9-12.9s5.8-12.9 12.9-12.9h32.3c7.1 0 12.9 5.8 12.9 12.9s-5.8 12.9-12.9 12.9H77.6z"
fill = "#ecb22e"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.zegreatrob.coupling.client.integration

import com.zegreatrob.coupling.client.CommandDispatcher
import com.zegreatrob.coupling.client.components.DispatchFunc
import com.zegreatrob.coupling.client.partyPageFunction
import com.zegreatrob.coupling.client.routing.CouplingQuery
import com.zegreatrob.coupling.model.CouplingQueryResult
import com.zegreatrob.coupling.sdk.gql.graphQuery
import com.zegreatrob.react.dataloader.ReloadFunc

val IntegrationPage = partyPageFunction { props, partyId ->
CouplingQuery(
Expand All @@ -18,7 +14,7 @@ val IntegrationPage = partyPageFunction { props, partyId ->
}
addToSlackUrl()
},
toNode = { _: ReloadFunc, _: DispatchFunc<CommandDispatcher>, result: CouplingQueryResult ->
toNode = { _, _, result ->
IntegrationContent.create(
party = result.party?.details?.data ?: return@CouplingQuery null,
integration = result.party?.integration?.data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ interface ClientDeletePairAssignmentsCommandDispatcher :
.deleteIt()
.voidResult()

private fun DeletePairAssignmentsCommand.compoundId() = PartyIdPairAssignmentDocumentId(partyId, pairAssignmentDocumentId)
private fun DeletePairAssignmentsCommand.compoundId() =
PartyIdPairAssignmentDocumentId(partyId, pairAssignmentDocumentId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import com.zegreatrob.coupling.model.party.PartyDetails
import com.zegreatrob.coupling.repository.party.PartyRecordSyntax

interface ClientPartyListQueryDispatcher : PartyRecordSyntax, PartyListQuery.Dispatcher {
override suspend fun perform(query: PartyListQuery): List<PartyDetails> = getPartyRecords().map(Record<PartyDetails>::data)
override suspend fun perform(query: PartyListQuery): List<PartyDetails> =
getPartyRecords().map(Record<PartyDetails>::data)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ import com.zegreatrob.coupling.action.stats.heatmap.CalculateHeatMapAction
import com.zegreatrob.coupling.model.pairassignmentdocument.PairAssignmentDocument
import com.zegreatrob.coupling.model.party.PartyDetails
import com.zegreatrob.coupling.model.player.Player
import com.zegreatrob.coupling.sdk.PartyLoadAllSyntax
import com.zegreatrob.testmints.action.ExecutableActionExecuteSyntax

interface ClientStatisticsQueryDispatcher :
ExecutableActionExecuteSyntax,
ComposeStatisticsAction.Dispatcher,
CalculateHeatMapAction.Dispatcher,
PartyLoadAllSyntax,
StatisticsQuery.Dispatcher {
CalculateHeatMapAction.Dispatcher {

override suspend fun perform(query: StatisticsQuery) = query.loadAll()

private suspend fun StatisticsQuery.loadAll() = partyId.loadAll()?.let { (party, players, history) ->
fun calculate(
party: PartyDetails,
players: List<Player>,
history: List<PairAssignmentDocument>,
): StatisticsQuery.Results {
val (report, heatmapData) = calculateStats(party, players, history)
StatisticsQuery.Results(party, players, history, report, heatmapData)
return StatisticsQuery.Results(party, players, history, report, heatmapData)
}

private fun calculateStats(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ import com.zegreatrob.coupling.repository.pin.PinRepository
import com.zegreatrob.coupling.repository.player.PlayerEmailRepository
import com.zegreatrob.coupling.repository.player.PlayerListGet
import com.zegreatrob.coupling.repository.player.PlayerListGetDeleted
import com.zegreatrob.coupling.sdk.CouplingSdk
import com.zegreatrob.coupling.sdk.CouplingSdkDispatcher
import com.zegreatrob.coupling.sdk.gql.GraphQuery
import com.zegreatrob.testmints.action.ActionCannon
import korlibs.time.TimeProvider
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromJsonElement
Expand All @@ -47,13 +48,13 @@ class MemoryRepositoryCatalog private constructor(
override val playerRepository: PlayerEmailRepository,
override val pairAssignmentDocumentRepository: PairAssignmentDocumentRepository,
override val pinRepository: PinRepository,
) : CouplingSdk,
) : CouplingSdkDispatcher,
ClientDeletePairAssignmentsCommandDispatcher,
ClientDeletePartyCommandDispatcher,
ClientDeletePinCommandDispatcher,
ClientDeletePlayerCommandDispatcher,
ClientPartyListQueryDispatcher,
ClientPartyPlayerQueryDispatcher,
// ClientPartyPlayerQueryDispatcher,
ClientSavePairAssignmentsCommandDispatcher,
ClientSavePartyCommandDispatcher,
ClientSavePinCommandDispatcher,
Expand Down Expand Up @@ -116,5 +117,5 @@ class MemoryRepositoryCatalog private constructor(

override suspend fun perform(command: GrantSlackAccessCommand): VoidResult = VoidResult.Accepted

override val sdk: CouplingSdk get() = this
val sdk: ActionCannon<CouplingSdkDispatcher> get() = ActionCannon(this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ class NewPartyCommand : SimpleSuspendAction<NewPartyCommandDispatcher, PartyDeta

interface NewPartyCommandDispatcher {
suspend fun perform(command: NewPartyCommand) = newParty()

private fun newParty() = PartyDetails(
id = PartyId(""),
defaultBadgeName = "Default",
alternateBadgeName = "Alternate",
name = "New Party",
)
}

fun newParty() = PartyDetails(
id = PartyId(""),
defaultBadgeName = "Default",
alternateBadgeName = "Alternate",
name = "New Party",
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ val PartyConfigPage by nfc<PageProps> { props ->
} else {
CouplingQuery(
commander = props.commander,
query = NewPartyCommand(),
toNode = { _, commandFunc, data -> PartyConfig.create(data, commandFunc) },
query = graphQuery { user() },
toNode = { _, commandFunc, _ -> PartyConfig.create(newParty(), commandFunc) },
key = props.partyId?.value,
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
package com.zegreatrob.coupling.client.player

import com.zegreatrob.coupling.action.player.callsign.FindCallSignAction
import com.zegreatrob.coupling.client.components.player.PlayerConfig
import com.zegreatrob.coupling.client.components.player.create
import com.zegreatrob.coupling.client.partyPageFunction
import com.zegreatrob.coupling.client.routing.CouplingQuery
import com.zegreatrob.coupling.client.routing.PageProps
import com.zegreatrob.coupling.client.routing.playerId
import com.zegreatrob.coupling.model.elements
import com.zegreatrob.coupling.model.party.PartyId
import com.zegreatrob.coupling.sdk.PartyPlayerQuery
import com.zegreatrob.coupling.model.player.Player
import com.zegreatrob.coupling.model.player.callsign.CallSign
import com.zegreatrob.coupling.sdk.gql.graphQuery

val PlayerPage = partyPageFunction { props: PageProps, partyId: PartyId ->
val playerId = props.playerId
CouplingQuery(
commander = props.commander,
query = PartyPlayerQuery(partyId, props.playerId),
toNode = { reload, commandFunc, (party, players, player) ->
PlayerConfig.create(party, player, players, reload, commandFunc)
query = graphQuery { party(partyId) { party(); playerList() } },
toNode = { reload, commandFunc, data ->
val partyDetails = data.party?.details?.data ?: return@CouplingQuery null
val playerList = data.party?.playerList?.elements ?: return@CouplingQuery null
val player = playerList.find { it.id == playerId }
?: playerList.defaultWithCallSign()
PlayerConfig.create(
party = partyDetails,
player = player,
players = playerList,
reload = reload,
dispatchFunc = commandFunc,
)
},
key = "${partyId.value}-${props.playerId}",
key = "${partyId.value}-$playerId",
)
}

private fun List<Player>.defaultWithCallSign() = object : FindCallSignAction.Dispatcher {}
.perform(FindCallSignAction(this, ""))
.let(::defaultPlayer)

private fun defaultPlayer(callSign: CallSign) = Player(
callSignAdjective = callSign.adjective,
callSignNoun = callSign.noun,
avatarType = null,
)
Loading

0 comments on commit 1e87b8c

Please sign in to comment.