Add queue info
This commit is contained in:
parent
f8717dc2bc
commit
d200d59fff
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user