Skip to content

Commit

Permalink
Change allowed_rank_range to take string ranks
Browse files Browse the repository at this point in the history
  • Loading branch information
anoek committed May 18, 2023
1 parent 1d5fb7a commit c5cdba5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 31 deletions.
16 changes: 7 additions & 9 deletions example_config.json5
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
*/
// release_delay: 100,


/** When a bot is to shutdown we send a `quit` command to the bot.
* This grace period is the number of milliseconds to wait for the bot to
* quit gracefully before sending a `SIGTERM` to the bot. Furthermore,
Expand All @@ -98,8 +97,8 @@

/** Bot to use for playing opening moves. This can be useful for ensuring
* your bot plays at least a few reasonable joseki moves if it is a weak
* bot.
*
* bot.
*
* The config for this bot is just like the main bot, with one additional
* option:
*
Expand Down Expand Up @@ -130,10 +129,10 @@
*
* The configuration for this bot is just like the main bot, with two additional
* options:
*
*
* `allowed_resigns`
* Which is the number of successive resigns allowed before the bot will resign.
*
*
* `moves_to_allow_before_checking_ratio`
* Which is the ratio of the board size to the number of moves
* to allow before we will start checking the ending bot for
Expand Down Expand Up @@ -284,11 +283,10 @@
*/
// allow_ranked: true,

/** +- 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
* @default ["30k", "9p"]
*/
// allowed_rank_range: 0,
//allowed_rank_range: ["30k", "9p"];

/** Allow handicap in ranked games
* @default true
Expand Down
8 changes: 4 additions & 4 deletions schema/Config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,14 @@
"allowed_rank_range": {
"type": "array",
"items": {
"type": "number"
"type": "string"
},
"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",
"description": "Allowed range for ranked games",
"default": [
0,
99
"30k",
"9p"
]
},
"allow_ranked_handicap": {
Expand Down
17 changes: 4 additions & 13 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,9 @@ export interface Config {
allow_unranked?: boolean;

/** 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, number];
* @default ["30k", "9p"]
*/
allowed_rank_range?: [string, string];

/** Allow handicap games for ranked games
* @default true
Expand Down Expand Up @@ -416,7 +407,7 @@ function defaults(): Config {
allowed_board_sizes: [9, 13, 19],
allow_ranked: true,
allow_unranked: true,
allowed_rank_range: [0, 99],
allowed_rank_range: ["30k", "9p"],
allow_ranked_handicap: true,
allow_unranked_handicap: true,
allowed_komi_range: [-99, 99],
Expand Down
26 changes: 21 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,20 +673,20 @@ class Main {
return undefined;
}
checkAllowedRank(game_is_ranked: boolean, player_rank: number): RejectionDetails | undefined {
const min_rank = rankstr_to_number(config.allowed_rank_range[0]);
const max_rank = rankstr_to_number(config.allowed_rank_range[1]);

if (!game_is_ranked) {
return;
}
if (
player_rank < config.allowed_rank_range[0] ||
player_rank > config.allowed_rank_range[1]
) {
if (player_rank < min_rank || player_rank > max_rank) {
return {
rejection_code: "player_rank_out_of_range",
details: {
allowed_rank_range: config.allowed_rank_range,
},
message:
player_rank < config.allowed_rank_range[0]
player_rank < min_rank
? `Your rank is too low to play against this bot.`
: `Your rank is too high to play against this bot.`,
};
Expand Down Expand Up @@ -744,4 +744,20 @@ function ignore() {
// do nothing
}

function rankstr_to_number(rank: string): number {
const base = parseInt(rank);
const suffix = rank[rank.length - 1].toLowerCase();

switch (suffix) {
case "p":
return 999;
case "k":
return 30 - base;
case "d":
return 29 + base;
}

throw new Error(`Invalid rank string: ${rank}`);
}

new Main();

0 comments on commit c5cdba5

Please sign in to comment.