Add queue info

This commit is contained in:
Anton Vakhrushev 2019-10-06 13:25:52 +03:00
parent f8717dc2bc
commit d200d59fff
3 changed files with 18 additions and 7 deletions

View File

@ -11,8 +11,7 @@ world = Game::World.new(ts)
router = CLI::CommandRouter.new router = CLI::CommandRouter.new
router.add "st" do router.add "st" do
printf "Stat:\n\tTime: %s\n\tCrystals: %d\n\tTarraform: %d\n", printf "Stat:\n Crystals: %d\n Tarraform: %d\n",
Time.unix(world.ts).to_local.to_s,
world.resources[Game::ResourceType::Crystal], world.resources[Game::ResourceType::Crystal],
world.resources[Game::ResourceType::Terraformation] world.resources[Game::ResourceType::Terraformation]
end end
@ -45,6 +44,13 @@ router.add "m" do
end end
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| router.add "harv {x} {y}" do |p|
x = p["x"].to_i32 x = p["x"].to_i32
y = p["y"].to_i32 y = p["y"].to_i32

View File

@ -1,6 +1,6 @@
class Game::Queue class Game::Queue
struct Item struct Item
def initialize(@ts : Int64, @command : Game::Command) def initialize(@ts : Int64, @command : Command)
end end
getter ts getter ts
@ -11,7 +11,7 @@ class Game::Queue
@data = [] of Item @data = [] of Item
end end
def push(ts : Int64, value : Game::Command) def push(ts : Int64, value : Command)
# very unoptimal algo # very unoptimal algo
@data.push(Item.new(ts, value)) @data.push(Item.new(ts, value))
@data.sort! do |a, b| @data.sort! do |a, b|
@ -25,4 +25,8 @@ class Game::Queue
end end
@data[-1].ts <= ts ? @data.pop : nil @data[-1].ts <= ts ? @data.pop : nil
end end
def top(n : Int32)
@data.last(n).reverse!
end
end end

View File

@ -6,22 +6,23 @@ class Game::World
def initialize(@ts = 0_i64) def initialize(@ts = 0_i64)
@map = Map.new @map = Map.new
@resources = Resources.new @resources = Resources.new
@tasks = Queue.new @queue = Queue.new
end end
getter ts getter ts
getter resources getter resources
getter map getter map
getter queue
def push(command : Command) def push(command : Command)
dur = command.start(self) dur = command.start(self)
done_at = @ts + dur.to_i64 done_at = @ts + dur.to_i64
@tasks.push(done_at, command) @queue.push(done_at, command)
end end
def run(ts : Int64) def run(ts : Int64)
loop do loop do
item = @tasks.pop(ts) item = @queue.pop(ts)
if item.nil? if item.nil?
break break
end end