2023-07-04 18:53:18 +02:00
|
|
|
from typing import List, Union
|
2023-03-18 01:19:04 +01:00
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
import more_itertools
|
2023-03-02 20:16:26 +01:00
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
from hanabi import hanab_game
|
|
|
|
from hanabi.live import hanab_live
|
2023-03-02 20:16:26 +01:00
|
|
|
|
2023-03-18 01:19:04 +01:00
|
|
|
# use same BASE62 as on hanab.live to encode decks
|
2023-07-04 20:06:06 +02:00
|
|
|
BASE62 = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
2023-03-02 20:16:26 +01:00
|
|
|
|
|
|
|
|
2023-03-02 21:28:37 +01:00
|
|
|
# Helper method, iterate over chunks of length n in a string
|
2023-03-02 20:38:48 +01:00
|
|
|
def chunks(s: str, n: int):
|
|
|
|
for i in range(0, len(s), n):
|
2023-07-04 20:06:06 +02:00
|
|
|
yield s[i:i + n]
|
2023-03-02 20:38:48 +01:00
|
|
|
|
2023-03-02 21:28:37 +01:00
|
|
|
|
2023-03-18 01:19:04 +01:00
|
|
|
# exception thrown by decompression methods if parsing fails
|
|
|
|
class InvalidFormatError(ValueError):
|
2023-07-04 20:06:06 +02:00
|
|
|
pass
|
2023-03-02 20:16:26 +01:00
|
|
|
|
2023-03-02 21:28:37 +01:00
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
def compress_actions(actions: List[hanab_game.Action]) -> str:
|
|
|
|
min_type = 0
|
|
|
|
max_type = 0
|
2023-03-02 20:16:26 +01:00
|
|
|
if len(actions) != 0:
|
2023-07-04 20:06:06 +02:00
|
|
|
min_type = min(map(lambda a: a.type.value, actions))
|
|
|
|
max_type = max(map(lambda a: a.type.value, actions))
|
|
|
|
type_range = max_type - min_type + 1
|
2023-03-18 01:19:04 +01:00
|
|
|
|
2023-03-02 20:16:26 +01:00
|
|
|
def compress_action(action):
|
2023-07-04 20:06:06 +02:00
|
|
|
# We encode action values with +1 to differentiate
|
2023-03-03 11:17:31 +01:00
|
|
|
# null (encoded 0) and 0 (encoded 1)
|
|
|
|
value = 0 if action.value is None else action.value + 1
|
2023-07-04 20:06:06 +02:00
|
|
|
if action.type == hanab_game.ActionType.VoteTerminate:
|
|
|
|
# This is currently a hack, the actual format has a 10 here,
|
2023-03-18 01:19:04 +01:00
|
|
|
# but we cannot encode this
|
2023-03-14 09:12:46 +01:00
|
|
|
value = 0
|
2023-03-18 01:19:04 +01:00
|
|
|
try:
|
2023-07-04 20:06:06 +02:00
|
|
|
a = BASE62[type_range * value + (action.type.value - min_type)]
|
2023-03-18 01:19:04 +01:00
|
|
|
b = BASE62[action.target]
|
|
|
|
except IndexError as e:
|
|
|
|
raise ValueError("Encoding action failed, value too large, found {}".format(value)) from e
|
2023-03-02 20:16:26 +01:00
|
|
|
return a + b
|
2023-03-18 01:19:04 +01:00
|
|
|
|
|
|
|
return "{}{}{}".format(
|
2023-07-04 20:06:06 +02:00
|
|
|
min_type,
|
|
|
|
max_type,
|
|
|
|
''.join(map(compress_action, actions))
|
2023-03-18 01:19:04 +01:00
|
|
|
)
|
2023-03-02 20:16:26 +01:00
|
|
|
|
2023-03-02 21:28:37 +01:00
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
def decompress_actions(actions_str: str) -> List[hanab_game.Action]:
|
2023-03-18 01:27:32 +01:00
|
|
|
if not len(actions_str) >= 2:
|
2023-03-18 01:19:04 +01:00
|
|
|
raise InvalidFormatError("min/max range not specified, found: {}".format(actions_str))
|
2023-03-02 20:38:48 +01:00
|
|
|
try:
|
2023-07-04 20:06:06 +02:00
|
|
|
min_type = int(actions_str[0])
|
|
|
|
max_type = int(actions_str[1])
|
2023-03-18 01:19:04 +01:00
|
|
|
except ValueError as e:
|
|
|
|
raise InvalidFormatError(
|
2023-07-04 20:06:06 +02:00
|
|
|
"min/max range of actions not specified, expected two integers, found {}".format(actions_str[:2])
|
2023-03-18 01:19:04 +01:00
|
|
|
) from e
|
2023-07-04 20:06:06 +02:00
|
|
|
if not min_type <= max_type:
|
|
|
|
raise InvalidFormatError("min/max range illegal, found [{},{}]".format(min_type, max_type))
|
|
|
|
type_range = max_type - min_type + 1
|
2023-03-18 01:19:04 +01:00
|
|
|
|
|
|
|
if not len(actions_str) % 2 == 0:
|
|
|
|
raise InvalidFormatError("Invalid action string length: Expected even number of characters")
|
|
|
|
|
|
|
|
for (index, char) in enumerate(actions_str[2:]):
|
2023-07-04 20:06:06 +02:00
|
|
|
if char not in BASE62:
|
2023-03-18 01:19:04 +01:00
|
|
|
raise InvalidFormatError(
|
2023-07-04 20:06:06 +02:00
|
|
|
"Invalid character at index {}: Found {}, expected one of {}".format(
|
|
|
|
index, char, BASE62
|
|
|
|
)
|
2023-03-18 01:19:04 +01:00
|
|
|
)
|
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
def decompress_action(action_idx: int, action: str):
|
2023-03-18 01:19:04 +01:00
|
|
|
try:
|
2023-07-04 20:06:06 +02:00
|
|
|
action_type_value = (BASE62.index(action[0]) % type_range) + min_type
|
|
|
|
action_type = hanab_game.ActionType(action_type_value)
|
2023-03-18 01:19:04 +01:00
|
|
|
except ValueError as e:
|
|
|
|
raise InvalidFormatError(
|
2023-07-04 20:06:06 +02:00
|
|
|
"Invalid action type at action {}: Found {}, expected one of {}".format(
|
|
|
|
action_idx, action_type_value,
|
|
|
|
[action_type.value for action_type in hanab_game.ActionType]
|
|
|
|
)
|
2023-03-18 01:19:04 +01:00
|
|
|
) from e
|
2023-07-04 20:06:06 +02:00
|
|
|
|
|
|
|
# We encode values with +1 to differentiate null (encoded 0) and 0 (encoded 1)
|
|
|
|
value = BASE62.index(action[0]) // type_range - 1
|
2023-03-18 01:19:04 +01:00
|
|
|
if value == -1:
|
|
|
|
value = None
|
2023-07-04 20:06:06 +02:00
|
|
|
if action_type in [hanab_game.ActionType.Play, hanab_game.ActionType.Discard]:
|
2023-03-18 01:19:04 +01:00
|
|
|
if value is not None:
|
|
|
|
raise InvalidFormatError(
|
2023-07-04 20:06:06 +02:00
|
|
|
"Invalid action value: Action at action index {} is Play/Discard, expected value None, "
|
|
|
|
"found: {}".format(action_idx, value)
|
2023-03-18 01:19:04 +01:00
|
|
|
)
|
2023-03-02 20:38:48 +01:00
|
|
|
target = BASE62.index(action[1])
|
2023-07-04 20:06:06 +02:00
|
|
|
return hanab_game.Action(action_type, target, value)
|
2023-03-18 01:19:04 +01:00
|
|
|
|
|
|
|
return [decompress_action(idx, a) for (idx, a) in enumerate(chunks(actions_str[2:], 2))]
|
2023-03-02 20:38:48 +01:00
|
|
|
|
2023-03-02 20:16:26 +01:00
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
def compress_deck(deck: List[hanab_game.DeckCard]) -> str:
|
|
|
|
assert (len(deck) != 0)
|
|
|
|
min_rank = min(map(lambda card: card.rank, deck))
|
|
|
|
max_rank = max(map(lambda card: card.rank, deck))
|
|
|
|
rank_range = max_rank - min_rank + 1
|
2023-03-18 01:19:04 +01:00
|
|
|
|
2023-03-02 20:16:26 +01:00
|
|
|
def compress_card(card):
|
2023-03-18 01:19:04 +01:00
|
|
|
try:
|
2023-07-04 20:06:06 +02:00
|
|
|
return BASE62[rank_range * card.suitIndex + (card.rank - min_rank)]
|
2023-03-18 01:19:04 +01:00
|
|
|
except IndexError as e:
|
|
|
|
raise InvalidFormatError(
|
2023-07-04 20:06:06 +02:00
|
|
|
"Could not compress card, suit or rank too large. Found: {}".format(card)
|
2023-03-18 01:19:04 +01:00
|
|
|
) from e
|
2023-07-04 20:06:06 +02:00
|
|
|
|
2023-03-18 01:19:04 +01:00
|
|
|
return "{}{}{}".format(
|
2023-07-04 20:06:06 +02:00
|
|
|
min_rank,
|
|
|
|
max_rank,
|
|
|
|
''.join(map(compress_card, deck))
|
2023-03-18 01:19:04 +01:00
|
|
|
)
|
2023-03-02 20:16:26 +01:00
|
|
|
|
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
def decompress_deck(deck_str: str) -> List[hanab_game.DeckCard]:
|
2023-03-18 01:19:04 +01:00
|
|
|
if len(deck_str) < 2:
|
|
|
|
raise InvalidFormatError("min/max rank range not specified, found: {}".format(deck_str))
|
|
|
|
try:
|
2023-07-04 20:06:06 +02:00
|
|
|
min_rank = int(deck_str[0])
|
|
|
|
max_rank = int(deck_str[1])
|
2023-03-18 01:19:04 +01:00
|
|
|
except ValueError as e:
|
|
|
|
raise InvalidFormatError(
|
2023-07-04 20:06:06 +02:00
|
|
|
"min/max rank range not specified, expected two integers, found {}".format(deck_str[:2])
|
2023-03-18 01:19:04 +01:00
|
|
|
) from e
|
2023-07-04 20:06:06 +02:00
|
|
|
if not max_rank >= min_rank:
|
2023-03-18 01:19:04 +01:00
|
|
|
raise InvalidFormatError(
|
2023-07-04 20:06:06 +02:00
|
|
|
"Invalid rank range, found [{},{}]".format(min_rank, max_rank)
|
2023-03-18 01:19:04 +01:00
|
|
|
)
|
2023-07-04 20:06:06 +02:00
|
|
|
rank_range = max_rank - min_rank + 1
|
2023-03-18 01:19:04 +01:00
|
|
|
|
|
|
|
for (index, char) in enumerate(deck_str[2:]):
|
2023-07-04 20:06:06 +02:00
|
|
|
if char not in BASE62:
|
2023-03-18 01:19:04 +01:00
|
|
|
raise InvalidFormatError(
|
2023-07-04 20:06:06 +02:00
|
|
|
"Invalid character at index {}: Found {}, expected one of {}".format(
|
|
|
|
index, char, BASE62
|
|
|
|
)
|
2023-03-18 01:19:04 +01:00
|
|
|
)
|
|
|
|
|
2023-03-02 20:16:26 +01:00
|
|
|
def decompress_card(card_char):
|
2023-07-04 20:06:06 +02:00
|
|
|
encoded = BASE62.index(card_char)
|
|
|
|
suit_index = encoded // rank_range
|
|
|
|
rank = encoded % rank_range + min_rank
|
|
|
|
return hanab_game.DeckCard(suit_index, rank)
|
2023-03-18 01:19:04 +01:00
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
return [decompress_card(card) for card in deck_str[2:]]
|
2023-03-02 20:16:26 +01:00
|
|
|
|
|
|
|
|
2023-03-18 01:19:04 +01:00
|
|
|
# compresses a standard GameState object into hanab.live format
|
|
|
|
# which can be used in json replay links
|
|
|
|
# The GameState object has to be standard / fitting hanab.live variants,
|
|
|
|
# otherwise compression is not possible
|
2023-07-04 20:06:06 +02:00
|
|
|
def compress_game_state(state: Union[hanab_game.GameState, hanab_live.HanabLiveGameState]) -> str:
|
|
|
|
if isinstance(state, hanab_live.HanabLiveGameState):
|
2023-05-19 12:43:49 +02:00
|
|
|
var_id = state.instance.variant_id
|
2023-05-20 14:31:43 +02:00
|
|
|
else:
|
2023-07-04 20:06:06 +02:00
|
|
|
assert isinstance(state, hanab_game.GameState)
|
|
|
|
var_id = hanab_live.HanabLiveInstance.select_standard_variant_id(state.instance)
|
2023-03-18 01:19:04 +01:00
|
|
|
out = "{}{},{},{}".format(
|
2023-07-04 20:06:06 +02:00
|
|
|
state.instance.num_players,
|
|
|
|
compress_deck(state.instance.deck),
|
|
|
|
compress_actions(state.actions),
|
|
|
|
var_id
|
|
|
|
)
|
2023-03-18 01:19:04 +01:00
|
|
|
with_dashes = ''.join(more_itertools.intersperse("-", out, 20))
|
|
|
|
return with_dashes
|
|
|
|
|
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
def decompress_game_state(game_str: str) -> hanab_game.GameState:
|
2023-03-02 21:27:00 +01:00
|
|
|
game_str = game_str.replace("-", "")
|
2023-03-18 01:19:04 +01:00
|
|
|
parts = game_str.split(",")
|
|
|
|
if not len(parts) == 3:
|
|
|
|
raise InvalidFormatError(
|
2023-07-04 20:06:06 +02:00
|
|
|
"Expected 3 comma-separated parts of game, found {}".format(
|
|
|
|
len(parts)
|
|
|
|
)
|
2023-03-18 01:19:04 +01:00
|
|
|
)
|
|
|
|
[players_deck, actions, variant_id] = parts
|
|
|
|
if len(players_deck) == 0:
|
|
|
|
raise InvalidFormatError("Expected nonempty first part")
|
|
|
|
try:
|
|
|
|
num_players = int(players_deck[0])
|
|
|
|
except ValueError as e:
|
|
|
|
raise InvalidFormatError(
|
|
|
|
"Expected number of players, found: {}".format(players_deck[0])
|
|
|
|
) from e
|
|
|
|
|
|
|
|
try:
|
|
|
|
deck = decompress_deck(players_deck[1:])
|
|
|
|
except InvalidFormatError as e:
|
|
|
|
raise InvalidFormatError("Error while parsing deck") from e
|
|
|
|
|
2023-03-02 21:27:00 +01:00
|
|
|
try:
|
2023-03-18 01:19:04 +01:00
|
|
|
actions = decompress_actions(actions)
|
|
|
|
except InvalidFormatError as e:
|
|
|
|
raise InvalidFormatError("Error while parsing actions") from e
|
|
|
|
|
|
|
|
try:
|
|
|
|
variant_id = int(variant_id)
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError("Expected variant id, found: {}".format(variant_id))
|
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
instance = hanab_game.HanabiInstance(deck, num_players)
|
|
|
|
game = hanab_game.GameState(instance)
|
|
|
|
|
2023-03-18 01:19:04 +01:00
|
|
|
# TODO: game is not in consistent state
|
|
|
|
game.actions = actions
|
2023-03-02 21:27:00 +01:00
|
|
|
return game
|
2023-03-13 17:21:25 +01:00
|
|
|
|
2023-03-18 01:19:04 +01:00
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
def link(game_state: hanab_game.GameState) -> str:
|
2023-03-18 01:19:04 +01:00
|
|
|
compressed = compress_game_state(game_state)
|
2023-05-20 14:31:43 +02:00
|
|
|
return "https://hanab.live/shared-replay-json/{}".format(compressed)
|