kxg.tokens.World

class kxg.tokens.World[source]

Bases: kxg.tokens.Token

Manage all of the tokens participating in the game.

Event Handlers:

on_start_game(*args, **kwargs)

on_update_game(*args, **kwargs)

on_finish_game(*args, **kwargs)

Inherited from Token

on_add_to_world(*args, **kwargs)

on_update_game(*args, **kwargs)

on_report_to_referee(reporter)

on_remove_from_world(*args, **kwargs)

Public Methods:

__init__()

Initialize self.

__repr__()

Return repr(self).

__iter__()

__len__()

__contains__(token_or_id)

__getstate__()

__setstate__(state)

get_token(id)

Return the token with the given id.

get_last_id()

Return the largest token id registered with the world.

is_locked()

Return True if the world is currently allowed to be modified.

end_game(*args, **kwargs)

has_game_ended()

Return True if the game has ended.

Inherited from Token

__init__()

Initialize self.

__repr__()

Return repr(self).

__getstate__()

__setstate__(state)

__extend__()

has_extension(actor)

get_extension(actor)

get_extensions()

watch_method(method_name, callback)

Register the given callback to be called whenever the method with the given name is called.

Inherited from ForumObserver

namedtuple(field_names, *[, rename, …])

Returns a new subclass of tuple with named fields.

__init__()

Initialize self.

__getstate__()

__setstate__(state)

subscribe_to_message(message_cls, callback)

subscribe_to_sync_response(message_cls, callback)

subscribe_to_undo_response(message_cls, callback)

unsubscribe_from_message(message_cls[, callback])

unsubscribe_from_sync_response(message_cls)

unsubscribe_from_undo_response(message_cls)

Private Methods:

_unlock_temporarily(**kwds)

Allow tokens to modify the world for the duration of a with-block.

_add_token(token)

_remove_token(token)

_get_nested_observers()

_set_actors(actors)

Tell the world which actors are running on this machine.

Inherited from Token

_give_id(id_factory)

_check_if_forum_observation_enabled()

Give a helpful error if the user attempts to subscribe or unsubscribe from messages while the token is not registered with a world.

_add_to_world(world, actors)

_create_extensions(actors)

_remove_from_world()

Clear all the internal data the token needed while it was part of the world.

Inherited from ForumObserver

_react_to_message(message)

_react_to_sync_response(message)

_react_to_undo_response(message)

_enable_forum_observation()

_disable_forum_observation()

_check_if_forum_observation_enabled()

Give a helpful error if the user attempts to subscribe or unsubscribe from messages while the token is not registered with a world.

_add_callback(event, message_cls, callback)

_drop_callback(event, message_cls, callback)

_call_callbacks(event, message)

_get_nested_observers()

class CallbackInfo(message_cls, callback)

Bases: tuple

__add__(value, /)

Return self+value.

__contains__(key, /)

Return key in self.

__eq__(value, /)

Return self==value.

__ge__(value, /)

Return self>=value.

__getattribute__(name, /)

Return getattr(self, name).

__getitem__(key, /)

Return self[key].

__getnewargs__()

Return self as a plain tuple. Used by copy and pickle.

__gt__(value, /)

Return self>value.

__hash__()

Return hash(self).

__iter__()

Implement iter(self).

__le__(value, /)

Return self<=value.

__len__()

Return len(self).

__lt__(value, /)

Return self<value.

__mul__(value, /)

Return self*value.

__ne__(value, /)

Return self!=value.

static __new__(_cls, message_cls, callback)

Create new instance of CallbackInfo(message_cls, callback)

__repr__()

Return a nicely formatted representation string

__rmul__(value, /)

Return value*self.

__slots__ = ()
_asdict()

Return a new OrderedDict which maps field names to their values.

_fields = ('message_cls', 'callback')
_fields_defaults = {}
classmethod _make(iterable)

Make a new CallbackInfo object from a sequence or iterable

_replace(**kwds)

Return a new CallbackInfo object replacing specified fields with new values

property callback

Alias for field number 1

count(value, /)

Return number of occurrences of value.

index(value, start=0, stop=9223372036854775807, /)

Return first index of value.

Raises ValueError if the value is not present.

property message_cls

Alias for field number 0

class WatchedMethod(method)

Bases: object

