Esempio n. 1
0
 def __init__ (self, password, from_address, to_address):
     PyChess.__init__(self)
     
     self.ports = (23, 5000)
     if not password:
         self.username = "******"
     else: self.username = "******"
     self.owner = "Lobais"
     self.password = password
     self.from_address = "The PyChess Bot <%s>" % from_address
     self.to_address = "Thomas Dybdahl Ahle <%s>" % to_address
     
     # Possible start times
     self.minutes = (1,2,3,4,5,6,7,8,9,10)
     self.gains = (0,5,10,15,20)
     # Possible colors. None == random
     self.colors = (WHITE, BLACK, None) 
     # The amount of random challenges, that PyChess sends with each seek
     self.challenges = 10
     enableEGTB()
     
     self.sudos = set()
     self.ownerOnline = False
     self.waitingForPassword = None
     self.log = []
     self.acceptedTimesettings = []
     
     self.worker = None
     
     repeat_sleep(self.sendChallenges, 60*1)
Esempio n. 2
0
    def __zerolistener(self, *args):
        # If we are called by a sleeper (rather than a signal) we need to pop
        # at least one time, as we might otherwise end up with items in the
        # heap, but no sleepers.
        if len(args) == 0 and self.heap:
            self.heap.pop()
        # Pop others (could this give a problem where too many are popped?)
        # No I don't think so. If a sleeper is too slow, so a later sleeper
        # comes before him and pops him, then it is most secure not to rely on
        # mr late, and start a new one.
        # We won't be 'one behind' always, because the previous pop doesnt
        # happen if the heap is empty.
        while self.heap and self.heap[-1] <= time():
            self.heap.pop()

        if self.getPlayerTime(WHITE) <= 0:
            #print 'emit for white'
            self.emit('zero_reached', WHITE)
        if self.getPlayerTime(BLACK) <= 0:
            #print 'emit for black'
            self.emit('zero_reached', BLACK)

        #print 'heap is now', self.heap

        t1 = time() + self.getPlayerTime(WHITE)
        t2 = time() + self.getPlayerTime(BLACK)
        t = min(t1, t2)

        if not self.heap or t < self.heap[-1]:
            s = t - time() + 0.01
            if s > 0:
                self.heap.append(t)
                # Because of recur, we wont get callback more than once.
                repeat.repeat_sleep(self.__zerolistener, s, recur=True)
Esempio n. 3
0
    def __init__(self, password, from_address, to_address):
        PyChess.__init__(self)

        self.ports = (23, 5000)
        if not password:
            self.username = "******"
        else:
            self.username = "******"
        self.owner = "Lobais"
        self.password = password
        self.from_address = "The PyChess Bot <%s>" % from_address
        self.to_address = "Thomas Dybdahl Ahle <%s>" % to_address

        # Possible start times
        self.minutes = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        self.gains = (0, 5, 10, 15, 20)
        # Possible colors. None == random
        self.colors = (WHITE, BLACK, None)
        # The amount of random challenges, that PyChess sends with each seek
        self.challenges = 10
        enableEGTB()

        self.sudos = set()
        self.ownerOnline = False
        self.waitingForPassword = None
        self.log = []
        self.acceptedTimesettings = []

        self.worker = None

        repeat_sleep(self.sendChallenges, 60 * 1)
 def __zerolistener(self, *args):
     # If we are called by a sleeper (rather than a signal) we need to pop
     # at least one time, as we might otherwise end up with items in the
     # heap, but no sleepers.
     if len(args) == 0 and self.heap:
         self.heap.pop()
     # Pop others (could this give a problem where too many are popped?)
     # No I don't think so. If a sleeper is too slow, so a later sleeper
     # comes before him and pops him, then it is most secure not to rely on
     # mr late, and start a new one.
     # We won't be 'one behind' always, because the previous pop doesnt
     # happen if the heap is empty.
     while self.heap and self.heap[-1] <= time():
         self.heap.pop()
     
     if self.getPlayerTime(WHITE) <= 0:
         #print 'emit for white'
         self.emit('zero_reached', WHITE)
     if self.getPlayerTime(BLACK) <= 0:
         #print 'emit for black'
         self.emit('zero_reached', BLACK)
     
     #print 'heap is now', self.heap
     
     t1 = time() + self.getPlayerTime(WHITE)
     t2 = time() + self.getPlayerTime(BLACK)
     t = min(t1,t2)
     
     if not self.heap or t < self.heap[-1]:
         s = t-time()+0.01
         if s > 0:
             self.heap.append(t)
             # Because of recur, we wont get callback more than once.
             repeat.repeat_sleep(self.__zerolistener, s, recur=True)
