def play(self, card_index): if self.need_pick is not None: raise ValueError(tr("You need to pick a card.")) card = self.hand[card_index] if card in HAZARDS: raise ValueError(tr("You cannot give an hazard card to yourself.")) if card in REMEDIES: remedy = REMEDIES[card] if remedy != self.hazard: raise ValueError( tr("You cannot play %s, you have an hazard card %s.") % (card, self.hazard)) elif self.hazard: raise ValueError( tr("You cannot play %s, you have an hazard card %s.") % (card, self.hazard)) elif (card != ROLL) and self.need_roll: raise ValueError(tr("You need a roll card.")) self.hand[card_index] = None self.need_pick = card_index self.played.append(card) if card == ROLL: self.need_roll = False elif card in REMEDIES: self.hazard = None return card
def createWidgets(self, window): self.widget = QWidget() layout = QVBoxLayout() self.widget.setLayout(layout) grid = QGridLayout() grid.addWidget(QLabel(tr("Label:")), 0, 0) self.label = QLineEdit(u"label") grid.addWidget(self.label, 0, 1) grid.addWidget(QLabel(tr("Type:")), 1, 0) self.game_types = QComboBox() grid.addWidget(self.game_types, 1, 1) layout.addLayout(grid) spacer = createVerticalSpacer() layout.addItem(spacer) button_box = QHBoxLayout() layout.addLayout(button_box) refresh = QPushButton('&Create') button_box.addWidget(refresh) window.connect(refresh, SIGNAL('clicked()'), self.create) leave = QPushButton('&Leave') button_box.addWidget(leave) window.connect(leave, SIGNAL('clicked()'), self.abort)
def discard(self, card_index): if self.need_pick is not None: raise ValueError(tr("You need to pick a card.")) if self.hand[card_index] is None: raise ValueError(tr("You have no card #%s.") % card_index) self.hand[card_index] = None self.need_pick = card_index
def start(self, player): if player != self.owner: raise ValueError(tr("Only the owner of the game (%s) can start it.") % self.owner.name) if self.state == PLAYING: raise ValueError(tr("The game has already started.")) if len(self.players) < self.min_players: raise ValueError(tr("You need at least %s players.") % self.min_players) self._start() self.state = PLAYING self.warning('Game started.') self.notifyAll(u'game_started', exclude=self.owner)
def enterPage(self): Page.enterPage(self) if self._init: return self._init = True server_types = self.client.command(u'game_types') server_types = set(server_types) & set((u'awale', u'1000 bornes')) if u'awale' in server_types: self.game_types.addItem(tr('Awale'), u'awale') if u'1000 bornes' in server_types: self.game_types.addItem(tr('Mille bornes'), u'1000 bornes')
def play(self, player, home): self.checkStatePlaying() current_player = player player_index = self.getPlayerIndex(player) if player_index != self.next_turn: raise ValueError("It's not your turn.") if not (1 <= home <= 6): raise ValueError(tr("Invalid home.")) played_home = home if player_index == 1: home += 5 else: home -= 1 seeds = self.homes[home] if not seeds: raise ValueError(tr("Home %s is empty.") % home) self.homes[home] = 0 while seeds: home = (home + 1) % 12 self.homes[home] += 1 seeds -= 1 collected = 0 while True: if player_index == 0: check = (0 <= home <= 5) else: check = (6 <= home <= 11) if check: break seeds = self.homes[home] if seeds not in (2, 3): break collected += seeds self.collected[player_index] += seeds self.homes[home] = 0 home = (home - 1) % 12 if collected: self.notifyAll(u'awale_collected', self.collected) player_index = (player_index + 1) % 2 self.next_turn = player_index self.notifyAll(u'awale_played', played_home, exclude=current_player) return SUCCESS
def addPlayer(self, player): if self.state != WAITING: raise ValueError(tr('You cannot join a game (%s) in state %s.') % (self.label, self.state)) if player in self.players: raise ValueError(tr('You are already playing to the game "%s".') % self.label) if player in self.observers: raise ValueError(tr('You are already an observer of the game "%s".') % self.label) if len(self.players) == self.max_players: raise ValueError(tr("A game cannot have more than %s players.") % self.max_players) index = len(self.players) self.players.append(player) self._player2index[player] = index player.playing.append(self) self.notifyAll(u'game_joined', player.identifier, exclude=player)
def init(self, game_id, game_type): self.game_id = game_id self.game_type = game_type game = self.client.getGame(game_id, use_cache=False) if self.window.options.debug: self.id_label.setText(tr("Game identifier: %s") % game['id']) self.type_label.setText(tr("Game type: %s") % game['type']) players = ', '.join(unicode(player_id) for player_id in game['players']) self.players_label.setText(tr("Players: %s") % players) self.state_label.setText(tr("State: %s") % game['state']) is_owner = (self.client.player_id == game[u'owner']) self.start_button.setEnabled(is_owner) return game
def _addGame(self, game): if (not self.display_all) and (game['state'] != WAITING): return False info = [u'%s: %s (#%s)' % (game['type'], game['label'], game['id'])] info.append(tr('players: %s') % u', '.join(map(unicode, game['players']))) if game['observers']: info.append(tr('observers: %s') % u', '.join(map(unicode, game['observers']))) info.append(game['state']) item = QListWidgetItem(u', '.join(info)) item.setData(GAME_ROLE, game) self.game_list.addItem(item) if self.watch_all: self.startWatching(game) else: self.stopWatching(game) return True
def _getGameType(self, game_id, game_type): game = self.getGame(game_id) if game.TYPE != game_type: raise ValueError( tr("Game %s is a game of type %s, not %s.") % (unicode(game), game.TYPE, game_type)) return game
def check(self, arg): errmsg = ValueType.check(self, arg) if errmsg is not None: return errmsg if not (self.min <= arg <= self.max): return tr("%s is not in range [%s; %s]") \ % (arg, self.min, self.max) return None
def check(self, arg): if not isinstance(arg, self.python_type): expected = self.python_type if not isinstance(expected, tuple): expected = (expected,) expected = ' or '.join(item.__name__ for item in expected) return tr("invalid type: %s instead of %s") \ % (type(arg).__name__, expected) return None
def playHazard(self, card_index, opponent_state): if self.need_pick is not None: raise ValueError(tr("You need to pick a card.")) card = self.hand[card_index] if card not in HAZARDS: raise ValueError(tr("%s is not an hazard card.") % card) if opponent_state.hazard: raise ValueError( tr("You cannot give an hazard card (%s) to %s: " "it has already an hazard card (%s).") % (card, self.player_name, opponent_state.hazard)) opponent_state.hazard = card opponent_state.need_roll = True self.hand[card_index] = None self.need_pick = card_index return card
def init(self, game_id): GamePage.init(self, game_id) game = self.client.getGame(game_id, use_cache=False) if game["state"] != "playing": raise UnicodeException(tr("Game aborted.")) self.is_player2 = game["players"][0] != self.player_id self.is_my_turn = game["next_turn"] == self.player_id self.fillTurn() self.fillHomes()
def decode(self, data, index): # -> (value, size): size in bytes arg_type = decodeUint8(data, index) if arg_type != self.type_id: raise ValueError( tr("Invalid type: %s instead of %s") % (arg_type, self.type_id)) index += 1 x = self._decode(data, index) arg, size = x return arg, 1 + size
def pick(self, player): self.checkStatePlaying() state = self.getState(player) card_index = state.need_pick if card_index is None: raise ValueError(tr("You already picked a card.")) card = self.desk[-1] del self.desk[-1] state.hand[card_index] = card state.need_pick = None return Response(u'mille_bornes_pick', card_index, card)
def createWidgets(self, window): self.widget = QWidget() layout = QVBoxLayout() self.widget.setLayout(layout) checkbox = QCheckBox(tr("Display started games")) checkbox.setChecked(self.display_all) layout.addWidget(checkbox) window.connect(checkbox, SIGNAL('stateChanged(int)'), self.setDisplayAll) self.game_list = QListWidget() layout.addWidget(self.game_list) window.connect( self.game_list.selectionModel(), SIGNAL("selectionChanged(QItemSelection, QItemSelection)"), self.selectEvent) watch = QCheckBox(tr("Watch all game state changes")) watch.setChecked(self.watch_all) layout.addWidget(watch) window.connect(watch, SIGNAL('stateChanged(int)'), self.setWatch) self.watch_checkbox = watch button_layout = QHBoxLayout() layout.addLayout(button_layout) create = QPushButton('&Create') button_layout.addWidget(create) window.connect(create, SIGNAL('clicked()'), self.create) self.join_button = QPushButton('&Join') button_layout.addWidget(self.join_button) window.connect(self.join_button, SIGNAL('clicked()'), self.join) refresh = QPushButton('&Refresh') button_layout.addWidget(refresh) window.connect(refresh, SIGNAL('clicked()'), self.refresh)
def play(self, player, old_pos, new_pos): self.checkStatePlaying() player_index = self.getPlayerIndex(player) color = WHITE if player_index == 0 else BLACK if self.next_turn != color: raise ValueError(tr("It's not your turn.")) old_index = parsePieceIndex(old_pos) new_index = parsePieceIndex(new_pos) piece = self.board[old_index] if not piece: raise ValueError(tr("There is no piece at %s.") % old_pos) moves = self.possibleMoves(old_index) if new_index not in moves: raise ValueError(tr("Invalid move: %s") % new_pos) moved_piece = self.board[old_index] if moved_piece.color != color: raise ValueError(tr("You cannot move your opponent's pieces.")) taken = self.board[new_index] self.board[old_index] = None self.board[new_index] = moved_piece self.notifyAll(u'chess_move', player.identifier, old_pos, new_pos, exclude=player) if taken is not None: self.notifyAll(u'chess_take', player.identifier, new_pos, taken.export(), exclude=player) if self.next_turn == WHITE: self.next_turn = BLACK else: self.next_turn = WHITE
def hazard(self, player, card_index, opponent): self.checkStatePlaying() if player is opponent: raise ValueError(tr("You cannot give you an hazard card.")) player_state = self.getState(player) opponent_state = self.getState(opponent, check_turn=False) card = player_state.playHazard(card_index, opponent_state) self.notifyAll(u'mille_bornes_hazard', player.identifier, card_index, card, opponent.identifier, exclude=player) self.nextTurn(player) return SUCCESS
def getResult(self, response): expected_result = self.PROTOCOL[self.command].result if response.command != expected_result: raise UnicodeException( tr("unexpected result of request %s: %s instead of %s") % (self.command, response.command, expected_result) ) result = response.arguments if len(result) == 0: return None elif len(result) == 1: return result[0] else: return result
def clientConnected(self, handler, address): try: client = self.clients[address] except KeyError: pass else: raise ValueError(tr("Client %s is already connected.") % unicode(client)) stat = self.getIPStats(address) if self.config.max_client_per_ip <= stat.nclients: raise ValueError( "Too much clients (%s) from your IP address" % stat.nclients) client = Client(self, handler, address) client.warning("Connected") self.clients[address] = client stat.nclients += 1 return client
def createWidgets(self, window): self.widget = QWidget() layout = QVBoxLayout() self.widget.setLayout(layout) if self.window.options.debug: self.id_label = QLabel() layout.addWidget(self.id_label) self.type_label = QLabel() layout.addWidget(self.type_label) self.players_label = QLabel() layout.addWidget(self.players_label) self.state_label = QLabel() layout.addWidget(self.state_label) self.autostart_checkbox = QCheckBox(tr("Autostart")) window.connect(self.autostart_checkbox, SIGNAL("stateChanged(int)"), self.autostartChanged) layout.addWidget(self.autostart_checkbox) spacer = createVerticalSpacer() layout.addItem(spacer) button_layout = QHBoxLayout() layout.addLayout(button_layout) self.start_button = QPushButton('&Start') button_layout.addWidget(self.start_button) window.connect(self.start_button, SIGNAL('clicked()'), self.start) refresh = QPushButton('&Refresh') button_layout.addWidget(refresh) window.connect(refresh, SIGNAL('clicked()'), self.refresh) leave = QPushButton('&Leave') button_layout.addWidget(leave) window.connect(leave, SIGNAL('clicked()'), self.leave)
def getPlayer(self, player_id): try: return self.players[player_id] except KeyError: raise ValueError(tr("The client %s has no player with identifier %s.") % (unicode(self), player_id))
def _unknownPlayer(self, player): raise ValueError( tr('The player %s (#%s) is not playing the game "%s".') % (player.name, player.identifier, self.label))
def addObserver(self, player): if player in self.players: raise ValueError(tr('You are already playing to the game "%s".') % self.label) if player in self.observers: raise ValueError(tr('You are already watching to the game "%s".') % self.label) self.observers.append(player)
def checkStatePlaying(self): if self.state == ABORTED: raise ValueError(tr("The game %s has aborted.") % unicode(self)) if self.state != PLAYING: raise ValueError(tr("The game %s has not started yet.") % unicode(self))
def game_create(self, client, player_id, game_type, label): """Create a new game""" player = client.getPlayer(player_id) label = label.strip() if not re.match(ur'[^\x00-\x1f]{1,100}$', label): raise ValueError(tr("Invalid game label: %r") % label)
def doLostServer(self): self.window.errorPopup(tr("Connection lost with the server. Exit.")) self.window.close()
def server_random_seed(self, client, seed): if not self.debug_mode: raise ValueError(tr("Command only available in debug mode.")) random.seed(seed) return SUCCESS
def getGame(self, game_id): try: return self.games[game_id] except KeyError: raise ValueError(tr("Unknown game identifier: %s") % game_id)