clean up file
This commit is contained in:
parent
7c82aff384
commit
a4a3cc90f0
1 changed files with 9 additions and 3 deletions
12
compress.py
12
compress.py
|
@ -3,12 +3,12 @@ from enum import Enum
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
import more_itertools
|
import more_itertools
|
||||||
|
|
||||||
BASE62 = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
||||||
|
|
||||||
|
BASE62 = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
COLORS = 'rygbp'
|
COLORS = 'rygbp'
|
||||||
|
|
||||||
|
|
||||||
|
# Some setup for conversion between variant id and name
|
||||||
with open("variants.json", 'r') as f:
|
with open("variants.json", 'r') as f:
|
||||||
VARIANTS = json.loads(f.read())
|
VARIANTS = json.loads(f.read())
|
||||||
|
|
||||||
|
@ -18,11 +18,13 @@ def variant_id(variant_name):
|
||||||
def variant_name(variant_id):
|
def variant_name(variant_id):
|
||||||
return next(var['name'] for var in VARIANTS if var['id'] == variant_id)
|
return next(var['name'] for var in VARIANTS if var['id'] == variant_id)
|
||||||
|
|
||||||
## Helper method, iterate over chunks of length n in a string
|
|
||||||
|
# Helper method, iterate over chunks of length n in a string
|
||||||
def chunks(s: str, n: int):
|
def chunks(s: str, n: int):
|
||||||
for i in range(0, len(s), n):
|
for i in range(0, len(s), n):
|
||||||
yield s[i:i+n]
|
yield s[i:i+n]
|
||||||
|
|
||||||
|
|
||||||
class DeckCard():
|
class DeckCard():
|
||||||
def __init__(self, suitIndex: int, rank: int):
|
def __init__(self, suitIndex: int, rank: int):
|
||||||
self.suitIndex: int = suitIndex
|
self.suitIndex: int = suitIndex
|
||||||
|
@ -46,6 +48,7 @@ class ActionType(Enum):
|
||||||
RankClue = 3
|
RankClue = 3
|
||||||
EndGame = 4
|
EndGame = 4
|
||||||
|
|
||||||
|
|
||||||
class Action():
|
class Action():
|
||||||
def __init__(self, type_: ActionType, target: int, value: Optional[int] = None):
|
def __init__(self, type_: ActionType, target: int, value: Optional[int] = None):
|
||||||
self.type = type_
|
self.type = type_
|
||||||
|
@ -75,6 +78,7 @@ class Action():
|
||||||
return "Player {} ends the game (code {})".format(self.target, self.value)
|
return "Player {} ends the game (code {})".format(self.target, self.value)
|
||||||
return "Undefined"
|
return "Undefined"
|
||||||
|
|
||||||
|
|
||||||
def compress_actions(actions: List[Action]) -> str:
|
def compress_actions(actions: List[Action]) -> str:
|
||||||
minType = 0
|
minType = 0
|
||||||
maxType = 0
|
maxType = 0
|
||||||
|
@ -92,6 +96,7 @@ def compress_actions(actions: List[Action]) -> str:
|
||||||
out += ''.join(map(compress_action, actions))
|
out += ''.join(map(compress_action, actions))
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
def decompress_actions(actions_str: str) -> List[Action]:
|
def decompress_actions(actions_str: str) -> List[Action]:
|
||||||
try:
|
try:
|
||||||
minType = int(actions_str[0])
|
minType = int(actions_str[0])
|
||||||
|
@ -173,6 +178,7 @@ def compressJSONGame(game_json: dict) -> str:
|
||||||
out += str(variant_id(variant))
|
out += str(variant_id(variant))
|
||||||
return ''.join(more_itertools.intersperse("-", out, 20))
|
return ''.join(more_itertools.intersperse("-", out, 20))
|
||||||
|
|
||||||
|
|
||||||
def decompressJSONGame(game_str: str)->dict:
|
def decompressJSONGame(game_str: str)->dict:
|
||||||
game = {}
|
game = {}
|
||||||
game_str = game_str.replace("-", "")
|
game_str = game_str.replace("-", "")
|
||||||
|
|
Loading…
Reference in a new issue