add some documentation
This commit is contained in:
parent
565f2aefb5
commit
c301813dc9
@ -47,6 +47,7 @@ pub fn end(_game: &Game, _turn: i32, _board: &Board, _you: &Battlesnake) {
|
|||||||
// See https://docs.battlesnake.com/api/example-move for available data
|
// See https://docs.battlesnake.com/api/example-move for available data
|
||||||
pub fn get_move(_game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Move {
|
pub fn get_move(_game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Move {
|
||||||
use Direction::{Down, Left, Right, Up};
|
use Direction::{Down, Left, Right, Up};
|
||||||
|
|
||||||
let mut is_move_safe: HashMap<_, _> =
|
let mut is_move_safe: HashMap<_, _> =
|
||||||
vec![(Up, true), (Down, true), (Left, true), (Right, true)]
|
vec![(Up, true), (Down, true), (Left, true), (Right, true)]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -7,7 +7,6 @@ use rocket::serde::{json::Json, Deserialize};
|
|||||||
use rocket::{get, launch, post, routes};
|
use rocket::{get, launch, post, routes};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
mod logic;
|
mod logic;
|
||||||
@ -17,12 +16,16 @@ mod logic;
|
|||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Deserialize, Serialize)]
|
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Deserialize, Serialize)]
|
||||||
pub enum Direction {
|
pub enum Direction {
|
||||||
|
/// Move left (-x)
|
||||||
#[serde(rename = "left")]
|
#[serde(rename = "left")]
|
||||||
Left,
|
Left,
|
||||||
|
/// Move up (+y)
|
||||||
#[serde(rename = "up")]
|
#[serde(rename = "up")]
|
||||||
Up,
|
Up,
|
||||||
|
/// Move right (+x)
|
||||||
#[serde(rename = "right")]
|
#[serde(rename = "right")]
|
||||||
Right,
|
Right,
|
||||||
|
/// Move down (-y)
|
||||||
#[serde(rename = "down")]
|
#[serde(rename = "down")]
|
||||||
Down,
|
Down,
|
||||||
}
|
}
|
||||||
@ -40,30 +43,120 @@ fn is_default<T: Default + PartialEq>(value: &T) -> bool {
|
|||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug)]
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
pub struct Game {
|
pub struct Game {
|
||||||
|
/// A unique identifier for this Game
|
||||||
id: String,
|
id: String,
|
||||||
ruleset: HashMap<String, Value>,
|
/// Information about the ruleset being used to run this game
|
||||||
|
ruleset: Ruleset,
|
||||||
|
/// The name of the map being played on.
|
||||||
|
map: String,
|
||||||
|
/// How much time your snake has to respond to requests for this Game
|
||||||
timeout: u32,
|
timeout: u32,
|
||||||
|
/// The source of this game.
|
||||||
|
///
|
||||||
|
/// One of:
|
||||||
|
/// - "tournament"
|
||||||
|
/// - "league"
|
||||||
|
/// - "arena"
|
||||||
|
/// - "challenge"
|
||||||
|
/// - "custom"
|
||||||
|
///
|
||||||
|
/// The values may change.
|
||||||
|
source: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
pub struct Ruleset {
|
||||||
|
/// Name of the ruleset being used to run this game.
|
||||||
|
name: String,
|
||||||
|
/// The release version of the [Rules](https://github.com/BattlesnakeOfficial/rules) module used in this game.
|
||||||
|
version: String,
|
||||||
|
/// A collection of specific settings being used by the current game that control how the rules
|
||||||
|
/// are applied.
|
||||||
|
settings: RulesetSettings,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
pub struct RulesetSettings {
|
||||||
|
/// Percentage chance of spawning a new food every round.
|
||||||
|
#[serde(rename = "foodSpawnChance")]
|
||||||
|
food_spawn_chance: i32,
|
||||||
|
/// Minimum food to keep on the board every turn.
|
||||||
|
#[serde(rename = "minimumFood")]
|
||||||
|
minimum_food: i32,
|
||||||
|
/// Health damage a snake will take when ending its turn in a hazard. This stacks on top of the
|
||||||
|
/// regular 1 damage a snake takes per turn.
|
||||||
|
#[serde(rename = "hazardDamagePerTurn")]
|
||||||
|
hazard_damage_per_turn: i32,
|
||||||
|
/// rules for the royale mode
|
||||||
|
royale: RulesetRoyale,
|
||||||
|
/// rules for the squad mode
|
||||||
|
squad: RulesetSquad,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
pub struct RulesetRoyale {
|
||||||
|
/// The number of turns between generating new hazards (shrinking the safe board space).
|
||||||
|
#[serde(rename = "shrinkEveryNTurns")]
|
||||||
|
shrink_every_n_turns: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::struct_excessive_bools)]
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
pub struct RulesetSquad {
|
||||||
|
/// Allow members of the same squad to move over each other without dying.
|
||||||
|
#[serde(rename = "allowBodyCollisions")]
|
||||||
|
allow_body_collisions: bool,
|
||||||
|
/// All squad members are eliminated when one is eliminated.
|
||||||
|
#[serde(rename = "sharedElimination")]
|
||||||
|
shared_elimination: bool,
|
||||||
|
/// All squad members share health.
|
||||||
|
#[serde(rename = "sharedHealth")]
|
||||||
|
shared_health: bool,
|
||||||
|
/// All squad members share length.
|
||||||
|
#[serde(rename = "sharedLength")]
|
||||||
|
shared_length: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug)]
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
pub struct Board {
|
pub struct Board {
|
||||||
|
/// The number of rows in the y-axis of the game board.
|
||||||
height: i32,
|
height: i32,
|
||||||
|
/// The number of columns in the x-axis of the game board.
|
||||||
width: i32,
|
width: i32,
|
||||||
|
/// Array of coordinates representing food locations on the game board.
|
||||||
food: Vec<Coord>,
|
food: Vec<Coord>,
|
||||||
|
/// Array of Battlesnakes representing all Battlesnakes remaining on the game board (including
|
||||||
|
/// yourself if you haven't been eliminated).
|
||||||
snakes: Vec<Battlesnake>,
|
snakes: Vec<Battlesnake>,
|
||||||
|
/// Array of coordinates representing hazardous locations on the game board.
|
||||||
hazards: Vec<Coord>,
|
hazards: Vec<Coord>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug)]
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
pub struct Battlesnake {
|
pub struct Battlesnake {
|
||||||
|
/// Unique identifier for this Battlesnake in the context of the current Game
|
||||||
id: String,
|
id: String,
|
||||||
|
/// Name given to this Battlesnake by its author
|
||||||
name: String,
|
name: String,
|
||||||
|
/// Health value of this Battlesnake, between 0 and 100
|
||||||
health: i32,
|
health: i32,
|
||||||
|
/// Array of coordinates representing this Battlesnake's location on the game board. This array
|
||||||
|
/// is ordered from head to tail
|
||||||
body: Vec<Coord>,
|
body: Vec<Coord>,
|
||||||
|
/// Coordinates for this Battlesnake's head. Equivalent to the first element of the body array.
|
||||||
head: Coord,
|
head: Coord,
|
||||||
|
/// Length of this Battlesnake from head to tail. Equivalent to the length of the body array.
|
||||||
length: i32,
|
length: i32,
|
||||||
|
/// The previous response time of this Battlesnake, in milliseconds. If the Battlesnake timed
|
||||||
|
/// out and failed to respond, the game timeout will be returned.
|
||||||
latency: String,
|
latency: String,
|
||||||
|
/// Message shouted by this Battlesnake on the previous turn.
|
||||||
shout: Option<String>,
|
shout: Option<String>,
|
||||||
|
/// The squad that the Battlesnake belongs to. Used to identify squad members in Squad Mode
|
||||||
|
/// games.
|
||||||
|
squad: String,
|
||||||
|
// /// The collection of customizations that control how this Battlesnake is displayed.
|
||||||
|
// customizations: {color, head, tail}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy, Deserialize, Serialize)]
|
#[derive(Debug, PartialEq, Eq, Clone, Copy, Deserialize, Serialize)]
|
||||||
|
Loading…
Reference in New Issue
Block a user