Skip to content

Commit

Permalink
Added capacity field to resources and effects query (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-karuna authored Jul 2, 2024
1 parent 0a53a3d commit 37910ec
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 21 deletions.
5 changes: 3 additions & 2 deletions cardinal/component/effect.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ func GetAllEffectTypes() []EffectType {
}

type Effect struct {
Type EffectType `json:"type"`
Amount int `json:"amount"`
Type EffectType `json:"type"`
Amount int `json:"amount"`
Capacity int `json:"capacity,omitempty"`
}

func (Effect) Name() string {
Expand Down
5 changes: 3 additions & 2 deletions cardinal/component/farming.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package component

type Farming struct {
Type ResourceType `json:"type"`
Speed float32 `json:"speed"`
Type ResourceType `json:"type"`
Speed float32 `json:"speed"`
Capacity int `json:"capacity,omitempty"`
}

func (Farming) Name() string {
Expand Down
1 change: 0 additions & 1 deletion cardinal/component/player_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package component
type PlayerResources struct {
Resources []Resource `json:"resources"`
Effects []Effect `json:"effects"`
// Farming []Farming `json:"farming"`
}

func (PlayerResources) Name() string {
Expand Down
5 changes: 3 additions & 2 deletions cardinal/component/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ func GetAllResourceTypes() []ResourceType {
}

type Resource struct {
Type ResourceType `json:"type"`
Amount float32 `json:"amount"`
Type ResourceType `json:"type"`
Amount float32 `json:"amount"`
Capacity int `json:"capacity,omitempty"`
}

func (Resource) Name() string {
Expand Down
31 changes: 20 additions & 11 deletions cardinal/query/building_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,34 @@ type BuildingsInfoRequest struct {
}

type BuildingInfoResponse struct {
Building comp.BuildingType `json:"building"`
TileType comp.TileType `json:"tileType"`
Resources []comp.Resource `json:"resources"`
Effect *comp.Effect `json:"effect,omitempty"`
Farming *comp.Farming `json:"farming,omitempty"`
Building comp.BuildingType `json:"building"`
TileType comp.TileType `json:"tileType"`
Resources []comp.Resource `json:"resources"`
Effect *comp.Effect `json:"effect,omitempty"`
Farming *comp.Farming `json:"farming,omitempty"`
UnitLimit int `json:"unitLimit,omitempty"`
StorageCapacity int `json:"storageCapacity,omitempty"`
}

func AllBuildings(_ cardinal.WorldContext, _ *BuildingsInfoRequest) (*[]BuildingInfoResponse, error) {
return GetAllBuildings(), nil
}

func GetAllBuildings() *[]BuildingInfoResponse {
buildings := make([]BuildingInfoResponse, len(comp.GetAllBuildingTypes()))
for i, buildingType := range comp.GetAllBuildingTypes() {
buildingConf := comp.BuildingConfigs[buildingType]

buildings[i] = BuildingInfoResponse{
Building: buildingType,
TileType: buildingConf.TileType,
Resources: buildingConf.Resources,
Effect: buildingConf.Effect,
Farming: buildingConf.Farming,
Building: buildingType,
TileType: buildingConf.TileType,
Resources: buildingConf.Resources,
Effect: buildingConf.Effect,
Farming: buildingConf.Farming,
UnitLimit: buildingConf.UnitLimit,
StorageCapacity: buildingConf.StorageCapacity,
}
}
return &buildings, nil

return &buildings
}
27 changes: 24 additions & 3 deletions cardinal/query/player_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func PlayerResources(world cardinal.WorldContext, req *PlayerResourcesRequest) (
}

aggregatedfarmingSlice := make([]comp.Farming, 0, len(aggregatedFarmingMap))

for resourceType, speed := range aggregatedFarmingMap {
aggregatedfarmingSlice = append(aggregatedfarmingSlice, comp.Farming{
Type: resourceType,
Expand All @@ -52,9 +51,31 @@ func PlayerResources(world cardinal.WorldContext, req *PlayerResourcesRequest) (
return nil, fmt.Errorf("error querying player %s resources", req.Nickname)
}

effectsMap := make(map[comp.EffectType]int)
capacity := 0
for _, building := range *GetAllBuildings() {
if building.Effect != nil {
effectsMap[building.Effect.Type] += building.Effect.Amount
}

capacity += building.StorageCapacity
}

resourcesResponse := make([]comp.Resource, 0, len(playerResources.Resources))
for _, resource := range playerResources.Resources {
resource.Capacity = capacity
resourcesResponse = append(resourcesResponse, resource)
}

effectsResponse := make([]comp.Effect, 0, len(playerResources.Effects))
for _, effect := range playerResources.Effects {
effect.Capacity = effectsMap[effect.Type]
effectsResponse = append(effectsResponse, effect)
}

return &PlayerResourcesResponse{
Resources: playerResources.Resources,
Effects: playerResources.Effects,
Resources: resourcesResponse,
Effects: effectsResponse,
Farming: aggregatedfarmingSlice,
}, err
}

0 comments on commit 37910ec

Please sign in to comment.