@@ -3,27 +3,46 @@ package keeper
33import (
44 "structs/x/structs/types"
55 "strings"
6+ "cosmossdk.io/math"
67)
78
89
9- func (cc * CurrentContext ) GetInfusionById (infusionKey string ) (* InfusionCache , bool ) {
10+ func (cc * CurrentContext ) GetInfusionById (infusionKey string ) * InfusionCache {
11+
1012 if cache , exists := cc .infusions [infusionKey ]; exists {
11- return cache , exists
13+ return cache
14+ }
15+
16+ infusionIdSplit := strings .Split (infusionKey , "-" )
17+ if len (infusionIdSplit ) != 3 {
18+ return & InfusionCache {}
19+ }
20+
21+ destinationId := infusionIdSplit [0 ] + "-" + infusionIdSplit [1 ]
22+ address := infusionIdSplit [2 ]
23+
24+ cc .infusions [infusionKey ] = & InfusionCache {
25+ InfusionId : infusionKey ,
26+ DestinationId : destinationId ,
27+ Address : address ,
28+ CC : cc ,
29+ DestinationFuelAttributeId : GetGridAttributeIDByObjectId (types .GridAttributeType_fuel , destinationId ),
30+ DestinationCapacityAttributeId : GetGridAttributeIDByObjectId (types .GridAttributeType_capacity , destinationId ),
1231 }
1332
14- return & InfusionCache {}, false
33+ return cc . infusions [ infusionKey ]
1534}
1635
1736
18- func (cc * CurrentContext ) GetInfusion (destinationType types. ObjectType , destinationId string , address string ) * InfusionCache {
19- infusionKey := destinationId + "/ " + address
37+ func (cc * CurrentContext ) GetInfusion (destinationId string , address string ) * InfusionCache {
38+ infusionKey := destinationId + "- " + address
2039
2140 if cache , exists := cc .infusions [infusionKey ]; exists {
2241 return cache
2342 }
2443
2544 cc .infusions [infusionKey ] = & InfusionCache {
26- DestinationType : destinationType ,
45+ InfusionId : infusionKey ,
2746 DestinationId : destinationId ,
2847 Address : address ,
2948 CC : cc ,
@@ -34,6 +53,34 @@ func (cc *CurrentContext) GetInfusion(destinationType types.ObjectType, destinat
3453 return cc .infusions [infusionKey ]
3554}
3655
56+ func (cc * CurrentContext ) GetAllInfusionByDestination (destinationId string ) (infusions []* InfusionCache ) {
57+ infusionIds := cc .k .GetAllInfusionIdsByDestination (cc .ctx , destinationId )
58+ for _ , infusionId := range infusionIds {
59+ infusion := cc .GetInfusionById (infusionId )
60+ infusions = append (infusions , infusion )
61+ }
62+ return
63+ }
64+
65+ func (cc * CurrentContext ) UpsertInfusion (destinationType types.ObjectType , destinationId string , address string , playerId string ) (* InfusionCache ){
66+ infusion := cc .GetInfusion (destinationId , address )
67+
68+ if infusion .CheckInfusion () != nil {
69+ infusion .Infusion = types.Infusion {
70+ DestinationId : destinationId ,
71+ DestinationType : destinationType ,
72+ Address : address ,
73+ PlayerId : playerId ,
74+ Commission : math .LegacyZeroDec (),
75+ }
76+
77+ infusion .InfusionLoaded = true
78+ infusion .Changed = true
79+ }
80+ return infusion
81+ }
82+
83+
3784func (cc * CurrentContext ) ProcessInfusionDestructionQueue () {
3885 for {
3986 queue := cc .k .GetInfusionDestructionQueue (cc .ctx , true )
@@ -42,28 +89,18 @@ func (cc *CurrentContext) ProcessInfusionDestructionQueue() {
4289 }
4390
4491 for _ , infusionId := range queue {
45- // infusionId format: "destinationId-address" (e.g. "3-1-cosmos1abc...")
46- // destinationId itself contains "-", so split on the last dash
47- lastDash := strings .LastIndex (infusionId , "-" )
48- if lastDash == - 1 {
49- continue
50- }
51- destinationId := infusionId [:lastDash ]
52- address := infusionId [lastDash + 1 :]
53-
54- infusion , found := cc .k .GetInfusion (cc .ctx , destinationId , address )
55- if found && infusion .Power == 0 && infusion .Defusing == 0 {
56- cache := cc .GetInfusion (infusion .DestinationType , destinationId , address )
57- cache .Destroy ()
92+ infusion := cc .GetInfusionById (infusionId )
93+ if (infusion .CheckInfusion () == nil && infusion .GetInfusion ().Power == 0 && infusion .GetInfusion ().Defusing == 0 ) {
94+ infusion .Destroy ()
5895 }
5996 }
6097 }
6198}
6299
63100func (cc * CurrentContext ) DestroyAllInfusions (infusionIds []string ) {
64101 for _ , infusionId := range infusionIds {
65- infusion , found := cc .GetInfusionById (infusionId )
66- if found {
102+ infusion := cc .GetInfusionById (infusionId )
103+ if infusion . CheckInfusion () == nil {
67104 infusion .Destroy ()
68105 }
69106 }
0 commit comments