Skip to content

Commit

Permalink
Allow closing railroads.
Browse files Browse the repository at this point in the history
If the game supports railroads closing, then allow them to be indicated
closed. Their reserved station slots will be forfeited.

As of the current supported games, a closed railroad is the same as a
removed railroad.
  • Loading branch information
Auzzy committed Aug 5, 2020
1 parent 456a5f4 commit 1c9cf69
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
5 changes: 5 additions & 0 deletions routes18xx/data/1889/game.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@
"1": "2",
"2": "3",
"3": "5"
},
"rules": {
"railroads": {
"can_close": false
}
}
}
17 changes: 13 additions & 4 deletions routes18xx/railroads.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def has_private_company(self, name):
def is_removed(self):
return True

class ClosedRailroad(RemovedRailroad):
@staticmethod
def create(name):
return ClosedRailroad(name, [])

def _split_station_entry(station_entry):
if ':' not in station_entry:
return station_entry, None
Expand Down Expand Up @@ -83,13 +88,17 @@ def load(game, board, railroads_rows):
if not info:
raise ValueError(f"Unrecognized railroad name: {name}")

trains_str = railroad_args.get("trains")
if trains_str and trains_str.lower() == "removed":
name = railroad_args["name"]
trains_str = (railroad_args.get("trains") or "").strip().lower()
if trains_str == "removed":
if not info.get("is_removable"):
raise ValueError("Attempted to remove a non-removable railroad.")

railroad = RemovedRailroad.create(name)
railroad = RemovedRailroad.create(railroad_args["name"])
elif trains_str == "closed":
if not game.rules.railroads_can_close:
raise ValueError(f"Attempted to close a railroad, although railroads cannot close in {game.name}.")

railroad = ClosedRailroad.create(railroad_args["name"])
else:
railroad_trains = trains.convert(train_info, trains_str)
railroad = Railroad.create(railroad_args["name"], railroad_trains)
Expand Down
7 changes: 5 additions & 2 deletions routes18xx/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ def load(game_json):
rules_json = game_json.get("rules", {})
return Rules(
rules_json.get("towns", {}),
rules_json.get("railroads", {}),
rules_json.get("stations", {}),
rules_json.get("privates", {}))

def __init__(self, town_rules, station_rules, privates_rules):
def __init__(self, town_rules, railroad_rules, station_rules, privates_rules):
self.towns_omit_from_limit = town_rules.get("omit_from_limit", False)

self.railroads_can_close = railroad_rules.get("can_close", True)

self.stations_reserved_until = station_rules.get("reserved_until")

self.privates_close = privates_rules.get("close")
self.privates_close = privates_rules.get("close", {})
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='routes-18xx',
version='0.7.1',
version='0.8',
author="Austin Noto-Moniz",
author_email="[email protected]",
description="Library for caluclating routes in 18xx train games.",
Expand Down

0 comments on commit 1c9cf69

Please sign in to comment.