Skip to content

Commit

Permalink
Add scaffolding to call into Rust
Browse files Browse the repository at this point in the history
Doing this early since we likely will want fine control over our search
strategy, and assuming that the RNG is similar to last year's it was
relatively straightforward to clone it in Rust (we can reuse our
implementation from last year).
  • Loading branch information
JesseEmond committed Oct 2, 2024
1 parent 9931cf7 commit 7aa519e
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 4 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Dev

## Setup

```sh
cd bot
python3 -m venv venv
./venv/bin/pip install -r dev-requirements.txt # Note! Not requirements.txt!
```

## Local bot test

Because we run Rust called from Python, the command to run the program is a bit
more involved. For simplicity, it is available in `run.sh`:

```sh
cd bot
./run.sh
```

This will:
- Build the Rust binary with `maturin`
- Install the built wheel program (force reinstall it)
- Run `application.py`

## Upload new version

```sh
cd bot
./package.sh
```

This will create a `devnull.zip` file.

Then:
- Navigate to the website
- Upload `devnull.zip`
- Wait for the build to complete
- Run `Try us!`
25 changes: 25 additions & 0 deletions bot/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Challenge zip
devnull.zip

# Python
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down Expand Up @@ -163,3 +164,27 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/


# Rust
Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# RustRover
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
17 changes: 17 additions & 0 deletions bot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "devnull_bot"
edition = "2021"
version = "0.1.0"

[lib]
name = "devnull_bot"
crate-type = ["cdylib", "rlib"]

[dependencies]
pyo3 = { version = "0.20.0", features = ["extension-module"] }

[dev-dependencies]
criterion = { version = "0.4", features = ["html_reports"] }

[profile.release]
debug = 1
3 changes: 3 additions & 0 deletions bot/bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from game_message import *
import search

import devnull_bot

# TODO: Threats only move every 5 ticks?
# TODO: What do the styles/personalities mean?

Expand All @@ -12,6 +14,7 @@
class Bot:
def __init__(self):
print("Initializing your super mega duper bot")
devnull_bot.hello_world()
self.prev_threats = None

def get_next_move(self, game_message: TeamGameState):
Expand Down
4 changes: 4 additions & 0 deletions bot/core-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Requirements that are needed for both devs and on-server.
websockets==10.4
dataclasses-json==0.6.7
mcts
2 changes: 2 additions & 0 deletions bot/dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-r core-requirements.txt
maturin
5 changes: 4 additions & 1 deletion bot/package.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
#!/bin/sh
rm devnull.zip; zip devnull.zip *.py requirements.txt
set -e
./venv/bin/maturin build --release
rm devnull.zip; zip devnull.zip *.py core-requirements.txt \
requirements.txt ./target/wheels/*.whl
8 changes: 5 additions & 3 deletions bot/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
websockets==10.4
dataclasses-json==0.6.7
mcts
# Note! This is the file that runs on Blitz servers, not what devs run.
# Notably, it expects a whl file for our Rust binary to already be built.
# Devs: see dev-requirements.txt
-r core-requirements.txt
./target/wheels/devnull_bot-0.1.0-cp310-cp310-manylinux_2_34_x86_64.whl
5 changes: 5 additions & 0 deletions bot/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -e
./venv/bin/maturin build --release
./venv/bin/pip install target/wheels/devnull_bot-0.1.0-cp310-cp310-manylinux_2_34_x86_64.whl --force-reinstall
./venv/bin/python application.py
13 changes: 13 additions & 0 deletions bot/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use pyo3::prelude::*;

#[pyfunction]
fn hello_world() -> PyResult<u128> {
println!("Hello world, from Rust!");
Ok(42)
}

#[pymodule]
fn devnull_bot(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(hello_world, m)?)?;
Ok(())
}

0 comments on commit 7aa519e

Please sign in to comment.