diff --git a/src/expansion.cr b/src/expansion.cr index 7ee8a68..240c43a 100644 --- a/src/expansion.cr +++ b/src/expansion.cr @@ -11,8 +11,7 @@ world = Game::World.new(ts) router = CLI::CommandRouter.new router.add "st" do - printf "Stat:\n\tTime: %s\n\tCrystals: %d\n\tTarraform: %d\n", - Time.unix(world.ts).to_local.to_s, + printf "Stat:\n Crystals: %d\n Tarraform: %d\n", world.resources[Game::ResourceType::Crystal], world.resources[Game::ResourceType::Terraformation] end @@ -45,6 +44,13 @@ router.add "m" do end end +router.add "q" 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}" do |p| x = p["x"].to_i32 y = p["y"].to_i32 diff --git a/src/game/queue.cr b/src/game/queue.cr index 6227f38..9597490 100644 --- a/src/game/queue.cr +++ b/src/game/queue.cr @@ -1,6 +1,6 @@ class Game::Queue struct Item - def initialize(@ts : Int64, @command : Game::Command) + def initialize(@ts : Int64, @command : Command) end getter ts @@ -11,7 +11,7 @@ class Game::Queue @data = [] of Item end - def push(ts : Int64, value : Game::Command) + def push(ts : Int64, value : Command) # very unoptimal algo @data.push(Item.new(ts, value)) @data.sort! do |a, b| @@ -25,4 +25,8 @@ class Game::Queue end @data[-1].ts <= ts ? @data.pop : nil end + + def top(n : Int32) + @data.last(n).reverse! + end end diff --git a/src/game/world.cr b/src/game/world.cr index 2fae7c6..1ac95d4 100644 --- a/src/game/world.cr +++ b/src/game/world.cr @@ -6,22 +6,23 @@ class Game::World def initialize(@ts = 0_i64) @map = Map.new @resources = Resources.new - @tasks = Queue.new + @queue = Queue.new end getter ts getter resources getter map + getter queue def push(command : Command) dur = command.start(self) done_at = @ts + dur.to_i64 - @tasks.push(done_at, command) + @queue.push(done_at, command) end def run(ts : Int64) loop do - item = @tasks.pop(ts) + item = @queue.pop(ts) if item.nil? break end