From 549d4fe36de6312d6c83cc2e7ac9e83c8c27e8eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20K=C3=A4nner?= Date: Tue, 24 Jun 2025 17:27:56 +0200 Subject: [PATCH] modularize simulation --- Cargo.lock | 1256 ++++++++++++++++- battlesnake/Cargo.toml | 24 + battlesnake/benches/simulation.rs | 20 +- battlesnake/src/main.rs | 132 +- battlesnake/src/types/mod.rs | 10 +- battlesnake/src/types/simulation/maps/mod.rs | 43 + .../src/types/simulation/maps/royale.rs | 86 ++ .../src/types/simulation/maps/standard.rs | 78 + .../{simulation.rs => simulation/mod.rs} | 324 ++--- .../src/types/simulation/rules/constrictor.rs | 38 + battlesnake/src/types/simulation/rules/mod.rs | 37 + .../src/types/simulation/rules/solo.rs | 36 + .../src/types/simulation/rules/standard.rs | 180 +++ battlesnake/src/types/wire.rs | 14 +- 14 files changed, 1989 insertions(+), 289 deletions(-) create mode 100644 battlesnake/src/types/simulation/maps/mod.rs create mode 100644 battlesnake/src/types/simulation/maps/royale.rs create mode 100644 battlesnake/src/types/simulation/maps/standard.rs rename battlesnake/src/types/{simulation.rs => simulation/mod.rs} (56%) create mode 100644 battlesnake/src/types/simulation/rules/constrictor.rs create mode 100644 battlesnake/src/types/simulation/rules/mod.rs create mode 100644 battlesnake/src/types/simulation/rules/solo.rs create mode 100644 battlesnake/src/types/simulation/rules/standard.rs diff --git a/Cargo.lock b/Cargo.lock index a7da80b..1e7e7c6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + [[package]] name = "anes" version = "0.1.6" @@ -82,6 +88,15 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "atoi" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f28d99ec8bfea296261ca1af174f24225171fea9664ba9003cbebee704810528" +dependencies = [ + "num-traits", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -152,6 +167,12 @@ dependencies = [ "tracing", ] +[[package]] +name = "az" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b7e4c2464d97fe331d41de9d5db0def0a96f4d823b8b32a2efd503578988973" + [[package]] name = "backtrace" version = "0.3.75" @@ -164,7 +185,7 @@ dependencies = [ "miniz_oxide", "object", "rustc-demangle", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -173,20 +194,39 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +[[package]] +name = "base64ct" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55248b47b0caf0546f7988906588779981c43bb1bc9d0c44087278f80cdb44ba" + [[package]] name = "battlesnake" version = "2.0.0" dependencies = [ "axum", + "az", "bitvec", + "blanket", + "bytemuck", + "clap", "criterion", "enum-iterator", + "enum_dispatch", "env_logger", + "flame", + "flamer", "float-ord", "futures-util", + "hashbrown", + "lfu_cache", "log", - "rand", + "lru", + "memmap2", + "rand 0.9.1", "serde", + "serde_json", + "sqlx", "tokio", ] @@ -195,6 +235,9 @@ name = "bitflags" version = "2.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" +dependencies = [ + "serde", +] [[package]] name = "bitvec" @@ -208,6 +251,17 @@ dependencies = [ "wyz", ] +[[package]] +name = "blanket" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56791e4bd64c99fc361e01008f45c984baa93f12a0957d1b3c51dd2c6baab453" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -223,6 +277,18 @@ version = "3.18.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "793db76d6187cd04dff33004d8e6c9cc4e05cd330500379d2394209271b4aeee" +[[package]] +name = "bytemuck" +version = "1.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c76a5792e44e4abe34d3abf15636779261d45a7450612059293d1d2cfc63422" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "bytes" version = "1.10.1" @@ -235,6 +301,15 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" +[[package]] +name = "cc" +version = "1.2.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "956a5e21988b87f372569b66183b78babf23ebc2e744b733e4350a752c4dafac" +dependencies = [ + "shlex", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -275,6 +350,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd60e63e9be68e5fb56422e397cf9baddded06dae1d2e523401542383bc72a9f" dependencies = [ "clap_builder", + "clap_derive", ] [[package]] @@ -283,8 +359,22 @@ version = "4.5.39" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89cc6392a1f72bbeb820d71f32108f61fdaf18bc526e1d23954168a67759ef51" dependencies = [ + "anstream", "anstyle", "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09176aae279615badda0765c0c0b3f6ed53f4709118af73cf4655d85d1530cd7" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -305,10 +395,25 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c" dependencies = [ - "lazy_static", + "lazy_static 1.5.0", "windows-sys 0.59.0", ] +[[package]] +name = "concurrent-queue" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "const-oid" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" + [[package]] name = "cpufeatures" version = "0.2.17" @@ -318,6 +423,21 @@ dependencies = [ "libc", ] +[[package]] +name = "crc" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9710d3b3739c2e349eb44fe848ad0b7c8cb1e42bd87ee49371df2f7acaf3e675" +dependencies = [ + "crc-catalog", +] + +[[package]] +name = "crc-catalog" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5" + [[package]] name = "criterion" version = "0.5.1" @@ -373,6 +493,15 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "crossbeam-queue" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f58bbc28f91df819d0aa2a2c00cd19754769c2fad90579b3592b1c9ba7a3115" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-utils" version = "0.8.21" @@ -401,6 +530,17 @@ version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a2330da5de22e8a3cb63252ce2abb30116bf5265e89c0e01bc17015ce30a476" +[[package]] +name = "der" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb" +dependencies = [ + "const-oid", + "pem-rfc7468", + "zeroize", +] + [[package]] name = "digest" version = "0.10.7" @@ -408,14 +548,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer", + "const-oid", "crypto-common", + "subtle", ] +[[package]] +name = "displaydoc" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "dotenvy" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" + [[package]] name = "either" version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" +dependencies = [ + "serde", +] [[package]] name = "encoding_rs" @@ -446,6 +608,18 @@ dependencies = [ "syn", ] +[[package]] +name = "enum_dispatch" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa18ce2bc66555b3218614519ac839ddb759a7d6720732f979ef8d13be147ecd" +dependencies = [ + "once_cell", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "env_filter" version = "0.1.3" @@ -475,18 +649,81 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" +[[package]] +name = "etcetera" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "136d1b5283a1ab77bd9257427ffd09d8667ced0570b6f938942bc7568ed5b943" +dependencies = [ + "cfg-if", + "home", + "windows-sys 0.48.0", +] + +[[package]] +name = "event-listener" +version = "5.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3492acde4c3fc54c845eaab3eed8bd00c7a7d881f78bfc801e43a93dec1331ae" +dependencies = [ + "concurrent-queue", + "parking", + "pin-project-lite", +] + +[[package]] +name = "flame" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fc2706461e1ee94f55cab2ed2e3d34ae9536cfa830358ef80acff1a3dacab30" +dependencies = [ + "lazy_static 0.2.11", + "serde", + "serde_derive", + "serde_json", + "thread-id", +] + +[[package]] +name = "flamer" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7693d9dd1ec1c54f52195dfe255b627f7cec7da33b679cd56de949e662b3db10" +dependencies = [ + "flame", + "quote", + "syn", +] + [[package]] name = "float-ord" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ce81f49ae8a0482e4c55ea62ebbd7e5a686af544c00b9d090bba3ff9be97b3d" +[[package]] +name = "flume" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da0e4dd2a88388a1f4ccc7c9ce104604dab68d9f408dc34cd45823d5a9069095" +dependencies = [ + "futures-core", + "futures-sink", + "spin", +] + [[package]] name = "fnv" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -509,6 +746,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -517,6 +755,34 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" +[[package]] +name = "futures-executor" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-intrusive" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d930c203dd0b6ff06e0201a4a2fe9149b43c684fd4420555b26d21b1a02956f" +dependencies = [ + "futures-core", + "lock_api", + "parking_lot", +] + +[[package]] +name = "futures-io" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" + [[package]] name = "futures-macro" version = "0.3.31" @@ -547,9 +813,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" dependencies = [ "futures-core", + "futures-io", "futures-macro", "futures-sink", "futures-task", + "memchr", "pin-project-lite", "pin-utils", "slab", @@ -565,6 +833,17 @@ dependencies = [ "version_check", ] +[[package]] +name = "getrandom" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.11.0+wasi-snapshot-preview1", +] + [[package]] name = "getrandom" version = "0.3.3" @@ -617,6 +896,26 @@ name = "hashbrown" version = "0.15.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash", +] + +[[package]] +name = "hashlink" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7382cf6263419f2d8df38c55d7da83da5c18aef87fc7a7fc1fb1e344edfe14c1" +dependencies = [ + "hashbrown", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "hermit-abi" @@ -624,6 +923,39 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f154ce46856750ed433c8649605bf7ed2de3bc35fd9d2a9f30cddd873c80cb08" +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hkdf" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b5f8eb2ad728638ea2c7d47a21db23b7b58a72ed6a38256b8a1849f15fbbdf7" +dependencies = [ + "hmac", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + +[[package]] +name = "home" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589533453244b0995c858700322199b2becb13b627df2851f64a2775d024abcf" +dependencies = [ + "windows-sys 0.59.0", +] + [[package]] name = "http" version = "1.3.1" @@ -706,6 +1038,113 @@ dependencies = [ "tower-service", ] +[[package]] +name = "icu_collections" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "200072f5d0e3614556f94a9930d5dc3e0662a652823904c3a75dc3b0af7fee47" +dependencies = [ + "displaydoc", + "potential_utf", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0cde2700ccaed3872079a65fb1a78f6c0a36c91570f28755dda67bc8f7d9f00a" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "436880e8e18df4d7bbc06d58432329d6458cc84531f7ac5f024e93deadb37979" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00210d6893afc98edb752b664b8890f0ef174c8adbb8d0be9710fa66fbbf72d3" + +[[package]] +name = "icu_properties" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "016c619c1eeb94efb86809b015c58f479963de65bdb6253345c1a1276f22e32b" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "potential_utf", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "298459143998310acd25ffe6810ed544932242d3f07083eee1084d83a71bd632" + +[[package]] +name = "icu_provider" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03c80da27b5f4187909049ee2d72f276f0d9f99a42c306bd0131ecfe04d8e5af" +dependencies = [ + "displaydoc", + "icu_locale_core", + "stable_deref_trait", + "tinystr", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "idna" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "686f825264d630750a544639377bae737628043f20d38bbc029e8f29ea968a7e" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acae9609540aa318d1bc588455225fb2085b9ed0c4f6bd0d9d5bcd86f1a0344" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + [[package]] name = "indexmap" version = "2.9.0" @@ -782,11 +1221,26 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" + [[package]] name = "lazy_static" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" +dependencies = [ + "spin", +] + +[[package]] +name = "lfu_cache" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18333c3c9ae8f731098d650474d07785570bf5ab1e1f33750731b7c9ef562009" [[package]] name = "libc" @@ -794,6 +1248,29 @@ version = "0.2.172" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" +[[package]] +name = "libm" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de" + +[[package]] +name = "libsqlite3-sys" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e99fb7a497b1e3339bc746195567ed8d3e24945ecd636e3619d20b9de9e9149" +dependencies = [ + "cc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "litemap" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "241eaef5fd12c88705a01fc1066c48c4b36e0dd4377dcdc7ec3942cea7a69956" + [[package]] name = "lock_api" version = "0.4.13" @@ -810,18 +1287,46 @@ version = "0.4.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" +[[package]] +name = "lru" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f8cc7106155f10bdf99a6f379688f543ad6596a415375b36a59a054ceda1198" +dependencies = [ + "hashbrown", +] + [[package]] name = "matchit" version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest", +] + [[package]] name = "memchr" version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "memmap2" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd3f7eed9d3848f8b98834af67102b720745c4ec028fcd0aa0239277e7de374f" +dependencies = [ + "libc", +] + [[package]] name = "mime" version = "0.3.17" @@ -865,6 +1370,43 @@ dependencies = [ "version_check", ] +[[package]] +name = "num-bigint-dig" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc84195820f291c7697304f3cbdadd1cb7199c0efc917ff5eafd71225c136151" +dependencies = [ + "byteorder", + "lazy_static 1.5.0", + "libm", + "num-integer", + "num-iter", + "num-traits", + "rand 0.8.5", + "smallvec", + "zeroize", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -872,6 +1414,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", + "libm", ] [[package]] @@ -901,6 +1444,12 @@ version = "11.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" +[[package]] +name = "parking" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" + [[package]] name = "parking_lot" version = "0.12.4" @@ -919,9 +1468,18 @@ checksum = "bc838d2a56b5b1a6c25f55575dfc605fabb63bb2365f6c2353ef9159aa69e4a5" dependencies = [ "cfg-if", "libc", - "redox_syscall", + "redox_syscall 0.5.12", "smallvec", - "windows-targets", + "windows-targets 0.52.6", +] + +[[package]] +name = "pem-rfc7468" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88b39c9bfcfc231068454382784bb460aae594343fb030d46e9f50a645418412" +dependencies = [ + "base64ct", ] [[package]] @@ -942,6 +1500,33 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkcs1" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8ffb9f10fa047879315e6625af03c164b16962a5368d724ed16323b68ace47f" +dependencies = [ + "der", + "pkcs8", + "spki", +] + +[[package]] +name = "pkcs8" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" +dependencies = [ + "der", + "spki", +] + +[[package]] +name = "pkg-config" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" + [[package]] name = "plotters" version = "0.3.7" @@ -985,6 +1570,15 @@ dependencies = [ "portable-atomic", ] +[[package]] +name = "potential_utf" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5a7c30837279ca13e7c867e9e40053bc68740f988cb07f7ca6df43cc734b585" +dependencies = [ + "zerovec", +] + [[package]] name = "ppv-lite86" version = "0.2.21" @@ -1024,14 +1618,35 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + [[package]] name = "rand" version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97" dependencies = [ - "rand_chacha", - "rand_core", + "rand_chacha 0.9.0", + "rand_core 0.9.3", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", ] [[package]] @@ -1041,7 +1656,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.9.3", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.16", ] [[package]] @@ -1050,7 +1674,7 @@ version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" dependencies = [ - "getrandom", + "getrandom 0.3.3", ] [[package]] @@ -1073,6 +1697,12 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "redox_syscall" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" + [[package]] name = "redox_syscall" version = "0.5.12" @@ -1120,6 +1750,26 @@ dependencies = [ "colored", ] +[[package]] +name = "rsa" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78928ac1ed176a5ca1d17e578a1825f3d81ca54cf41053a592584b020cfd691b" +dependencies = [ + "const-oid", + "digest", + "num-bigint-dig", + "num-integer", + "num-traits", + "pkcs1", + "pkcs8", + "rand_core 0.6.4", + "signature", + "spki", + "subtle", + "zeroize", +] + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -1218,6 +1868,23 @@ dependencies = [ "digest", ] +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + [[package]] name = "signal-hook-registry" version = "1.4.5" @@ -1227,6 +1894,16 @@ dependencies = [ "libc", ] +[[package]] +name = "signature" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" +dependencies = [ + "digest", + "rand_core 0.6.4", +] + [[package]] name = "slab" version = "0.4.9" @@ -1241,6 +1918,9 @@ name = "smallvec" version = "1.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" +dependencies = [ + "serde", +] [[package]] name = "socket2" @@ -1257,6 +1937,236 @@ name = "spin" version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +dependencies = [ + "lock_api", +] + +[[package]] +name = "spki" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" +dependencies = [ + "base64ct", + "der", +] + +[[package]] +name = "sqlx" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fefb893899429669dcdd979aff487bd78f4064e5e7907e4269081e0ef7d97dc" +dependencies = [ + "sqlx-core", + "sqlx-macros", + "sqlx-mysql", + "sqlx-postgres", + "sqlx-sqlite", +] + +[[package]] +name = "sqlx-core" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee6798b1838b6a0f69c007c133b8df5866302197e404e8b6ee8ed3e3a5e68dc6" +dependencies = [ + "base64", + "bytes", + "crc", + "crossbeam-queue", + "either", + "event-listener", + "futures-core", + "futures-intrusive", + "futures-io", + "futures-util", + "hashbrown", + "hashlink", + "indexmap", + "log", + "memchr", + "once_cell", + "percent-encoding", + "serde", + "serde_json", + "sha2", + "smallvec", + "thiserror", + "tokio", + "tokio-stream", + "tracing", + "url", +] + +[[package]] +name = "sqlx-macros" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2d452988ccaacfbf5e0bdbc348fb91d7c8af5bee192173ac3636b5fb6e6715d" +dependencies = [ + "proc-macro2", + "quote", + "sqlx-core", + "sqlx-macros-core", + "syn", +] + +[[package]] +name = "sqlx-macros-core" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19a9c1841124ac5a61741f96e1d9e2ec77424bf323962dd894bdb93f37d5219b" +dependencies = [ + "dotenvy", + "either", + "heck", + "hex", + "once_cell", + "proc-macro2", + "quote", + "serde", + "serde_json", + "sha2", + "sqlx-core", + "sqlx-mysql", + "sqlx-postgres", + "sqlx-sqlite", + "syn", + "tokio", + "url", +] + +[[package]] +name = "sqlx-mysql" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa003f0038df784eb8fecbbac13affe3da23b45194bd57dba231c8f48199c526" +dependencies = [ + "atoi", + "base64", + "bitflags", + "byteorder", + "bytes", + "crc", + "digest", + "dotenvy", + "either", + "futures-channel", + "futures-core", + "futures-io", + "futures-util", + "generic-array", + "hex", + "hkdf", + "hmac", + "itoa", + "log", + "md-5", + "memchr", + "once_cell", + "percent-encoding", + "rand 0.8.5", + "rsa", + "serde", + "sha1", + "sha2", + "smallvec", + "sqlx-core", + "stringprep", + "thiserror", + "tracing", + "whoami", +] + +[[package]] +name = "sqlx-postgres" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db58fcd5a53cf07c184b154801ff91347e4c30d17a3562a635ff028ad5deda46" +dependencies = [ + "atoi", + "base64", + "bitflags", + "byteorder", + "crc", + "dotenvy", + "etcetera", + "futures-channel", + "futures-core", + "futures-util", + "hex", + "hkdf", + "hmac", + "home", + "itoa", + "log", + "md-5", + "memchr", + "once_cell", + "rand 0.8.5", + "serde", + "serde_json", + "sha2", + "smallvec", + "sqlx-core", + "stringprep", + "thiserror", + "tracing", + "whoami", +] + +[[package]] +name = "sqlx-sqlite" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2d12fe70b2c1b4401038055f90f151b78208de1f9f89a7dbfd41587a10c3eea" +dependencies = [ + "atoi", + "flume", + "futures-channel", + "futures-core", + "futures-executor", + "futures-intrusive", + "futures-util", + "libsqlite3-sys", + "log", + "percent-encoding", + "serde", + "serde_urlencoded", + "sqlx-core", + "thiserror", + "tracing", + "url", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + +[[package]] +name = "stringprep" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b4df3d392d81bd458a8a621b8bffbd2302a12ffe288a9d931670948749463b1" +dependencies = [ + "unicode-bidi", + "unicode-normalization", + "unicode-properties", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" @@ -1275,6 +2185,17 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tap" version = "1.0.1" @@ -1301,6 +2222,27 @@ dependencies = [ "syn", ] +[[package]] +name = "thread-id" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7fbf4c9d56b320106cd64fd024dadfa0be7cb4706725fc44a7d7ce952d820c1" +dependencies = [ + "libc", + "redox_syscall 0.1.57", + "winapi", +] + +[[package]] +name = "tinystr" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d4f6d1145dcb577acf783d4e601bc1d76a13337bb54e6233add580b07344c8b" +dependencies = [ + "displaydoc", + "zerovec", +] + [[package]] name = "tinytemplate" version = "1.2.1" @@ -1311,6 +2253,21 @@ dependencies = [ "serde_json", ] +[[package]] +name = "tinyvec" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09b3661f17e86524eccd4371ab0429194e0d7c008abb45f7a7495b1719463c71" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "tokio" version = "1.45.1" @@ -1340,6 +2297,17 @@ dependencies = [ "syn", ] +[[package]] +name = "tokio-stream" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eca58d7bba4a75707817a2c44174253f9236b2d5fbd055602e9d5c07c139a047" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + [[package]] name = "tokio-tungstenite" version = "0.26.2" @@ -1401,9 +2369,21 @@ checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" dependencies = [ "log", "pin-project-lite", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1ffbcf9c6f6b99d386e7444eb608ba646ae452a36b39737deb9663b610f662" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tracing-core" version = "0.1.34" @@ -1424,7 +2404,7 @@ dependencies = [ "http", "httparse", "log", - "rand", + "rand 0.9.1", "sha1", "thiserror", "utf-8", @@ -1436,24 +2416,68 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f" +[[package]] +name = "unicode-bidi" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5" + [[package]] name = "unicode-ident" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" +[[package]] +name = "unicode-normalization" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unicode-properties" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e70f2a8b45122e719eb623c01822704c4e0907e7e426a05927e1a1cfff5b75d0" + +[[package]] +name = "url" +version = "2.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32f8b686cadd1473f4bd0117a5d28d36b1ade384ea9b5069a1c40aefed7fda60" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + [[package]] name = "utf-8" version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + [[package]] name = "utf8parse" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version_check" version = "0.9.5" @@ -1485,6 +2509,12 @@ dependencies = [ "wit-bindgen-rt", ] +[[package]] +name = "wasite" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b" + [[package]] name = "wasm-bindgen" version = "0.2.100" @@ -1553,6 +2583,32 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "whoami" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6994d13118ab492c3c80c1f81928718159254c53c472bf9ce36f8dae4add02a7" +dependencies = [ + "redox_syscall 0.5.12", + "wasite", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + [[package]] name = "winapi-util" version = "0.1.9" @@ -1562,13 +2618,28 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -1577,7 +2648,22 @@ version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -1586,28 +2672,46 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_gnu" version = "0.52.6" @@ -1620,24 +2724,48 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.6" @@ -1653,6 +2781,12 @@ dependencies = [ "bitflags", ] +[[package]] +name = "writeable" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea2f10b9bb0928dfb1b42b65e1f9e36f7f54dbdf08457afefb38afcdec4fa2bb" + [[package]] name = "wyz" version = "0.5.1" @@ -1670,6 +2804,30 @@ dependencies = [ "rich_progress_bar", ] +[[package]] +name = "yoke" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f41bb01b8226ef4bfd589436a297c53d118f65921786300e427be8d487695cc" +dependencies = [ + "serde", + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38da3c9736e16c5d3c8c597a9aaa5d1fa565d0532ae05e27c24aa62fb32c0ab6" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + [[package]] name = "zerocopy" version = "0.8.25" @@ -1689,3 +2847,63 @@ dependencies = [ "quote", "syn", ] + +[[package]] +name = "zerofrom" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zeroize" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" + +[[package]] +name = "zerotrie" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36f0bbd478583f79edad978b407914f61b2972f5af6fa089686016be8f9af595" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", +] + +[[package]] +name = "zerovec" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a05eb080e015ba39cc9e23bbe5e7fb04d5fb040350f99f34e338d5fdd294428" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b96237efa0c878c64bd89c436f661be4e46b2f3eff1ebb976f7ef2321d2f58f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/battlesnake/Cargo.toml b/battlesnake/Cargo.toml index 7beb3af..fc00ac4 100644 --- a/battlesnake/Cargo.toml +++ b/battlesnake/Cargo.toml @@ -3,6 +3,7 @@ authors = ["Max Känner"] name = "battlesnake" version = "2.0.0" edition = "2024" +rust-version = "1.86" readme = "README.md" repository = "https://git.mkaenner.de/max/battlesnake" @@ -10,11 +11,21 @@ keywords = ["battlesnake"] description = """ A simple Battlesnake written in Rust """ +default-run = "battlesnake" [lints.clippy] pedantic = "warn" nursery = "warn" +[[bin]] +name = "battlesnake" + +[[bin]] +name = "seed-cracker" + +[[bin]] +name = "generate" + [dependencies] # server tokio = { version = "1.43", features = ["full"] } @@ -31,6 +42,19 @@ enum-iterator = "2.1" rand = "0.9" float-ord = "0.3" futures-util = "0.3.31" +az = "1.2.1" +blanket = "0.4.0" +hashbrown = "0.15.4" +serde_json = "1.0.140" +clap = { version = "4.5.39", features = ["derive"] } +sqlx = { version = "0.8.6", features = ["runtime-tokio", "sqlite"] } +enum_dispatch = "0.3.13" +lru = "0.14.0" +lfu_cache = "1.3.0" +memmap2 = "0.9.5" +bytemuck = "1.23.1" +flame = "0.2.2" +flamer = "0.5.0" [dev-dependencies] criterion = "0.5" diff --git a/battlesnake/benches/simulation.rs b/battlesnake/benches/simulation.rs index 7aa0ecf..9785687 100644 --- a/battlesnake/benches/simulation.rs +++ b/battlesnake/benches/simulation.rs @@ -10,7 +10,7 @@ use criterion::{Bencher, BenchmarkId, Criterion, criterion_group, criterion_main use battlesnake::types::{ Coord, - simulation::Board, + simulation::Game as Board, wire::{Battlesnake, Board as WireBoard, Game, Request, RoyaleSettings, Ruleset, Settings}, }; use rand::{SeedableRng, rngs::SmallRng}; @@ -78,13 +78,8 @@ fn standard(c: &mut Criterion) { let benchmark = |b: &mut Bencher, board: &Board| { b.iter(|| { let mut board = board.clone(); - let turn = board.simulate_random(&mut SmallRng::from_os_rng(), |board| { - if board.num_snakes() <= 1 { - Some(board.turn()) - } else { - None - } - }); + board.simulate_random(&mut SmallRng::from_os_rng()); + let turn = board.board.turn(); if turn < turns_min.load(Ordering::Relaxed) { turns_min.store(turn, Ordering::Relaxed); @@ -155,13 +150,8 @@ fn constrictor(c: &mut Criterion) { let benchmark = |b: &mut Bencher, board: &Board| { b.iter(|| { let mut board = board.clone(); - let turn = board.simulate_random(&mut SmallRng::from_os_rng(), |board| { - if board.num_snakes() <= 1 { - Some(board.turn()) - } else { - None - } - }); + board.simulate_random(&mut SmallRng::from_os_rng()); + let turn = board.board.turn(); if turn < turns_min.load(Ordering::Relaxed) { turns_min.store(turn, Ordering::Relaxed); diff --git a/battlesnake/src/main.rs b/battlesnake/src/main.rs index ff4750e..2c3289e 100644 --- a/battlesnake/src/main.rs +++ b/battlesnake/src/main.rs @@ -5,22 +5,26 @@ use std::{ use axum::{ Router, - extract::Json, + extract::{Json, State}, response, routing::{get, post}, }; use battlesnake::types::{ Direction, - simulation::Board, + simulation::{Board, Game}, wire::{Request, Response}, }; use float_ord::FloatOrd; use futures_util::future::join_all; +use hashbrown::HashMap; use log::{debug, error, info, trace, warn}; use rand::prelude::*; use serde::Serialize; use tokio::{ + fs::File, + io::AsyncWriteExt, net::TcpListener, + sync::mpsc::{UnboundedSender, unbounded_channel}, time::{Duration, Instant}, }; @@ -30,12 +34,15 @@ static THREADS: AtomicUsize = AtomicUsize::new(1); async fn main() { env_logger::init(); + let (sender, mut receiver) = unbounded_channel(); + debug!("Creating routes"); let app = Router::new() .route("/", get(info)) .route("/start", post(start)) .route("/move", post(get_move)) - .route("/end", post(end)); + .route("/end", post(end)) + .with_state(sender); let threads = env::var("THREADS") .ok() @@ -51,6 +58,53 @@ async fn main() { debug!("Creating listener"); let port = env::var("PORT").unwrap_or_else(|_| "8000".into()); let listener = TcpListener::bind(format!("0.0.0.0:{port}")).await.unwrap(); + + debug!("Starting observer"); + tokio::spawn(async move { + let mut games = HashMap::new(); + while let Some((request_type, request)) = receiver.recv().await { + match request_type { + RequestType::Start => { + let game_id = request.game.id.clone(); + info!("Got start request {game_id}"); + if let Some(old_requests) = games.insert(game_id, vec![request]) { + warn!("evicted duplicate game: {old_requests:?}"); + } + } + RequestType::GetMove => { + let game_id = request.game.id.clone(); + info!("Got move request {game_id}"); + games.entry(game_id).or_default().push(request); + } + RequestType::End => { + let game_id = request.game.id.clone(); + info!("Got end request {game_id}"); + if let Some(mut requests) = games.remove(&game_id) { + requests.push(request); + let json = match serde_json::to_vec_pretty(&requests) { + Ok(json) => json, + Err(e) => { + error!("Unable to serealize json: {e}"); + continue; + } + }; + match File::create_new(format!("games/{game_id}.json")).await { + Ok(mut file) => { + if let Err(e) = file.write_all(&json).await { + error!("Unable to write jsone: {e}"); + } + } + Err(e) => error!("Unable to open file: {e}"), + } + } else { + warn!("end of game without game: {request:?}"); + } + } + } + } + warn!("Observer stopped"); + }); + debug!("Starting server"); axum::serve(listener, app).await.unwrap(); } @@ -77,22 +131,41 @@ struct Info { version: &'static str, } -async fn start(request: Json) { - let board = Board::from(&*request); +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] +enum RequestType { + Start, + GetMove, + End, +} + +async fn start( + State(sender): State>, + Json(request): Json, +) { + if let Err(e) = sender.send((RequestType::Start, request.clone())) { + warn!("Unable to observe request: {e}"); + } + let board = Board::from(&request); info!("got start request: {board}"); } #[allow(clippy::too_many_lines)] -async fn get_move(request: Json) -> response::Json { +async fn get_move( + State(sender): State>, + Json(request): Json, +) -> response::Json { let start = Instant::now(); - let board = Board::from(&*request); + if let Err(e) = sender.send((RequestType::GetMove, request.clone())) { + warn!("Unable to observe request: {e}"); + } + let board = Game::from(&request); let timeout = Duration::from_millis(u64::from(request.game.timeout)); - let id = board.get_id(&request.you.id).unwrap_or_else(|| { + let id = board.board.get_id(&request.you.id).unwrap_or_else(|| { error!("My id is not in the simulation board"); 0 }); - debug!("got move request: {board}"); - let actions = board.valid_actions(id).collect::>(); + debug!("got move request: {}", board.board); + let actions = board.board.valid_actions(id).collect::>(); if actions.len() <= 1 { info!( "only one possible action. Fast forwarding {:?}", @@ -111,10 +184,6 @@ async fn get_move(request: Json) -> response::Json { start.elapsed().as_millis() ); } - let end_condition: fn(&Board) -> Option<()> = match &*request.game.ruleset.name { - "solo" => end_solo, - _ => end_standard, - }; let score_fn: fn(&Board, u8) -> u32 = match &*request.game.ruleset.name { "solo" => score_solo, _ => score_standard, @@ -132,22 +201,25 @@ async fn get_move(request: Json) -> response::Json { let mut mcts_actions = Vec::new(); while start.elapsed() < timeout * 4 / 5 { let mut board = board.clone(); - while end_condition(&board).is_none() { + let mut game_over = false; + while !game_over { mcts_actions.clear(); mcts_actions.extend(mcts_managers.iter_mut().filter_map(|mcts_manager| { mcts_manager - .next_action(&board, c, &mut rng) + .next_action(&board.board, c, &mut rng) .map(|action| (mcts_manager.snake, action)) })); - board.next_turn(&mcts_actions, &mut rng); + game_over = board.next_turn_random(&mcts_actions, &mut rng); if mcts_actions.is_empty() { break; } } - board.simulate_random(&mut rng, end_condition); + if !game_over { + board.simulate_random(&mut rng); + } for mcts_manager in &mut mcts_managers { let id = mcts_manager.snake; - let score = score_fn(&board, id); + let score = score_fn(&board.board, id); mcts_manager.apply_score(score); } } @@ -200,16 +272,8 @@ async fn get_move(request: Json) -> response::Json { }) } -fn end_solo(board: &Board) -> Option<()> { - (board.valid_actions(0).count() == 0).then_some(()) -} - -fn end_standard(board: &Board) -> Option<()> { - (board.num_snakes() <= 1).then_some(()) -} - -fn score_solo(board: &Board, id: u8) -> u32 { - u32::try_from(board.length(id)).unwrap_or(0) +const fn score_solo(board: &Board, _id: u8) -> u32 { + board.turn() } fn score_standard(board: &Board, id: u8) -> u32 { @@ -220,8 +284,14 @@ fn score_standard(board: &Board, id: u8) -> u32 { } } -async fn end(request: Json) { - let board = Board::from(&*request); +async fn end( + State(sender): State>, + Json(request): Json, +) { + if let Err(e) = sender.send((RequestType::End, request.clone())) { + warn!("Unable to observe request: {e}"); + } + let board = Board::from(&request); info!("got end request: {board}"); } diff --git a/battlesnake/src/types/mod.rs b/battlesnake/src/types/mod.rs index 75cb91f..618eee2 100644 --- a/battlesnake/src/types/mod.rs +++ b/battlesnake/src/types/mod.rs @@ -1,10 +1,12 @@ +use std::fmt::Display; + use enum_iterator::Sequence; use serde::{Deserialize, Serialize}; pub mod simulation; pub mod wire; -#[derive(Debug, PartialEq, Eq, Clone, Copy, Deserialize)] +#[derive(Debug, PartialEq, Eq, Clone, Copy, Deserialize, Serialize)] pub struct Coord { pub x: u8, pub y: u8, @@ -33,6 +35,12 @@ impl Coord { } } +impl Display for Coord { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "({}, {})", self.x, self.y) + } +} + #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Serialize, Sequence)] #[serde(rename_all = "lowercase")] pub enum Direction { diff --git a/battlesnake/src/types/simulation/maps/mod.rs b/battlesnake/src/types/simulation/maps/mod.rs new file mode 100644 index 0000000..432f29f --- /dev/null +++ b/battlesnake/src/types/simulation/maps/mod.rs @@ -0,0 +1,43 @@ +mod royale; +mod standard; + +use blanket::blanket; +use enum_dispatch::enum_dispatch; +use rand::Rng; +use royale::Royale; +use standard::Standard; + +use crate::types::wire::Game; + +use super::Board; + +#[blanket(derive(Ref, Arc, Mut, Box))] +#[enum_dispatch(Maps)] +pub trait Map { + /// Called before the board is updated + fn pre_update(&self, board: &mut Board, rng: &mut impl Rng); + + /// Called after the board is updated + fn post_update(&self, board: &mut Board, rng: &mut impl Rng); +} + +#[enum_dispatch] +#[derive(Debug, Clone, Copy)] +pub enum Maps { + Standard, + Royale, +} + +pub trait MapId { + /// ID of the map for detection + const ID: &str; +} + +impl From<&Game> for Maps { + fn from(value: &Game) -> Self { + match value.map.as_ref() { + Royale::ID => Royale::from(&value.ruleset.settings).into(), + _ => Standard::from(&value.ruleset.settings).into(), + } + } +} diff --git a/battlesnake/src/types/simulation/maps/royale.rs b/battlesnake/src/types/simulation/maps/royale.rs new file mode 100644 index 0000000..a92784b --- /dev/null +++ b/battlesnake/src/types/simulation/maps/royale.rs @@ -0,0 +1,86 @@ +use rand::{Rng, seq::IteratorRandom}; + +use crate::types::{Coord, Direction, simulation::Board, wire::Settings}; + +use super::{Map, MapId, standard::Standard}; + +#[derive(Debug, Clone, Copy)] +pub struct Royale { + standard: Standard, + shrink_every_n_turns: u8, +} + +impl Map for Royale { + fn pre_update(&self, board: &mut Board, rng: &mut impl Rng) { + self.standard.pre_update(board, rng); + } + + fn post_update(&self, board: &mut Board, rng: &mut impl Rng) { + self.standard.post_update(board, rng); + + // Royale uses the current turn to generate hazards, not the previous turn that's in the + // board state + let turn = board.turn + 1; + + if turn < u32::from(self.shrink_every_n_turns) { + return; + } + + let side = enum_iterator::all::() + .choose(rng) + .unwrap_or(Direction::Up); + match side { + Direction::Up => { + if let Some(i) = board.hazard.first_zero() { + let y = board.linear_to_coord(i).y; + for x in 0..board.width { + let i = board.coord_to_linear(Coord { x, y }); + board.hazard.set(i, true); + } + } + } + Direction::Down => { + if let Some(i) = board.hazard.last_zero() { + let y = board.linear_to_coord(i).y; + for x in 0..board.width { + let i = board.coord_to_linear(Coord { x, y }); + board.hazard.set(i, true); + } + } + } + Direction::Left => { + if let Some(i) = board.hazard.first_zero() { + let x = board.linear_to_coord(i).y; + for y in 0..board.height { + let i = board.coord_to_linear(Coord { x, y }); + board.hazard.set(i, true); + } + } + } + Direction::Right => { + if let Some(i) = board.hazard.last_zero() { + let x = board.linear_to_coord(i).y; + for y in 0..board.height { + let i = board.coord_to_linear(Coord { x, y }); + board.hazard.set(i, true); + } + } + } + } + } +} + +impl MapId for Royale { + const ID: &str = "royale"; +} + +impl From<&Settings> for Royale { + fn from(value: &Settings) -> Self { + let standard = Standard::from(value); + let shrink_every_n_turns = value.royale.shrink_every_n_turns; + Self { + standard, + shrink_every_n_turns, + } + } +} diff --git a/battlesnake/src/types/simulation/maps/standard.rs b/battlesnake/src/types/simulation/maps/standard.rs new file mode 100644 index 0000000..eb043f6 --- /dev/null +++ b/battlesnake/src/types/simulation/maps/standard.rs @@ -0,0 +1,78 @@ +use az::SaturatingAs; +use rand::{Rng, seq::SliceRandom}; + +use crate::types::{Coord, simulation::Board, wire::Settings}; + +use super::{Map, MapId}; + +#[derive(Debug, Clone, Copy)] +pub struct Standard { + min_food: u16, + food_spawn_chance: u8, +} + +impl Map for Standard { + fn pre_update(&self, _board: &mut Board, _rng: &mut impl Rng) {} + + fn post_update(&self, board: &mut Board, rng: &mut impl Rng) { + let food_needed = self.check_food_needing_placement(board, rng); + if food_needed > 0 { + Self::place_food_randomly(board, rng, food_needed); + } + } +} + +impl MapId for Standard { + const ID: &str = "standard"; +} + +impl From<&Settings> for Standard { + fn from(value: &Settings) -> Self { + let min_food = value.minimum_food; + let food_spawn_chance = value.food_spawn_chance; + Self { + min_food, + food_spawn_chance, + } + } +} + +impl Standard { + fn check_food_needing_placement(self, board: &Board, rng: &mut impl rand::Rng) -> u16 { + let num_current_food: u16 = board.food.count_ones().saturating_as(); + + if num_current_food < self.min_food { + return self.min_food - num_current_food; + } + if self.food_spawn_chance > 0 && (100 - rng.random_range(0..100)) < self.food_spawn_chance { + return 1; + } + 0 + } + + fn place_food_randomly(board: &mut Board, rng: &mut impl rand::Rng, food_needed: u16) { + let mut unoccupied_points: Vec<_> = board.get_unoccupied_points(false, false).collect(); + Self::place_food_ramdomly_at_positions( + board, + rng, + food_needed, + unoccupied_points.as_mut_slice(), + ); + } + + fn place_food_ramdomly_at_positions( + board: &mut Board, + rng: &mut impl rand::Rng, + food_needed: u16, + positions: &mut [Coord], + ) { + let food_needed = usize::from(food_needed).min(positions.len()); + + positions.shuffle(rng); + + for tile in &positions[..food_needed] { + let i = board.coord_to_linear(*tile); + board.food.set(i, true); + } + } +} diff --git a/battlesnake/src/types/simulation.rs b/battlesnake/src/types/simulation/mod.rs similarity index 56% rename from battlesnake/src/types/simulation.rs rename to battlesnake/src/types/simulation/mod.rs index d1b1eed..1c4b6d8 100644 --- a/battlesnake/src/types/simulation.rs +++ b/battlesnake/src/types/simulation/mod.rs @@ -1,3 +1,6 @@ +mod maps; +mod rules; + use std::{ collections::VecDeque, fmt::Display, @@ -6,9 +9,12 @@ use std::{ sync::Arc, }; +use az::SaturatingAs; use bitvec::prelude::*; use log::{error, warn}; +use maps::{Map, Maps}; use rand::prelude::*; +use rules::{Ruleset, Rulesets}; use super::{Coord, Direction, wire::Request}; @@ -58,19 +64,79 @@ impl DerefMut for SmallBitBox { } } +#[derive(Clone)] +pub struct Game { + pub board: Board, + map: Maps, + ruleset: Rulesets, +} + +impl From<&Request> for Game { + fn from(value: &Request) -> Self { + let board = value.into(); + let map = (&value.game).into(); + let ruleset = (&value.game.ruleset).into(); + Self { + board, + map, + ruleset, + } + } +} + +impl Game { + pub fn simulate_random(&mut self, rng: &mut impl Rng) { + loop { + let random_actions: Vec<_> = (0..self.board.snakes.len()) + .filter_map(|i| { + self.board + .valid_actions_index(i) + .choose(rng) + .map(|direction| (i.saturating_as(), direction)) + }) + .collect(); + if self.next_turn(&random_actions, rng) { + break; + } + } + } + + pub fn next_turn_random(&mut self, actions: &[(u8, Direction)], rng: &mut impl Rng) -> bool { + let random_actions: Vec<_> = (0..self.board.snakes.len()) + .filter_map(|i| { + actions + .iter() + .find(|(j, _)| i == usize::from(*j)) + .copied() + .or_else(|| { + self.board + .valid_actions_index(i) + .choose(rng) + .map(|direction| (i.saturating_as(), direction)) + }) + }) + .collect(); + self.next_turn(&random_actions, rng) + } + + pub fn next_turn(&mut self, actions: &[(u8, Direction)], rng: &mut impl Rng) -> bool { + self.map.pre_update(&mut self.board, rng); + let game_over = self.ruleset.execute(&mut self.board, actions); + self.map.post_update(&mut self.board, rng); + self.board.turn += 1; + game_over + } +} + #[derive(Debug, PartialEq, Eq, Clone)] pub struct Board { width: u8, height: u8, - hazard_damage: u8, - food_spawn_chance: u8, - min_food: u16, turn: u32, food: SmallBitBox, hazard: SmallBitBox, free: SmallBitBox, snakes: Vec, - constrictor: bool, id_map: Arc<[(u8, Arc)]>, } @@ -94,15 +160,11 @@ impl From<&Request> for Board { let mut board = Self { width, height, - hazard_damage: value.game.ruleset.settings.hazard_damage_per_turn, - food_spawn_chance: value.game.ruleset.settings.food_spawn_chance, - min_food: value.game.ruleset.settings.minimum_food, turn: value.turn, food: SmallBitBox::new(false, fields), hazard: SmallBitBox::new(false, fields), free: SmallBitBox::new(true, fields), snakes: Vec::with_capacity(value.board.snakes.len()), - constrictor: &*value.game.ruleset.name == "constrictor", id_map: id_map.into(), }; @@ -116,11 +178,12 @@ impl From<&Request> for Board { board.hazard.set(index, true); } + let constrictor = value.game.ruleset.name.as_ref() == "constrictor"; for (id, snake) in value.board.snakes.iter().enumerate() { for &tile in snake .body .iter() - .take(snake.body.len() - usize::from(!board.constrictor)) + .take(snake.body.len() - usize::from(!constrictor)) { let index = board.coord_to_linear(tile); board.free.set(index, false); @@ -139,17 +202,7 @@ impl From<&Request> for Board { impl Display for Board { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - writeln!( - f, - "{} {}x{} {}% ({}) {}dmg @ {}", - if self.constrictor { "constrictor" } else { "" }, - self.width, - self.height, - self.food_spawn_chance, - self.min_food, - self.hazard_damage, - self.turn - )?; + writeln!(f, "{}x{} @ {}", self.width, self.height, self.turn)?; for y in (0..self.height).rev() { for x in 0..self.width { @@ -279,30 +332,6 @@ impl Board { }) } - pub fn simulate_random( - &mut self, - rng: &mut impl RngCore, - stop: impl Fn(&Self) -> Option, - ) -> T { - loop { - if let Some(score) = stop(self) { - break score; - } - self.next_turn(&[], rng); - } - } - - pub fn next_turn(&mut self, actions: &[(u8, Direction)], rng: &mut impl RngCore) { - self.move_standard(actions, rng); - self.starvation_standard(); - self.hazard_damage_standard(); - self.feed_snakes_standard(); - self.eliminate_snake_standard(); - self.update_free_map(); - self.spawn_food(rng); - self.turn += 1; - } - fn id_to_index(&self, id: u8) -> Option { self.snakes.binary_search_by_key(&id, |snake| snake.id).ok() } @@ -313,127 +342,6 @@ impl Board { .filter(move |direction| self.is_free(head.wrapping_apply(*direction))) } - fn move_standard(&mut self, actions: &[(u8, Direction)], rng: &mut impl RngCore) { - for i in 0..self.snakes.len() { - let snake = &self.snakes[i]; - let action = actions.iter().find(|(id, _)| *id == snake.id).map_or_else( - || { - self.valid_actions_index(i) - .choose(rng) - .unwrap_or(Direction::Up) - }, - |(_, action)| *action, - ); - let new_head = snake.head().wrapping_apply(action); - self.snakes[i].advance(new_head); - } - } - - fn starvation_standard(&mut self) { - for snake in &mut self.snakes { - snake.health = snake.health.saturating_sub(1); - } - } - - fn hazard_damage_standard(&mut self) { - let mut i = 0; - while i < self.snakes.len() { - let head = self.snakes[i].head(); - if self.is_in_bounds(head) { - let head_index = self.coord_to_linear(head); - if self.hazard[head_index] && !self.food[head_index] { - let health = &mut self.snakes[i].health; - *health = health.saturating_sub(1); - if *health == 0 { - let snake = self.snakes.remove(i); - for tile in snake.body { - let index = self.coord_to_linear(tile); - self.free.set(index, true); - } - continue; - } - } - } - i += 1; - } - } - - fn feed_snakes_standard(&mut self) { - if self.constrictor { - for snake in &mut self.snakes { - snake.feed(); - } - } else { - let mut eaten_food = vec![]; - for i in 0..self.snakes.len() { - let head = self.snakes[i].head(); - if self.is_in_bounds(head) { - let head_index = self.coord_to_linear(head); - if self.food[head_index] { - eaten_food.push(head_index); - self.snakes[i].feed(); - } - } - } - for food_index in eaten_food { - self.food.set(food_index, false); - } - } - } - - fn eliminate_snake_standard(&mut self) { - // eliminate out of health and out of bounds - let mut i = 0; - while i < self.snakes.len() { - let snake = &self.snakes[i]; - if snake.health == 0 || !self.is_in_bounds(snake.head()) { - let snake = self.snakes.remove(i); - for tile in snake.body.iter().skip(1) { - if self.is_in_bounds(*tile) { - let index = self.coord_to_linear(*tile); - self.free.set(index, true); - } - } - continue; - } - i += 1; - } - - // look for collisions - let mut collisions = vec![]; - for snake in &self.snakes { - let head = snake.head(); - let head_index = self.coord_to_linear(head); - if !self.free[head_index] { - collisions.push(snake.id); - continue; - } - for snake2 in &self.snakes { - if snake.id != snake2.id - && snake.head() == snake2.head() - && snake.body.len() <= snake2.body.len() - { - collisions.push(snake.id); - break; - } - } - } - - // apply collisions - let mut i = 0; - while i < self.snakes.len() { - if collisions.contains(&self.snakes[i].id) { - let snake = self.snakes.remove(i); - for tile in snake.body { - let index = self.coord_to_linear(tile); - self.free.set(index, true); - } - continue; - } - i += 1; - } - } - fn update_free_map(&mut self) { // free tails for snake in &self.snakes { @@ -452,65 +360,49 @@ impl Board { } } - fn spawn_food(&mut self, rng: &mut impl RngCore) { - let food_needed = self.check_food_needing_placement(rng); - - if food_needed > 0 { - self.place_food_randomly(food_needed, rng); - } - } - - fn check_food_needing_placement(&self, rng: &mut impl RngCore) -> u16 { - let min_food = self.min_food; - let food_spawn_chance = self.food_spawn_chance; - let num_current_food = u16::try_from(self.food.count_ones()).unwrap_or(u16::MAX); - - if num_current_food < min_food { - return min_food - num_current_food; - } - if food_spawn_chance > 0 && (100 - rng.random_range(0..100)) < food_spawn_chance { - return 1; - } - - 0 - } - - fn place_food_randomly(&mut self, amount: u16, rng: &mut impl RngCore) { - let tails: Vec<_> = self - .snakes + fn get_unoccupied_points( + &self, + include_possible_moves: bool, + include_hazards: bool, + ) -> impl Iterator + use<'_> { + let possible_moves: Vec<_> = if include_possible_moves { + Vec::new() + } else { + self.snakes + .iter() + .flat_map(|snake| { + let head = snake.head(); + enum_iterator::all::() + .map(move |direction| head.wrapping_apply(direction)) + .filter(|tile| self.is_in_bounds(*tile)) + .map(|tile| self.coord_to_linear(tile)) + }) + .collect() + }; + self.free .iter() - .map(|snake| self.coord_to_linear(snake.tail())) - .collect(); - let possible_moves: Vec<_> = self - .snakes - .iter() - .flat_map(|snake| { - let head = snake.head(); - enum_iterator::all::() - .map(move |direction| head.wrapping_apply(direction)) - .filter(|tile| self.is_in_bounds(*tile)) - .map(|tile| self.coord_to_linear(tile)) - }) - .collect(); - let unoccupied_points = self - .free - .iter() - .by_vals() + .zip(self.food.iter()) + .zip(self.hazard.iter()) .enumerate() - .zip(self.hazard.iter().by_vals()) - .filter_map(|((i, free), hazard)| (!hazard && free).then_some(i)) - .filter(|i| !tails.contains(i)) - .filter(|i| !possible_moves.contains(i)); - - for food_spot in unoccupied_points.choose_multiple(rng, usize::from(amount)) { - self.food.set(food_spot, true); - } + .filter(move |(i, ((free, food), hazard))| { + **free + && !**food + && (include_hazards || !**hazard) + && (include_possible_moves || !possible_moves.contains(i)) + }) + .map(|(i, _)| self.linear_to_coord(i)) } fn coord_to_linear(&self, coord: Coord) -> usize { usize::from(coord.x) + usize::from(coord.y) * usize::from(self.width) } + fn linear_to_coord(&self, linear: usize) -> Coord { + let x = (linear % usize::from(self.width)).saturating_as(); + let y = (linear / usize::from(self.width)).saturating_as(); + Coord { x, y } + } + const fn is_in_bounds(&self, coord: Coord) -> bool { coord.x < self.width && coord.y < self.height } diff --git a/battlesnake/src/types/simulation/rules/constrictor.rs b/battlesnake/src/types/simulation/rules/constrictor.rs new file mode 100644 index 0000000..374e2a5 --- /dev/null +++ b/battlesnake/src/types/simulation/rules/constrictor.rs @@ -0,0 +1,38 @@ +use crate::types::{Direction, simulation::Board, wire}; + +use super::{ + Ruleset, + standard::{DamageHazards, eliminate_snakes, game_over, move_snakes, reduce_snake_health}, +}; + +#[derive(Debug, Clone, Copy)] +pub struct Constrictor { + damage_hazards: DamageHazards, +} + +impl From<&wire::Ruleset> for Constrictor { + fn from(value: &wire::Ruleset) -> Self { + Self { + damage_hazards: (&value.settings).into(), + } + } +} + +impl Ruleset for Constrictor { + fn execute(&self, board: &mut Board, actions: &[(u8, Direction)]) -> bool { + let game_over = game_over(board); + move_snakes(board, actions); + reduce_snake_health(board); + self.damage_hazards.execute(board); + eliminate_snakes(board); + grow_snakes(board); + board.update_free_map(); + game_over + } +} + +fn grow_snakes(board: &mut Board) { + for snake in &mut board.snakes { + snake.feed(); + } +} diff --git a/battlesnake/src/types/simulation/rules/mod.rs b/battlesnake/src/types/simulation/rules/mod.rs new file mode 100644 index 0000000..da3de39 --- /dev/null +++ b/battlesnake/src/types/simulation/rules/mod.rs @@ -0,0 +1,37 @@ +mod constrictor; +mod solo; +mod standard; + +use constrictor::Constrictor; +use enum_dispatch::enum_dispatch; +use solo::Solo; +use standard::Standard; + +use crate::types::{Direction, wire}; + +use super::Board; + +#[enum_dispatch] +pub trait Ruleset { + /// executes one turn of the ruleset. + /// Returns true if the game is over + fn execute(&self, board: &mut Board, actions: &[(u8, Direction)]) -> bool; +} + +#[enum_dispatch(Ruleset)] +#[derive(Debug, Clone, Copy)] +pub enum Rulesets { + Standard, + Constrictor, + Solo, +} + +impl From<&wire::Ruleset> for Rulesets { + fn from(value: &wire::Ruleset) -> Self { + match value.name.as_ref() { + "solo" => Solo::from(value).into(), + "constrictor" => Constrictor::from(value).into(), + _ => Standard::from(value).into(), + } + } +} diff --git a/battlesnake/src/types/simulation/rules/solo.rs b/battlesnake/src/types/simulation/rules/solo.rs new file mode 100644 index 0000000..91f20f5 --- /dev/null +++ b/battlesnake/src/types/simulation/rules/solo.rs @@ -0,0 +1,36 @@ +use crate::types::{Direction, simulation::Board, wire}; + +use super::{ + Ruleset, + standard::{DamageHazards, eliminate_snakes, feed_snakes, move_snakes, reduce_snake_health}, +}; + +#[derive(Debug, Clone, Copy)] +pub struct Solo { + damage_hazards: DamageHazards, +} + +impl From<&wire::Ruleset> for Solo { + fn from(value: &wire::Ruleset) -> Self { + Self { + damage_hazards: (&value.settings).into(), + } + } +} + +impl Ruleset for Solo { + fn execute(&self, board: &mut Board, actions: &[(u8, Direction)]) -> bool { + let game_over = game_over(board); + move_snakes(board, actions); + reduce_snake_health(board); + self.damage_hazards.execute(board); + feed_snakes(board); + eliminate_snakes(board); + board.update_free_map(); + game_over + } +} + +pub fn game_over(board: &Board) -> bool { + board.num_snakes() == 0 +} diff --git a/battlesnake/src/types/simulation/rules/standard.rs b/battlesnake/src/types/simulation/rules/standard.rs new file mode 100644 index 0000000..0d035c9 --- /dev/null +++ b/battlesnake/src/types/simulation/rules/standard.rs @@ -0,0 +1,180 @@ +use crate::types::{ + Direction, + simulation::Board, + wire::{self, Settings}, +}; + +use super::Ruleset; + +#[derive(Debug, Clone, Copy)] +pub struct Standard { + damage_hazards: DamageHazards, +} + +impl From<&wire::Ruleset> for Standard { + fn from(value: &wire::Ruleset) -> Self { + Self { + damage_hazards: (&value.settings).into(), + } + } +} + +impl Ruleset for Standard { + fn execute(&self, board: &mut Board, actions: &[(u8, Direction)]) -> bool { + let game_over = game_over(board); + move_snakes(board, actions); + reduce_snake_health(board); + self.damage_hazards.execute(board); + feed_snakes(board); + eliminate_snakes(board); + board.update_free_map(); + game_over + } +} + +pub fn move_snakes(board: &mut Board, actions: &[(u8, Direction)]) { + for i in 0..board.snakes.len() { + let snake = &board.snakes[i]; + let action = actions.iter().find(|(id, _)| *id == snake.id).map_or_else( + || { + let head = snake.body[0]; + let previous_head = snake.body[1]; + let delta_x = i16::from(head.x) - i16::from(previous_head.x); + let delta_y = i16::from(head.y) - i16::from(previous_head.y); + if delta_x == 0 && delta_y == 0 { + Direction::Up + } else if delta_x.abs() > delta_y.abs() { + if delta_x < 0 { + Direction::Left + } else { + Direction::Right + } + } else if delta_y < 0 { + Direction::Down + } else { + Direction::Up + } + }, + |(_, action)| *action, + ); + let new_head = snake.head().wrapping_apply(action); + board.snakes[i].advance(new_head); + } +} + +pub fn reduce_snake_health(board: &mut Board) { + for snake in &mut board.snakes { + snake.health = snake.health.saturating_sub(1); + } +} + +#[derive(Debug, Clone, Copy)] +pub struct DamageHazards { + damage: u8, +} + +impl DamageHazards { + pub fn execute(self, board: &mut Board) { + let mut i = 0; + while i < board.snakes.len() { + let head = board.snakes[i].head(); + if board.is_in_bounds(head) { + let head_index = board.coord_to_linear(head); + if board.hazard[head_index] && !board.food[head_index] { + let health = &mut board.snakes[i].health; + *health = health.saturating_sub(self.damage); + if *health == 0 { + let snake = board.snakes.remove(i); + for tile in snake.body { + let index = board.coord_to_linear(tile); + board.free.set(index, true); + } + continue; + } + } + } + i += 1; + } + } +} + +impl From<&Settings> for DamageHazards { + fn from(value: &Settings) -> Self { + let damage = value.hazard_damage_per_turn; + Self { damage } + } +} + +pub fn eliminate_snakes(board: &mut Board) { + // eliminate out of health and out of bounds + let mut i = 0; + while i < board.snakes.len() { + let snake = &board.snakes[i]; + if snake.health == 0 || !board.is_in_bounds(snake.head()) { + let snake = board.snakes.remove(i); + for tile in snake.body.iter().skip(1) { + if board.is_in_bounds(*tile) { + let index = board.coord_to_linear(*tile); + board.free.set(index, true); + } + } + continue; + } + i += 1; + } + + // look for collisions + let mut collisions = vec![]; + for snake in &board.snakes { + let head = snake.head(); + let head_index = board.coord_to_linear(head); + if !board.free[head_index] { + collisions.push(snake.id); + continue; + } + for snake2 in &board.snakes { + if snake.id != snake2.id + && snake.head() == snake2.head() + && snake.body.len() <= snake2.body.len() + { + collisions.push(snake.id); + break; + } + } + } + + // apply collisions + let mut i = 0; + while i < board.snakes.len() { + if collisions.contains(&board.snakes[i].id) { + let snake = board.snakes.remove(i); + for tile in snake.body { + let index = board.coord_to_linear(tile); + board.free.set(index, true); + } + continue; + } + i += 1; + } +} + +pub fn feed_snakes(board: &mut Board) { + let mut eaten_food = vec![]; + for i in 0..board.snakes.len() { + let head = board.snakes[i].head(); + if board.is_in_bounds(head) { + let head_index = board.coord_to_linear(head); + if board.food[head_index] { + eaten_food.push(head_index); + board.snakes[i].feed(); + } + } + } + for food_index in eaten_food { + board.food.set(food_index, false); + } +} + +pub fn game_over(board: &Board) -> bool { + board.num_snakes() <= 1 +} diff --git a/battlesnake/src/types/wire.rs b/battlesnake/src/types/wire.rs index 7fe22c5..56ff99a 100644 --- a/battlesnake/src/types/wire.rs +++ b/battlesnake/src/types/wire.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use super::{Coord, Direction}; -#[derive(Debug, PartialEq, Eq, Clone, Deserialize)] +#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)] pub struct Request { /// Game object describing the game being played. pub game: Game, @@ -16,7 +16,7 @@ pub struct Request { pub you: Battlesnake, } -#[derive(Debug, PartialEq, Eq, Clone, Deserialize)] +#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)] pub struct Game { /// A unique identifier for this Game. pub id: Arc, @@ -36,7 +36,7 @@ pub struct Game { pub source: Arc, } -#[derive(Debug, PartialEq, Eq, Clone, Deserialize)] +#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)] pub struct Ruleset { /// Name of the ruleset being used to run this game. pub name: Arc, @@ -47,7 +47,7 @@ pub struct Ruleset { pub settings: Settings, } -#[derive(Debug, PartialEq, Eq, Clone, Copy, Deserialize)] +#[derive(Debug, PartialEq, Eq, Clone, Copy, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct Settings { /// Percentage chance of spawning a new food every round. @@ -61,14 +61,14 @@ pub struct Settings { pub royale: RoyaleSettings, } -#[derive(Debug, PartialEq, Eq, Clone, Copy, Deserialize)] +#[derive(Debug, PartialEq, Eq, Clone, Copy, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct RoyaleSettings { /// The number of turns between generating new hazards (shrinking the safe board space). pub shrink_every_n_turns: u8, } -#[derive(Debug, PartialEq, Eq, Clone, Deserialize)] +#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)] pub struct Board { /// The number of rows in the y-axis of the game board. pub height: u8, @@ -83,7 +83,7 @@ pub struct Board { pub snakes: Vec, } -#[derive(Debug, PartialEq, Eq, Clone, Deserialize)] +#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)] pub struct Battlesnake { /// Unique identifier for this Battlesnake in the context of the current Game. pub id: Arc,