Skip to content

Commit

Permalink
Upgrade WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
asaf-kali committed Jan 6, 2024
1 parent 11c43a8 commit adc03e4
Show file tree
Hide file tree
Showing 15 changed files with 306 additions and 33 deletions.
72 changes: 72 additions & 0 deletions playground/cheaters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import logging
import random

from codenames.game.color import TeamColor
from codenames.game.exceptions import QuitGame
from codenames.game.player import GamePlayers
from codenames.game.runner import GameRunner
from codenames.online.codenames_game.runner import CodenamesGameRunner

from playground.boards.english import * # noqa
from playground.boards.hebrew import * # noqa
from playground.printer import print_results
from solvers.cheater.cheaters import CheaterGuesser, CheaterHinter
from solvers.cli import CLIGuesser # noqa
from solvers.models import ( # noqa
DEFAULT_MODEL_ADAPTER,
HEBREW_SUFFIX_ADAPTER,
IS_STEMMED_ENV_KEY,
MODEL_NAME_ENV_KEY,
ModelIdentifier,
load_language_async,
load_model_async,
)
from solvers.naive import NaiveGuesser, NaiveHinter # noqa
from solvers.other.naive_cli_guesser import ModelAwareCliGuesser # noqa

random.seed(42)
log = logging.getLogger(__name__)


def get_cheaters() -> GamePlayers:
blue_hinter = CheaterHinter(name="Yoda", team_color=TeamColor.BLUE)
red_hinter = CheaterHinter(name="Einstein", team_color=TeamColor.RED)
blue_guesser = CheaterGuesser(name="Anakin", team_color=TeamColor.BLUE, hinter=blue_hinter)
red_guesser = CheaterGuesser(name="Newton", team_color=TeamColor.RED, hinter=red_hinter)
return GamePlayers.from_collection([blue_hinter, blue_guesser, red_hinter, red_guesser])


def run_offline(board: Board = ENGLISH_BOARDS[2]): # noqa: F405
log.info("Running cheaters game...")
log.setLevel(logging.INFO)
game_runner = None
try:
players = get_cheaters()
game_runner = GameRunner(players=players, board=board)
game_runner.run_game()
except QuitGame:
log.info("Game quit")
except: # noqa
log.exception("Error occurred")
finally:
print_results(game_runner) # type: ignore


def run_online():
log.info("Running online game...")
online_manager = runner = None
try:
players = get_cheaters()
online_manager = CodenamesGameRunner(*players.hinters, *players.guessers, show_host=True)
runner = online_manager.auto_start()
except QuitGame:
log.info("Game quit")
except: # noqa
log.exception("Error occurred")
finally:
print_results(runner)
online_manager.close()


if __name__ == "__main__":
run_online()
21 changes: 15 additions & 6 deletions playground/offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import os
import random

from codenames.game.color import TeamColor
from codenames.game.exceptions import QuitGame
from codenames.game.player import GamePlayers
from codenames.game.runner import GameRunner

from playground.boards.english import * # noqa
Expand Down Expand Up @@ -44,18 +46,25 @@ def run_offline(board: Board = ENGLISH_BOARDS[2]): # noqa: F405
game_runner = None
try:
# blue_hinter = GPTHinter(name="Yoda", api_key=GPT_API_KEY)
blue_hinter = NaiveHinter(name="Yoda", model_adapter=adapter, max_group_size=4)
red_hinter = NaiveHinter(name="Einstein", model_identifier=model_id, model_adapter=adapter, max_group_size=3)
blue_hinter = NaiveHinter(name="Yoda", team_color=TeamColor.BLUE, model_adapter=adapter, max_group_size=4)
red_hinter = NaiveHinter(
name="Einstein",
team_color=TeamColor.RED,
model_identifier=model_id,
model_adapter=adapter,
max_group_size=3,
)
# red_hinter = GPTHinter(name="Einstein", api_key=GPT_API_KEY)
# red_hinter = OlympicHinter(name="Yoda", model_adapter=adapter)
blue_guesser = CLIGuesser(name="Anakin")
blue_guesser = CLIGuesser(name="Anakin", team_color=TeamColor.BLUE)
# blue_guesser = GPTGuesser(name="Anakin", api_key=GPT_API_KEY)
# blue_guesser = NaiveGuesser(name="Anakin", model_identifier=model_id, model_adapter=adapter)
red_guesser = CLIGuesser(name="Newton")
red_guesser = CLIGuesser(name="Newton", team_color=TeamColor.RED)
# red_guesser = GPTGuesser(name="Newton", api_key=GPT_API_KEY)
# red_guesser = NaiveGuesser(name="Newton", model_identifier=model_id, model_adapter=adapter)
game_runner = GameRunner(blue_hinter, red_hinter, blue_guesser, red_guesser)
game_runner.run_game(board=board)
players = GamePlayers.from_collection([blue_hinter, blue_guesser, red_hinter, red_guesser])
game_runner = GameRunner(players=players, board=board)
game_runner.run_game()
except QuitGame:
log.info("Game quit")
except: # noqa
Expand Down
23 changes: 16 additions & 7 deletions playground/online_codenames_game.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os

