From 63ede6d4ae35384f78d83aa7ae9b811b069c8b45 Mon Sep 17 00:00:00 2001 From: Fernando Valverde Date: Thu, 28 Sep 2023 19:16:37 -0600 Subject: [PATCH] Version 0.2.0 bump (#28) * Version + Readme bump * Bump dependencies + split .env & .env.test * Minor tweaks --- .env.sample | 4 ++++ .github/workflows/ci.yml | 1 + .gitignore | 3 ++- README.md | 38 +++++++++++++++++--------------------- shard.lock | 10 +++++----- shard.yml | 7 +++---- src/app.cr | 6 +++++- 7 files changed, 37 insertions(+), 32 deletions(-) create mode 100644 .env.sample diff --git a/.env.sample b/.env.sample new file mode 100644 index 0000000..4f6ebbd --- /dev/null +++ b/.env.sample @@ -0,0 +1,4 @@ +DATABASE_URL="postgres://localhost:5432/crystal_snake" +REDIS_URL="redis://localhost:6379/0" +DISABLE_DB_PERSIST="true" +LOG_LEVEL="DEBUG" \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6cd36ab..8a29cb9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,6 +10,7 @@ jobs: env: DATABASE_URL: postgres://postgres:postgres@localhost:5432/battlesnake-test KEMAL_ENV: test + LOG_LEVEL: WARN services: postgres: diff --git a/.gitignore b/.gitignore index f57eb37..60decda 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ app worker bundle -.env \ No newline at end of file +.env +.env.test \ No newline at end of file diff --git a/README.md b/README.md index c42af95..c3c7817 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ I wrote a few blog posts about this project. Check out [the first one here](http ## Installation -You'll need Crystal, Postgres 14 & Redis locally. The app uses [sam.cr](https://github.com/imdrasil/sam.cr) for ease of development. +You'll need Crystal, Postgres & Redis locally. The app uses [sam.cr](https://github.com/imdrasil/sam.cr) for ease of development. ```bash # Install dependencies @@ -22,40 +22,35 @@ make sam dev # Run tests make sam test - -# Use a custom strategy -STRATEGY="ChaseClosestFood" make sam dev ``` ## Development -The app can be configured using an `.env` file (example below) - -```bash -DATABASE_URL="postgresql://localhost:5432/battlesnake" -REDIS_URL="redis://localhost:6379" -HONEYCOMB_API_KEY="" -STRATEGY="CautiousCarol" -LOG_LEVEL="DEBUG" -``` +The app can be configured copying the `.env.sample` file as `.env` in the directory root. For local development I use the [BattleSnake CLI](https://github.com/BattlesnakeOfficial/rules/tree/main/cli) with the local server running. A basic example looks like this: ```bash -# Solo game -battlesnake play -W 11 -H 11 --name dev --url http://localhost:8080 -g solo -v +# Solo game with ChaseClosestFood strategy +battlesnake play -W 11 -H 11 --name dev --url http://localhost:8080/chase_closest_food -g solo -v -# 1v1 against itself (you should be able to use any public snake if you know their URL) -battlesnake play -W 11 -H 11 --name dev --url http://localhost:8080 --name dev2 --url http://localhost:8080 -v +# Play RandomValid vs CautiousCarol strategies +battlesnake play -W 11 -H 11 --name RandomValid --url http://localhost:8080/random_valid --name CautiousCarol --url http://localhost:8080/cautious_carol -v ``` #### Strategies & Architecture -The server is built on the `src/app.cr` file and you can choose to use any of the available strategies ([list available here](https://github.com/fdocr/CrystalSnake/tree/main/src/strategy)). +The server is built on the `src/app.cr` file and you can choose to use [any of the available strategies](https://github.com/fdocr/CrystalSnake/tree/main/src/strategy). + +To start hacking a new one create a new strategy (in the `src/strategy` folder) that inherits from `Stategy::Base`. They're all initialized with a `BattleSnake::Context` and the server calls the `#move` method on it to respond with. You can re-use strategies within each other, i.e. `Strategy::ChaseClosestFood` uses the `Strategy::RandomValid` strategy when it can't reach any food on the board. -To start hacking a new one create a new strategy (in that folder) that inherits from `Stategy::Base`. They're all initialized with a `BattleSnake::Context` and you call `#move` method to chose a move to respond with (the method your logic will live). You can re-use strategies within each other, i.e. `Strategy::ChaseClosestFood` uses the `Strategy::RandomValid` strategy when reachable food doesn't exist on the board. +In order to start using a new strategy: +1. Add new entry in case clause to `src/strategy/base.cr` + - Must return the new strategy object +2. The string in the case clause will determine its path + - i.e. `http://localhost:8080/new_strategy` -You can also use `Strategy::Utils` class methods like `Strategy::Utils.a_star` which implements the [A* Search Algorithm](https://en.wikipedia.org/wiki/A*_search_algorithm) (list of [Utils helper methods](https://github.com/fdocr/CrystalSnake/tree/main/src/strategy/utils) available). +Strategies can use `Strategy::Utils` class methods like `Strategy::Utils.a_star` which implements the [A* Search Algorithm](https://en.wikipedia.org/wiki/A*_search_algorithm). Check out the [available Utils helper methods](https://github.com/fdocr/CrystalSnake/tree/main/src/strategy/utils). ## Deployment @@ -68,10 +63,11 @@ ENV variables are used to [customize your snake](https://docs.battlesnake.com/gu - `SNAKE_COLOR` (i.e. "#cccccc") - `SNAKE_HEAD` - `SNAKE_TAIL` -- `STRATEGY` (i.e. "CautiousCarol") ## Contributing +Make sure to test your changes. First copy `.env.sample` as `.env.test` so you can run the test suite with `make sam test`. + 1. Fork it () 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) diff --git a/shard.lock b/shard.lock index bf8070f..4a29545 100644 --- a/shard.lock +++ b/shard.lock @@ -18,7 +18,7 @@ shards: exception_page: git: https://github.com/crystal-loot/exception_page.git - version: 0.3.0 + version: 0.3.1 i18n: git: https://github.com/crimson-knight/i18n.cr.git @@ -30,11 +30,11 @@ shards: jennifer: git: https://github.com/imdrasil/jennifer.cr.git - version: 0.12.0+git.commit.926a1e38d017817bc7f2925ae62f07592fac0220 + version: 0.13.0 kemal: git: https://github.com/kemalcr/kemal.git - version: 1.3.0 + version: 1.4.0 mosquito: git: https://github.com/mosquito-cr/mosquito.git @@ -45,8 +45,8 @@ shards: version: 0.1.0+git.commit.62869371a53eeaff95bc9abe282788947a9aaf5d opentelemetry: - git: https://github.com/fdocr/opentelemetry.git - version: 0.3.4+git.commit.f8a9f89d2ced194e9f0e93af3177c7184e4253be + git: https://github.com/jgaskins/opentelemetry.git + version: 0.3.4 pg: git: https://github.com/will/crystal-pg.git diff --git a/shard.yml b/shard.yml index f01a170..a85d250 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: CrystalSnake -version: 0.1.0 +version: 0.2.0 authors: - Fernando Valverde @@ -14,11 +14,10 @@ dependencies: priority-queue: github: spider-gazelle/priority-queue opentelemetry: - github: fdocr/opentelemetry - commit: f8a9f89 + github: jgaskins/opentelemetry jennifer: github: imdrasil/jennifer.cr - commit: 926a1e3 + version: ~> 0.13.0 pg: github: will/crystal-pg version: ~> 0.26.0 diff --git a/src/app.cr b/src/app.cr index e521463..cb6e9dd 100644 --- a/src/app.cr +++ b/src/app.cr @@ -4,7 +4,11 @@ require "./battle_snake/**" require "./strategy/**" require "dotenv" -Dotenv.load if File.exists?(".env") +dev_env = Kemal.config.env == "development" +Dotenv.load if File.exists?(".env") && dev_env +test_env = Kemal.config.env == "test" +Dotenv.load(path: ".env.test") if File.exists?(".env.test") && test_env + require "../config/**" require "./models/**" require "./jobs/**"