First attempt to command line interface
This commit is contained in:
parent
349dc124e4
commit
9cb02de22a
106
src/expansion.cr
106
src/expansion.cr
@ -2,73 +2,61 @@ require "colorize"
|
||||
require "./command"
|
||||
require "./map"
|
||||
require "./queue"
|
||||
require "./resources"
|
||||
require "./world"
|
||||
|
||||
enum ResourceType
|
||||
Crystal
|
||||
Terraformation
|
||||
UserWorld = World.new
|
||||
|
||||
def normalize_command(cmd)
|
||||
cmd.downcase.gsub(/\s+/, ' ').strip
|
||||
end
|
||||
|
||||
class Resources
|
||||
def initialize
|
||||
@values = {} of ResourceType => Int32
|
||||
ResourceType.each do |t|
|
||||
@values[t] = 0
|
||||
def run_command(cmd)
|
||||
case
|
||||
when md = /^st/.match(cmd)
|
||||
printf "Stat:\n\tTime: %d\n\tCrystals: %d\n\tTarraform: %d\n",
|
||||
UserWorld.ts,
|
||||
UserWorld.resources[ResourceType::Crystal],
|
||||
UserWorld.resources[ResourceType::Terraformation]
|
||||
when md = /^m/.match(cmd)
|
||||
UserWorld.map.print
|
||||
when md = /^run (?P<ts>\d+)$/.match(cmd)
|
||||
ts = md["ts"].to_i32
|
||||
UserWorld.run(ts)
|
||||
printf "Run to %d\n", ts
|
||||
when md = /^harv (?P<x>\d+)\s+(?P<y>\d+)$/.match(cmd)
|
||||
x = md["x"].to_i32
|
||||
y = md["y"].to_i32
|
||||
UserWorld.push(BuildCrystalHarvesterCommand.new(Point.new(x, y)))
|
||||
printf "Build harvester at %d %d\n", x, y
|
||||
when md = /^rest (?P<x>\d+)\s+(?P<y>\d+)$/.match(cmd)
|
||||
x = md["x"].to_i32
|
||||
y = md["y"].to_i32
|
||||
UserWorld.push(BuildCrystalRestorerCommand.new(Point.new(x, y)))
|
||||
when md = /^terr (?P<x>\d+)\s+(?P<y>\d+)$/.match(cmd)
|
||||
x = md["x"].to_i32
|
||||
y = md["y"].to_i32
|
||||
UserWorld.push(BuildTerraformerCommand.new(Point.new(x, y)))
|
||||
else
|
||||
printf "Out > %s\n", cmd
|
||||
end
|
||||
printf "\n"
|
||||
end
|
||||
|
||||
def [](t : ResourceType)
|
||||
@values[t]
|
||||
end
|
||||
|
||||
def inc(t : ResourceType, value : Int32)
|
||||
@values[t] = @values[t] + value
|
||||
end
|
||||
end
|
||||
|
||||
class World
|
||||
property ts : Int32
|
||||
|
||||
def initialize
|
||||
@ts = 0
|
||||
@map = Map.new
|
||||
@resources = Resources.new
|
||||
@tasks = App::Queue.new
|
||||
end
|
||||
|
||||
def resources
|
||||
@resources
|
||||
end
|
||||
|
||||
def map
|
||||
@map
|
||||
end
|
||||
|
||||
def push(command : Command)
|
||||
dur = command.start(self)
|
||||
done_at = @ts + dur
|
||||
printf "world : %d : plan `%s` at %d\n", @ts, typeof(command), done_at
|
||||
@tasks.push(done_at, command)
|
||||
end
|
||||
|
||||
def run(ts : Int32)
|
||||
loop do
|
||||
item = @tasks.pop(ts)
|
||||
if item.nil?
|
||||
printf "In > "
|
||||
cmd = read_line()
|
||||
norm = normalize_command(cmd)
|
||||
if norm == "exit"
|
||||
break
|
||||
end
|
||||
command = item.command
|
||||
@ts = item.ts
|
||||
command.finish(self)
|
||||
printf "world : %d : finish `%s`\n", @ts, typeof(command)
|
||||
end
|
||||
end
|
||||
run_command(norm)
|
||||
end
|
||||
|
||||
w = World.new
|
||||
w.map.print
|
||||
w.push(BuildCrystalHarvesterCommand.new(Point.new(2, 3)))
|
||||
w.push(BuildCrystalRestorerCommand.new(Point.new(1, 2)))
|
||||
w.push(BuildTerraformerCommand.new(Point.new(3, 2)))
|
||||
w.run(2000)
|
||||
w.map.print
|
||||
pp w.resources
|
||||
# w.map.print
|
||||
# w.push(BuildCrystalHarvesterCommand.new(Point.new(2, 3)))
|
||||
# w.push(BuildCrystalRestorerCommand.new(Point.new(1, 2)))
|
||||
# w.push(BuildTerraformerCommand.new(Point.new(3, 2)))
|
||||
# w.run(2000)
|
||||
# w.map.print
|
||||
# pp w.resources
|
||||
|
21
src/resources.cr
Normal file
21
src/resources.cr
Normal file
@ -0,0 +1,21 @@
|
||||
enum ResourceType
|
||||
Crystal
|
||||
Terraformation
|
||||
end
|
||||
|
||||
class Resources
|
||||
def initialize
|
||||
@values = {} of ResourceType => Int32
|
||||
ResourceType.each do |t|
|
||||
@values[t] = 0
|
||||
end
|
||||
end
|
||||
|
||||
def [](t : ResourceType)
|
||||
@values[t]
|
||||
end
|
||||
|
||||
def inc(t : ResourceType, value : Int32)
|
||||
@values[t] = @values[t] + value
|
||||
end
|
||||
end
|
34
src/world.cr
Normal file
34
src/world.cr
Normal file
@ -0,0 +1,34 @@
|
||||
class World
|
||||
property ts : Int32
|
||||
|
||||
def initialize
|
||||
@ts = 0
|
||||
@map = Map.new
|
||||
@resources = Resources.new
|
||||
@tasks = App::Queue.new
|
||||
end
|
||||
|
||||
getter ts
|
||||
getter resources
|
||||
getter map
|
||||
|
||||
def push(command : Command)
|
||||
dur = command.start(self)
|
||||
done_at = @ts + dur
|
||||
printf "world : %d : plan `%s` at %d\n", @ts, typeof(command), done_at
|
||||
@tasks.push(done_at, command)
|
||||
end
|
||||
|
||||
def run(ts : Int32)
|
||||
loop do
|
||||
item = @tasks.pop(ts)
|
||||
if item.nil?
|
||||
break
|
||||
end
|
||||
command = item.command
|
||||
@ts = item.ts
|
||||
command.finish(self)
|
||||
printf "world : %d : finish `%s`\n", @ts, typeof(command)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user