Skip to content

Commit 3fb833f

Browse files
author
abstrct
committed
starting to chip away at struct refactoring
1 parent 566898d commit 3fb833f

6 files changed

Lines changed: 41 additions & 40 deletions

File tree

x/structs/keeper/fleet_cache.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ func (cache *FleetCache) ID() string {
4242
}
4343

4444
func (cache *FleetCache) LoadFleet() (found bool) {
45-
cache.Fleet, found = cache.CC.k.GetFleet(cache.CC.ctx, cache.FleetId)
45+
cache.Fleet, cache.FleetLoaded = cache.CC.k.GetFleet(cache.CC.ctx, cache.FleetId)
4646

47-
if (!found) {
47+
if (!cache.FleetLoaded) {
4848
fleet := types.CreateEmptyFleet()
4949
// Set the ID of the appended value
5050
fleet.Id = cache.FleetId
@@ -53,15 +53,13 @@ func (cache *FleetCache) LoadFleet() (found bool) {
5353
player, _ := cache.CC.GetPlayer(cache.PlayerId)
5454
player.SetFleetId(cache.FleetId)
5555

56-
structure := cache.CC.InitialCommandShipStruct(cache.PlayerId, cache.FleetId)
57-
cache.SetCommandStruct(structure.GetStructId())
58-
5956
cache.Fleet = fleet
6057
cache.Changed = true
58+
cache.FleetLoaded = true
59+
structure := cache.CC.InitialCommandShipStruct(cache)
60+
cache.SetCommandStruct(structure.GetStructId())
6161
}
6262

63-
cache.FleetLoaded = true
64-
6563
return cache.FleetLoaded
6664
}
6765

@@ -261,7 +259,7 @@ func (cache *FleetCache) PeaceDeal() (){
261259

262260
func (cache *FleetCache) BuildInitiateReadiness(structure *types.Struct, structType *StructTypeCache, ambit types.Ambit, ambitSlot uint64) (error) {
263261
if structure.GetOwner() != cache.GetOwnerId() {
264-
return types.NewStructOwnershipError(structure.GetStructId(), cache.GetOwnerId(), structure.GetOwner()).WithLocation("fleet", cache.GetFleetId())
262+
return types.NewStructOwnershipError(structure.Id, cache.GetOwnerId(), structure.GetOwner()).WithLocation("fleet", cache.GetFleetId())
265263
}
266264

267265
if cache.IsAway() {
@@ -280,17 +278,17 @@ func (cache *FleetCache) BuildInitiateReadiness(structure *types.Struct, structT
280278
}
281279

282280
if (structType.GetStructType().Category != types.ObjectType_fleet) {
283-
return types.NewStructLocationError(structType.GetId(), ambit.String(), "outside_planet")
281+
return types.NewStructLocationError(structType.ID(), ambit.String(), "outside_planet")
284282
}
285283

286284
// Check that the Struct can exist in the specified ambit
287-
if types.Ambit_flag[ambit]&structType.PossibleAmbit == 0 {
288-
return types.NewStructLocationError(structType.GetId(), ambit.String(), "invalid_ambit")
285+
if types.Ambit_flag[ambit]&structType.GetStructType().PossibleAmbit == 0 {
286+
return types.NewStructLocationError(structType.ID(), ambit.String(), "invalid_ambit")
289287
}
290288

291289
if structType.GetStructType().Type == types.CommandStruct {
292290
if cache.HasCommandStruct() {
293-
return types.NewStructBuildError(structType.GetId(), "fleet", cache.GetFleetId(), "command_exists")
291+
return types.NewStructBuildError(structType.ID(), "fleet", cache.GetFleetId(), "command_exists")
294292
}
295293

296294
} else {
@@ -312,14 +310,14 @@ func (cache *FleetCache) BuildInitiateReadiness(structure *types.Struct, structT
312310
slots = cache.GetFleet().SpaceSlots
313311
slot = cache.GetFleet().Space[ambitSlot]
314312
default:
315-
return types.NewStructBuildError(structType.GetId(), "fleet", cache.GetFleetId(), "invalid_ambit").WithAmbit(ambit.String())
313+
return types.NewStructBuildError(structType.ID(), "fleet", cache.GetFleetId(), "invalid_ambit").WithAmbit(ambit.String())
316314
}
317315

318316
if (ambitSlot >= slots) {
319-
return types.NewStructBuildError(structType.GetId(), "fleet", cache.GetFleetId(), "slot_unavailable").WithSlot(ambitSlot)
317+
return types.NewStructBuildError(structType.ID(), "fleet", cache.GetFleetId(), "slot_unavailable").WithSlot(ambitSlot)
320318
}
321319
if (slot != "") {
322-
return types.NewStructBuildError(structType.GetId(), "fleet", cache.GetFleetId(), "slot_occupied").WithSlot(ambitSlot).WithExistingStruct(slot)
320+
return types.NewStructBuildError(structType.ID(), "fleet", cache.GetFleetId(), "slot_occupied").WithSlot(ambitSlot).WithExistingStruct(slot)
323321
}
324322
}
325323
return nil
@@ -333,12 +331,12 @@ func (cache *FleetCache) MoveReadiness(structure *StructCache, ambit types.Ambit
333331
}
334332

335333
if (structure.GetStructType().Category != types.ObjectType_fleet) {
336-
return types.NewStructLocationError(structure.GetStructType().GetId(), ambit.String(), "outside_planet")
334+
return types.NewStructLocationError(structure.GetTypeId(), ambit.String(), "outside_planet")
337335
}
338336

339337
// Check that the Struct can exist in the specified ambit
340338
if types.Ambit_flag[ambit]&structure.GetStructType().PossibleAmbit == 0 {
341-
return types.NewStructLocationError(structure.GetStructType().GetId(), ambit.String(), "invalid_ambit")
339+
return types.NewStructLocationError(structure.GetTypeId(), ambit.String(), "invalid_ambit")
342340
}
343341

344342
if structure.GetStructType().Type != types.CommandStruct {
@@ -360,14 +358,14 @@ func (cache *FleetCache) MoveReadiness(structure *StructCache, ambit types.Ambit
360358
slots = cache.GetFleet().SpaceSlots
361359
slot = cache.GetFleet().Space[ambitSlot]
362360
default:
363-
return types.NewStructBuildError(structure.GetStructType().GetId(), "fleet", cache.GetFleetId(), "invalid_ambit").WithAmbit(ambit.String())
361+
return types.NewStructBuildError(structure.GetTypeId(), "fleet", cache.GetFleetId(), "invalid_ambit").WithAmbit(ambit.String())
364362
}
365363

366364
if (ambitSlot >= slots) {
367-
return types.NewStructBuildError(structure.GetStructType().GetId(), "fleet", cache.GetFleetId(), "slot_unavailable").WithSlot(ambitSlot)
365+
return types.NewStructBuildError(structure.GetTypeId(), "fleet", cache.GetFleetId(), "slot_unavailable").WithSlot(ambitSlot)
368366
}
369367
if (slot != "") {
370-
return types.NewStructBuildError(structure.GetStructType().GetId(), "fleet", cache.GetFleetId(), "slot_occupied").WithSlot(ambitSlot).WithExistingStruct(slot)
368+
return types.NewStructBuildError(structure.GetTypeId(), "fleet", cache.GetFleetId(), "slot_occupied").WithSlot(ambitSlot).WithExistingStruct(slot)
371369
}
372370
}
373371
return nil

x/structs/keeper/msg_server_address_register.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ func (k msgServer) AddressRegister(goCtx context.Context, msg *types.MsgAddressR
2727
return &types.MsgAddressRegisterResponse{}, err
2828
}
2929

30-
if !player.LoadPlayer() {
30+
if !player.CheckPlayer() != nil {
3131
return &types.MsgAddressRegisterResponse{}, types.NewObjectNotFoundError("player", msg.PlayerId)
3232
}
3333

34-
3534
// Is the address associated with an account yet
3635
playerFoundForAddress := cc.GetPlayerIndexFromAddress(msg.Address)
3736
if (playerFoundForAddress > 0) {
@@ -47,7 +46,7 @@ func (k msgServer) AddressRegister(goCtx context.Context, msg *types.MsgAddressR
4746
// Does this creator address have the permissions to do this
4847
addressPermissionId := GetAddressPermissionIDBytes(msg.Creator)
4948
// The calling address must have a minimum of the same permission level
50-
if (!k.PermissionHasAll(ctx, addressPermissionId, types.Permission(msg.Permissions))) {
49+
if (!cc.PermissionHasAll(addressPermissionId, types.Permission(msg.Permissions))) {
5150
return &types.MsgAddressRegisterResponse{}, types.NewPermissionError("address", msg.Creator, "", "", uint64(msg.Permissions), "address_association")
5251
}
5352

@@ -88,7 +87,7 @@ func (k msgServer) AddressRegister(goCtx context.Context, msg *types.MsgAddressR
8887

8988
// Add the permission to the new address
9089
newAddressPermissionId := GetAddressPermissionIDBytes(msg.Address)
91-
k.PermissionAdd(ctx, newAddressPermissionId, types.Permission(msg.Permissions))
90+
cc.PermissionAdd(newAddressPermissionId, types.Permission(msg.Permissions))
9291

9392

9493
// Move Funds

x/structs/keeper/planet_cache.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func (cache *PlanetCache) IsSuccessful(successRate fraction.Fraction) bool {
305305

306306
func (cache *PlanetCache) BuildInitiateReadiness(structure *types.Struct, structType *StructTypeCache, ambit types.Ambit, ambitSlot uint64) (error) {
307307
if structure.GetOwner() != cache.GetOwnerId() {
308-
return types.NewStructOwnershipError(structure.GetStructId(), cache.GetOwnerId(), structure.GetOwner()).WithLocation("planet", cache.GetPlanetId())
308+
return types.NewStructOwnershipError(structure.Id, cache.GetOwnerId(), structure.GetOwner()).WithLocation("planet", cache.GetPlanetId())
309309
}
310310

311311
if structType.GetStructType().Type == types.CommandStruct {
@@ -325,12 +325,12 @@ func (cache *PlanetCache) BuildInitiateReadiness(structure *types.Struct, struct
325325
}
326326

327327
if (structType.GetStructType().Category != types.ObjectType_planet) {
328-
return types.NewStructLocationError(structType.GetId(), ambit.String(), "outside_planet")
328+
return types.NewStructLocationError(structType.ID(), ambit.String(), "outside_planet")
329329
}
330330

331331
// Check that the Struct can exist in the specified ambit
332-
if types.Ambit_flag[ambit]&structType.PossibleAmbit == 0 {
333-
return types.NewStructLocationError(structType.GetId(), ambit.String(), "invalid_ambit")
332+
if types.Ambit_flag[ambit]&structType.GetStructType().PossibleAmbit == 0 {
333+
return types.NewStructLocationError(structType.ID(), ambit.String(), "invalid_ambit")
334334
}
335335

336336
var slots uint64
@@ -350,14 +350,14 @@ func (cache *PlanetCache) BuildInitiateReadiness(structure *types.Struct, struct
350350
slots = cache.GetPlanet().SpaceSlots
351351
slot = cache.GetPlanet().Space[ambitSlot]
352352
default:
353-
return types.NewStructBuildError(structType.GetId(), "planet", cache.GetPlanetId(), "invalid_ambit").WithAmbit(ambit.String())
353+
return types.NewStructBuildError(structType.ID(), "planet", cache.GetPlanetId(), "invalid_ambit").WithAmbit(ambit.String())
354354
}
355355

356356
if (ambitSlot >= slots) {
357-
return types.NewStructBuildError(structType.GetId(), "planet", cache.GetPlanetId(), "slot_unavailable").WithSlot(ambitSlot)
357+
return types.NewStructBuildError(structType.ID(), "planet", cache.GetPlanetId(), "slot_unavailable").WithSlot(ambitSlot)
358358
}
359359
if (slot != "") {
360-
return types.NewStructBuildError(structType.GetId(), "planet", cache.GetPlanetId(), "slot_occupied").WithSlot(ambitSlot).WithExistingStruct(slot)
360+
return types.NewStructBuildError(structType.ID(), "planet", cache.GetPlanetId(), "slot_occupied").WithSlot(ambitSlot).WithExistingStruct(slot)
361361
}
362362

363363
return nil
@@ -371,16 +371,16 @@ func (cache *PlanetCache) MoveReadiness(structure *StructCache, ambit types.Ambi
371371
}
372372

373373
if structure.GetStructType().Type == types.CommandStruct {
374-
return types.NewStructLocationError(structure.GetStructType().GetId(), ambit.String(), "command_struct_fleet_only")
374+
return types.NewStructLocationError(structure.GetTypeId(), ambit.String(), "command_struct_fleet_only")
375375
}
376376

377377
if (structure.GetStructType().Category != types.ObjectType_planet) {
378-
return types.NewStructLocationError(structure.GetStructType().GetId(), ambit.String(), "outside_planet")
378+
return types.NewStructLocationError(structure.GetTypeId(), ambit.String(), "outside_planet")
379379
}
380380

381381
// Check that the Struct can exist in the specified ambit
382382
if types.Ambit_flag[ambit]&structure.GetStructType().PossibleAmbit == 0 {
383-
return types.NewStructLocationError(structure.GetStructType().GetId(), ambit.String(), "invalid_ambit")
383+
return types.NewStructLocationError(structure.GetTypeId(), ambit.String(), "invalid_ambit")
384384
}
385385

386386
var slots uint64
@@ -400,14 +400,14 @@ func (cache *PlanetCache) MoveReadiness(structure *StructCache, ambit types.Ambi
400400
slots = cache.GetPlanet().SpaceSlots
401401
slot = cache.GetPlanet().Space[ambitSlot]
402402
default:
403-
return types.NewStructBuildError(structure.GetStructType().GetId(), "planet", cache.GetPlanetId(), "invalid_ambit").WithAmbit(ambit.String())
403+
return types.NewStructBuildError(structure.GetTypeId(), "planet", cache.GetPlanetId(), "invalid_ambit").WithAmbit(ambit.String())
404404
}
405405

406406
if (ambitSlot >= slots) {
407-
return types.NewStructBuildError(structure.GetStructType().GetId(), "planet", cache.GetPlanetId(), "slot_unavailable").WithSlot(ambitSlot)
407+
return types.NewStructBuildError(structure.GetTypeId(), "planet", cache.GetPlanetId(), "slot_unavailable").WithSlot(ambitSlot)
408408
}
409409
if (slot != "") {
410-
return types.NewStructBuildError(structure.GetStructType().GetId(), "planet", cache.GetPlanetId(), "slot_occupied").WithSlot(ambitSlot).WithExistingStruct(slot)
410+
return types.NewStructBuildError(structure.GetTypeId(), "planet", cache.GetPlanetId(), "slot_occupied").WithSlot(ambitSlot).WithExistingStruct(slot)
411411
}
412412

413413
return nil

x/structs/keeper/struct_cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,12 @@ func (cache *StructCache) SetOwnerId(owner string) {
233233

234234
func (cache *StructCache) ResetBlockStartOreMine() {
235235
uctx := sdk.UnwrapSDKContext(cache.CC.ctx)
236-
cache.CC.SetStructAttribute(cache.BlockStartOreMineAttributeId, uint64(uctx.BlockHeight())
236+
cache.CC.SetStructAttribute(cache.BlockStartOreMineAttributeId, uint64(uctx.BlockHeight()))
237237
}
238238

239239
func (cache *StructCache) ResetBlockStartOreRefine() {
240240
uctx := sdk.UnwrapSDKContext(cache.CC.ctx)
241-
cache.CC.SetStructAttribute(cache.BlockStartOreRefineAttributeId, uint64(uctx.BlockHeight())
241+
cache.CC.SetStructAttribute(cache.BlockStartOreRefineAttributeId, uint64(uctx.BlockHeight()))
242242
}
243243

244244
func (cache *StructCache) ClearBlockStartOreMine() {

x/structs/keeper/struct_context.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package keeper
22

3+
import (
4+
"structs/x/structs/types"
5+
)
6+
37
// GetStruct returns a StructCache, loading from store if not already cached.
48
// Returns the same instance if called multiple times with the same ID.
59
func (cc *CurrentContext) GetStruct(structId string) *StructCache {

x/structs/types/struct.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func CreateBaseStruct(structTypeId uint64, creator string, owner string, locatio
2020
Creator: creator,
2121
Owner: owner,
2222

23-
Type: structType.Id,
23+
Type: structTypeId,
2424

2525
LocationType: locationType,
2626
OperatingAmbit: ambit,

0 commit comments

Comments
 (0)