Skip to content

Commit

Permalink
Merge branch 'v8' into devel
Browse files Browse the repository at this point in the history
  • Loading branch information
anoek committed Apr 17, 2023
2 parents 0d8d5ee + fed3d40 commit d86115f
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 49 deletions.
42 changes: 29 additions & 13 deletions schema/Config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,33 +179,44 @@
19
]
},
"allow_ranked": {
"type": "boolean",
"description": "Allowed ranked games",
"default": true
},
"allow_unranked": {
"type": "boolean",
"description": "Allowed unranked games",
"default": true
},
"allowed_rank_range": {
"type": "number",
"description": "+- the number of ranks allowed to play against this bot. Note that ranked games are always limited to +-9. 0 to disable rank restrictions.",
"default": 0
"type": "array",
"items": {
"type": "number"
},
"minItems": 2,
"maxItems": 2,
"description": "Allowed range for ranked games Ranks are encoded as a number starting at 0 which represents 30k and counting up. For example: 30k = 0 29k = 1 20k = 10 10k = 20 1k = 29 1d = 30 9d = 38",
"default": [
0,
99
]
},
"allow_handicap": {
"type": "boolean",
"description": "Allow handicap games",
"default": true
},
"min_rank": {
"type": "number",
"description": "Minimum rank to accept games from",
"default": 0,
"minimum": 0,
"maximum": 35
},
"hidden": {
"type": "boolean",
"description": "Hide the bot from the public bot list",
"default": false
},
"decline_new_challenges": {
"type": "boolean",
"description": "Decline all new challenges. This implies hidden.",
"default": false
},
"showboard": {
"type": "boolean",
"description": "Used for debugging, will issue a showboard command when we've loaded the board state into the bot",
Expand All @@ -215,6 +226,11 @@
"type": "number",
"description": "If set, bot moves will be delayed when made before `min_move_time` ms. This is primarily a user experience thing as can make players feel rushed if the bots are responding too quickly.",
"default": 1500
},
"max_games_per_player": {
"type": "number",
"description": "Maximum amount of ongoing games to allow concurrently by the same player",
"default": 1
}
},
"required": [
Expand Down Expand Up @@ -247,7 +263,7 @@
},
"send_pv_data": {
"type": "boolean",
"description": "Send the principal variation (PV) values. Note that your bot must output this data in a way that can be parsed.\n\nSee `pv_format` for more details on formatting and parsing PV values .",
"description": "Send the principal variation (PV) values. Note that your bot must output this data in a way that can be parsed.",
"default": true
},
"release_delay": {
Expand Down Expand Up @@ -287,7 +303,7 @@
},
"send_pv_data": {
"type": "boolean",
"description": "Send the principal variation (PV) values. Note that your bot must output this data in a way that can be parsed.\n\nSee `pv_format` for more details on formatting and parsing PV values .",
"description": "Send the principal variation (PV) values. Note that your bot must output this data in a way that can be parsed.",
"default": true
},
"release_delay": {
Expand Down Expand Up @@ -331,7 +347,7 @@
},
"send_pv_data": {
"type": "boolean",
"description": "Send the principal variation (PV) values. Note that your bot must output this data in a way that can be parsed.\n\nSee `pv_format` for more details on formatting and parsing PV values .",
"description": "Send the principal variation (PV) values. Note that your bot must output this data in a way that can be parsed.",
"default": true
},
"release_delay": {
Expand Down
2 changes: 1 addition & 1 deletion src/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ export class Game extends EventEmitter<Events> {

// pause_control.paused comes from the human opponent, any other keys
// are system pauses, vacations, stone removal phase, weekend, etc.
if (Object.keys(pause_control).length === 1 && pause_control.paused) {
if (pause_control?.paused && Object.keys(pause_control).length === 1) {
const pause_duration_s = (Date.now() - clock.paused_since) / 1000;
this.log("Clock has beed paused for ", pause_duration_s, " seconds");
if (pause_duration_s > config.max_pause_time) {
Expand Down
102 changes: 83 additions & 19 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import * as JSON5 from "json5";
import * as yargs from "yargs";
import * as ConfigSchema from "../schema/Config.schema.json";
import { Validator } from "jsonschema";
import { EventEmitter } from "eventemitter3";

interface Events {
reloaded: () => void;
}

export const config_event_emitter = new EventEmitter<Events>();
export type BotTimeControlSystems = "fischer" | "byoyomi" | "simple";

/** Bot config */
Expand Down Expand Up @@ -105,34 +111,45 @@ export interface Config {
*/
allowed_board_sizes?: number[] | (number | "all" | "square");

/** Allowed ranked games
* @default true
*/
allow_ranked?: boolean;

/** Allowed unranked games
* @default true
*/
allow_unranked?: boolean;

/** +- the number of ranks allowed to play against this bot. Note that
* ranked games are always limited to +-9. 0 to disable rank restrictions.
* @default 0
/** Allowed range for ranked games
* Ranks are encoded as a number starting at 0 which represents 30k and
* counting up. For example:
* 30k = 0
* 29k = 1
* 20k = 10
* 10k = 20
* 1k = 29
* 1d = 30
* 9d = 38
* @default [0, 99]
*/
allowed_rank_range?: number;
allowed_rank_range?: [number, number];

/** Allow handicap games
* @default true
*/
allow_handicap?: boolean;

/** Minimum rank to accept games from
* @default 0
* @minimum 0
* @maximum 35
*/
min_rank?: number;

/** Hide the bot from the public bot list
* @default false
*/
hidden?: boolean;

/** Decline all new challenges. This implies hidden.
* @default false
*/
decline_new_challenges?: boolean;

/** Used for debugging, will issue a showboard command when we've loaded
* the board state into the bot
* @default false
Expand All @@ -147,6 +164,11 @@ export interface Config {
*/
min_move_time?: number;

/** Maximum amount of ongoing games to allow concurrently by the same player
* @default 1
*/
max_games_per_player?: number;

/**********/
/* Hidden */
/**********/
Expand Down Expand Up @@ -230,8 +252,6 @@ export interface BotConfig {
/** Send the principal variation (PV) values. Note that your bot must output this
* data in a way that can be parsed.
*
* See `pv_format` for more details on formatting and parsing PV values .
*
* @default true
*/
send_pv_data?: boolean;
Expand Down Expand Up @@ -279,7 +299,6 @@ function defaults(): Config {
return {
apikey: "",
server: "https://online-go.com",
min_rank: 0,
verbosity: 1,
max_pause_time: 300,
allowed_time_control_systems: ["fischer", "byoyomi", "simple"],
Expand All @@ -298,11 +317,14 @@ function defaults(): Config {
},

allowed_board_sizes: [9, 13, 19],
allow_ranked: true,
allow_unranked: true,
allowed_rank_range: 0,
allowed_rank_range: [0, 99],
allow_handicap: true,
hidden: false,
decline_new_challenges: false,
min_move_time: 1500,
max_games_per_player: 1,

greeting: {
en: "Hello, I am a bot. Good luck, have fun!",
Expand Down Expand Up @@ -368,9 +390,36 @@ function opening_bot_config_defaults(): Partial<BotConfig> {
return base;
}

export const config: Config = load_config_or_exit();
export let config: Config = try_load_config();

function try_load_config(): Config {
try {
return load_config_or_throw();
} catch (e) {
console.error("Error loading config file:", e.message);
process.exit(1);
}
}

let reload_debounce = null;
function reload_config(config_path: string): void {
if (reload_debounce) {
return;
}
console.info("Reloading config file ", config_path);
reload_debounce = setTimeout(() => {
reload_debounce = null;
try {
config = load_config_or_throw();
config_event_emitter.emit("reloaded");
} catch (e) {
console.error("Error loading config file:", e.message);
return;
}
}, 100);
}

function load_config_or_exit(): Config {
function load_config_or_throw(): Config {
yargs(process.argv.slice(2))
.usage("# Usage: $0 -c <config.json5> [options]")
.usage("# $0 -c <config.json5> [options] -- <bot command>")
Expand Down Expand Up @@ -418,6 +467,12 @@ function load_config_or_exit(): Config {

const filename = args.config;

if (filename) {
fs.watch(filename, () => {
reload_config(filename);
});
}

/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const contents = filename ? fs.readFileSync(filename, "utf8") : "{}";
const raw = JSON5.parse(contents);
Expand Down Expand Up @@ -488,12 +543,21 @@ function load_config_or_exit(): Config {
console.error(``);
console.error(``);

process.exit(1);
throw new Error("Invalid config file");
}
//console.info(yargs.argv);
//console.info(with_defaults);

with_defaults._config_version = 1;

return with_defaults;
return sanity_check_and_patch_config(with_defaults);
}

function sanity_check_and_patch_config(config: Config): Config {
if (config.decline_new_challenges) {
console.warn("Declining new challenges, hiding bot");
config.hidden = true;
}

return config;
}
Loading

0 comments on commit d86115f

Please sign in to comment.