Improve command queue
This commit is contained in:
parent
5acf1694ee
commit
bbd16a92b1
13
src/queue.cr
13
src/queue.cr
@ -1,23 +1,22 @@
|
|||||||
class App::CommandQueue
|
class App::CommandQueue
|
||||||
def initialize
|
def initialize
|
||||||
@data = [] of {Int32, Command}
|
@data = [] of NamedTuple(ts: Int32, cmd: Command)
|
||||||
end
|
end
|
||||||
|
|
||||||
def push(ts : Int32, cmd : Command)
|
def push(ts : Int32, cmd : Command)
|
||||||
@data.push({ts, cmd})
|
@data.push({ts: ts, cmd: cmd})
|
||||||
@data.sort! do |x|
|
@data.sort! do |x|
|
||||||
-x[0]
|
-x.[:ts]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def pop(ts : Int32) : Command | Nil
|
def pop(ts : Int32)
|
||||||
if @data.size == 0
|
if @data.size == 0
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
last_ts = @data[-1][0]
|
last_ts = @data[-1][:ts]
|
||||||
if last_ts <= ts
|
if last_ts <= ts
|
||||||
last_item = @data.pop
|
return @data.pop
|
||||||
return last_item[1]
|
|
||||||
else
|
else
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
@ -15,16 +15,8 @@ class Resources
|
|||||||
end
|
end
|
||||||
|
|
||||||
abstract class Command
|
abstract class Command
|
||||||
def initialize(ts : Int32)
|
|
||||||
@ts = ts
|
|
||||||
end
|
|
||||||
|
|
||||||
def ts
|
|
||||||
@ts
|
|
||||||
end
|
|
||||||
|
|
||||||
abstract def supports?(world : World) : Bool
|
abstract def supports?(world : World) : Bool
|
||||||
abstract def run(world : World)
|
abstract def run(ts : Int32, world : World)
|
||||||
end
|
end
|
||||||
|
|
||||||
class BuildMillCommand < Command
|
class BuildMillCommand < Command
|
||||||
@ -32,10 +24,10 @@ class BuildMillCommand < Command
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(world : World)
|
def run(ts : Int32, world : World)
|
||||||
puts "build mill"
|
puts "build mill"
|
||||||
c = GetWoodCommand.new(@ts + 5)
|
c = GetWoodCommand.new
|
||||||
world.push(c)
|
world.push(ts + 5, c)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -44,10 +36,10 @@ class BuildForesterHouseCommand < Command
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(world : World)
|
def run(ts : Int32, world : World)
|
||||||
puts "build forester house"
|
puts "build forester house"
|
||||||
c = GrowWoodCommand.new(@ts + 10)
|
c = GrowWoodCommand.new
|
||||||
world.push(c)
|
world.push(ts + 10, c)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -56,12 +48,12 @@ class GetWoodCommand < Command
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(world : World)
|
def run(ts : Int32, world : World)
|
||||||
res = world.resources
|
res = world.resources
|
||||||
res.add_wood(10)
|
res.add_wood(10)
|
||||||
puts "get wood"
|
puts "get wood"
|
||||||
c = GetWoodCommand.new(@ts + 5)
|
c = GetWoodCommand.new
|
||||||
world.push(c)
|
world.push(ts + 5, c)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -70,11 +62,11 @@ class GrowWoodCommand < Command
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(world : World)
|
def run(ts : Int32, world : World)
|
||||||
res = world.resources
|
res = world.resources
|
||||||
puts "grow wood"
|
puts "grow wood"
|
||||||
c = GetWoodCommand.new(@ts + 5)
|
c = GetWoodCommand.new
|
||||||
world.push(c)
|
world.push(ts + 5, c)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -159,23 +151,23 @@ class World
|
|||||||
@map
|
@map
|
||||||
end
|
end
|
||||||
|
|
||||||
def push(command : Command) : Bool
|
def push(ts : Int32, command : Command) : Bool
|
||||||
if !command.supports?(self)
|
if !command.supports?(self)
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
printf "push command %d\n", command.ts
|
printf "push command %d\n", ts
|
||||||
@queue.push(command.ts, command)
|
@queue.push(ts, command)
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(ts : Int32)
|
def run(ts : Int32)
|
||||||
loop do
|
loop do
|
||||||
cmd = @queue.pop(ts)
|
item = @queue.pop(ts)
|
||||||
if cmd.nil?
|
if item.nil?
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
printf "pop command %d\n", cmd.ts
|
printf "pop command %d\n", item[:ts]
|
||||||
cmd.run(self)
|
item[:cmd].run(item[:ts], self)
|
||||||
end
|
end
|
||||||
printf "Wood: %d\n", @resources.wood
|
printf "Wood: %d\n", @resources.wood
|
||||||
end
|
end
|
||||||
@ -183,6 +175,6 @@ end
|
|||||||
|
|
||||||
w = World.new
|
w = World.new
|
||||||
w.map.print
|
w.map.print
|
||||||
w.push(BuildMillCommand.new(0))
|
w.push(0, BuildMillCommand.new)
|
||||||
w.push(BuildForesterHouseCommand.new(0))
|
w.push(1, BuildForesterHouseCommand.new)
|
||||||
w.run(20)
|
w.run(20)
|
||||||
|
Loading…
Reference in New Issue
Block a user