Example #1
0
    def checkExpiration(self):
        #executing on thread
        while self.running:
            time.sleep(1)
            #print "Testing"
            for e in self.events.keys():
                # test elapsed time since last modification in seconds
                #print str(time.time() - self.events[e])

                if time.time(
                ) - self.events[e] > SECONDS_TO_WAIT_FOR_FILE_COMPLETE:
                    # file is "complete"

                    self.events.pop(e, None)
                    global FILENAME
                    global DIRNAME
                    global players
                    readytoread = 1

                    #test if the file is done being written to, if its not we should get an exception

                    if readytoread:
                        playednames = set()
                        updatedplayers = []
                        winteam = {'players': [], 'score': 0}
                        loseteam = {'players': [], 'score': 0}
                        #pprint.pprint(players)
                        #print "*** NEW GAME ***"

                        newplayers = hitzSaveRead(
                            os.path.join(DIRNAME, FILENAME))
                        #print newplayers
                        #
                        # compare new records to old records
                        #
                        #pprint.pprint(newplayers)
                        d = DictDiffer(newplayers, players)
                        deleteName = 0
                        if len(d.added()) > 0:
                            print "New players: " + ' '.join(
                                i for i in d.added())

                            for player in d.added():
                                if player != '':
                                    players[player] = {
                                        'name': player,
                                        'assists': 0,
                                        'goals': 0,
                                        'hits': 0,
                                        'shots': 0,
                                        'gamesPlayed': 0,
                                        'consecutiveLosses': 0,
                                        'wins': 0
                                    }
                                else:
                                    deleteName = 1
                        playednames = set(d.added().union(d.changed()))
                        if (len(d.added()) + len(d.changed())) != 6:
                            print "*** somethings fucky, there weren't 6 players"
                            pprint.pprint(playednames)

                        elif deleteName != 1:
                            print "***NEW GAME***"
                            print "["
                            for player in playednames:
                                oldrecord = players[player]
                                newrecord = newplayers[player]
                                currentgamestatschanged = DictDiffer(
                                    newrecord, oldrecord).changed()
                                playeroutput = {
                                    'name': player,
                                    'assists': 0,
                                    'goals': 0,
                                    'hits': 0,
                                    'shots': 0,
                                    'gamesPlayed': 0,
                                    'consecutiveLosses': 0,
                                    'wins': 0
                                }
                                for key in currentgamestatschanged:
                                    playeroutput[
                                        key] += newrecord[key] - oldrecord[key]
                                pprint.pprint(playeroutput)
                                print ","

                                if newrecord['wins'] > oldrecord['wins']:
                                    winteam['players'].append(player)
                                    winteam['score'] += (newrecord['goals'] -
                                                         oldrecord['goals'])
                                else:
                                    loseteam['players'].append(player)
                                    loseteam['score'] += (newrecord['goals'] -
                                                          oldrecord['goals'])
                            print "]"
                            print str(winteam['players']) + ' beat ' + str(
                                loseteam['players']) + ' : ' + str(
                                    winteam['score']) + ' - ' + str(
                                        loseteam['score'])

                        players = newplayers
    def checkExpiration(self):
        #executing on thread
        while self.running:
            time.sleep(1)
            #print "Testing"
            for e in self.events.keys():
                # test elapsed time since last modification in seconds
                #print str(time.time() - self.events[e])

                if time.time() - self.events[e] > SECONDS_TO_WAIT_FOR_FILE_COMPLETE:
                    # file is "complete"

                    self.events.pop(e, None)
                    global FILENAME
                    global DIRNAME
                    global players
                    readytoread=1

                    #test if the file is done being written to, if its not we should get an exception


                    if readytoread:
                        playednames = set()
                        updatedplayers=[]
                        winteam={'players':[],'score':0}
                        loseteam={'players':[],'score':0}
                        #pprint.pprint(players)
                        #print "*** NEW GAME ***"

                        newplayers = hitzSaveRead(os.path.join(DIRNAME,FILENAME))
                        #print newplayers
                        #
                        # compare new records to old records
                        #
                        #pprint.pprint(newplayers)
                        d=DictDiffer(newplayers, players)
                        deleteName=0
                        if len(d.added())>0:
                            print "New players: " + ' '.join(i for i in d.added())

                            for player in d.added():
                                if player != '':
                                    players[player]={'name':player,'assists':0, 'goals':0, 'hits':0, 'shots':0,
                                    'gamesPlayed':0,'consecutiveLosses':0,'wins':0}
                                else:
                                    deleteName = 1
                        playednames = set(d.added().union(d.changed()))
                        if (len(d.added())+len(d.changed()))!= 6:
                            print "*** somethings fucky, there weren't 6 players"
                            pprint.pprint(playednames)
                            

                        elif deleteName !=1:
                            print "***NEW GAME***"
                            print "["
                            for player in playednames:
                                oldrecord = players[player]
                                newrecord = newplayers[player]
                                currentgamestatschanged = DictDiffer(newrecord, oldrecord).changed()
                                playeroutput={'name':player,'assists':0, 'goals':0, 'hits':0, 'shots':0,
                                'gamesPlayed':0,'consecutiveLosses':0,'wins':0}
                                for key in currentgamestatschanged:
                                    playeroutput[key]+=newrecord[key]-oldrecord[key]
                                pprint.pprint(playeroutput)
                                print ","

                                if newrecord['wins']>oldrecord['wins']:
                                    winteam['players'].append(player)
                                    winteam['score']+=(newrecord['goals']-oldrecord['goals'])
                                else:
                                    loseteam['players'].append(player)
                                    loseteam['score']+=(newrecord['goals']-oldrecord['goals'])
                            print "]"
                            print str(winteam['players'])+' beat '+str(loseteam['players'])+' : '+str(winteam['score'])+' - '+str(loseteam['score'])






                        players = newplayers                    
