diff --git a/battlesnake/src/logic.rs b/battlesnake/src/logic.rs index 811e07b..6353022 100644 --- a/battlesnake/src/logic.rs +++ b/battlesnake/src/logic.rs @@ -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 pub fn get_move(_game: &Game, turn: i32, board: &Board, you: &Battlesnake) -> Move { use Direction::{Down, Left, Right, Up}; + let mut is_move_safe: HashMap<_, _> = vec![(Up, true), (Down, true), (Left, true), (Right, true)] .into_iter() diff --git a/battlesnake/src/main.rs b/battlesnake/src/main.rs index 0b52606..3b5ae45 100644 --- a/battlesnake/src/main.rs +++ b/battlesnake/src/main.rs @@ -7,7 +7,6 @@ use rocket::serde::{json::Json, Deserialize}; use rocket::{get, launch, post, routes}; use serde::Serialize; use serde_json::Value; -use std::collections::HashMap; use std::env; mod logic; @@ -17,12 +16,16 @@ mod logic; #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Deserialize, Serialize)] pub enum Direction { + /// Move left (-x) #[serde(rename = "left")] Left, + /// Move up (+y) #[serde(rename = "up")] Up, + /// Move right (+x) #[serde(rename = "right")] Right, + /// Move down (-y) #[serde(rename = "down")] Down, } @@ -40,30 +43,120 @@ fn is_default(value: &T) -> bool { #[derive(Deserialize, Serialize, Debug)] pub struct Game { + /// A unique identifier for this Game id: String, - ruleset: HashMap, + /// 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, + /// 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)] pub struct Board { + /// The number of rows in the y-axis of the game board. height: i32, + /// The number of columns in the x-axis of the game board. width: i32, + /// Array of coordinates representing food locations on the game board. food: Vec, + /// Array of Battlesnakes representing all Battlesnakes remaining on the game board (including + /// yourself if you haven't been eliminated). snakes: Vec, + /// Array of coordinates representing hazardous locations on the game board. hazards: Vec, } #[derive(Deserialize, Serialize, Debug)] pub struct Battlesnake { + /// Unique identifier for this Battlesnake in the context of the current Game id: String, + /// Name given to this Battlesnake by its author name: String, + /// Health value of this Battlesnake, between 0 and 100 health: i32, + /// Array of coordinates representing this Battlesnake's location on the game board. This array + /// is ordered from head to tail body: Vec, + /// Coordinates for this Battlesnake's head. Equivalent to the first element of the body array. head: Coord, + /// Length of this Battlesnake from head to tail. Equivalent to the length of the body array. 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, + /// Message shouted by this Battlesnake on the previous turn. shout: Option, + /// 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)]