from codenames.game.color import TeamColor
from codenames.game.exceptions import QuitGame
from codenames.online.codenames_game.adapter import CodenamesGameLanguage
from codenames.online.codenames_game.runner import CodenamesGameRunner
Expand Down Expand Up @@ -36,23 +37,31 @@

def run_online():
log.info("Running online game...")
online_manager = None
online_manager = runner = None
try:
# blue_hinter = GPTHinter(name="Einstein", api_key=GPT_API_KEY)
blue_hinter = NaiveHinter("Einstein", model_identifier=model_id, model_adapter=adapter) # noqa
red_hinter = NaiveHinter(name="Yoda", model_identifier=model_id, model_adapter=adapter) # noqa
blue_guesser = NaiveGuesser(name="Newton", model_identifier=model_id, model_adapter=adapter) # noqa
blue_hinter = NaiveHinter(
"Einstein", team_color=TeamColor.BLUE, model_identifier=model_id, model_adapter=adapter
)
red_hinter = NaiveHinter(
name="Yoda", team_color=TeamColor.RED, model_identifier=model_id, model_adapter=adapter
)
blue_guesser = NaiveGuesser(
name="Newton", team_color=TeamColor.BLUE, model_identifier=model_id, model_adapter=adapter
)
# red_guesser = GPTGuesser(name="Anakin", api_key=GPT_API_KEY)
red_guesser = NaiveGuesser(name="Anakin", model_identifier=model_id, model_adapter=adapter) # noqa
red_guesser = NaiveGuesser(
name="Anakin", team_color=TeamColor.RED, model_identifier=model_id, model_adapter=adapter
)
online_manager = CodenamesGameRunner(blue_hinter, red_hinter, blue_guesser, red_guesser, show_host=True)
# online_manager = CodenamesGameGameRunner(blue_hinter, red_hinter, blue_guesser, red_guesser, show_host=False)
online_manager.auto_start()
runner = online_manager.auto_start()
except QuitGame:
log.info("Game quit")
except: # noqa
log.exception("Error occurred")
finally:
print_results(online_manager.game_runner)
print_results(runner)
online_manager.close()


Expand Down
5 changes: 3 additions & 2 deletions playground/printer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import Optional

from codenames.game.move import GuessMove, HintMove, Move, PassMove
from codenames.game.player import Player, PlayerRole
Expand All @@ -8,7 +9,7 @@
log = logging.getLogger(__name__)


def print_results(game_runner: GameRunner):
def print_results(game_runner: Optional[GameRunner]):
if game_runner is None or game_runner.state is None:
return
state = game_runner.state
Expand Down Expand Up @@ -42,7 +43,7 @@ def _print_moves(game_runner: GameRunner):

def _get_player(game_runner: GameRunner, move: Move) -> Player:
role = PlayerRole.HINTER if isinstance(move, HintMove) else PlayerRole.GUESSER
return game_runner.get_player(team_color=move.team_color, role=role)
return game_runner.players.get_player(team_color=move.team_color, role=role)


def _print_result(state: GameState):
Expand Down
121 changes: 120 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ openai = { version = "^0.27", optional = true }
[tool.poetry.group.test.dependencies]
pytest = "7.2.2"
coverage = "7.2.3"
codenames = { extras = ["web"], version = "^4.0.3" }

[tool.poetry.group.lint.dependencies]
ruff = "^0.0.269"
Expand Down
Empty file added solvers/cheater/__init__.py
Empty file.
Loading

0 comments on commit adc03e4

Please sign in to comment.