Esempio n. 5
0
 def setModel (self, model):
     self.model = model
     self.model.connect("time_changed", self.time_changed)
     self.model.connect("player_changed", self.player_changed)
     self.formatedCache = [formatTime (
         self.model.getPlayerTime (self.model.movingColor or WHITE))] * 2
     if model.secs!=0 or model.gain!=0:
         repeat_sleep(self.update, 0.1)
 def __init__(self, width, height):
     gtk.DrawingArea.__init__(self)
     self.connect("expose_event", self.__expose)
     self.surface = self.__loadSvg(addDataPrefix("glade/throbber.svg"),
                                   width, height)
     self.width = width
     self.height = height
     self.started = False
     repeat_sleep(self.redraw, 1. / MAX_FPS)
Esempio n. 7
0
 def __init__(self, width, height):
     gtk.DrawingArea.__init__(self)
     self.connect("expose_event", self.__expose)
     self.surface = self.__loadSvg(addDataPrefix("glade/throbber.svg"),
                                   width, height)
     self.width = width
     self.height = height
     self.started = False
     repeat_sleep(self.redraw, 1./MAX_FPS)
Esempio n. 8
0
 def __init__(self, width, height):       
     GObject.GObject.__init__(self)        
     self.connect("draw", self.__expose)
     self.surface = self.__loadSvg(addDataPrefix("glade/throbber.svg"),
                                   width, height)
     self.width = width
     self.height = height
     self.started = False
     self.stopped = False
     repeat_sleep(self.redraw, 1./MAX_FPS)
Esempio n. 9
0
 def __init__(self, width, height):
     GObject.GObject.__init__(self)
     self.connect("draw", self.__expose)
     self.surface = self.__loadSvg(addDataPrefix("glade/throbber.svg"),
                                   width, height)
     self.width = width
     self.height = height
     self.started = False
     self.stopped = False
     repeat_sleep(self.redraw, 1. / MAX_FPS)
Esempio n. 10
0
 def setModel (self, model):
     self.model = model
     if model != None:
         self.model.connect("time_changed", self.time_changed)
         self.model.connect("player_changed", self.player_changed)
         self.formatedCache = [self.formatTime (
             self.model.getPlayerTime (self.model.movingColor or WHITE))] * 2
         repeat_sleep(self.update, 0.1)
     else:
         self.formatedCache = None
