|
| 1 | +# Quickstart |
| 2 | + |
| 3 | +## Installing |
| 4 | + |
| 5 | +Install the Open Mafia Engine package from PyPI, with recommended extras: |
| 6 | + |
| 7 | +```sh |
| 8 | +pip install open_mafia_engine[recommended] |
| 9 | +``` |
| 10 | + |
| 11 | +This gives you the ability to run everything, and even create your own |
| 12 | +abilities, factions, game types, etc. |
| 13 | + |
| 14 | +## Running a simple game in the console |
| 15 | + |
| 16 | +The Engine comes with a built-in "playground" where users can become familiar |
| 17 | +with how the Engine works. You can run it from the command line (it's a console |
| 18 | +application): |
| 19 | + |
| 20 | +```sh |
| 21 | +mafia-cli |
| 22 | +``` |
| 23 | + |
| 24 | +You can enter `help` for a short introduction and command overview. |
| 25 | + |
| 26 | +See the [Examples](../examples/index.md) or [Command Reference](../reference/commands.md) |
| 27 | +for more information on how this app works. |
| 28 | + |
| 29 | +## Creating your own game |
| 30 | + |
| 31 | +The most important part of the Open Mafia Engine is that you can create your own |
| 32 | +custom game, with different roles, factions, rule sets and so on. |
| 33 | + |
| 34 | +To create a game with pre-existing roles, just make a |
| 35 | +[game builder function][open_mafia_engine.core.builder.game_builder] |
| 36 | +that takes player names (and maybe some other options). |
| 37 | + |
| 38 | +```python |
| 39 | +import open_mafia_engine.api as mafia |
| 40 | + |
| 41 | +@mafia.game_builder("my_example") |
| 42 | +def make_example_game(player_names: List[str]) -> mafia.Game: |
| 43 | + # Create the game object and some core helper objects |
| 44 | + game = mafia.Game() |
| 45 | + mafia.GameEnder(game) |
| 46 | + tally = mafia.LynchTally(game) |
| 47 | + |
| 48 | + # Create the main factions |
| 49 | + t = mafia.Faction(game, "Town") |
| 50 | + mafia.OCLastFactionStanding(game, t) |
| 51 | + |
| 52 | + m = mafia.Faction(game, "Mafia") |
| 53 | + mafia.OCLastFactionStanding(game, m) |
| 54 | + mafia.FactionChatCreatorAux(game, m) |
| 55 | + |
| 56 | + |
| 57 | + # Create the Town player |
| 58 | + a0 = mafia.Actor(game, player_names[0]) |
| 59 | + t.add_actor(a0) |
| 60 | + # They can only vote |
| 61 | + vote = mafia.VoteAbility(game, a0, name="Vote", tally=tally) |
| 62 | + mafia.PhaseConstraint(game, vote, phase="day") |
| 63 | + |
| 64 | + |
| 65 | + # Create the Mafia player |
| 66 | + a1 = mafia.Actor(game, player_names[1]) |
| 67 | + m.add_actor(a1) |
| 68 | + # They can vote... |
| 69 | + vote = mafia.VoteAbility(game, a1, name="Vote", tally=tally) |
| 70 | + mafia.PhaseConstraint(game, vote, phase="day") |
| 71 | + # ... and perform the Mafia kill (which has a lot of constraints) |
| 72 | + mk = mafia.KillAbility( |
| 73 | + game, |
| 74 | + a1, |
| 75 | + name="Mafia Kill", |
| 76 | + desc="Kill the target. Only 1 mafioso can use this.", |
| 77 | + ) |
| 78 | + mafia.LimitPerPhaseActorConstraint(game, mk, limit=1) |
| 79 | + mafia.LimitPerPhaseKeyConstraint(game, mk, key="mafia_kill_limit") |
| 80 | + mafia.PhaseConstraint(game, mk, phase="night") |
| 81 | + mafia.ConstraintNoSelfFactionTarget(game, mk) |
| 82 | + |
| 83 | + return game |
| 84 | +``` |
| 85 | + |
| 86 | +Then you can create the game object: |
| 87 | + |
| 88 | +```python |
| 89 | +game = make_example_game(["Alice", "Bob"]) |
| 90 | +``` |
| 91 | + |
| 92 | +Or, if saved in a model and imported: |
| 93 | + |
| 94 | +```python |
| 95 | +builder = mafia.GameBuilder.load("my_example") |
| 96 | +game = builder.build(["Alice", "Bob"]) |
| 97 | +``` |
| 98 | + |
| 99 | +Technically, you could do this outside a function, but functions are much |
| 100 | +more reusable. 😃 |
| 101 | + |
| 102 | +## Running your game in the console app |
| 103 | + |
| 104 | +NOTE: This extra parameter doesn't work yet. |
| 105 | + |
| 106 | +Assuming you have registered your function with the game builder, the Console |
| 107 | +Application can load your Python module: |
| 108 | + |
| 109 | +```sh |
| 110 | +mafia-cli --import your_module.py |
| 111 | +``` |
| 112 | + |
| 113 | +And you can load it inside the console app by the name you gave it: |
| 114 | + |
| 115 | +```sh |
| 116 | +admin create-game my_example |
| 117 | +``` |
0 commit comments