First attempt to command line interface
This commit is contained in:
parent
349dc124e4
commit
9cb02de22a
110
src/expansion.cr
110
src/expansion.cr
@ -2,73 +2,61 @@ require "colorize"
|
|||||||
require "./command"
|
require "./command"
|
||||||
require "./map"
|
require "./map"
|
||||||
require "./queue"
|
require "./queue"
|
||||||
|
require "./resources"
|
||||||
|
require "./world"
|
||||||
|
|
||||||
enum ResourceType
|
UserWorld = World.new
|
||||||
Crystal
|
|
||||||
Terraformation
|
def normalize_command(cmd)
|
||||||
|
cmd.downcase.gsub(/\s+/, ' ').strip
|
||||||
end
|
end
|
||||||
|
|
||||||
class Resources
|
def run_command(cmd)
|
||||||
def initialize
|
case
|
||||||
@values = {} of ResourceType => Int32
|
when md = /^st/.match(cmd)
|
||||||
ResourceType.each do |t|
|
printf "Stat:\n\tTime: %d\n\tCrystals: %d\n\tTarraform: %d\n",
|
||||||
@values[t] = 0
|
UserWorld.ts,
|
||||||
end
|
UserWorld.resources[ResourceType::Crystal],
|
||||||
end
|
UserWorld.resources[ResourceType::Terraformation]
|
||||||
|
when md = /^m/.match(cmd)
|
||||||
def [](t : ResourceType)
|
UserWorld.map.print
|
||||||
@values[t]
|
when md = /^run (?P<ts>\d+)$/.match(cmd)
|
||||||
end
|
ts = md["ts"].to_i32
|
||||||
|
UserWorld.run(ts)
|
||||||
def inc(t : ResourceType, value : Int32)
|
printf "Run to %d\n", ts
|
||||||
@values[t] = @values[t] + value
|
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
|
end
|
||||||
|
printf "\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
class World
|
loop do
|
||||||
property ts : Int32
|
printf "In > "
|
||||||
|
cmd = read_line()
|
||||||
def initialize
|
norm = normalize_command(cmd)
|
||||||
@ts = 0
|
if norm == "exit"
|
||||||
@map = Map.new
|
break
|
||||||
@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?
|
|
||||||
break
|
|
||||||
end
|
|
||||||
command = item.command
|
|
||||||
@ts = item.ts
|
|
||||||
command.finish(self)
|
|
||||||
printf "world : %d : finish `%s`\n", @ts, typeof(command)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
run_command(norm)
|
||||||
end
|
end
|
||||||
|
|
||||||
w = World.new
|
# w.map.print
|
||||||
w.map.print
|
# w.push(BuildCrystalHarvesterCommand.new(Point.new(2, 3)))
|
||||||
w.push(BuildCrystalHarvesterCommand.new(Point.new(2, 3)))
|
# w.push(BuildCrystalRestorerCommand.new(Point.new(1, 2)))
|
||||||
w.push(BuildCrystalRestorerCommand.new(Point.new(1, 2)))
|
# w.push(BuildTerraformerCommand.new(Point.new(3, 2)))
|
||||||
w.push(BuildTerraformerCommand.new(Point.new(3, 2)))
|
# w.run(2000)
|
||||||
w.run(2000)
|
# w.map.print
|
||||||
w.map.print
|
# pp w.resources
|
||||||
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