Names refactoring

This commit is contained in:
Anton Vakhrushev 2019-10-13 13:19:43 +03:00
parent bccff5461d
commit 19f6d479f2
11 changed files with 114 additions and 120 deletions

View File

@ -10,9 +10,9 @@ describe Game::Building do
bg = Game::Building.new Game::Building::Type::CrystalMiner, **{ bg = Game::Building.new Game::Building::Type::CrystalMiner, **{
production: Game::Production.new( production: Game::Production.new(
ts: 20, ts: 20,
input: Game::Resources.new, input: Game::ResourceBag.new,
output: Game::Resources.new({ output: Game::ResourceBag.new({
Game::Resources::Type::Crystals => 100, Game::Resource::Type::Crystals => 100,
}) })
), ),
} }

View File

@ -22,8 +22,8 @@ describe Game::Command do
building = Game::Building.new Game::Building::Type::StartPoint, **{ building = Game::Building.new Game::Building::Type::StartPoint, **{
construction: Game::Construction.new( construction: Game::Construction.new(
ts: 10, ts: 10,
cost: Game::Resources.new({ cost: Game::ResourceBag.new({
Game::Resources::Type::Crystals => 100, Game::Resource::Type::Crystals => 100,
}) })
), ),
} }

View File

@ -3,34 +3,34 @@ require "./spec_helper"
module Game::Test module Game::Test
describe Deposit do describe Deposit do
it "should be created fulfilled" do it "should be created fulfilled" do
dep = Deposit.new(Resources::Type::Crystals, 100) dep = Deposit.new(Resource::Type::Crystals, 100)
dep.cap.should eq 100 dep.cap.should eq 100
dep.cur.should eq 100 dep.cur.should eq 100
end end
it "can be created partially filled" do it "can be created partially filled" do
dep = Deposit.new(Resources::Type::Crystals, 100, 20) dep = Deposit.new(Resource::Type::Crystals, 100, 20)
dep.cap.should eq 100 dep.cap.should eq 100
dep.cur.should eq 20 dep.cur.should eq 20
end end
it "should be decreased with span" do it "should be decreased with span" do
dep = Deposit.new(Resources::Type::Crystals, 100) dep = Deposit.new(Resource::Type::Crystals, 100)
dep.dec Deposit::Span.new(Resources::Type::Crystals, 20) dep.dec Resource.new(Resource::Type::Crystals, 20)
dep.cap.should eq 100 dep.cap.should eq 100
dep.cur.should eq 80 dep.cur.should eq 80
end end
it "should not be increased above capacity" do it "should not be increased above capacity" do
dep = Deposit.new(Resources::Type::Crystals, 100, 20) dep = Deposit.new(Resource::Type::Crystals, 100, 20)
dep.inc Deposit::Span.new(Resources::Type::Crystals, 100) dep.inc Resource.new(Resource::Type::Crystals, 100)
dep.cap.should eq 100 dep.cap.should eq 100
dep.cur.should eq 100 dep.cur.should eq 100
end end
it "should not be decreased below zero" do it "should not be decreased below zero" do
dep = Deposit.new(Resources::Type::Crystals, 100) dep = Deposit.new(Resource::Type::Crystals, 100)
dep.dec Deposit::Span.new(Resources::Type::Crystals, 120) dep.dec Resource.new(Resource::Type::Crystals, 120)
dep.cap.should eq 100 dep.cap.should eq 100
dep.cur.should eq 0 dep.cur.should eq 0
end end

View File

@ -1,10 +1,10 @@
require "./spec_helper" require "./spec_helper"
module Test::GameResources module Game::TestResourceBag
alias Res = Game::Resources alias Res = ResourceBag
alias ResType = Game::Resources::Type alias ResType = Resource::Type
describe Game::Resources do describe ResourceBag do
it "should be created from hash" do it "should be created from hash" do
res = Res.new({ResType::Crystals => 100}) res = Res.new({ResType::Crystals => 100})
res[ResType::Crystals].should eq 100 res[ResType::Crystals].should eq 100

View File

@ -1,6 +1,6 @@
module Game module Game
class Production class Production
def initialize(@ts : TimeSpan, @input : Resources, @output : Resources) def initialize(@ts : TimeSpan, @input : ResourceBag, @output : ResourceBag)
end end
getter ts getter ts
@ -9,17 +9,17 @@ module Game
end end
class Mining class Mining
def initialize(@ts : TimeSpan, @dep : Deposit::Span) def initialize(@ts : TimeSpan, @resource : Resource)
end end
getter ts getter ts
getter dep getter resource
end end
class Construction class Construction
def initialize( def initialize(
@ts : TimeSpan, @ts : TimeSpan,
@cost : Resources, @cost : ResourceBag,
@requirements : Array(Building::Type) = [] of Building::Type @requirements : Array(Building::Type) = [] of Building::Type
) )
end end
@ -29,11 +29,11 @@ module Game
getter requirements getter requirements
def self.immediatly def self.immediatly
Construction.new 0, Resources.new Construction.new 0, ResourceBag.new
end end
def self.free(ts : TimeSpan) def self.free(ts : TimeSpan)
Construction.new ts, Resources.new Construction.new ts, ResourceBag.new
end end
end end

