Skip to content

Commit

Permalink
Set version, rename enum, write doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Terkwood committed Oct 30, 2020
1 parent 5f9e2dc commit 567e3f0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 31 deletions.
2 changes: 1 addition & 1 deletion botlink/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion botlink/bot-model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["Terkwood <[email protected]>"]
edition = "2018"
name = "bot-model"
version = "0.2.0"
version = "1.0.0"

[dependencies]
core-model = {git = "https://github.com/Terkwood/BUGOUT", rev = "20e6620"}
Expand Down
10 changes: 5 additions & 5 deletions botlink/bot-model/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{AlphaNumCoord, Difficulty};
use super::{AlphaNumCoord, BotMode};
use core_model::GameId;
use move_model::{GameState, Player};
use serde_derive::{Deserialize, Serialize};
Expand Down Expand Up @@ -26,7 +26,7 @@ pub struct AttachBot {
pub game_id: GameId,
pub player: Player,
pub board_size: Option<u8>,
pub difficulty: Difficulty,
pub bot_mode: BotMode,
}

/// This reply is sent once a bot is listening
Expand All @@ -49,7 +49,7 @@ mod tests {
game_id: GameId(Uuid::nil()),
player: Player::BLACK,
board_size: Some(9),
difficulty: Difficulty::Max,
bot_mode: BotMode::KataGoInstant,
};
let json = serde_json::to_string(&expected).expect("to_string");
let actual: AttachBot = serde_json::from_str(&json).expect("from_str");
Expand All @@ -73,11 +73,11 @@ mod tests {
game_id: GameId(Uuid::nil()),
player: Player::BLACK,
board_size: Some(19),
difficulty: Difficulty::Easy,
bot_mode: BotMode::KataGoFullStrength,
};
let json = serde_json::to_string(&input).expect("to_string");
assert!(json.contains("gameId"));
assert!(json.contains("boardSize"));
assert!(json.contains("difficulty"));
assert!(json.contains("botMode"));
}
}
18 changes: 11 additions & 7 deletions botlink/bot-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ pub mod api;

use serde_derive::{Deserialize, Serialize};

/// This enum represents various Go-playing programs with different
/// difficulties and time constraints. v1.0 uses only KataGo, but
/// in https://github.com/Terkwood/BUGOUT/issues/322 and
/// https://github.com/Terkwood/BUGOUT/issues/440 we plan to investigate
/// using other programs to provide much easier computer-controlled
/// opponents.
#[derive(Copy, Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Ord, PartialOrd)]
pub enum Difficulty {
Easy,
Medium,
Hard,
Max,
pub enum BotMode {
KataGoInstant,
KataGoFullStrength,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AlphaNumCoord(pub char, pub u16);

#[test]
fn test_difficulty_json() {
let input = Difficulty::Max;
let input = BotMode::KataGoInstant;
let json = serde_json::to_string(&input).expect("to_string");
assert_eq!(json, "\"Max\"")
assert_eq!(json, "\"KataGoInstant\"")
}
12 changes: 6 additions & 6 deletions botlink/src/max_visits.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use bot_model::Difficulty;
use bot_model::BotMode;

/// Lower than 2 and you'll see an error
const EASY: u16 = 2;
const MEDIUM: u16 = 25;
const HARD: u16 = 100;

pub fn convert(difficulty: Difficulty) -> Option<u16> {
pub fn convert(difficulty: BotMode) -> Option<u16> {
match difficulty {
Difficulty::Easy => Some(EASY),
Difficulty::Medium => Some(MEDIUM),
Difficulty::Hard => Some(HARD),
Difficulty::Max => None,
BotMode::Easy => Some(EASY),
BotMode::Medium => Some(MEDIUM),
BotMode::Hard => Some(HARD),
BotMode::Max => None,
}
}
12 changes: 6 additions & 6 deletions botlink/src/repo/difficulty.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use super::{expire, RepoErr};
use bot_model::Difficulty;
use bot_model::BotMode;
use core_model::GameId;
use redis::{Client, Commands};
use std::sync::Arc;

pub trait DifficultyRepo: Send + Sync {
fn get(&self, game_id: &GameId) -> Result<Option<Difficulty>, RepoErr>;
fn put(&self, game_id: &GameId, difficulty: Difficulty) -> Result<(), RepoErr>;
fn get(&self, game_id: &GameId) -> Result<Option<BotMode>, RepoErr>;
fn put(&self, game_id: &GameId, difficulty: BotMode) -> Result<(), RepoErr>;
}

impl DifficultyRepo for Arc<Client> {
fn get(&self, game_id: &GameId) -> Result<Option<Difficulty>, RepoErr> {
fn get(&self, game_id: &GameId) -> Result<Option<BotMode>, RepoErr> {
match self.get_connection() {
Ok(mut conn) => {
let key = difficulty_key(game_id);
Expand All @@ -23,7 +23,7 @@ impl DifficultyRepo for Arc<Client> {

match data {
Ok(Some(bytes)) => {
let deser: Result<Difficulty, _> = bincode::deserialize(&bytes);
let deser: Result<BotMode, _> = bincode::deserialize(&bytes);
deser.map(|d| Some(d)).map_err(|e| RepoErr::SerDes(e))
}
Ok(None) => Ok(None),
Expand All @@ -34,7 +34,7 @@ impl DifficultyRepo for Arc<Client> {
}
}

fn put(&self, game_id: &GameId, difficulty: Difficulty) -> Result<(), RepoErr> {
fn put(&self, game_id: &GameId, difficulty: BotMode) -> Result<(), RepoErr> {
let key = difficulty_key(&game_id);
let mut conn = self.get_connection()?;
let bytes = bincode::serialize(&difficulty)?;
Expand Down
10 changes: 5 additions & 5 deletions botlink/src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn process_attach_bot(ab: &AttachBot, opts: &mut StreamOpts) {
error!("Failed to write board size {:?}", e)
}

if let Err(e) = opts.difficulty_repo.put(&ab.game_id, ab.difficulty) {
if let Err(e) = opts.difficulty_repo.put(&ab.game_id, ab.bot_mode) {
error!("Failed to put difficulty {:?}", e)
}

Expand Down Expand Up @@ -100,7 +100,7 @@ fn process_game_state(game_state: &GameState, opts: &mut StreamOpts) {
if let Err(e) = opts.compute_move_in.send(ComputeMove {
game_id: game_id.clone(),
game_state: game_state.clone(),
max_visits: max_visits::convert(difficulty.unwrap_or(bot_model::Difficulty::Max)),
max_visits: max_visits::convert(difficulty.unwrap_or(bot_model::BotMode::Max)),
}) {
error!("WS SEND ERROR {:?}", e)
}
Expand Down Expand Up @@ -165,11 +165,11 @@ mod tests {

struct FakeDifficultyRepo;
impl DifficultyRepo for FakeDifficultyRepo {
fn get(&self, _game_id: &GameId) -> Result<Option<Difficulty>, RepoErr> {
fn get(&self, _game_id: &GameId) -> Result<Option<BotMode>, RepoErr> {
Ok(None)
}

fn put(&self, _game_id: &GameId, _difficulty: Difficulty) -> Result<(), RepoErr> {
fn put(&self, _game_id: &GameId, _difficulty: BotMode) -> Result<(), RepoErr> {
Ok(())
}
}
Expand Down Expand Up @@ -255,7 +255,7 @@ mod tests {
game_id: GAME_ID.clone(),
player,
board_size,
difficulty: Difficulty::Max,
bot_mode: BotMode::Max,
}),
)]),
});
Expand Down

0 comments on commit 567e3f0

Please sign in to comment.