Skip to content

Commit

Permalink
Version 0.2.0 bump (#28)
Browse files Browse the repository at this point in the history
* Version + Readme bump

* Bump dependencies + split .env & .env.test

* Minor tweaks
  • Loading branch information
fdocr authored Sep 29, 2023
1 parent 39c0b03 commit 63ede6d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DATABASE_URL="postgres://localhost:5432/crystal_snake"
REDIS_URL="redis://localhost:6379/0"
DISABLE_DB_PERSIST="true"
LOG_LEVEL="DEBUG"
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/battlesnake-test
KEMAL_ENV: test
LOG_LEVEL: WARN

services:
postgres:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
app
worker
bundle
.env
.env
.env.test
38 changes: 17 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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="<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

Expand All @@ -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 (<https://github.com/fdocr/CrystalSnake/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
Expand Down
10 changes: 5 additions & 5 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: CrystalSnake
version: 0.1.0
version: 0.2.0

authors:
- Fernando Valverde <[email protected]>
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/app.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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/**"
Expand Down

0 comments on commit 63ede6d

Please sign in to comment.