View File

@ -11,7 +11,7 @@ module Game
Building.new Building::Type::CrystalMiner, **{ Building.new Building::Type::CrystalMiner, **{
mining: Mining.new( mining: Mining.new(
ts: 20, ts: 20,
dep: Deposit::Span.new(Resources::Type::Crystals, 40) resource: Resource.new(Resource::Type::Crystals, 40)
), ),
} }
) )
@ -20,14 +20,14 @@ module Game
Building.new Building::Type::CrystalRestorer, **{ Building.new Building::Type::CrystalRestorer, **{
construction: Construction.new( construction: Construction.new(
ts: 30, ts: 30,
cost: Resources.new({ cost: ResourceBag.new({
Resources::Type::Crystals => 100, Resource::Type::Crystals => 100,
}), }),
requirements: [] of Game::Building::Type requirements: [] of Game::Building::Type
), ),
restoration: Mining.new( restoration: Mining.new(
ts: 30, ts: 30,
dep: Deposit::Span.new(Resources::Type::Crystals, 20) resource: Resource.new(Resource::Type::Crystals, 20)
), ),
} }
) )
@ -36,18 +36,18 @@ module Game
Building.new Building::Type::Terraformer, **{ Building.new Building::Type::Terraformer, **{
construction: Construction.new( construction: Construction.new(
ts: 120, ts: 120,
cost: Resources.new({ cost: ResourceBag.new({
Resources::Type::Crystals => 300, Resource::Type::Crystals => 300,
}), }),
requirements: [] of Game::Building::Type requirements: [] of Game::Building::Type
), ),
production: Production.new( production: Production.new(
ts: 60, ts: 60,
input: Resources.new({ input: ResourceBag.new({
Resources::Type::Crystals => 50, Resource::Type::Crystals => 50,
}), }),
output: Resources.new({ output: ResourceBag.new({
Resources::Type::Terraformation => 5, Resource::Type::Terraformation => 5,
}) })
), ),
} }

View File

@ -27,47 +27,46 @@ module Game
def finish(world : World) def finish(world : World)
world.map.set(BuildingTile.new(@point, @building)) world.map.set(BuildingTile.new(@point, @building))
# world.push(MineCommand.new(@point)) if @building.mining
world.push(MineCommand.new(@point))
end
end end
end end
# class MineCommand < Command class MineCommand < Command
# @output : Resources | Nil @holded : Resource | Nil = nil
# def initialize(@point : Point) def initialize(@point : Point)
# end end
# def start(world : World) : TimeSpan def start(world : World) : TimeSpan
# tile = world.map.get(@point).as(BuildingTile) tile = world.map.get(@point).as(BuildingTile)
# building = tile.building building = tile.building
# production = building.production mining = building.mining.as(Mining)
# if !world.resources.has(production.input) # deposit_tile = nearest_deposit(world, mining.resource.type)
# return production.ts # if deposit_tile
# end # @holded = deposit_tile.dep.dec(mining.resource)
# target_resource = production.type # end
# world.resources.dec(production.input) mining.ts
# @output = production.output end
# production.ts
# end
# def finish(world : World) def finish(world : World)
# if @output.nil? if @holded
# return # world.resources.inc(@holded)
# end end
# world.resources.inc(output) world.push(MineCommand.new(@point))
# world.push(MineCommand.new(@point)) end
# end
# def desc : String def desc : String
# sprintf "Build harvester site at %d,%d", @point.x, @point.y sprintf "Mine `smth` from %d,%d", @point.x, @point.y
# end end
# # private def nearest_deposit(world : World, res : Resources::Type) # private def nearest_deposit(world : World, res_type : Resource::Type)
# # world.map.nearest_tile @point do |tile| # world.map.nearest_tile @point do |tile|
# # tile.has_role(TileRole::Deposit) && tile.cur > 0 # tile.is_a?(DepositTile) && tile.dep.type == res_type && tile.dep.cur > 0
# # end # end
# # end # end
# end end
# class BuildCrystalHarvesterCommand < Command # class BuildCrystalHarvesterCommand < Command
# BUILD_TIME = 30 # BUILD_TIME = 30

View File

