{"id":20487187,"url":"https://github.com/fdocr/crystalsnake","last_synced_at":"2025-04-13T15:25:50.673Z","repository":{"id":81850154,"uuid":"568447931","full_name":"fdocr/CrystalSnake","owner":"fdocr","description":"A BattleSnake template written in Crystal","archived":false,"fork":false,"pushed_at":"2023-10-21T15:28:14.000Z","size":3610,"stargazers_count":4,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-06T04:26:21.885Z","etag":null,"topics":["battlesnake","crystal","kemal"],"latest_commit_sha":null,"homepage":"https://fdocr.github.io/CrystalSnake/","language":"Crystal","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fdocr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-20T15:09:29.000Z","updated_at":"2023-10-24T13:54:34.000Z","dependencies_parsed_at":null,"dependency_job_id":"96127a32-ea11-483e-9d63-a582c0c72e43","html_url":"https://github.com/fdocr/CrystalSnake","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fdocr%2FCrystalSnake","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fdocr%2FCrystalSnake/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fdocr%2FCrystalSnake/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fdocr%2FCrystalSnake/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fdocr","download_url":"https://codeload.github.com/fdocr/CrystalSnake/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248734145,"owners_count":21153156,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["battlesnake","crystal","kemal"],"created_at":"2024-11-15T16:40:21.721Z","updated_at":"2025-04-13T15:25:50.648Z","avatar_url":"https://github.com/fdocr.png","language":"Crystal","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Crystal Snake\n\nThis is a [Battle Snake](https://play.battlesnake.com/) project written in [Crystal](https://crystal-lang.org/) using [Kemal](https://kemalcr.com/)\n\nI strongly recommend reading the docs in [https://fdocr.github.io/CrystalSnake/](https://fdocr.github.io/CrystalSnake/). You'll find detailed information on classes and methods there :)\n\nI wrote a few blog posts about this project. Check out [the first one here](https://dev.to/fdocr/learning-crystal-with-battlesnake-3chj).\n\n## Installation\n\nYou'll need Crystal, Postgres \u0026 Redis locally. The app uses [sam.cr](https://github.com/imdrasil/sam.cr) for ease of development.\n\n```bash\n# Install dependencies\nshards install\n\n# Create DB \u0026 run migrations\nmake sam db:setup\n\n# Start development server in port 8080 with live reload\nmake sam dev\n\n# Run tests\nmake sam test\n```\n\n## Development\n\nThe app can be configured copying the `.env.sample` file as `.env` in the directory root.\n\nFor 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:\n\n```bash\n# Solo game with ChaseClosestFood strategy\nbattlesnake play -W 11 -H 11 --name dev --url http://localhost:8080/chase_closest_food -g solo -v\n\n# Play RandomValid vs CautiousCarol strategies\nbattlesnake play -W 11 -H 11 --name RandomValid --url http://localhost:8080/random_valid  --name CautiousCarol --url http://localhost:8080/cautious_carol -v\n```\n\n#### Strategies \u0026 Architecture\n\nThe 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).\n\nTo 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.\n\nIn order to start using a new strategy:\n1. Add new entry in case clause to `src/strategy/base.cr`\n   - Must return the new strategy object\n2. The string in the case clause will determine its path\n   - i.e. `http://localhost:8080/new_strategy`\n\nStrategies 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).\n\n## Deployment\n\nI'm currently using [DigitalOcean App Platform](https://www.digitalocean.com/products/app-platform). The [Dockerfile](/Dockerfile) is detected and deployed on their cheapest tier. It \"should work\" in lots of other platforms with this setup, but [open an issue](https://github.com/fdocr/CrystalSnake/issues/new) if you need help troubleshooting or to discuss other solutions.\n\n**Customizations**\n\nENV variables are used to [customize your snake](https://docs.battlesnake.com/guides/customizations)\n\n- `SNAKE_COLOR` (i.e. \"#cccccc\")\n- `SNAKE_HEAD`\n- `SNAKE_TAIL`\n\n## Contributing\n\nMake sure to test your changes. First copy `.env.sample` as `.env.test` so you can run the test suite with `make sam test`.\n\n1. Fork it (\u003chttps://github.com/fdocr/CrystalSnake/fork\u003e)\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n\n## Contributors\n\n- [Fernando Valverde](https://github.com/fdocr) - creator and maintainer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffdocr%2Fcrystalsnake","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffdocr%2Fcrystalsnake","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffdocr%2Fcrystalsnake/lists"}