Esempio n. 11
0
    def __zerolistener(self, *args):
        # If we are called by a sleeper (rather than a signal) we need to pop
        # at least one time, as we might otherwise end up with items in the
        # heap, but no sleepers.
        if len(args) == 0 and self.heap:
            self.heap.pop()
        # Pop others (could this give a problem where too many are popped?)
        # No I don't think so. If a sleeper is too slow, so a later sleeper
        # comes before him and pops him, then it is most secure not to rely on
        # mr late, and start a new one.
        # We won't be 'one behind' always, because the previous pop doesnt
        # happen if the heap is empty.
        while self.heap and self.heap[-1] <= time():
            self.heap.pop()
        
        if self.getPlayerTime(WHITE) <= 0:
            #print 'emit for white'
            #self.emit('zero_reached', WHITE)
        if self.getPlayerTime(BLACK) <= 0:
            #print 'emit for black'
            #self.emit('zero_reached', BLACK)
        
        #print 'heap is now', self.heap
        
        t1 = time() + self.getPlayerTime(WHITE)
        t2 = time() + self.getPlayerTime(BLACK)
        t = min(t1,t2)
        
        if not self.heap or t < self.heap[-1]:
            s = t-time()+0.01
            if s > 0:
                self.heap.append(t)
                # Because of recur, we wont get callback more than once.
                repeat.repeat_sleep(self.__zerolistener, s, recur=True)
                #print 'repeat on', s
    
    ############################################################################
    # Interacting                                                              #
    ############################################################################
    
    def setMovingColor (self, movingColor):
        self.movingColor = movingColor
        #self.emit("player_changed")
    
    def tap (self):
        
        if self.paused: return
        
        if self.started:
            t = self.intervals[self.movingColor][-1] + self.gain
            if self.counter != None:
                t -= time() - self.counter
            self.intervals[self.movingColor].append(t)
        else:
            self.intervals[self.movingColor].append (
                    self.intervals[self.movingColor][-1] )
            
            if len(self.intervals[0]) + len(self.intervals[1]) >= 4:
                self.started = True
        
        self.movingColor = 1-self.movingColor
        
        if self.started:
            self.counter = time()
            #self.emit("time_changed")
        
        #self.emit("player_changed")
    
    def start (self):
        if self.started: return
        self.started = True
        
        self.counter = time()
        #self.emit("time_changed")
    
    def end (self):
        self.pause()
        self.ended = True
    
    def pause (self):
        log.debug("TimeModel.pause: self=%s\n" % self)
        if self.paused: return
        self.paused = True
        
        if self.counter != None:
            self.pauseInterval = time()-self.counter
        
        self.counter = None
        #self.emit("time_changed")
        #self.emit("pause_changed", True)
    
    def resume (self):
        log.debug("TimeModel.resume: self=%s\n" % self)
        if not self.paused: return
        
        self.paused = False
        self.counter = time() - self.pauseInterval
        
        #self.emit("pause_changed", False)
    
    ############################################################################
    # Undo and redo in TimeModel                                               #
    ############################################################################
    
    def undoMoves (self, moves):
        """ Sets time and color to move, to the values they were having in the
            beginning of the ply before the current.
        his move.
        Example:
        White intervals (is thinking): [120, 130, ...]
        Black intervals:               [120, 115]
        Is undoed to:
        White intervals:               [120, 130]
        Black intervals (is thinking): [120, ...] """
        
        if not self.started:
            self.start()
        
        for i in xrange(moves):
            self.movingColor = 1-self.movingColor
            del self.intervals[self.movingColor][-1]
        
        if len(self.intervals[0]) + len(self.intervals[1]) >= 4:
            self.counter = time()
        else:
            self.started = False
            self.counter = None
        
        #self.emit("time_changed")
        #self.emit("player_changed")
    
    ############################################################################
    # Updating                                                                 #
    ############################################################################
    
    def updatePlayer (self, color, secs):
        if color == self.movingColor and self.started:
            self.counter = secs + time() - self.intervals[color][-1]
        else: self.intervals[color][-1] = secs
        #self.emit("time_changed")
    
    def syncClock (self, wsecs, bsecs):
        """ Syncronize clock to e.g. fics time """
        if self.movingColor == WHITE:
            if self.started:
                self.counter = wsecs + time() - self.intervals[WHITE][-1]
            else: self.intervals[WHITE][-1] = wsecs
            self.intervals[BLACK][-1] = bsecs
        else:
            if self.started:
                self.counter = bsecs + time() - self.intervals[BLACK][-1]
            else: self.intervals[BLACK][-1] = bsecs
            self.intervals[WHITE][-1] = wsecs
        #self.emit("time_changed")
    
    ############################################################################
    # Info                                                                     #
    ############################################################################
    
    def getPlayerTime (self, color):
        if color == self.movingColor and self.started:
            if self.paused:
                return self.intervals[color][-1] - self.pauseInterval
            elif self.counter:
                return self.intervals[color][-1] - (time() - self.counter)
        return self.intervals[color][-1]
    
    def getInitialTime (self):
        return self.intervals[WHITE][0]

    def getElapsedMoveTime (self, ply):
        movecount, color = divmod(ply, 2)
        return self.intervals[1-color][movecount-1] - self.intervals[1-color][movecount] if movecount > 0 else 0
        
    @property
    def display_text (self):
        t = ("%d " % self.minutes) + _("min")
        if self.gain != 0:
            t += (" + %d " % self.gain) + _("sec")
        return t