@ -1,40 +1,30 @@
module Game class Game::Deposit
class Deposit @cur : Capacity = 0
class Span
def initialize(@res : Resources::Type, @cap : Capacity)
end
getter res def initialize(@type : Resource::Type, @cap : Capacity)
getter cap @cur = @cap
end end
@cur : Capacity = 0 def initialize(@type : Resource::Type, @cap : Capacity, @cur : Capacity)
end
def initialize(@res : Resources::Type, @cap : Capacity) getter type
@cur = @cap getter cap
end getter cur
def initialize(@res : Resources::Type, @cap : Capacity, @cur : Capacity) def inc(resource : Resource)
end check_res resource.type
@cur = Math.min(@cap, @cur + resource.amount)
end
getter res def dec(resource : Resource)
getter cap check_res resource.type
getter cur @cur = Math.max(0, @cur - resource.amount)
end
def inc(span : Span) private def check_res(other_res_type : Resource::Type)
check_res span.res if @type != other_res_type
@cur = Math.min(@cap, @cur + span.cap) raise ResourceMismatch.new
end
def dec(span : Span)
check_res span.res
@cur = Math.max(0, @cur - span.cap)
end
private def check_res(other_res : Resources::Type)
if @res != other_res
raise ResourceMismatch.new
end
end end
end end
end end

View File

@ -1,19 +1,27 @@
require "./exception" require "./exception"
class Game::Resources struct Game::Resource
enum Type enum Type
Crystals Crystals
Terraformation Terraformation
end end
alias ResourceBag = Hash(Type, Capacity) def initialize(@type : Type, @amount : Capacity)
end
@values : ResourceBag getter type
getter amount
end
def initialize(vals : ResourceBag | Nil = nil) class Game::ResourceBag
@values = {} of Type => Capacity alias ResourceHash = Hash(Resource::Type, Capacity)
Type.each { |t, v| @values[t] = 0 }
if vals.is_a?(ResourceBag) @values : ResourceHash
def initialize(vals : ResourceHash | Nil = nil)
@values = ResourceHash.new
Resource::Type.each { |t, v| @values[t] = 0 }
if vals.is_a?(ResourceHash)
vals.each do |i| vals.each do |i|
t, v = i t, v = i
@values[t] = v @values[t] = v
@ -21,15 +29,15 @@ class Game::Resources
end end
end end
def [](t : Type) def [](t : Resource::Type)
@values.fetch(t, 0) @values.fetch(t, 0)
end end
def has(t : Type, value : Capacity) : Bool def has(t : Resource::Type, value : Capacity) : Bool
@values[t] >= value @values[t] >= value
end end
def has(vs : ResourceBag) : Bool def has(vs : ResourceHash) : Bool
vs.reduce true do |acc, entry| vs.reduce true do |acc, entry|
t = entry[0] t = entry[0]
v = entry[1] v = entry[1]
@ -41,7 +49,7 @@ class Game::Resources
has vs.to_hash has vs.to_hash
end end
def inc(t : Type, value : Capacity) def inc(t : Resource::Type, value : Capacity)
new_value = @values.fetch(t, 0) + value new_value = @values.fetch(t, 0) + value
if new_value < 0 if new_value < 0
raise NotEnoughtResources.new raise NotEnoughtResources.new
@ -57,11 +65,11 @@ class Game::Resources
true true
end end
def dec(t : Type, value : Capacity) def dec(t : Resource::Type, value : Capacity)
inc(t, -value) inc(t, -value)
end end
def to_hash : ResourceBag def to_hash : ResourceHash
@values.clone @values.clone
end end
end end

View File

@ -10,7 +10,9 @@ module Game
getter cap getter cap
getter cur getter cur
abstract def has_role(role : TileRole) : Bool def has_role(role : TileRole) : Bool
false
end
def letter : Char def letter : Char
' ' ' '
@ -124,14 +126,9 @@ module Game
end end
class DepositTile < Tile class DepositTile < Tile
def initialize(@point : Point, @res : Resources::Type, @cap : Capacity) def initialize(@point : Point, @dep : Deposit)
end end
def has_role(role : TileRole) : Bool getter dep
role == TileRole::Deposit
end
getter res
getter cap
end end
end end

View File

@ -5,7 +5,7 @@ class Game::World
def initialize(@map : Map, @ts : TimePoint = 0_i64) def initialize(@map : Map, @ts : TimePoint = 0_i64)
@start_ts = @ts @start_ts = @ts
@resources = Resources.new @resources = ResourceBag.new
@queue = Queue.new @queue = Queue.new
@finished = false @finished = false
@score = 0 @score = 0
@ -41,6 +41,6 @@ class Game::World
end end
def win? def win?
@resources[Resources::Type::Terraformation] >= 100 @resources[Resource::Type::Terraformation] >= 100
end end
end end