-1

I have updated this code with modifications suggested by Reticality. I would like to access the value of hands_count which resides within the array of packets[] and or the array type[] below.

def history2packets(self, history, game_id, cache):
    game_index = 0
    player_list_index = 7
    packets = []
    for event in history:
        self.event = event
        type = event[0]
        if type == "game":
            (type, level, hand_serial, hands_count, time, variant, betting_structure, player_list, dealer, serial2chips) = event
            if len(serial2chips) > 1:
                nochips = 0
                for (serial, chips) in serial2chips.iteritems():
                    if serial == 'values':
                        continue
                    packets.append(PacketPokerPlayerChips(game_id = game_id,
                                                          serial = serial,
                                                          bet = nochips,
                                                          money = chips))
            packets.append(PacketPokerInGame(game_id = game_id,
                                             players = player_list))
            if self.previous_dealer == dealer:
                previous_dealer = -1
            else:
                previous_dealer = self.previous_dealer
            packets.append(PacketPokerDealer(game_id = game_id,
                                             dealer = dealer,
                                             previous_dealer = previous_dealer))
            self.previous_dealer = dealer
            packets.append(PacketPokerStart(game_id = game_id,
                                            hand_serial = hand_serial,
                                            hands_count = hands_count,
                                            time = time,
                                            level = level))

and use that value in another function like so:

def autoDeal(self):
    self.cancelDealTimeout()
    if not self.allReadyToPlay():
        for player in self.game.playersAll():
            if player.getUserData()['ready'] == False:
                for avatar in self.avatar_collection.get(player.serial):
                    if self.factory.verbose > 1:
                        self.message("Player %d marked as having a bugous PokerProcessingHand protocol" %  player.serial)
                        avatar.bugous_processing_hand = True
    if self.event[2] != 0:    
        self.beginTurn()
        self.update()
    else:
        reactor.callLater(10, self.beginTurn)
        self.update()

I am still getting an error but a different one.

2015-02-19 22:10:11-0500 [-] Unhandled Error
Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/twisted/application/app.py", line 445, in startReactor
    self.config, oldstdout, oldstderr, self.profiler, reactor)
  File "/usr/lib/python2.6/dist-packages/twisted/application/app.py", line 348, in runReactorWithLogging
    reactor.run()
  File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line 1170, in run
    self.mainLoop()
  File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line 1179, in mainLoop
    self.runUntilCurrent()
--- <exception caught here> ---
  File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line 778, in runUntilCurrent
    call.func(*call.args, **call.kw)
  File "/usr/lib/python2.6/dist-packages/pokernetwork/pokertable.py", line 738, in autoDeal
    if self.event[2] != 0:
exceptions.AttributeError: PokerTable instance has no attribute 'event'

Both function are within the same module and class. What else could be wrong?

spire
  • 23
  • 3

2 Answers2

0

The variable event is defined in history2packets, but it is not global. So when you refer to a variable with this name in a different function, that raises a NameError.

jammon
  • 3,404
  • 3
  • 20
  • 29
0

The variable event is defined within the local scope of the method history2packets. If you refer to it within another scope (e.g. a function or method) it will raise a NameError, as it was not defined in that scope.

A quick way to fix this is putting a line saying global event at the top of both methods, but this is usually frowned upon. Another way is returning event from history2packets and make the parameters of autoDeal autoDeal(self, event). This means you could call:

e = history2packets(...)
# 5 lines later
autodeal(e)

Another way to do this assumes history2packets and autoDeal are 2 methods of the same class. Modify dome code here in history2packets:

    packets = []
    for event in history:
         self.event = event
         type = event[0]

And then, make autoDeal

def autoDeal(self):
    self.cancelDealTimeout()
    if not self.allReadyToPlay():
        # Some lines later
    if self.event[2] != 0:  # This is the changed line  
        # Same as before
  • Please have another look, I have implemented your suggestions but there is still a problem. I am really trying to understand this, your answer seems comprehensive but so far I haven't any luck with any of the scenarios you have provided. – spire Feb 20 '15 at 03:21
  • That means they are not in the same instance of a class. You should then probably do the `global event` solution writing `global event` at the top of `history2packets`, so `event` is in the global scope. –  Feb 20 '15 at 09:20
  • I couldn't get the history2packets() method to give up the value of hands_count. I believe that hands_count didn't get populated at the instant when autoDeal() needed it. However, I was able to achieve the delay of the first deal by globalizing two other variables in another module. So, instead of using the hands_count variable as the crux for the delay, I used the servers change of state variables, (e.g. when the game server changed from registering for a game to running of the game). I used the globalized variable named old_state as the test in autoDeal() in order to make it work. – spire Feb 21 '15 at 05:01