def __init__(self, handle): config.update_handle(handle) self.handle = handle Gtk.Window.__init__(self, title=handle) self.set_default_size(1,1) self.set_border_width(5) self.set_modal(True) self.connect('key_press_event', self.key_cmd) self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK) Box = Gtk.VBox().new(False, 1) frame = Gtk.Frame() Box.pack_start(frame, False, False, 0) vbox = Gtk.VBox().new(True, 1) frame.add(vbox) button = Gtk.Button.new_with_mnemonic("_Match") button.command = lambda x: 'match {} {} {}'.format(x.handle, x.match_time.get_value_as_int(), x.match_incr.get_value_as_int()) button.command_send = True button.connect("clicked", self.on_button_clicked) vbox.pack_start(button, False, False, 0) hbox = Gtk.HBox().new(True, 1) hbox.pack_start(Gtk.Label().new('Time (min)'), False, False, 0) self.match_time = Gtk.SpinButton() self.match_time.set_adjustment(Gtk.Adjustment(5, 0, 100, 1, 10, 0)) self.match_time.get_adjustment().configure(5, 0, 100, 1, 10, 0) self.match_time.set_activates_default(True) hbox.pack_start(self.match_time, False, False, 0) vbox.pack_start(hbox, False, False, 0) hbox = Gtk.HBox().new(True, 1) hbox.pack_start(Gtk.Label().new('Incr (sec)'), False, False, 0) self.match_incr = Gtk.SpinButton() self.match_incr.set_adjustment(Gtk.Adjustment(10, 0, 100, 1, 10, 0)) self.match_incr.get_adjustment().configure(10, 0, 100, 1, 10, 0) self.match_incr.set_activates_default(True) hbox.pack_start(self.match_incr, False, False, 0) vbox.pack_start(hbox, False, False, 0) for label, command, command_send in [ ('_Tell', lambda x: 'tell {} '.format(x.handle), False), ('_Finger', lambda x: 'finger {}'.format(x.handle), True) ]: button = Gtk.Button.new_with_mnemonic(label) button.command = command button.command_send = command_send button.connect("clicked", self.on_button_clicked) Box.pack_start(button, False, False, 0) button = Gtk.Button.new_with_mnemonic("_Cancel") button.connect("clicked", self.on_cancel) Box.pack_start(button, False, False, 0) self.add(Box) self.realize() self.get_window().set_transient_for( Gdk.Screen.get_default().get_active_window()) self.show_all()
def print(self, text, attr=None): if (len(self.txt_list.body) and (text.find('\n') == 0 or len(text) == 0)): txt_ = self.txt_list.body[-1].get_text()[0] if txt_.find('\n') == 0 or len(txt_) == 0: return if attr: txt = (attr, text) else: for rule in self.TEXT_RE: regxp = rule[0].match(text) if regxp: if 'handle' in regxp.groupdict(): config.update_handle(regxp.group('handle')) txt = rule[1](regxp, text) break else: txt = (urwid.AttrSpec(config.console.default_color, 'default'), text) if txt: text = txt[1] nc = [(int(x,16) if int(x,16)>=15 else int(x,16)+2) for x in txt[0].foreground[1::]] nc = '#{:x}{:x}{:x}'.format(*nc) htxt = [] ii = 0 for m in self.hl_rule.finditer(text): if m.start()-ii > 0: htxt.append(text[ii:m.start()]) htxt.append((urwid.AttrSpec(nc +',bold', txt[0].background), m.group())) ii = m.end() if ii < len(text): htxt.append(text[ii::]) if len(htxt): txt = (txt[0], htxt) if ':[Game ' in text: self._last_AB = ctxt = ConsoleText(txt) else: ctxt = ConsoleText(txt) if self.body_size: if (len(self.txt_list.body)-1 == self.txt_list.calculate_visible(self.body_size)[0][2]): self.txt_list.body.append(ctxt) self.txt_list.set_focus(len(self.txt_list.body)-1) else: self.txt_list.body.append(ctxt) else: self.txt_list.body.append(ctxt) self.txt_list.set_focus(len(self.txt_list.body)-1) return ctxt
def send_cmd(self, cmd, echo=False, wait_for=None, ans_buff=None, save_history=True, record_handle=True): if hasattr(self, 'fics'): if save_history: if (len(self.cmd_line.cmd_history) and self.cmd_line.cmd_history[-1] != cmd or not len(self.cmd_line.cmd_history)): self.cmd_line.cmd_history.append(cmd) if record_handle: if ' ' in cmd and len(cmd.split())>1: m = self.handle_rule.match(cmd.split()[1]) if m: config.update_handle(m.group()) data = cmd.translate(config.TRANS_TABLE) \ .encode("ascii", "ignore")+b'\n' config.log(data, sent=True) if wait_for: self._wait_for_buf.clear() self._wait_for_txt = wait_for config.log('Waiting for: '+str(wait_for)) self.fics.write(data) if wait_for: if not self._wait_for_sem.acquire(timeout=5): self._wait_for_txt = None self.ML_recording = False config.log('Waiting for failed') return False config.log('Waiting for succeed') self._wait_for_txt = None if isinstance(ans_buff, list): ans_buff.append(''.join(self._wait_for_buf)) if echo: self.print('> '+cmd, urwid.AttrSpec(config.console.echo_color, 'default')) return True
def set_state(self, new_state): if isinstance(new_state, GameState): state = new_state else: state = Style12(new_state) self.interruptus = False self.last_style12 = state if not self._history.update(state) and self.opponent != '': self.fill_history(state) if state in self._history or self.kind & KIND_EXAMINING: r = self._history.set_mainline(state) if self.board and self.board.movetree: if r: self.board.movetree.set_mainline(*r) else: self.board.movetree.repopulate() elif self.board and self.board.movetree: self.board.movetree.update_node(state) self.move_sent = False if state is self._history[-1]: self.turn = state.turn #flush premove if (self.kind & KIND_PLAYING and len(self.selected) == 2 and self.side == self.turn and self.halfmove != state.halfmove): config.cli.send_cmd((postopos(self.selected[0])+ postopos(self.selected[1])), save_history=False) # move was ilegal? preserve selected piece elif (len(self.selected) == 2 and self.halfmove == state.halfmove): self.selected.pop() elif (self.kind & KIND_PLAYING and len(self.selected) == 1 and self.side == self.turn): pass elif state is not self._history[-1]: pass else: self.selected.clear() self.halfmove = state.halfmove if (not self.player_names == state.names or (abs(self._kind) == 2 and self._kind != state.relation)): self.number = state.game_number self.player = [state.names[0]+self.rating[0], state.names[1]+self.rating[1]] self.player_names = state.names self._kind = state.relation self.kind = (KIND_PLAYING if abs(self._kind) == 1 else KIND_OBSERVING if self._kind == 0 else KIND_EXAMINING if self._kind == 2 else KIND_ISOLATED if self._kind == -3 else KIND_OBSERVING | KIND_EXAMINING) self.side = ((self.turn and (self._kind == 1)) or ((self._kind == -1) and not self.turn) or (self.kind ^ KIND_PLAYING and (self.player_names[0] != config.fics_user or self.player_names[1] == config.fics_user))) self.opponent = self.player_names[not self.side] self.me = self.player_names[self.side] self.itime = state.itime self.iinc = state.iinc self.name = ('Game {}: '.format(state.game_number) + ('Observing ' if self.kind & KIND_OBSERVING else 'Examining ' if self.kind & KIND_EXAMINING else '')+ ' v/s '.join(self.player[::-1])+' - '+ '/'.join([self.itime, self.iinc])+ # (' - '+self.rated if self.rated else '' )+ (' - ' + self.result if self.result else '') ).strip() if self.kind & KIND_OBSERVING: self.get_old_moves() if self.kind & (KIND_EXAMINING | KIND_PLAYING): config.gui.seek_graph_destroy() if self.board: self.board.reset(True) for hdl in self.player_names: config.update_handle(hdl) else: if self.board: self.board.reset(False)