Add queue command descriptions
This commit is contained in:
parent
9dad61f7a2
commit
3264ce3ec2
@ -10,6 +10,10 @@ macro define_dummy_classes(count)
|
|||||||
|
|
||||||
def finish(world)
|
def finish(world)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def desc : String
|
||||||
|
""
|
||||||
|
end
|
||||||
end
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
end
|
end
|
||||||
|
@ -15,19 +15,19 @@ class CLI::CommandRouter
|
|||||||
@routes = [] of Route
|
@routes = [] of Route
|
||||||
end
|
end
|
||||||
|
|
||||||
def add(route, &block : RouteHandler)
|
def add(route : String, &block : RouteHandler)
|
||||||
pattern = route_to_regex(route)
|
add(route, "", &block)
|
||||||
@routes.push(Route.new(route, pattern, "", block))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def add(route, desc, &block : RouteHandler)
|
def add(route : String, desc : String, &block : RouteHandler)
|
||||||
pattern = route_to_regex(route)
|
pattern = route_to_regex(route)
|
||||||
@routes.push(Route.new(route, pattern, desc, block))
|
@routes.push(Route.new(route, pattern, desc, block))
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle(command)
|
def handle(command : String)
|
||||||
@routes.each do |route|
|
@routes.each do |route|
|
||||||
handle_pattern(command, route.pattern, route.handler)
|
result = handle_pattern(command, route.pattern, route.handler)
|
||||||
|
break if result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -35,14 +35,15 @@ class CLI::CommandRouter
|
|||||||
@routes
|
@routes
|
||||||
end
|
end
|
||||||
|
|
||||||
private def handle_pattern(command, pattern, cb)
|
private def handle_pattern(command, pattern, cb) : Bool
|
||||||
m = command.match(pattern)
|
m = command.match(pattern)
|
||||||
return if m.nil?
|
return false if m.nil?
|
||||||
groups = m.named_captures
|
groups = m.named_captures
|
||||||
nil_groups = groups.select { |k, v| v.nil? }
|
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 }
|
params = groups.transform_values { |v| v.to_s }
|
||||||
cb.call params
|
cb.call params
|
||||||
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
private def route_to_regex(route) : Regex
|
private def route_to_regex(route) : Regex
|
||||||
|
@ -6,20 +6,6 @@ world = Game::World.new(ts)
|
|||||||
|
|
||||||
router = CLI::CommandRouter.new
|
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|
|
router.add "harv {x} {y}", "Build harvester at x,y" do |p|
|
||||||
x = p["x"].to_i32
|
x = p["x"].to_i32
|
||||||
y = p["y"].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))
|
world.push(Game::BuildTerraformerCommand.new(point))
|
||||||
end
|
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)
|
def render_map(world)
|
||||||
size = world.map.size
|
size = world.map.size
|
||||||
(0...size).each do |x|
|
(0...size).each do |x|
|
||||||
@ -70,6 +63,17 @@ def render_map(world)
|
|||||||
end
|
end
|
||||||
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)
|
def render_resources(world)
|
||||||
printf "Resources:\n Crystals: %5d\n Terraformation: %5d\n",
|
printf "Resources:\n Crystals: %5d\n Terraformation: %5d\n",
|
||||||
world.resources[Game::ResourceType::Crystal],
|
world.resources[Game::ResourceType::Crystal],
|
||||||
@ -77,10 +81,12 @@ def render_resources(world)
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_world(world)
|
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?
|
if world.win?
|
||||||
printf "YOU WIN!!!\n\n"
|
printf "YOU WIN!!!\n\n"
|
||||||
end
|
end
|
||||||
|
render_commands world
|
||||||
|
printf "\n"
|
||||||
render_resources world
|
render_resources world
|
||||||
printf "\n"
|
printf "\n"
|
||||||
render_map world
|
render_map world
|
||||||
|
@ -4,6 +4,7 @@ module Game
|
|||||||
abstract class Command
|
abstract class Command
|
||||||
abstract def start(world : World) : Int32
|
abstract def start(world : World) : Int32
|
||||||
abstract def finish(world : World)
|
abstract def finish(world : World)
|
||||||
|
abstract def desc : String
|
||||||
end
|
end
|
||||||
|
|
||||||
class BuildCrystalHarvesterCommand < Command
|
class BuildCrystalHarvesterCommand < Command
|
||||||
@ -21,6 +22,10 @@ module Game
|
|||||||
world.map.set(CrystalHarvesterTile.new(@point))
|
world.map.set(CrystalHarvesterTile.new(@point))
|
||||||
world.push(HarvestCrystalCommand.new(@point))
|
world.push(HarvestCrystalCommand.new(@point))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def desc : String
|
||||||
|
sprintf "Build harvester site at %d,%d", @point.x, @point.y
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class HarvestCrystalCommand < Command
|
class HarvestCrystalCommand < Command
|
||||||
@ -50,6 +55,10 @@ module Game
|
|||||||
world.push(HarvestCrystalCommand.new(@point))
|
world.push(HarvestCrystalCommand.new(@point))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def desc : String
|
||||||
|
sprintf "Harvest crystals at %d,%d", @point.x, @point.y
|
||||||
|
end
|
||||||
|
|
||||||
private def nearest_deposit(world : World)
|
private def nearest_deposit(world : World)
|
||||||
world.map.nearest_tile @point do |tile|
|
world.map.nearest_tile @point do |tile|
|
||||||
tile.has_role(TileRole::CrystalDeposits) && tile.cur > 0
|
tile.has_role(TileRole::CrystalDeposits) && tile.cur > 0
|
||||||
@ -80,6 +89,10 @@ module Game
|
|||||||
world.map.set(CrystalRestorerTile.new(@point))
|
world.map.set(CrystalRestorerTile.new(@point))
|
||||||
world.push(RestoreCrystalCommand.new(@point))
|
world.push(RestoreCrystalCommand.new(@point))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def desc : String
|
||||||
|
sprintf "Build crystal restorer at %d,%d", @point.x, @point.y
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class RestoreCrystalCommand < Command
|
class RestoreCrystalCommand < Command
|
||||||
@ -109,6 +122,10 @@ module Game
|
|||||||
world.push(RestoreCrystalCommand.new(@point))
|
world.push(RestoreCrystalCommand.new(@point))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def desc : String
|
||||||
|
sprintf "Restore crystals at %d,%d", @point.x, @point.y
|
||||||
|
end
|
||||||
|
|
||||||
private def nearest_deposit(world : World)
|
private def nearest_deposit(world : World)
|
||||||
world.map.nearest_tile @point do |tile|
|
world.map.nearest_tile @point do |tile|
|
||||||
tile.has_role(TileRole::CrystalDeposits) && tile.cur < tile.cap
|
tile.has_role(TileRole::CrystalDeposits) && tile.cur < tile.cap
|
||||||
@ -133,6 +150,10 @@ module Game
|
|||||||
world.map.set(TerraformerTile.new(@point))
|
world.map.set(TerraformerTile.new(@point))
|
||||||
world.push(TerraformCommand.new(@point))
|
world.push(TerraformCommand.new(@point))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def desc : String
|
||||||
|
sprintf "Build terraformer at %d,%d", @point.x, @point.y
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class TerraformCommand < Command
|
class TerraformCommand < Command
|
||||||
@ -146,6 +167,10 @@ module Game
|
|||||||
PRODUCTION_TIME
|
PRODUCTION_TIME
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def desc : String
|
||||||
|
"Terraform planet"
|
||||||
|
end
|
||||||
|
|
||||||
def finish(world : World)
|
def finish(world : World)
|
||||||
world.resources.inc(ResourceType::Terraformation, PRODUCTION_VALUE)
|
world.resources.inc(ResourceType::Terraformation, PRODUCTION_VALUE)
|
||||||
world.push(TerraformCommand.new(@point))
|
world.push(TerraformCommand.new(@point))
|
||||||
|
Loading…
Reference in New Issue
Block a user