예제 #1
0
def install():
  global hp
  from guppy import hpy
  hp = hpy()

  aspects.wrap_function(Match.__init__, incrementCount)
  aspects.wrap_function(Match.__del__, decrementCount)
예제 #2
0
def install():
    def wrapNextTurn(func):
        def inner(self):
            # Gets the time that the player returned their message
            currentTime = time()
            # If this isn't the first time, calculate the time elapsed
            try:
                timeSpent = currentTime - self.turnStartTime
            except:
                timeSpent = 0
            # Make certain this game is tracked by the timeout tick
            if self not in games:
                games.append(self)
            # determine who's turn it is
            justWent, aboutToGo = (0,
                                   1) if self.turn == self.players[0] else (1,
                                                                            0)
            # charge the player that just went for the time they took
            self.objects.players[justWent].time -= timeSpent
            if self.objects.players[justWent].time <= 0:
                self.declareWinner(self.players[aboutToGo],
                                   'Opponent timed out')
                games.remove(self)
            print self.objects.players[0].time, self.objects.players[1].time
            # calls the turn update
            result = func(self)
            # gives the player who is about to go some more time
            self.objects.players[aboutToGo].time += self.timeInc
            # records the start of the turn
            self.turnStartTime = time()
            return result

        return inner

    aspects.wrap_function(Match.nextTurn, wrapNextTurn)

    def tick():
        currentTime = time()
        for i in games:
            p = i.objects.players
            if len(i.players) > 1:
                elapsed = currentTime - i.turnStartTime
                if i.turn == i.players[0]:
                    if p[0].time < elapsed:
                        i.declareWinner(i.players[1], 'Opponent timed out')
                elif i.turn == i.players[1]:
                    if p[1].time < elapsed:
                        i.declareWinner(i.players[0], 'Opponent timed out')
                else:
                    games.remove(i)
            else:
                games.remove(i)

        reactor.callLater(1, tick)

    reactor.callWhenRunning(reactor.callLater, 1, tick)
예제 #3
0
def install():
  def wrapNextTurn(func):
    def inner(self):
      # Gets the time that the player returned their message
      currentTime = time()
      # If this isn't the first time, calculate the time elapsed
      try: timeSpent = currentTime - self.turnStartTime
      except AttributeError: timeSpent = 0
      # Make certain this game is tracked by the timeout tick
      if self not in games:
        games.append(self)
      # determine who's turn it is
      justWent, aboutToGo = (0, 1) if self.turn == self.players[0] else (1, 0)
      # charge the player that just went for the time they took
      self.objects.players[justWent].time -= timeSpent
      if self.objects.players[justWent].time <= 0:
        self.declareWinner(self.players[aboutToGo], 'Player %d ran out of time'%(justWent + 1))
        games.remove(self)
      # calls the turn update
      result = func(self)
      if self.winner is None:
        # gives the player who is about to go some more time
        self.objects.players[aboutToGo].time += self.timeInc
        # records the start of the turn
        self.turnStartTime = time()
      else:
        games.remove(self)
      return result
    return inner

  aspects.wrap_function(Match.nextTurn, wrapNextTurn)

  def tick():
    currentTime = time()
    for i in games:
      p = i.objects.players
      if len(i.players) > 1:
        elapsed = currentTime - i.turnStartTime
        if i.turn == i.players[0]:
          if p[0].time < elapsed:
            i.declareWinner(i.players[1], 'Player 1 ran out of time')
        elif i.turn == i.players[1]:
          if p[1].time < elapsed:
            i.declareWinner(i.players[0], 'Player 2 ran out of time')
        else:
          games.remove(i)
      else:
        games.remove(i)

    reactor.callLater(1, tick)

  reactor.callWhenRunning(reactor.callLater, 1, tick)
예제 #4
0
파일: test.py 프로젝트: cherez/aspects
def test_nested():
    @aspects.wrapper
    def t():
        return 1
    def inc(func):
        def inner():
            return func() + 1
        return inner
    for i in range(1, 10):
        if t() != i:
            return False
        aspects.wrap_function(t, inc)
    return True
예제 #5
0
파일: test.py 프로젝트: cherez/aspects
def test_global():
    def inc(func):
        def inner():
            return func() + 1
        return inner

    if t() != 1:
        return False
    aspects.wrap_function(t, inc)
    if t() != 2:
        return False

    return True
예제 #6
0
파일: test.py 프로젝트: cherez/aspects
def test_instance():
    class C:
        def t(self):
            return 1

    def inc(func):
        def inner(self):
            return func(self) + 1
        return inner

    c = C()
    if c.t() != 1:
        return False
    aspects.wrap_function(c.t, inc)
    if c.t() != 2:
        return False
    c = C()
    if c.t() != 1:
        return False
    return True