Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finalize bot model, rename Difficulty->Bot #441

Merged
merged 2 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions botlink/Cargo.lock

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

4 changes: 3 additions & 1 deletion botlink/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 = "botlink"
version = "1.0.0-e"
version = "1.0.0-f"

[dependencies]
base64 = "0.13.0"
Expand All @@ -23,3 +23,5 @@ tokio = {version = "0.2.20", default-features = false, features = ["io-std", "io
tokio-tungstenite = "0.10.1"
tungstenite = "0.10.1"
uuid = {version = "0.8.1", features = ["v4", "serde"]}
serde_derive = "1.0.117"
serde = "1.0.117"
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, Bot};
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: Bot,
}

/// 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: Bot::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: Bot::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("bot"));
}
}
34 changes: 24 additions & 10 deletions botlink/bot-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,34 @@ 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 Bot {
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 json = serde_json::to_string(&input).expect("to_string");
assert_eq!(json, "\"Max\"")
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_kata_instant_json() {
let input = Bot::KataGoInstant;
let json = serde_json::to_string(&input).expect("to_string");
assert_eq!(json, "\"KataGoInstant\"")
}
#[test]
fn test_kata_full_json() {
let input = Bot::KataGoFullStrength;
let json = serde_json::to_string(&input).expect("to_string");
assert_eq!(json, "\"KataGoFullStrength\"")
}
}
16 changes: 6 additions & 10 deletions botlink/src/max_visits.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use bot_model::Difficulty;
use bot_model::Bot;

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

pub fn convert(difficulty: Difficulty) -> Option<u16> {
match difficulty {
Difficulty::Easy => Some(EASY),
Difficulty::Medium => Some(MEDIUM),
Difficulty::Hard => Some(HARD),
Difficulty::Max => None,
pub fn convert(bot: Bot) -> Option<u16> {
match bot {
Bot::KataGoInstant => Some(INSTANT),
Bot::KataGoFullStrength => None,
}
}
6 changes: 2 additions & 4 deletions botlink/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ use redis::Client;
use std::sync::Arc;

pub struct Components {
pub ab_repo: Box<dyn AttachedBotsRepo>,
pub board_size_repo: Arc<dyn BoardSizeRepo>,
pub difficulty_repo: Box<dyn DifficultyRepo>,
pub attachment_repo: Box<dyn AttachmentRepo>,
pub xreader: Box<dyn XReader>,
pub xadder: Arc<dyn XAdder>,
pub xack: Arc<dyn XAck>,
Expand All @@ -34,9 +33,8 @@ impl Components {
unbounded();

Components {
ab_repo: Box::new(client.clone()),
attachment_repo: Box::new(client.clone()),
board_size_repo: Arc::new(client.clone()),
difficulty_repo: Box::new(client.clone()),
xreader: Box::new(client.clone()),
xadder: Arc::new(client.clone()),
xack: Arc::new(client),
Expand Down
47 changes: 0 additions & 47 deletions botlink/src/repo/attached_bots.rs

This file was deleted.

62 changes: 62 additions & 0 deletions botlink/src/repo/attachment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use super::{expire, RepoErr};
use bot_model::Bot;
use core_model::GameId;
use move_model::Player;
use redis::{Client, Commands};
use serde_derive::{Deserialize, Serialize};
use std::sync::Arc;

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Attachment {
pub bot: Bot,
pub player: Player,
pub game_id: GameId,
}

pub trait AttachmentRepo: Send + Sync {
fn get(&self, game_id: &GameId, player: Player) -> Result<Option<Attachment>, RepoErr>;
fn put(&self, attachment: &Attachment) -> Result<(), RepoErr>;
}

impl AttachmentRepo for Arc<Client> {
fn get(&self, game_id: &GameId, player: Player) -> Result<Option<Attachment>, RepoErr> {
match self.get_connection() {
Ok(mut conn) => {
let key = attachment_id(game_id, player);
let data: Result<Option<Vec<u8>>, _> =
conn.get(&key).map_err(|e| RepoErr::Redis(e));

if data.is_ok() {
expire(&key, &mut conn)?
}

match data {
Ok(Some(bytes)) => {
let deser: Result<Attachment, _> = bincode::deserialize(&bytes);
deser.map(|d| Some(d)).map_err(|e| RepoErr::SerDes(e))
}
Ok(None) => Ok(None),
Err(e) => Err(e),
}
}
Err(e) => Err(RepoErr::Redis(e)),
}
}

fn put(&self, attachment: &Attachment) -> Result<(), RepoErr> {
let key = attachment_id(&attachment.game_id, attachment.player);
let mut conn = self.get_connection()?;
let bytes = bincode::serialize(&attachment)?;
let done = conn.set(&key, bytes).map_err(|e| RepoErr::Redis(e))?;
expire(&key, &mut conn)?;
Ok(done)
}
}

fn attachment_id(game_id: &GameId, player: Player) -> String {
format!(
"/BUGOUT/botlink/attachment/{}_{}",
game_id.0.to_string(),
player.to_string()
)
}
49 changes: 0 additions & 49 deletions botlink/src/repo/difficulty.rs

This file was deleted.

6 changes: 2 additions & 4 deletions botlink/src/repo/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
mod attached_bots;
mod attachment;
mod board_size;
mod difficulty;
mod expire;

pub use attached_bots::*;
pub use attachment::*;
pub use board_size::*;
pub use difficulty::*;
use expire::*;

#[derive(Debug)]
Expand Down
Loading