Skip to content

Commit

Permalink
Alter gateway, model tests, browser, tinybrain
Browse files Browse the repository at this point in the history
  • Loading branch information
Terkwood committed Oct 30, 2020
1 parent 29163ac commit fc05599
Show file tree
Hide file tree
Showing 19 changed files with 86 additions and 165 deletions.
2 changes: 1 addition & 1 deletion botlink/bot-model/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ mod tests {
let json = serde_json::to_string(&input).expect("to_string");
assert!(json.contains("gameId"));
assert!(json.contains("boardSize"));
assert!(json.contains("botMode"));
assert!(json.contains("bot"));
}
}
20 changes: 15 additions & 5 deletions botlink/bot-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@ pub enum Bot {
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AlphaNumCoord(pub char, pub u16);

#[test]
fn test_difficulty_json() {
let input = Bot::KataGoInstant;
let json = serde_json::to_string(&input).expect("to_string");
assert_eq!(json, "\"KataGoInstant\"")
#[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\"")
}
}
4 changes: 2 additions & 2 deletions botlink/src/max_visits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use bot_model::Bot;
/// Lower than 2 and you'll see an error
const INSTANT: u16 = 2;

pub fn convert(difficulty: Bot) -> Option<u16> {
match difficulty {
pub fn convert(bot: Bot) -> Option<u16> {
match bot {
Bot::KataGoInstant => Some(INSTANT),
Bot::KataGoFullStrength => None,
}
Expand Down
53 changes: 0 additions & 53 deletions botlink/src/repo/dead_ab.rs

This file was deleted.

24 changes: 12 additions & 12 deletions botlink/src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,30 @@ fn process(event: &StreamInput, opts: &mut StreamOpts) {

fn process_attach_bot(ab: &AttachBot, opts: &mut StreamOpts) {
use bot_model::api::BotAttached;
let mut game_state = move_model::GameState {
game_id: core_model::GameId(ab.game_id.0),
captures: move_model::Captures::default(),
turn: 1,
moves: vec![],
board: move_model::Board::default(),
player_up: move_model::Player::BLACK,
};

if let Err(e) = opts.attachment_repo.put(&Attachment {
game_id: ab.game_id.clone(),
player: ab.player,
bot: ab.bot,
}) {
error!("Error attaching bot {:?}", e)
} else if let Err(e) = opts.board_size_repo.put(&ab.game_id, game_state.board.size) {
error!("Failed to write board size {:?}", e)
} else {
info!("Stream: Set up game state for attach bot {:?}", ab);
let mut game_state = move_model::GameState {
game_id: core_model::GameId(ab.game_id.0),
captures: move_model::Captures::default(),
turn: 1,
moves: vec![],
board: move_model::Board::default(),
player_up: move_model::Player::BLACK,
};

if let Some(bs) = ab.board_size {
game_state.board.size = bs.into()
}

if let Err(e) = opts.board_size_repo.put(&ab.game_id, game_state.board.size) {
error!("Failed to write board size {:?}", e)
}

if let Err(e) = opts.xadd.xadd_game_state(&game_state) {
error!(
"Error writing redis stream for game state changelog : {:?}",
Expand Down
2 changes: 1 addition & 1 deletion browser/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bugout-browser",
"productName": "BUGOUT",
"version": "1.0.0-c",
"version": "1.0.0-d",
"description": "Graphical user interface for BUGOUT Go/Baduk/Weiqi",
"author": "Terkwood <[email protected]>",
"homepage": "https://github.com/Terkwood/BUGOUT",
Expand Down
12 changes: 6 additions & 6 deletions browser/src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const DrawerManager = require("./DrawerManager");
// BUGOUT 🦹🏻‍ Bundle Bloat Protector
import BoardSizeModal from "./bugout/BoardSizeModal";
import GameLobbyModal from "./bugout/WelcomeModal";
import BotDifficultyModal from "./bugout/BotDifficultyModal";
import BotModal from "./bugout/BotModal";
import IdleStatusModal from "./bugout/IdleStatusModal";
import MultiplayerColorPrefModal from "./bugout/MultiplayerColorPrefModal";
import OpponentPassedModal from "./bugout/OpponentPassedModal";
Expand Down Expand Up @@ -1337,19 +1337,19 @@ class App extends Component {
}),
appEvents: this.events,
}),
h(BotDifficultyModal, {
h(BotModal, {
data: state.multiplayer,
update: (botDifficulty) => {
update: (bot) => {
// This value is used by other modals to compute whether
// they should turn on
this.setState({
multiplayer: { ...this.state.multiplayer, botDifficulty },
multiplayer: { ...this.state.multiplayer, bot },
});

// This will be intercepted in gtp.js, which is already
// establishing backend connectivity while the user
// is busy answering the bot difficulty dialog
this.events.emit("bot-difficulty-selected", { botDifficulty });
// is busy answering the bot dialog
this.events.emit("bot-selected", { bot });
},
}),
h(WaitForOpponentModal, {
Expand Down
9 changes: 4 additions & 5 deletions browser/src/components/bugout/BoardSizeModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ const ALLOWED_ENTRY_METHODS = [
EntryMethod.PLAY_BOT,
];

const isTurnedOn = (entryMethod, botDifficulty) => {
const isTurnedOn = (entryMethod, bot) => {
let entryOk =
entryMethod !== undefined && ALLOWED_ENTRY_METHODS.includes(entryMethod);
let ifBotOk =
entryMethod === EntryMethod.PLAY_BOT ? botDifficulty !== undefined : true;
let ifBotOk = entryMethod === EntryMethod.PLAY_BOT ? bot !== undefined : true;
return entryOk && ifBotOk;
};

Expand All @@ -29,11 +28,11 @@ class BoardSizeModal extends Component {
return h("div", { id });
}

let { entryMethod, botDifficulty } = data;
let { entryMethod, bot } = data;

let { showDialog, turnedOnOnce } = this.state;

let turnOn = isTurnedOn(entryMethod, botDifficulty);
let turnOn = isTurnedOn(entryMethod, bot);

let hide = !((turnOn && !turnedOnOnce) || showDialog);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ const { h, Component } = require("preact");
// 🦹🏻‍ Bundle Bloat Protector
import Dialog from "preact-material-components/Dialog";

const {
BotDifficulty,
EntryMethod,
} = require("../../modules/multiplayer/bugout");
const { Bot, EntryMethod } = require("../../modules/multiplayer/bugout");

class BotDifficultyModal extends Component {
class BotModal extends Component {
constructor() {
super();
this.state = { showDialog: false, turnedOnOnce: false };
}

render({ id = "select-bot-difficulty", data, update }) {
render({ id = "select-bot", data, update }) {
if (data == undefined) {
return h("div", { id });
}
Expand All @@ -37,11 +34,11 @@ class BotDifficultyModal extends Component {
id,
isOpen: true,
},
h(Dialog.Header, null, "Bot Difficulty"),
h(Dialog.Header, null, "Choose Bot"),
h(
Dialog.Body,
null,
"Choose a difficulty level for the AI. Easier bots take less time to move."
"KataGo: Full Strength takes significantly longer to compute moves."
),
h(
Dialog.Footer,
Expand All @@ -52,10 +49,10 @@ class BotDifficultyModal extends Component {
accept: true,
onClick: () => {
this.setState({ showDialog: false, turnedOnOnce: true });
update(BotDifficulty.EASY);
update(Bot.KATAGO_INSTANT);
},
},
"Easy 🍼"
"KataGo: Instant Play 🐇"
)
),
h(
Expand All @@ -67,44 +64,14 @@ class BotDifficultyModal extends Component {
accept: true,
onClick: () => {
this.setState({ showDialog: false, turnedOnOnce: true });
update(BotDifficulty.MEDIUM);
update(Bot.KATAGO_FULL_STRENGTH);
},
},
"Medium 🤓"
)
),
h(
Dialog.Footer,
null,
h(
Dialog.FooterButton,
{
accept: true,
onClick: () => {
this.setState({ showDialog: false, turnedOnOnce: true });
update(BotDifficulty.HARD);
},
},
"Hard 😈"
)
),
h(
Dialog.Footer,
null,
h(
Dialog.FooterButton,
{
accept: true,
onClick: () => {
this.setState({ showDialog: false, turnedOnOnce: true });
update(BotDifficulty.MAX);
},
},
"Max 👹"
"KataGo: Full Strength 👹"
)
)
);
}
}

export default BotDifficultyModal;
export default BotModal;
9 changes: 4 additions & 5 deletions browser/src/components/bugout/PlayBotColorSelectionModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ class PlayBotColorSelectionModal extends Component {
return h("div", { id });
}

let { entryMethod, botDifficulty } = data;
let { entryMethod, bot } = data;

let turnOn =
entryMethod && entryMethod == EntryMethod.PLAY_BOT && botDifficulty;
let turnOn = entryMethod && entryMethod == EntryMethod.PLAY_BOT && bot;

let { showDialog, turnedOnOnce } = this.state;

Expand All @@ -46,7 +45,7 @@ class PlayBotColorSelectionModal extends Component {
accept: true,
onClick: () => {
this.setState({ showDialog: false, turnedOnOnce: true });
sabaki.events.emit("play-bot-color-selected", {
sabaki.events.emit("human-color-selected", {
humanColor: ColorPref.BLACK,
});
},
Expand All @@ -63,7 +62,7 @@ class PlayBotColorSelectionModal extends Component {
cancel: true,
onClick: () => {
this.setState({ showDialog: false, turnedOnOnce: true });
sabaki.events.emit("play-bot-color-selected", {
sabaki.events.emit("human-color-selected", {
humanColor: ColorPref.WHITE,
});
},
Expand Down
12 changes: 5 additions & 7 deletions browser/src/modules/multiplayer/bugout.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,9 @@ const Player = {
WHITE: "WHITE",
};

const BotDifficulty = {
EASY: "Easy",
MEDIUM: "Medium",
HARD: "Hard",
MAX: "Max",
const Bot = {
KATAGO_INSTANT: "KataGoInstant",
KATAGO_FULL_STRENGTH: "KataGoFullStrength",
};

/** private to isValidGameId */
Expand Down Expand Up @@ -235,7 +233,7 @@ const load = () => {
}
});

app.events.on("play-bot-color-selected", ({ humanColor }) => {
app.events.on("human-color-selected", ({ humanColor }) => {
if (humanColor[0].toUpperCase() === "W") {
app.generateMove({ firstMove: true });
}
Expand All @@ -260,4 +258,4 @@ exports.EntryMethod = EntryMethod;
exports.Player = Player;
exports.IdleStatus = IdleStatus;
exports.BoardSize = BoardSize;
exports.BotDifficulty = BotDifficulty;
exports.Bot = Bot;
Loading

0 comments on commit fc05599

Please sign in to comment.