diff --git a/.gitignore b/.gitignore index 3b583ec..76cb2d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ /.crystal/ +/.shards/ +/bin/ +/build/ /docs/ /lib/ -/bin/ -/.shards/ *.dwarf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d97bbce --- /dev/null +++ b/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine:edge as builder + +RUN apk add -u crystal shards libc-dev diff --git a/Makefile b/Makefile index 9a0bd3d..972aff5 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,8 @@ +.PHONY: build-docker +build-docker: + docker pull alpine:edge + docker build -t village-crystal . + .PHONY: format format: ./cr tool format ./src ./spec @@ -5,3 +10,8 @@ format: .PHONY: spec spec: ./cr spec --warnings all --error-on-warnings + +.PHONY: release +release: + mkdir -p build + ./cr build ./src/village.cr --release --no-debug --static -o build/village diff --git a/cr b/cr index cf5ed3d..17862fc 100755 --- a/cr +++ b/cr @@ -6,4 +6,4 @@ docker run -it \ -u "$(id -u):$(id -g)" \ -v "$PWD:/app" \ -w "/app" \ - crystallang/crystal:0.30.0 crystal "$@" \ No newline at end of file + village-crystal crystal "$@" diff --git a/shards b/shards index 86d865a..4d4974a 100755 --- a/shards +++ b/shards @@ -6,4 +6,4 @@ docker run -it \ -u "$(id -u):$(id -g)" \ -v "$PWD:/app" \ -w "/app" \ - crystallang/crystal:0.30.0 shards "$@" + village-crystal shards "$@" diff --git a/src/village.cr b/src/village.cr index 774229c..b96cd78 100644 --- a/src/village.cr +++ b/src/village.cr @@ -1,5 +1,76 @@ -module Village - VERSION = "0.1.0" +class Resources + def initialize + @wood = 0 + end - # TODO: Put your code here + def add_wood(x) + @wood += x + end + + def wood + @wood + end end + +abstract class Command + def initialize(ts : Int32) + @ts = ts + end + + def ts + @ts + end + + abstract def run(queue : CommandQueue, res : Resources) +end + +class BuildMillCommand < Command + def run(queue : CommandQueue, res : Resources) + puts "build mill" + c = GetWoodCommand.new(@ts + 5) + queue.push(c) + end +end + +class GetWoodCommand < Command + def run(queue : CommandQueue, res : Resources) + res.add_wood(10) + puts "get wood" + c = GetWoodCommand.new(@ts + 5) + queue.push(c) + end +end + +class CommandQueue + def initialize + @resources = Resources.new + @data = Array(Command).new + end + + def push(command : Command) + printf "push command %d\n", command.ts + @data.push(command) + @data.sort! do |c| + -c.ts + end + end + + def run(ts : Int32) + while @data.size != 0 + c = @data.pop + printf "pop command %d\n", c.ts + if c.ts > ts + break + end + c.run(self, @resources) + end + printf "Wood: %d\n", @resources.wood + end +end + +q = CommandQueue.new +q.push(BuildMillCommand.new(0)) +q.push(BuildMillCommand.new(0)) +q.push(BuildMillCommand.new(0)) +q.push(BuildMillCommand.new(2)) +q.run(10)