__call__(*args, **kwargs)

Call self as a function.

__init__(method)

Initialize self. See help(type(self)) for accurate signature.

add_watcher(watcher)
__contains__(token_or_id)[source]
__extend__()
__getstate__()[source]
__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

__iter__()[source]
__len__()[source]
__repr__()[source]

Return repr(self).

__setstate__(state)[source]
_add_callback(event, message_cls, callback)
_add_to_world(world, actors)
_add_token(token)[source]
_call_callbacks(event, message)
_check_if_forum_observation_enabled()

Give a helpful error if the user attempts to subscribe or unsubscribe from messages while the token is not registered with a world.

This can easily happen if the user attempts to subscribe to messages in the constructor. However, because the constructor is only called on one client and message handlers cannot be pickled, subscribing at this time would create hard-to-find synchronization bugs.

_create_extensions(actors)
_disable_forum_observation()
_drop_callback(event, message_cls, callback)
_enable_forum_observation()
_get_nested_observers()[source]
_give_id(id_factory)
_react_to_message(message)
_react_to_sync_response(message)
_react_to_undo_response(message)
_remove_from_world()

Clear all the internal data the token needed while it was part of the world.

Note that this method doesn’t actually remove the token from the world. That’s what World._remove_token does. This method is just responsible for setting the internal state of the token being removed.

_remove_token(token)[source]
_set_actors(actors)[source]

Tell the world which actors are running on this machine. This information is used to create extensions for new tokens.

_unlock_temporarily(**kwds)[source]

Allow tokens to modify the world for the duration of a with-block.

It’s important that tokens only modify the world at appropriate times, otherwise the changes they make may not be communicated across the network to other clients. To help catch and prevent these kinds of errors, the game engine keeps the world locked most of the time and only briefly unlocks it (using this method) when tokens are allowed to make changes. When the world is locked, token methods that aren’t marked as being read-only can’t be called. When the world is unlocked, any token method can be called. These checks can be disabled by running python with optimization enabled.

You should never call this method manually from within your own game. This method is intended to be used by the game engine, which was carefully designed to allow the world to be modified only when safe. Calling this method yourself disables an important safety check.

end_game(*args, **kwargs)[source]
get_extension(actor)
get_extensions()
get_last_id()[source]

Return the largest token id registered with the world.

If no tokens have been added to the world, the id for the world itself (0) is returned. This means that the first “real” token id is 1.

get_token(id)[source]

Return the token with the given id.

If no token with the given id is registered to the world, an IndexError is raised.

has_extension(actor)
has_game_ended()[source]

Return True if the game has ended.

property has_id
property has_world
property id
is_locked()[source]

Return True if the world is currently allowed to be modified.

namedtuple(field_names, *, rename=False, defaults=None, module=None)

Returns a new subclass of tuple with named fields.

>>> Point = namedtuple('Point', ['x', 'y'])
>>> Point.__doc__                   # docstring for the new class
'Point(x, y)'
>>> p = Point(11, y=22)             # instantiate with positional args or keywords
>>> p[0] + p[1]                     # indexable like a plain tuple
33
>>> x, y = p                        # unpack like a regular tuple
>>> x, y
(11, 22)
>>> p.x + p.y                       # fields also accessible by name
33
>>> d = p._asdict()                 # convert to a dictionary
>>> d['x']
11
>>> Point(**d)                      # convert from a dictionary
Point(x=11, y=22)
>>> p._replace(x=100)               # _replace() is like str.replace() but targets named fields
Point(x=100, y=22)
on_add_to_world(*args, **kwargs)
on_finish_game(*args, **kwargs)[source]
on_remove_from_world(*args, **kwargs)
on_report_to_referee(reporter)
on_start_game(*args, **kwargs)[source]
on_update_game(*args, **kwargs)[source]
subscribe_to_message(message_cls, callback)
subscribe_to_sync_response(message_cls, callback)
subscribe_to_undo_response(message_cls, callback)
unsubscribe_from_message(message_cls, callback=None)
unsubscribe_from_sync_response(message_cls, callback=None)
unsubscribe_from_undo_response(message_cls, callback=None)
watch_method(method_name, callback)

Register the given callback to be called whenever the method with the given name is called. You can easily take advantage of this feature in token extensions by using the watch_token decorator.

property world