Example #3
0
from watchdog.events import FileSystemEventHandler
from hitzSaveRead import hitzSaveRead
import os, pprint

#FILENAME = 'SLUS_201.40_0.bin'
FILENAME = 'hitzsave'
DIRNAME = '/media/pool/playstation2/VMC/'
OFFSET = 46088
# OFFSET = 8 # for a save file directly use 8, for a VMC file use 46088

#FILENAME = 'hitzsave'
#DIRNAME = 'C:\saves'

SECONDS_TO_WAIT_FOR_FILE_COMPLETE = 30

players = hitzSaveRead(os.path.join(DIRNAME, FILENAME), OFFSET)
#print players
'''def didPlay(newrecord,oldrecord):

    # Get the dictionary of the value differences from the old record to the new record
    stats = { key: value-oldrecord[key] for (key, value) in newrecord.iteritems() if key !='name'}
    stats['name']=oldrecord['name']


    return stats'''


class DictDiffer(object):
    """
    Calculate the difference between two dictionaries as:
    (1) items added
from watchdog.events import  FileSystemEventHandler
from hitzSaveRead import hitzSaveRead
import os, pprint

#FILENAME = 'SLUS_201.40_0.bin'
FILENAME = 'hitzsave'
DIRNAME = '/media/pool/playstation2/VMC/'
OFFSET=46088
# OFFSET = 8 # for a save file directly use 8, for a VMC file use 46088

#FILENAME = 'hitzsave'
#DIRNAME = 'C:\saves'

SECONDS_TO_WAIT_FOR_FILE_COMPLETE=30

players = hitzSaveRead(os.path.join(DIRNAME,FILENAME), OFFSET)
#print players

'''def didPlay(newrecord,oldrecord):

    # Get the dictionary of the value differences from the old record to the new record
    stats = { key: value-oldrecord[key] for (key, value) in newrecord.iteritems() if key !='name'}
    stats['name']=oldrecord['name']


    return stats'''

class DictDiffer(object):
    """
    Calculate the difference between two dictionaries as:
    (1) items added