From 9cb02de22aadf11924e4a5cd54a528eec9789541 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Fri, 27 Sep 2019 16:48:36 +0300 Subject: [PATCH] First attempt to command line interface --- src/expansion.cr | 110 +++++++++++++++++++++-------------------------- src/resources.cr | 21 +++++++++ src/world.cr | 34 +++++++++++++++ 3 files changed, 104 insertions(+), 61 deletions(-) create mode 100644 src/resources.cr create mode 100644 src/world.cr diff --git a/src/expansion.cr b/src/expansion.cr index 7aa0c1b..183866b 100644 --- a/src/expansion.cr +++ b/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 - end - end - - def [](t : ResourceType) - @values[t] - end - - def inc(t : ResourceType, value : Int32) - @values[t] = @values[t] + value +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\d+)$/.match(cmd) + ts = md["ts"].to_i32 + UserWorld.run(ts) + printf "Run to %d\n", ts + when md = /^harv (?P\d+)\s+(?P\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\d+)\s+(?P\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\d+)\s+(?P\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 -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? - break - end - command = item.command - @ts = item.ts - command.finish(self) - printf "world : %d : finish `%s`\n", @ts, typeof(command) - end +loop do + printf "In > " + cmd = read_line() + norm = normalize_command(cmd) + if norm == "exit" + break 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 diff --git a/src/resources.cr b/src/resources.cr new file mode 100644 index 0000000..e9cdf14 --- /dev/null +++ b/src/resources.cr @@ -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 diff --git a/src/world.cr b/src/world.cr new file mode 100644 index 0000000..adc83c1 --- /dev/null +++ b/src/world.cr @@ -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