Move app to separate class
This commit is contained in:
parent
454ecc60f6
commit
fe7d096891
7
spec/app_spec.cr
Normal file
7
spec/app_spec.cr
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
require "./spec_helper"
|
||||||
|
|
||||||
|
describe App do
|
||||||
|
it "should compile" do
|
||||||
|
app = App.new
|
||||||
|
end
|
||||||
|
end
|
@ -1,6 +1,7 @@
|
|||||||
require "spec"
|
require "spec"
|
||||||
require "../src/game/**"
|
require "../src/game/**"
|
||||||
require "../src/cli/**"
|
require "../src/cli/**"
|
||||||
|
require "../src/app"
|
||||||
|
|
||||||
def create_map_2x2 : Game::Map
|
def create_map_2x2 : Game::Map
|
||||||
map = Game::Map.new 2, 2
|
map = Game::Map.new 2, 2
|
||||||
|
149
src/app.cr
Normal file
149
src/app.cr
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
require "colorize"
|
||||||
|
require "./game/**"
|
||||||
|
require "./cli/**"
|
||||||
|
|
||||||
|
class App
|
||||||
|
@ts : Game::TimePoint
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@map = Game::Generator.make 5, 5
|
||||||
|
@ts = Time.local.to_unix
|
||||||
|
@world = Game::World.new @map, @ts
|
||||||
|
@buildings = Game::BuildingFactory.new
|
||||||
|
@router = CLI::CommandRouter.new
|
||||||
|
|
||||||
|
@buildings.items.each do |i|
|
||||||
|
t = i[:t]
|
||||||
|
b = i[:b]
|
||||||
|
route = sprintf "%s {x} {y}", b.name.downcase
|
||||||
|
desc = sprintf "Build %s at x,y", b.name
|
||||||
|
@router.add route, desc do |p|
|
||||||
|
x = p["x"].to_i32
|
||||||
|
y = p["y"].to_i32
|
||||||
|
point = Game::Point.new(x, y)
|
||||||
|
@world.push(Game::BuildCommand.new(point, b))
|
||||||
|
printf "Build %s at %d %d\n", b.name, x, y
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@router.add "help", "Show all commands" do |p|
|
||||||
|
printf "Commands:\n"
|
||||||
|
@router.routes.each do |r|
|
||||||
|
printf " %s - %s\n", r.route, r.desc
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_time(ts)
|
||||||
|
t = Time.unix(ts).in(Time::Location.load("Europe/Moscow"))
|
||||||
|
# It's future, baby
|
||||||
|
t += 200.years
|
||||||
|
t.to_s("%Y-%m-%d %H:%M:%S")
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_map(world)
|
||||||
|
rows = world.map.rows
|
||||||
|
cols = world.map.cols
|
||||||
|
(0...rows).each do |x|
|
||||||
|
if x == 0
|
||||||
|
printf "+"
|
||||||
|
(0...cols).each do |y|
|
||||||
|
printf "------+"
|
||||||
|
end
|
||||||
|
print "\n"
|
||||||
|
end
|
||||||
|
printf "|"
|
||||||
|
(0...cols).each do |y|
|
||||||
|
tile = world.map.get(x, y)
|
||||||
|
printf "%s %d%d|", tile.letter.colorize(:green), x, y
|
||||||
|
end
|
||||||
|
print "\n"
|
||||||
|
printf "|"
|
||||||
|
(0...cols).each do |y|
|
||||||
|
printf " |"
|
||||||
|
end
|
||||||
|
print "\n"
|
||||||
|
printf "|"
|
||||||
|
(0...cols).each do |y|
|
||||||
|
tile = world.map.get(x, y)
|
||||||
|
if tile.letter == 'v'
|
||||||
|
printf "%6d|", world.map.get(x, y).cur
|
||||||
|
else
|
||||||
|
printf " |", world.map.get(x, y).cur
|
||||||
|
end
|
||||||
|
end
|
||||||
|
print "\n"
|
||||||
|
printf "+"
|
||||||
|
(0...cols).each do |y|
|
||||||
|
printf "------+"
|
||||||
|
end
|
||||||
|
print "\n"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_commands(world)
|
||||||
|
items = world.queue.top(5)
|
||||||
|
if items.size != 0
|
||||||
|
printf "Queue:\n"
|
||||||
|
end
|
||||||
|
wts = world.ts
|
||||||
|
items.each do |i|
|
||||||
|
ts_diff = i.ts - wts
|
||||||
|
if ts_diff < 60
|
||||||
|
done_time = sprintf " %02ds", ts_diff
|
||||||
|
else
|
||||||
|
done_time = sprintf "%d:%02d", ts_diff // 60, ts_diff % 60
|
||||||
|
end
|
||||||
|
printf " %s, %s, %s\n", render_time(i.ts), done_time, i.command.desc
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_resources(world)
|
||||||
|
printf "Resources:\n Crystals: %5d\n Terraformation: %5d\n",
|
||||||
|
world.resources[Game::Resources::Type::Crystals],
|
||||||
|
world.resources[Game::Resources::Type::Terraformation]
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_world(world)
|
||||||
|
printf "Now:\n %s\n\n", render_time(world.ts)
|
||||||
|
if world.win?
|
||||||
|
printf "YOU WIN!!! Score: %d\n\n", world.score
|
||||||
|
end
|
||||||
|
render_commands world
|
||||||
|
printf "\n"
|
||||||
|
render_resources world
|
||||||
|
printf "\n"
|
||||||
|
render_map world
|
||||||
|
printf "\n"
|
||||||
|
end
|
||||||
|
|
||||||
|
def normalize_command(cmd)
|
||||||
|
cmd.downcase.gsub(/\s+/, ' ').strip
|
||||||
|
end
|
||||||
|
|
||||||
|
CLEAR_SCREEN_ESC_CODE = "\u{001b}[2J"
|
||||||
|
|
||||||
|
def run
|
||||||
|
printf CLEAR_SCREEN_ESC_CODE
|
||||||
|
loop do
|
||||||
|
render_world @world
|
||||||
|
printf "In > "
|
||||||
|
cmd = read_line()
|
||||||
|
norm = normalize_command(cmd)
|
||||||
|
if norm == "exit"
|
||||||
|
break
|
||||||
|
end
|
||||||
|
printf CLEAR_SCREEN_ESC_CODE
|
||||||
|
current_time = Time.local.to_unix
|
||||||
|
@world.run current_time
|
||||||
|
begin
|
||||||
|
@router.handle cmd
|
||||||
|
rescue Game::NotEnoughtResources
|
||||||
|
printf ">>> Not enought resources <<<\n"
|
||||||
|
rescue Game::InvalidPlaceForBuilding
|
||||||
|
printf ">>> Can't build here <<<\n"
|
||||||
|
end
|
||||||
|
printf "\n"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
143
src/expansion.cr
143
src/expansion.cr
@ -1,141 +1,4 @@
|
|||||||
require "colorize"
|
require "./app"
|
||||||
require "./game/**"
|
|
||||||
require "./cli/**"
|
|
||||||
|
|
||||||
map = Game::Generator.make 5, 5
|
app = App.new
|
||||||
ts = Time.local.to_unix
|
app.run
|
||||||
world = Game::World.new map, ts
|
|
||||||
buildings = Game::BuildingFactory.new
|
|
||||||
router = CLI::CommandRouter.new
|
|
||||||
|
|
||||||
buildings.items.each do |i|
|
|
||||||
t = i[:t]
|
|
||||||
b = i[:b]
|
|
||||||
route = sprintf "%s {x} {y}", b.name.downcase
|
|
||||||
desc = sprintf "Build %s at x,y", b.name
|
|
||||||
router.add route, desc do |p|
|
|
||||||
x = p["x"].to_i32
|
|
||||||
y = p["y"].to_i32
|
|
||||||
point = Game::Point.new(x, y)
|
|
||||||
world.push(Game::BuildCommand.new(point, b))
|
|
||||||
printf "Build %s at %d %d\n", b.name, x, y
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
router.add "help", "Show all commands" do |p|
|
|
||||||
printf "Commands:\n"
|
|
||||||
router.routes.each do |r|
|
|
||||||
printf " %s - %s\n", r.route, r.desc
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def render_time(ts)
|
|
||||||
t = Time.unix(ts).in(Time::Location.load("Europe/Moscow"))
|
|
||||||
# It's future, baby
|
|
||||||
t += 200.years
|
|
||||||
t.to_s("%Y-%m-%d %H:%M:%S")
|
|
||||||
end
|
|
||||||
|
|
||||||
def render_map(world)
|
|
||||||
rows = world.map.rows
|
|
||||||
cols = world.map.cols
|
|
||||||
(0...rows).each do |x|
|
|
||||||
if x == 0
|
|
||||||
printf "+"
|
|
||||||
(0...cols).each do |y|
|
|
||||||
printf "------+"
|
|
||||||
end
|
|
||||||
print "\n"
|
|
||||||
end
|
|
||||||
printf "|"
|
|
||||||
(0...cols).each do |y|
|
|
||||||
tile = world.map.get(x, y)
|
|
||||||
printf "%s %d%d|", tile.letter.colorize(:green), x, y
|
|
||||||
end
|
|
||||||
print "\n"
|
|
||||||
printf "|"
|
|
||||||
(0...cols).each do |y|
|
|
||||||
printf " |"
|
|
||||||
end
|
|
||||||
print "\n"
|
|
||||||
printf "|"
|
|
||||||
(0...cols).each do |y|
|
|
||||||
tile = world.map.get(x, y)
|
|
||||||
if tile.letter == 'v'
|
|
||||||
printf "%6d|", world.map.get(x, y).cur
|
|
||||||
else
|
|
||||||
printf " |", world.map.get(x, y).cur
|
|
||||||
end
|
|
||||||
end
|
|
||||||
print "\n"
|
|
||||||
printf "+"
|
|
||||||
(0...cols).each do |y|
|
|
||||||
printf "------+"
|
|
||||||
end
|
|
||||||
print "\n"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def render_commands(world)
|
|
||||||
items = world.queue.top(5)
|
|
||||||
if items.size != 0
|
|
||||||
printf "Queue:\n"
|
|
||||||
end
|
|
||||||
wts = world.ts
|
|
||||||
items.each do |i|
|
|
||||||
ts_diff = i.ts - wts
|
|
||||||
if ts_diff < 60
|
|
||||||
done_time = sprintf " %02ds", ts_diff
|
|
||||||
else
|
|
||||||
done_time = sprintf "%d:%02d", ts_diff // 60, ts_diff % 60
|
|
||||||
end
|
|
||||||
printf " %s, %s, %s\n", render_time(i.ts), done_time, i.command.desc
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def render_resources(world)
|
|
||||||
printf "Resources:\n Crystals: %5d\n Terraformation: %5d\n",
|
|
||||||
world.resources[Game::Resources::Type::Crystals],
|
|
||||||
world.resources[Game::Resources::Type::Terraformation]
|
|
||||||
end
|
|
||||||
|
|
||||||
def render_world(world)
|
|
||||||
printf "Now:\n %s\n\n", render_time(world.ts)
|
|
||||||
if world.win?
|
|
||||||
printf "YOU WIN!!! Score: %d\n\n", world.score
|
|
||||||
end
|
|
||||||
render_commands world
|
|
||||||
printf "\n"
|
|
||||||
render_resources world
|
|
||||||
printf "\n"
|
|
||||||
render_map world
|
|
||||||
printf "\n"
|
|
||||||
end
|
|
||||||
|
|
||||||
def normalize_command(cmd)
|
|
||||||
cmd.downcase.gsub(/\s+/, ' ').strip
|
|
||||||
end
|
|
||||||
|
|
||||||
CLEAR_SCREEN_ESC_CODE = "\u{001b}[2J"
|
|
||||||
|
|
||||||
printf CLEAR_SCREEN_ESC_CODE
|
|
||||||
loop do
|
|
||||||
render_world world
|
|
||||||
printf "In > "
|
|
||||||
cmd = read_line()
|
|
||||||
norm = normalize_command(cmd)
|
|
||||||
if norm == "exit"
|
|
||||||
break
|
|
||||||
end
|
|
||||||
printf CLEAR_SCREEN_ESC_CODE
|
|
||||||
current_time = Time.local.to_unix
|
|
||||||
world.run current_time
|
|
||||||
begin
|
|
||||||
router.handle cmd
|
|
||||||
rescue Game::NotEnoughtResources
|
|
||||||
printf ">>> Not enought resources <<<\n"
|
|
||||||
rescue Game::InvalidPlaceForBuilding
|
|
||||||
printf ">>> Can't build here <<<\n"
|
|
||||||
end
|
|
||||||
printf "\n"
|
|
||||||
end
|
|
||||||
|
@ -9,7 +9,7 @@ module Game
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Mining
|
class Mining
|
||||||
def initialize(@ts : TimeSpan, @dep : DepositSpan)
|
def initialize(@ts : TimeSpan, @dep : Deposit::Span)
|
||||||
end
|
end
|
||||||
|
|
||||||
getter ts
|
getter ts
|
||||||
|
@ -4,22 +4,20 @@ module Game
|
|||||||
@items = [] of NamedTuple(t: Building::Type, b: Building)
|
@items = [] of NamedTuple(t: Building::Type, b: Building)
|
||||||
|
|
||||||
add(
|
add(
|
||||||
Building.new Building::Type::StartPoint, "Start", storage: 100
|
Building.new Building::Type::StartPoint, storage: 100
|
||||||
)
|
)
|
||||||
|
|
||||||
add(
|
add(
|
||||||
Building.new Building::Type::CrystalMiner, "Miner", **{
|
Building.new Building::Type::CrystalMiner, **{
|
||||||
mining: Production.new(
|
mining: Mining.new(
|
||||||
ts: 20,
|
ts: 20,
|
||||||
input: Resources.new,
|
dep: Deposit::Span.new(Resources::Type::Crystals, 40)
|
||||||
res: Resources::Type::Crystals,
|
|
||||||
cap: 40,
|
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
add(
|
add(
|
||||||
Building.new Building::Type::CrystalRestorer, "Restorer", **{
|
Building.new Building::Type::CrystalRestorer, **{
|
||||||
construction: Construction.new(
|
construction: Construction.new(
|
||||||
ts: 30,
|
ts: 30,
|
||||||
cost: Resources.new({
|
cost: Resources.new({
|
||||||
@ -27,17 +25,15 @@ module Game
|
|||||||
}),
|
}),
|
||||||
requirements: [] of Game::Building::Type
|
requirements: [] of Game::Building::Type
|
||||||
),
|
),
|
||||||
restoration: Restoration.new(
|
restoration: Mining.new(
|
||||||
ts: 30,
|
ts: 30,
|
||||||
input: Resources.new,
|
dep: Deposit::Span.new(Resources::Type::Crystals, 20)
|
||||||
res: Resources::Type::Crystals,
|
|
||||||
cap: 20
|
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
add(
|
add(
|
||||||
Building.new Building::Type::Terraformer, "Terraformator", **{
|
Building.new Building::Type::Terraformer, **{
|
||||||
construction: Construction.new(
|
construction: Construction.new(
|
||||||
ts: 120,
|
ts: 120,
|
||||||
cost: Resources.new({
|
cost: Resources.new({
|
||||||
|
Loading…
Reference in New Issue
Block a user