Simulation

simulation.*

class ts2.simulation.Simulation(options, trackItems, routes, trainTypes, services, trns, messageLogger)[source]

Bases: PyQt5.QtCore.QObject

The Simulation class holds all the game logic.

__init__(options, trackItems, routes, trainTypes, services, trns, messageLogger)[source]
Parameters:
  • options
  • trackItems
  • routes
  • trainTypes
  • services
  • trns
  • messageLogger
activateRoute(siId, persistent=False, force=False)[source]

This slot is normally connected to a SignalItem signalSelected signal, which itself is emitted when a signal is left-clicked.

It is in charge of:

  • Checking whether this is the first signal to be selected, if it the case, _selectedSignal is set to this signal and the function returns.
  • Otherwise, it checks whether there exists a possible route between `_selectedSignal` and this signal. If it is the case, and that no other active route conflicts with this route, it is activated.

The following signals are emitted depending of the situation:

  • routeActivated
  • noRouteBetweenSignals
  • conflictingRoute
Parameters:siId (str) – ID of the

SignalItem owner of the SignalGraphicItem that has been left-clicked.

addTrain(train)[source]

Adds a train to the trains list.

Parameters:train – The train instance to add to the list
Returns:Checks that all TrackItem‘s are

linked together :rtype: bool

conflictingRoute

pyqtSignal(Route)

context
Returns:the context of this Simulation object
Return type:GAME

Find the items that are linked together through their coordinates and populate the _nextItem and _previousItem variables of each items.

currentTime
Returns:the current sim time
Return type:QtCore.QTime
desactivateRoute(siId)[source]

This slot is normally connected to the SignalItem‘s signalUnSelected, which itself is emitted when a signal is right-clicked. It is in charge of deactivating the routes starting from this signal.

Parameters:siId – The ID of the signalItem owner of the signalGraphicsItem that has been right-clicked.
static distanceBetween(p1, p2)[source]

Calculates the distance between both points p1 and p2 in pixels @param p1 @param p2 @return

findRoute(si1, si2)[source]

Checks whether a route exists between two signals.

Parameters:si1 – The SignalItem of

the first signal :param si2: The SignalItem of the second signal :return: The route between signal si1 and si2 if it exists, otherwise None :rtype: Route or None

for_json()[source]

Dumps the simulation to JSON.

getLineItem(placeCode, trackCode)[source]
Parameters:
  • placeCode
  • trackCode
Returns:

the LineItem instance defined by

placeCode and trackCode.

initialize(simulationWindow)[source]

Initializes the simulation.

Parameters:simulationWindow
messageLogger
Returns:the message logger of the simulation.
Return type:MessageLogger
noRouteBetweenSignals

pyqtSignal(SignalItem, SignalItem)

option(key)[source]
Parameters:key (str) –
Returns:the simulation option specified by key
Type:mixed
pause(paused=True)[source]

Toggle pause.

Parameters:paused – If paused is True pause the game, else continue.
place(placeCode)[source]
Parameters:placeCode (str) –
Returns:a place defined by placeCode.
Return type:Place or None
places
Returns:places dictionary
Return type:dict of Place‘s
routes
Returns:the routes of the simulation
Type:Route
saveGame(fileName)[source]

Saves the game.

Parameters:fileName (str) – fileName to write
scene
Returns:the QGraphicsScene on which the simulation scenery is

displayed

scorer
Returns:the scorer instance of the simulation
Return type:Scorer
selectedServiceModel
Returns:the selected service model
Return type:ServiceInfoModel
selectedTrainModel
Returns:the trainlist model
Return type:TrainInfoModel
selectionChanged

pyqtSignal()

service(serviceCode)[source]
Parameters:serviceCode (str) –
Returns:a service defined by serviceCode.
Return type:Service or None
serviceListModel
Returns:the service model
Return type:ServiceListModel
services
Returns:services dictionary
Return type:dict of Service‘s
setTimeFactor(timeFactor)[source]
Parameters:timeFactor (int) – Sets the time factor to timeFactor.
startTime
Returns:the time at which the simulation starts.
Return type:QtCore.QTime
timeChanged

pyqtSignal(QtCore.QTime)

timeElapsed

pyqtSignal(float)

timerOut()[source]

Changes the simulation time and emits the timeChanged and the timeElapsed signals This function is normally connected to the timer timeout signal.

trackItem(tiId)[source]
Parameters:tiId – trackitem id
Returns:the trackItems with tiId
Return type:TrackItem or None
trackItems
Returns:the trackItems in simulation.
Return type:dict of TrackItem
trainListModel
Returns:the trainlist model
Return type:TrainListModel
trainSelected

pyqtSignal(int)

trainStatusChanged

pyqtSignal(int)

trainTypes
Returns:a dict of the TrainType of

the simulation :rtype: dict

trains

Todo

@trains property docs

updateContext(tabNum)[source]

Updates the context of the simulation. Does nothing in the base class.

updatePlaces()[source]

Updates the places dictionary from TrackItem data.

updateSelection()[source]

Updates the trackItem selection. Does nothing in the base simulation class.

ts2.simulation.json_hook(dct)[source]

Hook method for json.load().

ts2.simulation.load(simulationWindow, jsonStream)[source]

Loads the simulation from jsonStream and returns it.

The logic of loading is the following:

  1. We create the graph of objects from json.load(). When initialized, each object stores its JSON data.
  2. When all the objects are created, we call the initialize() method of the Simulation which calls in turn the initialize() method of each object.

This method will create all the missing links between the object and the simulation (and other objects).

Parameters:
  • simulationWindow
  • jsonStream
ts2.simulation.translate(str, str, str disambiguation=None, int n=-1) → str