Skip to content

Commit 8009429

Browse files
author
abstrct
committed
substation refactoring
1 parent 0e679f4 commit 8009429

2 files changed

Lines changed: 1 addition & 116 deletions

File tree

x/structs/keeper/substation.go

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@ import (
1919

2020
// GetNextPlayerId allocate a new substation ID
2121
func (k Keeper) GetNextSubstationId(ctx context.Context) uint64 {
22-
2322
nextId := k.GetSubstationCount(ctx)
24-
2523
k.SetSubstationCount(ctx, nextId + 1)
26-
2724
return nextId
2825
}
2926

@@ -60,7 +57,6 @@ func (k Keeper) SetSubstation(ctx context.Context, substation types.Substation)
6057

6158
ctxSDK := sdk.UnwrapSDKContext(ctx)
6259
_ = ctxSDK.EventManager().EmitTypedEvent(&types.EventSubstation{Substation: &substation})
63-
6460
}
6561

6662

@@ -74,77 +70,6 @@ func (k Keeper) ClearSubstation(ctx context.Context, substationId string) {
7470
_ = ctxSDK.EventManager().EmitTypedEvent(&types.EventDelete{ ObjectId: substationId })
7571
}
7672

77-
// RemoveSubstation removes a substation from the store
78-
func (k Keeper) RemoveSubstation(ctx context.Context, substationId string, migrationSubstationId string) {
79-
store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), types.KeyPrefix(types.SubstationKey))
80-
81-
/*
82-
* This is going to start out very inefficient. We'll need to tackle
83-
* ways to improve these types of graph traversal
84-
*/
85-
playerConnections := k.GetGridAttribute(ctx, GetGridAttributeIDByObjectId(types.GridAttributeType_connectionCount, substationId))
86-
if (playerConnections > 0) {
87-
88-
connectedPlayers := k.GetAllPlayerBySubstation(ctx, substationId)
89-
// Need all players connected
90-
if (migrationSubstationId == "") {
91-
for _, disconnectPlayer := range connectedPlayers {
92-
k.SubstationDisconnectPlayer(ctx, disconnectPlayer)
93-
}
94-
} else {
95-
if (migrationSubstationId == substationId) {
96-
// TODO Move/copy this check to the message verification
97-
return // error
98-
}
99-
migrationSubstation, migrationSubstationFound := k.GetSubstation(ctx, migrationSubstationId)
100-
if (!migrationSubstationFound) {
101-
return // error
102-
}
103-
for _, migratePlayer := range connectedPlayers {
104-
k.SubstationConnectPlayer(ctx, migrationSubstation, migratePlayer)
105-
}
106-
107-
}
108-
}
109-
110-
111-
/* TODO
112-
* This isn't all super amazing, it's a lot of scans. Allocations Out being the
113-
* least of the problem but Allocations In will be super inefficient.
114-
*
115-
* Potential solution in the future is to have a Decommissioning state for substations
116-
* where the object isn't deleted until all other things are moved/disconnected but it
117-
* monitors allocation connection count until these values are zero.
118-
*
119-
* Basically, don't let it be deleted until that's all dealt with manually.
120-
*/
121-
122-
// Destroy allocations out
123-
allocationsOut := k.GetAllAllocationBySourceIndex(ctx, substationId)
124-
k.DestroyAllAllocations(ctx, allocationsOut)
125-
126-
// Disconnect allocations in
127-
// TODO Need a more efficient way than scan
128-
allocationsIn := k.GetAllAllocationByDestinationIndex(ctx, substationId)
129-
k.DestroyAllAllocations(ctx, allocationsIn)
130-
131-
132-
// Clear out Grid attributes
133-
k.ClearGridAttribute(ctx, GetGridAttributeIDByObjectId(types.GridAttributeType_load, substationId))
134-
k.ClearGridAttribute(ctx, GetGridAttributeIDByObjectId(types.GridAttributeType_capacity, substationId))
135-
136-
k.ClearGridAttribute(ctx, GetGridAttributeIDByObjectId(types.GridAttributeType_connectionCount, substationId))
137-
k.ClearGridAttribute(ctx, GetGridAttributeIDByObjectId(types.GridAttributeType_connectionCapacity, substationId))
138-
139-
k.ClearGridAttribute(ctx, GetGridAttributeIDByObjectId(types.GridAttributeType_allocationPointerStart, substationId))
140-
k.ClearGridAttribute(ctx, GetGridAttributeIDByObjectId(types.GridAttributeType_allocationPointerEnd, substationId))
141-
142-
store.Delete([]byte(substationId))
143-
144-
ctxSDK := sdk.UnwrapSDKContext(ctx)
145-
_ = ctxSDK.EventManager().EmitTypedEvent(&types.EventDelete{ ObjectId: substationId })
146-
}
147-
14873
// GetSubstation returns a substation from its id
14974
func (k Keeper) GetSubstation(ctx context.Context, substationId string) (val types.Substation, found bool) {
15075
store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), types.KeyPrefix(types.SubstationKey))
@@ -175,44 +100,3 @@ func (k Keeper) GetAllSubstation(ctx context.Context) (list []types.Substation)
175100
}
176101

177102

178-
179-
func (k Keeper) SubstationConnectPlayer(ctx context.Context, substation types.Substation, player types.Player) (types.Player, error) {
180-
181-
// If the player is already on a substation then disconnect them from it first
182-
if (player.SubstationId != "") {
183-
k.SetGridAttributeDecrement(ctx, GetGridAttributeIDByObjectId(types.GridAttributeType_connectionCount, player.SubstationId), 1)
184-
185-
// Update Connection Capacity for the old Substation
186-
k.UpdateGridConnectionCapacity(ctx, player.SubstationId)
187-
}
188-
189-
// Update the player record
190-
player.SubstationId = substation.Id
191-
// Commit the player changes
192-
k.SetPlayer(ctx, player)
193-
194-
195-
k.SetGridAttributeIncrement(ctx, GetGridAttributeIDByObjectId(types.GridAttributeType_connectionCount, player.SubstationId), 1)
196-
// Update Connection Capacity
197-
k.UpdateGridConnectionCapacity(ctx, player.SubstationId)
198-
199-
return player, nil
200-
201-
}
202-
203-
func (k Keeper) SubstationDisconnectPlayer(ctx context.Context, player types.Player) (types.Player, error) {
204-
205-
// If the player is already on a substation then disconnect them from it first
206-
if (player.SubstationId != "") {
207-
k.SetGridAttributeDecrement(ctx, GetGridAttributeIDByObjectId(types.GridAttributeType_connectionCount, player.SubstationId), 1)
208-
// Update Connection Capacity for the old Substation
209-
k.UpdateGridConnectionCapacity(ctx, player.SubstationId)
210-
}
211-
212-
// Update the player record
213-
player.SubstationId = ""
214-
// Commit the player changes
215-
k.SetPlayer(ctx, player)
216-
217-
return player, nil
218-
}

x/structs/keeper/substation_cache.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func (cache *SubstationCache) Delete(migrationSubstation *SubstationCache) {
8888
connectedPlayers := cc.GetAllPlayerBySubstation(substationId)
8989
for _, disconnectPlayer := range connectedPlayers {
9090
if migrationSubstation.GetSubstationId() == cache.GetSubstationId() {
91+
// Somewhat punishes tomfoolery but whatever
9192
disconnectPlayer.DisconnectSubstation()
9293
} else {
9394
disconnectPlayer.MigrateSubstation(migrationSubstation.GetSubstationId())

0 commit comments

Comments
 (0)