Add queue command descriptions

This commit is contained in:
Anton Vakhrushev 2019-10-06 19:35:22 +03:00
parent 9dad61f7a2
commit 3264ce3ec2
4 changed files with 60 additions and 24 deletions

View File

@ -10,6 +10,10 @@ macro define_dummy_classes(count)
def finish(world)
end
def desc : String
""
end
end
{% end %}
end

View File

@ -15,19 +15,19 @@ class CLI::CommandRouter
@routes = [] of Route
end
def add(route, &block : RouteHandler)
pattern = route_to_regex(route)
@routes.push(Route.new(route, pattern, "", block))
def add(route : String, &block : RouteHandler)
add(route, "", &block)
end
def add(route, desc, &block : RouteHandler)
def add(route : String, desc : String, &block : RouteHandler)
pattern = route_to_regex(route)
@routes.push(Route.new(route, pattern, desc, block))
end
def handle(command)
def handle(command : String)
@routes.each do |route|
handle_pattern(command, route.pattern, route.handler)
result = handle_pattern(command, route.pattern, route.handler)
break if result
end
end
@ -35,14 +35,15 @@ class CLI::CommandRouter
@routes
end
private def handle_pattern(command, pattern, cb)
private def handle_pattern(command, pattern, cb) : Bool
m = command.match(pattern)
return if m.nil?
return false if m.nil?
groups = m.named_captures
nil_groups = groups.select { |k, v| v.nil? }
return if nil_groups.size != 0
return false if nil_groups.size != 0
params = groups.transform_values { |v| v.to_s }
cb.call params
true
end
private def route_to_regex(route) : Regex

View File

@ -6,20 +6,6 @@ world = Game::World.new(ts)
router = CLI::CommandRouter.new
router.add "h", "Show all commands" do |p|
printf "Commands:\n"
router.routes.each do |r|
printf " %s - %s\n", r.route, r.desc
end
end
router.add "q", "Show command queue" do |p|
items = world.queue.top(5)
items.each do |i|
printf "%s, %s\n", Time.unix(i.ts).to_local.to_s, typeof(i.command)
end
end
router.add "harv {x} {y}", "Build harvester at x,y" do |p|
x = p["x"].to_i32
y = p["y"].to_i32
@ -42,6 +28,13 @@ router.add "terr {x} {y}", "Build terraformator at x,y" do |p|
world.push(Game::BuildTerraformerCommand.new(point))
end
router.add "help", "Show all commands" do |p|
printf "Commands:\n"
router.routes.each do |r|
printf " %s - %s\n", r.route, r.desc
end
end
def render_map(world)
size = world.map.size
(0...size).each do |x|
@ -70,6 +63,17 @@ def render_map(world)
end
end
def render_commands(world)
items = world.queue.top(5)
if items.size != 0
printf "Queue:\n"
end
time = ->(ts : Int64) { Time.unix(ts).to_local.to_s }
items.each do |i|
printf " %s, %s\n", time.call(i.ts), i.command.desc
end
end
def render_resources(world)
printf "Resources:\n Crystals: %5d\n Terraformation: %5d\n",
world.resources[Game::ResourceType::Crystal],
@ -77,10 +81,12 @@ def render_resources(world)
end
def render_world(world)
printf "Now: %s\n\n", Time.unix(world.ts).to_local.to_s
printf "Now:\n %s\n\n", Time.unix(world.ts).to_local.to_s
if world.win?
printf "YOU WIN!!!\n\n"
end
render_commands world
printf "\n"
render_resources world
printf "\n"
render_map world

View File

@ -4,6 +4,7 @@ module Game
abstract class Command
abstract def start(world : World) : Int32
abstract def finish(world : World)
abstract def desc : String
end
class BuildCrystalHarvesterCommand < Command
@ -21,6 +22,10 @@ module Game
world.map.set(CrystalHarvesterTile.new(@point))
world.push(HarvestCrystalCommand.new(@point))
end
def desc : String
sprintf "Build harvester site at %d,%d", @point.x, @point.y
end
end
class HarvestCrystalCommand < Command
@ -50,6 +55,10 @@ module Game
world.push(HarvestCrystalCommand.new(@point))
end
def desc : String
sprintf "Harvest crystals at %d,%d", @point.x, @point.y
end
private def nearest_deposit(world : World)
world.map.nearest_tile @point do |tile|
tile.has_role(TileRole::CrystalDeposits) && tile.cur > 0
@ -80,6 +89,10 @@ module Game
world.map.set(CrystalRestorerTile.new(@point))
world.push(RestoreCrystalCommand.new(@point))
end
def desc : String
sprintf "Build crystal restorer at %d,%d", @point.x, @point.y
end
end
class RestoreCrystalCommand < Command
@ -109,6 +122,10 @@ module Game
world.push(RestoreCrystalCommand.new(@point))
end
def desc : String
sprintf "Restore crystals at %d,%d", @point.x, @point.y
end
private def nearest_deposit(world : World)
world.map.nearest_tile @point do |tile|
tile.has_role(TileRole::CrystalDeposits) && tile.cur < tile.cap
@ -133,6 +150,10 @@ module Game
world.map.set(TerraformerTile.new(@point))
world.push(TerraformCommand.new(@point))
end
def desc : String
sprintf "Build terraformer at %d,%d", @point.x, @point.y
end
end
class TerraformCommand < Command
@ -146,6 +167,10 @@ module Game
PRODUCTION_TIME
end
def desc : String
"Terraform planet"
end
def finish(world : World)
world.resources.inc(ResourceType::Terraformation, PRODUCTION_VALUE)
world.push(TerraformCommand.new(@point))