From bbd16a92b17538b8f00a40a56bba9ace0a2f473c Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Fri, 13 Sep 2019 21:14:46 +0300 Subject: [PATCH] Improve command queue --- src/queue.cr | 13 ++++++------- src/village.cr | 52 +++++++++++++++++++++----------------------------- 2 files changed, 28 insertions(+), 37 deletions(-) diff --git a/src/queue.cr b/src/queue.cr index 9d531aa..6f8e6f0 100644 --- a/src/queue.cr +++ b/src/queue.cr @@ -1,23 +1,22 @@ class App::CommandQueue def initialize - @data = [] of {Int32, Command} + @data = [] of NamedTuple(ts: Int32, cmd: Command) end def push(ts : Int32, cmd : Command) - @data.push({ts, cmd}) + @data.push({ts: ts, cmd: cmd}) @data.sort! do |x| - -x[0] + -x.[:ts] end end - def pop(ts : Int32) : Command | Nil + def pop(ts : Int32) if @data.size == 0 return nil end - last_ts = @data[-1][0] + last_ts = @data[-1][:ts] if last_ts <= ts - last_item = @data.pop - return last_item[1] + return @data.pop else nil end diff --git a/src/village.cr b/src/village.cr index 3722264..9b34038 100644 --- a/src/village.cr +++ b/src/village.cr @@ -15,16 +15,8 @@ class Resources end abstract class Command - def initialize(ts : Int32) - @ts = ts - end - - def ts - @ts - end - abstract def supports?(world : World) : Bool - abstract def run(world : World) + abstract def run(ts : Int32, world : World) end class BuildMillCommand < Command @@ -32,10 +24,10 @@ class BuildMillCommand < Command return true end - def run(world : World) + def run(ts : Int32, world : World) puts "build mill" - c = GetWoodCommand.new(@ts + 5) - world.push(c) + c = GetWoodCommand.new + world.push(ts + 5, c) end end @@ -44,10 +36,10 @@ class BuildForesterHouseCommand < Command return true end - def run(world : World) + def run(ts : Int32, world : World) puts "build forester house" - c = GrowWoodCommand.new(@ts + 10) - world.push(c) + c = GrowWoodCommand.new + world.push(ts + 10, c) end end @@ -56,12 +48,12 @@ class GetWoodCommand < Command return true end - def run(world : World) + def run(ts : Int32, world : World) res = world.resources res.add_wood(10) puts "get wood" - c = GetWoodCommand.new(@ts + 5) - world.push(c) + c = GetWoodCommand.new + world.push(ts + 5, c) end end @@ -70,11 +62,11 @@ class GrowWoodCommand < Command return true end - def run(world : World) + def run(ts : Int32, world : World) res = world.resources puts "grow wood" - c = GetWoodCommand.new(@ts + 5) - world.push(c) + c = GetWoodCommand.new + world.push(ts + 5, c) end end @@ -159,23 +151,23 @@ class World @map end - def push(command : Command) : Bool + def push(ts : Int32, command : Command) : Bool if !command.supports?(self) return false end - printf "push command %d\n", command.ts - @queue.push(command.ts, command) + printf "push command %d\n", ts + @queue.push(ts, command) true end def run(ts : Int32) loop do - cmd = @queue.pop(ts) - if cmd.nil? + item = @queue.pop(ts) + if item.nil? break end - printf "pop command %d\n", cmd.ts - cmd.run(self) + printf "pop command %d\n", item[:ts] + item[:cmd].run(item[:ts], self) end printf "Wood: %d\n", @resources.wood end @@ -183,6 +175,6 @@ end w = World.new w.map.print -w.push(BuildMillCommand.new(0)) -w.push(BuildForesterHouseCommand.new(0)) +w.push(0, BuildMillCommand.new) +w.push(1, BuildForesterHouseCommand.new) w.run(20)