Skip to content

Commit

Permalink
libriichi3p
Browse files Browse the repository at this point in the history
  • Loading branch information
latorc committed Apr 15, 2024
1 parent f9c2900 commit 4920c19
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ test/
/dev_docs
/_*.py
/settings*.json

libriichi3p/*.pyd
libriichi3p/*.so
18 changes: 14 additions & 4 deletions bot/bot_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
import threading
import json
from mjai.engine import get_engine
from mjai.engine3p import get_engine as get_engine_3p
from common.utils import ModelFileException
from common.mj_helper import MJAI_TYPE
from common.log_helper import LOGGER
try:
import libriichi
except: # pylint: disable=bare-except
import riichi as libriichi
import libriichi3p

from .bot import Bot, BotType, GameMode

Expand All @@ -30,6 +28,13 @@ def __init__(self, model_files:dict[GameMode, str]) -> None:
if not Path(v).exists() or not Path(v).is_file():
LOGGER.warning("Cannot find model file for mode %s:%s", k,v)
self._supported_modes.remove(k)
if k == GameMode.MJ3P:
# test import libraries for 3p
try:
import libriichi3p
except Exception as e: # pylint: disable=bare-except
LOGGER.warning("Cannot import libriichi3p: %s", e)
self._supported_modes.remove(k)
if not self._supported_modes:
raise ModelFileException("No valid model files found")

Expand All @@ -49,8 +54,13 @@ def _init_bot_impl(self, mode:GameMode=GameMode.MJ4P):
engine = get_engine(self.model_files[mode])
self.mjai_bot = libriichi.mjai.Bot(engine, self.seat)
elif mode == GameMode.MJ3P:
engine = get_engine_3p(self.model_files[mode])
self.mjai_bot = libriichi3p.mjai.Bot(engine, self.seat)
try:
import libriichi3p
from mjai.engine3p import get_engine as get_engine_3p
engine = get_engine_3p(self.model_files[mode])
self.mjai_bot = libriichi3p.mjai.Bot(engine, self.seat)
except Exception as e:
raise e
else:
raise NotImplementedError(f"Mode {mode} not supported")
self.str_input_history.clear()
Expand Down
5 changes: 3 additions & 2 deletions libriichi3p/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import platform
import importlib.util
from common.utils import sub_file

assert sys.version_info >= (3, 10), "Python version must be 3.10 or higher"
assert sys.version_info <= (3, 12), "Python version must be 3.12 or lower"
Expand All @@ -26,9 +27,8 @@ def load_module():
raise EnvironmentError(f"Unsupported OS: {platform.system()}")

# Adjust the path to the directory where the .pyd file is stored
dir_path = os.path.dirname(__file__) # Get the directory of the current file
filename = f"libriichi3p-{python_version}-{proc_str}-{os_ext_str}"
file_path = os.path.join(dir_path, filename) # Combine with the filename
file_path = sub_file("libriichi3p", filename)
if not os.path.exists(file_path):
raise ImportError(f"Could not find file: {file_path}")

Expand All @@ -42,3 +42,4 @@ def load_module():
raise ImportError(f"Could not import: {file_path}")

libriichi3p = load_module()

2 changes: 1 addition & 1 deletion scripts/generate_exe.bat
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ REM robocopy . .\dist\MahjongCopilot settings.json
REM robocopy models dist\MahjongCopilot\models /E
robocopy resources dist\MahjongCopilot\resources /E
robocopy liqi_proto dist\MahjongCopilot\liqi_proto /E
robocopy .\libriichi3p\ dist\MahjongCopilot\_internal\libriichi3p\ *.pyd *.so /E
REM robocopy .\libriichi3p\ dist\MahjongCopilot\_internal\libriichi3p\ *.pyd *.so /E
robocopy .venv\Lib\site-packages\playwright\driver\package\.local-browsers dist\MahjongCopilot\_internal\playwright\driver\package\.local-browsers /E
explorer.exe dist

0 comments on commit 4920c19

Please sign in to comment.