1from gi.repository import GObject
2
3
4class PlayerIsDead(Exception):
5    """ Used instead of returning a move,
6        when an engine crashes, or a nonlocal player disconnects """
7    pass
8
9
10class PassInterrupt(Exception):
11    """ Used instead of returning a move, when a players turn is interrupted but not changed.
12        This may happen when undoMoves doesn't changes the current player """
13    pass
14
15
16class TurnInterrupt(Exception):
17    """ Used instead of returning a move, when a players turn is interrupted.
18        This may happen when undoMoves changes the current player """
19    pass
20
21
22class InvalidMove(Exception):
23    """ Used instead of returning a move,
24        when an engine plays an invalid move """
25    pass
26
27
28class GameEnded(Exception):
29    """ Used instead of returning a move on game end """
30    pass
31
32
33class Player(GObject.GObject):
34
35    __gsignals__ = {
36        "offer": (GObject.SignalFlags.RUN_FIRST, None, (object, )),
37        "withdraw": (GObject.SignalFlags.RUN_FIRST, None, (object, )),
38        "decline": (GObject.SignalFlags.RUN_FIRST, None, (object, )),
39        "accept": (GObject.SignalFlags.RUN_FIRST, None, (object, )),
40        "name_changed": (GObject.SignalFlags.RUN_FIRST, None, ()),
41    }
42
43    def __init__(self):
44        GObject.GObject.__init__(self)
45        self.name = ""
46        self.ichandle = None
47        self.icrating = None
48
49    def setName(self, name):
50        """ __repr__ should return this name """
51        self.name = name
52        self.emit("name_changed")
53
54    def __repr__(self):
55        return self.name
56
57    @property
58    def time(self):
59        pass  # Optional
60
61    # Starting the game
62
63    def prestart(self):
64        pass  # Optional
65
66    def start(self, event, is_dead):
67        event.set()
68
69    def setOptionInitialBoard(self, model):
70        pass  # Optional. Further defined in Engine.py
71
72    # Ending the game
73
74    def end(self, status, reason):
75        """ Called when the game ends in a normal way. Use this for shutting
76            down engines etc. """
77        raise NotImplementedError
78
79    def kill(self, reason):
80        """ Called when game has too die fast and ugly. Mostly used in case of
81            errors and stuff. Use for closing connections etc. """
82        raise NotImplementedError
83
84    # Send the player move updates
85
86    def makeMove(self, board1, move, board2):
87        """ Takes a board object, and if ply>lowply the latest move object and
88            second latest board object as well. Otherwise these two are None.
89            Retruns: A new move object, witch the player wants to do. """
90        raise NotImplementedError
91
92    def putMove(self, board1, move, board2):
93        """ Like makeMove, but doesn't block and doesn't return anything.
94            putMove is only used when the player is spectatctor to a game """
95        # Optional
96
97    def updateTime(self, secs, opsecs):
98        """ Updates the player with the current remaining time as a float of
99            seconds """
100        # Optional
101
102    # nteracting with the player
103
104    def pause(self):
105        """ Should stop the player from thinking until resume is called """
106        raise NotImplementedError
107
108    def resume(self):
109        """ Should resume player to think if he's paused """
110        raise NotImplementedError
111
112    def hurry(self):
113        """ Forces engines to move now, and sends a hurry message to nonlocal
114            human players """
115        # Optional
116
117    def undoMoves(self, moves, gamemodel):
118        """ Undo 'moves' moves and makes the latest board in gamemodel the
119            current """
120        # Optional
121
122    def playerUndoMoves(self, moves, gamemodel):
123        """ Some players undo different depending on whether they are players or
124            spectators. This is a convenient way to handle that. """
125        # Optional
126        return self.undoMoves(moves, gamemodel)
127
128    def spectatorUndoMoves(self, moves, gamemodel):
129        """ Some players undo different depending on whether they are players or
130            spectators. This is a convenient way to handle that. """
131        # Optional
132        return self.undoMoves(moves, gamemodel)
133
134    def putMessage(self, message):
135        """ Sends the player a chatmessage """
136        # Optional
137
138    # Offer handling
139
140    def offer(self, offer):
141        """ The players opponent has offered the player offer. If the player
142            accepts, it should respond by mirroring the offer with
143            emit("accept", offer). If it should either ignore the offer or emit
144            "decline"."""
145        raise NotImplementedError
146
147    def offerDeclined(self, offer):
148        """ An offer sent by the player was responded negative by the
149            opponent """
150        # Optional
151
152    def offerWithdrawn(self, offer):
153        """ An offer earlier offered to the player has been withdrawn """
154        # Optional
155
156    def offerError(self, offer, error):
157        """ An offer, accept or action made by the player has been refused by
158            the game model. """
159        # Optional
160