Snippets

David Márquez parse PLAYER_INFO

Created by David Márquez
# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)]
# Embedded file name: scripts/common/battle_results_shared.py
import struct
from itertools import izip
from battle_results_constants import BATTLE_RESULT_ENTRY_TYPE as ENTRY_TYPE
from dictpackers import *

class PREMIUM_TYPE:
    NONE = 0
    BASIC = 1
    PLUS = 2
    VIP = 4
    TYPES_SORTED = (
     BASIC, PLUS, VIP)
    ANY = BASIC | PLUS | VIP
    AFFECTING_TYPES = PLUS | VIP

    @classmethod
    def activePremium(cls, premMask):
        for premType in reversed(cls.TYPES_SORTED):
            if premMask & premType:
                return premType

        return cls.NONE

    @classmethod
    def initialData(cls):
        return {cls.BASIC: 0, 
           cls.PLUS: 0, 
           cls.VIP: 0, 
           'premMask': 0}

class PREM_BONUS_TYPES:
    CREDITS = 0
    XP = 1
    TMEN_XP = 2

VEH_INTERACTION_DETAILS = (
 ('spotted', 'B', 1, 0),
 ('deathReason', 'b', 10, -1),
 ('directHits', 'H', 65535, 0),
 ('directEnemyHits', 'H', 65535, 0),
 ('explosionHits', 'H', 65535, 0),
 ('piercings', 'H', 65535, 0),
 ('piercingEnemyHits', 'H', 65535, 0),
 ('damageDealt', 'I', 4294967295L, 0),
 ('damageAssistedTrack', 'H', 65535, 0),
 ('damageAssistedRadio', 'H', 65535, 0),
 ('damageAssistedStun', 'H', 65535, 0),
 ('damageAssistedSmoke', 'H', 65535, 0),
 ('damageAssistedInspire', 'H', 65535, 0),
 ('crits', 'I', 4294967295L, 0),
 ('fire', 'H', 65535, 0),
 ('stunNum', 'H', 65535, 0),
 ('stunDuration', 'f', 65535.0, 0.0),
 ('damageBlockedByArmor', 'I', 4294967295L, 0),
 ('damageReceived', 'H', 65535, 0),
 ('rickochetsReceived', 'H', 65535, 0),
 ('noDamageDirectHitsReceived', 'H', 65535, 0),
 ('targetKills', 'B', 255, 0))
VEH_INTERACTION_DETAILS_NAMES = [ x[0] for x in VEH_INTERACTION_DETAILS ]
VEH_INTERACTION_DETAILS_MAX_VALUES = dict((x[0], x[2]) for x in VEH_INTERACTION_DETAILS)
VEH_INTERACTION_DETAILS_INIT_VALUES = [ x[3] for x in VEH_INTERACTION_DETAILS ]
VEH_INTERACTION_DETAILS_LAYOUT = ('').join([ x[1] for x in VEH_INTERACTION_DETAILS ])
VEH_INTERACTION_DETAILS_INDICES = dict((x[1][0], x[0]) for x in enumerate(VEH_INTERACTION_DETAILS))
VEH_INTERACTION_DETAILS_TYPES = dict((x[0], x[1]) for x in VEH_INTERACTION_DETAILS)
VEH_INTERACTIVE_STATS = ('xp', 'damageDealt', 'capturePts', 'flagActions', 'winPoints',
                         'deathCount', 'resourceAbsorbed', 'stopRespawn', 'equipmentDamage',
                         'equipmentKills')
VEH_INTERACTIVE_STATS_INDICES = dict((x[1], x[0]) for x in enumerate(VEH_INTERACTIVE_STATS))
AVATAR_PRIVATE_STATS = ('ragePoints', )
AVATAR_PRIVATE_STATS_INDICES = dict((x[1], x[0]) for x in enumerate(AVATAR_PRIVATE_STATS))
_PREM_TYPE_TO_FACTOR100_NAMES = {PREM_BONUS_TYPES.CREDITS: {PREMIUM_TYPE.BASIC: 'premiumCreditsFactor100', 
                              PREMIUM_TYPE.PLUS: 'premiumPlusCreditsFactor100', 
                              PREMIUM_TYPE.VIP: 'premiumVipCreditsFactor100'}, 
   PREM_BONUS_TYPES.XP: {PREMIUM_TYPE.BASIC: 'premiumXPFactor100', 
                         PREMIUM_TYPE.PLUS: 'premiumPlusXPFactor100', 
                         PREMIUM_TYPE.VIP: 'premiumVipXPFactor100'}, 
   PREM_BONUS_TYPES.TMEN_XP: {PREMIUM_TYPE.BASIC: 'premiumTmenXPFactor100', 
                              PREMIUM_TYPE.PLUS: 'premiumPlusTmenXPFactor100', 
                              PREMIUM_TYPE.VIP: 'premiumVipXPTmenFactor100'}}

PLAYER_INFO = [
 (
  'accountDBID', int, 0, None, 'any', ENTRY_TYPE.ACCOUNT_SELF),
 (
  'team', int, 1, None, 'skip', ENTRY_TYPE.ACCOUNT_SELF),
 (
  'clanDBID', int, 0, None, 'skip', ENTRY_TYPE.ACCOUNT_SELF),
 (
  'fortClanDBIDs', list, [], None, 'skip', ENTRY_TYPE.ACCOUNT_SELF),
 (
  'prebattleID', int, 0, None, 'skip', ENTRY_TYPE.PLAYER_INFO),
 (
  'team', int, 1, None, 'skip', ENTRY_TYPE.PLAYER_INFO),
 (
  'igrType', int, 0, None, 'skip', ENTRY_TYPE.PLAYER_INFO)]
PLAYER_INFO_META = Meta(PLAYER_INFO)

class UNIT_CLAN_MEMBERSHIP:
    NONE = 0
    ANY = 1
    SAME = 2


def dictToList(indices, d):
    l = [
     None] * len(indices)
    for name, index in indices.iteritems():
        l[index] = d[name]

    return l


def listToDict(names, l):
    d = {}
    for x in enumerate(names):
        d[x[1]] = l[x[0]]

    return d


class _VehicleInteractionDetailsItem(object):

    @staticmethod
    def __fmt2py(format):
        if format in ('f', ):
            return float
        return int

    def __init__(self, values, offset):
        self.__values = values
        self.__offset = offset

    def __getitem__(self, key):
        return self.__values[(self.__offset + VEH_INTERACTION_DETAILS_INDICES[key])]

    def __setitem__(self, key, value):
        self.__values[self.__offset + VEH_INTERACTION_DETAILS_INDICES[key]] = min(self.__fmt2py(VEH_INTERACTION_DETAILS_TYPES[key])(value), VEH_INTERACTION_DETAILS_MAX_VALUES[key])

    def __str__(self):
        return str(dict(self))

    def __iter__(self):
        return izip(VEH_INTERACTION_DETAILS_NAMES, self.__values[self.__offset:])


class VehicleInteractionDetails(object):

    def __init__(self, uniqueVehIDs, values):
        self.__uniqueVehIDs = uniqueVehIDs
        self.__values = values
        size = len(VEH_INTERACTION_DETAILS)
        self.__offsets = dict((x[1], x[0] * size) for x in enumerate(uniqueVehIDs))

    @staticmethod
    def fromPacked(packed):
        count = len(packed) / struct.calcsize(('').join(['<2I', VEH_INTERACTION_DETAILS_LAYOUT]))
        packedVehIDsLayout = '<%dI' % (2 * count,)
        packedVehIDsLen = struct.calcsize(packedVehIDsLayout)
        flatIDs = struct.unpack(packedVehIDsLayout, packed[:packedVehIDsLen])
        uniqueVehIDs = []
        for i in xrange(0, len(flatIDs), 2):
            uniqueVehIDs.append((flatIDs[i], flatIDs[(i + 1)]))

        values = struct.unpack('<' + VEH_INTERACTION_DETAILS_LAYOUT * count, packed[packedVehIDsLen:])
        return VehicleInteractionDetails(uniqueVehIDs, values)

    def __getitem__(self, uniqueVehID):
        if not isinstance(uniqueVehID, tuple):
            raise UserWarning(('Argument uniqueVehID should be tuple: {}').format(uniqueVehID))
        offset = self.__offsets.get(uniqueVehID, None)
        if offset is None:
            self.__uniqueVehIDs.append(uniqueVehID)
            offset = len(self.__values)
            self.__values += VEH_INTERACTION_DETAILS_INIT_VALUES
            self.__offsets[uniqueVehID] = offset
        return _VehicleInteractionDetailsItem(self.__values, offset)

    def __contains__(self, uniqueVehID):
        if not isinstance(uniqueVehID, tuple):
            raise UserWarning(('Argument uniqueVehID should be tuple: {}').format(uniqueVehID))
        return uniqueVehID in self.__offsets

    def __str__(self):
        return str(self.toDict())

    def pack(self):
        count = len(self.__uniqueVehIDs)
        flatIDs = []
        for uniqueID in self.__uniqueVehIDs:
            flatIDs.append(uniqueID[0])
            flatIDs.append(uniqueID[1])

        try:
            packed = struct.pack(('<%dI' % (2 * count)), *flatIDs) + struct.pack(('<' + VEH_INTERACTION_DETAILS_LAYOUT * count), *self.__values)
        except Exception as e:
            #from debug_utils import LOG_ERROR
            #LOG_ERROR('PACKING EXCEPTION', e, str(self))
            packed = ''

        return packed

    def toDict(self):
        return dict([ ((vehID, vehIdx), dict(_VehicleInteractionDetailsItem(self.__values, offset))) for (vehID, vehIdx), offset in self.__offsets.iteritems()
                    ])
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#################################################### 
# World of Tanks Battle Results to JSON            # 
# by BadButton at wotnumbers.com                   # 
# originally by Phalynx www.vbaddict.net (retired) # 
#################################################### 
import struct, json, time, sys, os, zlib, traceback, datetime
import cPickle, StringIO
import battle_results_shared
import battle_results_common
from itertools import izip 

VEH_INTERACTION_DETAILS_LEGACY = ('spotted', 'killed', 'hits', 'he_hits', 'pierced', 'damageDealt', 'damageAssisted', 'crits', 'fire') 
VEH_INTERACTION_DETAILS_INDICES_LEGACY = dict(((x[1], x[0]) for x in enumerate(VEH_INTERACTION_DETAILS_LEGACY))) 

VEHICLE_DEVICE_TYPE_NAMES = ('engine', 'ammoBay', 'fuelTank', 'radio', 'track', 'gun', 'turretRotator', 'surveyingDevice')
VEHICLE_TANKMAN_TYPE_NAMES = ('commander', 'driver', 'radioman', 'gunner', 'loader')

VEH_INTERACTION_DETAILS = (
 ('spotted', 'B', 1, 0),
 ('deathReason', 'b', 10, -1),
 ('directHits', 'H', 65535, 0),
 ('secondaryDirectHits', 'H', 65535, 0),
 ('explosionHits', 'H', 65535, 0),
 ('piercings', 'H', 65535, 0),
 ('secondaryPiercings', 'H', 65535, 0),
 ('damageDealt', 'H', 65535, 0),
 ('damageAssistedTrack', 'H', 65535, 0),
 ('damageAssistedRadio', 'H', 65535, 0),
 ('damageAssistedStun', 'H', 65535, 0),
 ('crits', 'I', 4294967295L, 0),
 ('fire', 'H', 65535, 0),
 ('stunNum', 'H', 65535, 0),
 ('stunDuration', 'f', 65535.0, 0.0),
 ('damageBlockedByArmor', 'I', 4294967295L, 0),
 ('damageReceived', 'H', 65535, 0),
 ('rickochetsReceived', 'H', 65535, 0),
 ('noDamageDirectHitsReceived', 'H', 65535, 0),
 ('targetKills', 'B', 255, 0))
VEH_INTERACTION_DETAILS_NAMES = [ x[0] for x in VEH_INTERACTION_DETAILS ]
VEH_INTERACTION_DETAILS_MAX_VALUES = dict(((x[0], x[2]) for x in VEH_INTERACTION_DETAILS))
VEH_INTERACTION_DETAILS_INIT_VALUES = [ x[3] for x in VEH_INTERACTION_DETAILS ]
VEH_INTERACTION_DETAILS_LAYOUT = ''.join([ x[1] for x in VEH_INTERACTION_DETAILS ])
VEH_INTERACTION_DETAILS_INDICES = dict(((x[1][0], x[0]) for x in enumerate(VEH_INTERACTION_DETAILS)))
VEH_INTERACTION_DETAILS_TYPES = dict(((x[0], x[1]) for x in VEH_INTERACTION_DETAILS))
  
  
parser = dict()
parser['version'] = "1.10.1.0"
parser['name'] = 'http://wotnumbers.com'
parser['processingTime'] = int(time.mktime(time.localtime()))

def usage(): 
    print str(sys.argv[0]) + 'battleresult-filename.dat [options]'
    print 'Options:'
    print '-f Formats output result to JSON pretty print (includes line breaks and indents)'
    print '-l Logging to file enabled, output to file: wotbr2j_log.txt'

  
def main(): 

    #import struct, json, time, sys, os, shutil, datetime
    global filename_source, filename_target, option_format, parser, option_logging, log_file
    global cachefile 
    
    cachefile = None
    option_format = 0
    option_logging = 0

    if len(sys.argv) == 1: 
        usage() 
        sys.exit(2) 

    script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
    for argument in sys.argv:
        #ironpython modified
        if argument == "-f": 
            option_format = 1
        if argument == "-l": 
            option_logging = 1
            log_file = os.path.join(script_dir, "wotbr2j_log.txt")

    filename_source = str(sys.argv[1])

    printmessage('', 1)
    printmessage('### WoT Battle Result to Json - version ' + parser['version'] + ' ###', 1) 
    printmessage('Time: ' + str(datetime.datetime.now()), 1)
    printmessage('Encoding: ' + str(sys.getdefaultencoding()) + ' - ' + str(sys.getfilesystemencoding()), 1)
    if argument == "-l":
        printmessage('Logging to file: ' + log_file, 0) 
    printmessage('Processing file: ' + filename_source, 1) 
      
    filename_target = os.path.splitext(filename_source)[0] 
    filename_target = filename_target + '.json' 
    
    if not os.path.exists(filename_source) or not os.path.isfile(filename_source):
        exitwitherror('Battle Result does not exists! file: ' + filename_source)
    if not os.access(filename_source, os.R_OK):
        exitwitherror('Cannot read Battle result, read-access was denied for file: ' + filename_source)

    cachefile = open(filename_source, 'rb') 
              
    try: 
        legacyBattleResultVersion, battleResults = cPickle.load(cachefile)
    except Exception, e: 
        exitwitherror('Battle Result cannot be read (pickle could not be read) ' + e.message) 

    if not 'battleResults' in locals(): 
        exitwitherror('Battle Result cannot be read (battleResults class does not exist)') 
        
    # Process file
    printmessage('Start processing', 1)
    issuccess, bresult = convertToFullForm(battleResults) 
    if issuccess == 0:
        printmessage("Failed processing battle: %s" % (bresult), 1)
        exitwitherror('Battle result cannot be read')
    
    if not 'personal' in bresult:
        exitwitherror('Battle Result cannot be read (personal class does not exist)')
    
    if len(list(bresult['personal'].keys())) < 10:
        for vehTypeCompDescr, ownResults in bresult['personal'].copy().iteritems():
            if type(ownResults) is dict:
                if 'details' in ownResults:
                    ownResults['details'] = handleDetailsCrits(ownResults['details'])
                
                for field in ('damageEventList', 'xpReplay', 'creditsReplay', 'tmenXPReplay', 'fortResourceReplay', 'goldReplay', 'freeXPReplay'):
                    ownResults[field] = None
                
            bresult['personal'][vehTypeCompDescr] = ownResults
    
    parser['result'] = 'ok'
    bresult['parser'] = parser
    
    # write json file now
    dumpjson(bresult) 

    printmessage('### Done ###', 1) 
    printmessage('', 0) 
    cachefile.close()
    # sys.exit(0) 

def convertToFullForm(compactForm): 
    # data structures copied to classes with short name
    brs = battle_results_shared #version 1.10.0.4
    brc = battle_results_common #from version 1.10.1.0

    #read raw binary data from battle file
    arenaUniqueID, avatarResults, battle_results, pickled = compactForm
    
    #convert to readable data
    battle_results = Unpickler.loads(zlib.decompress(battle_results))
    avatarResults = Unpickler.loads(zlib.decompress(avatarResults))

    #split battle_result 
    battle_results = battle_results.items()[0]
    tankId = battle_results[0]
    battle_results = battle_results[1]

    
    try:
        #prepare result class, add some initial values
        personal = {}
        br2jResult = {
            'arenaUniqueID': arenaUniqueID,
            'tankId': tankId,
            'personal': personal,
            'common': {},
            'players': {},
            'vehicles': {},
            'avatars': {}
        }

        #check for valid battle result   
        if len(brc.BATTLE_RESULTS) + 1 != len(battle_results):
           structLen = str(len(brc.BATTLE_RESULTS) + 1) 
           dataLen = str(len(battle_results))
           return 0, {'error': 'Wrong number of items in class: BATTLE_RESULTS (found: {}, expecting {})'.format(dataLen, structLen)}

        personal['avatar'] = battle_results = brc.BATTLE_RESULTS_META.unpackWthoutChecksum(battle_results)

        # for vehTypeCompDescr, ownResults in fullResultsList.iteritems():
        #     vehPersonal = personal[vehTypeCompDescr] = brs.VEH_FULL_RESULTS.unpackWthoutChecksum(ownResults)
        #     if type(vehPersonal) is dict:
        #         try:
        #             vehPersonal['details'] = brc.VehicleInteractionDetails.fromPacked(vehPersonal['details']).toDict()
        #         except Exception: 
        #             return 0, {}
        #         vehPersonal['isPrematureLeave'] = avatarResults['isPrematureLeave']
        #         vehPersonal['fairplayViolations'] = avatarResults['fairplayViolations']

        commonAsList, playersAsList, vehiclesAsList, avatarsAsList = Unpickler.loads(zlib.decompress(pickled))
        
        # COMMON_RESULTS_META = Meta(brc.BATTLE_RESULTS)
        # br2jResult['common'] = brc.BATTLE_RESULTS.unpackWthoutChecksum(commonAsList)
        #Fix from WoT 1.9.1.0
        # if 'accountCompDescr' in br2jResult['common']:
        #    del br2jResult['common']['accountCompDescr']

        for accountDBID, playerAsList in playersAsList.iteritems():
           br2jResult['players'][accountDBID] = brs.PLAYER_INFO_META.unpackWthoutChecksum(playerAsList)

        # for accountDBID, avatarAsList in avatarsAsList.iteritems():
        #     br2jResult['avatars'][accountDBID] = brs.AVATAR_PUBLIC_RESULTS.unpackWthoutChecksum(avatarAsList)

        # for vehicleID, vehiclesInfo in vehiclesAsList.iteritems():
        #     br2jResult['vehicles'][vehicleID] = []
        #     for vehTypeCompDescr, vehicleInfo in vehiclesInfo.iteritems():
        #         br2jResult['vehicles'][vehicleID].append(brs.VEH_PUBLIC_RESULTS.unpackWthoutChecksum(vehicleInfo))
        return 1, br2jResult 

    except IndexError, i:
        printmessage(traceback.format_exc(i), 1)
        return 0, {'error': '%s' % i.message}
    except KeyError, i:
        printmessage(traceback.format_exc(i), 1)
        return 0, {'error': 'Missing key in data: %s' % i.message}
    except Exception, e:
        printmessage(traceback.format_exc(e), 1)
        return 0, {'error': e}
    

def prepareForJSON(bresult):
    ## Fix values in personal class for non-valid json parameters
    if 'personal' in bresult:
        for subLevel, subLevelValues in bresult['personal'].copy().iteritems():

            #Remove unwanted entries
            if subLevel == 'avatar': 
                if 'avatarDamageEventList' in bresult['personal'][subLevel]:
                    del bresult['personal'][subLevel]['avatarDamageEventList']
                if 'squadBonusInfo' in bresult['personal'][subLevel]:
                    del bresult['personal'][subLevel]['squadBonusInfo']
            else:
                if 'c11nProgress' in bresult['personal'][subLevel]:
                    del bresult['personal'][subLevel]['c11nProgress']

            #Inspect and clean up values
            if subLevelValues is not None:
                #MC: This is a hack to remove suspicious entries ending with "The resulting string is not a valid number", typically removing binary 
                for detail in subLevelValues:
                    if (type(subLevelValues[detail]) is str):
                        subLevelValues[detail] = 0
                #Fix detail section having array as key
                if 'details' in subLevelValues:
                    newValues = arrayDictToString(subLevelValues['details'])
                    bresult['personal'][subLevel]['details'] = newValues
                #Fix for club section, old code from Phalynx, unsure if relevant any more or what it does ^_^
                if 'club' in subLevelValues:
                    if subLevelValues['club'] is not None:
                        if 'club' in subLevelValues:
                            if 'clubDossierPopUps' in subLevelValues['club']:
                                oldClubDossier = subLevelValues['club']['clubDossierPopUps'].copy()
                                subLevelValues['club']['clubDossierPopUps'] = dict()
                                for achievement, amount in oldClubDossier.iteritems():
                                    bresult['personal'][subLevel]['club']['clubDossierPopUps'][str(list(achievement)[0]) + '-' + str(list(achievement)[1])] = amount
        
        if len(bresult['personal'].copy())>1 and len(bresult['personal'].copy())<10 :
            pass
        
        #for subLevel, subLevelValues in bresult['personal'].copy().iteritems():
        #    if subLevelValues is not None:
        #        for detail in subLevelValues:
        #            if (type(subLevelValues[detail]) is str): # MC: This is a hack to remove suspicious entries. The resulting string is not a valid number.
        #                subLevelValues[detail] = 0
                    
        #        if 'details' in subLevelValues:
        #            newValues = detailsDictToString(subLevelValues['details'])
        #            bresult['personal'][subLevel]['details'] = newValues
    return bresult
            
def arrayDictToString(mydict):
    mydictcopy = dict()
    
    if not type(mydict) is dict:
        return mydictcopy    
    
    for key, value in mydict.iteritems():
        value['vehicleid'] = key[0]
        value['typeCompDescr'] = key[1]
        mydictcopy[str(key[0]) + '-' + str(key[1])] = value
    return mydictcopy
    
def exitwitherror(message, e=None, abort=False):
    global parser, cachefile
    if e is None:
        printmessage(message, 1)
    else:
        printmessage(message + e.message, 1)
        printmessage(traceback.format_exc(e), 1)
    if not abort:
        dossierheader = dict() 
        dossierheader['parser'] = dict() 
        dossierheader['parser']['result'] = "error"
        dossierheader['parser']['message'] = message 
        dumpjson(dossierheader) 
    if cachefile is not None:
        cachefile.close() 
    sys.exit(1) 

def dumpjson(bresult): 
    global option_logging, option_format, filename_target
    bresult = prepareForJSON(bresult)
    try:
        finalfile = open(filename_target, 'w') 
        
        # Debug section
        #test = json.dumps(bresult['arenaUniqueID'], ensure_ascii=False)
        #test = json.dumps(bresult['avatars'], ensure_ascii=False)
        #test = json.dumps(bresult['common'], ensure_ascii=False)
        #test = json.dumps(bresult['parser'], ensure_ascii=False)
        #test = json.dumps(bresult['players'], ensure_ascii=False)
        #test = json.dumps(bresult['vehicles'], ensure_ascii=False)
        #vehTypeCompDescr, ownResults = bresult['personal']
        #vehTypeCompDescrData = bresult['personal'][vehTypeCompDescr]
        #ownResultsData = bresult['personal'][ownResults]
        #test = json.dumps(vehTypeCompDescrData, ensure_ascii=False)
        #test = json.dumps(ownResultsData, ensure_ascii=False)
        
        if option_format == 1: 
            finalfile.write(json.dumps(bresult, ensure_ascii=False, skipkeys=True, sort_keys=True, indent=4)) 
        else: 
            finalfile.write(json.dumps(bresult, ensure_ascii=False, skipkeys=True))        
        # IRONPYTHON MODIFIED: close dossier output file
        finalfile.close()
    except Exception, e:
        if finalfile is not None: 
            finalfile.close() # IRONPYTHON MODIFIED: close dossier output file
        exitwitherror("Exception: ", e, abort=True)

def dictToList(indices, d): 
    l = [None] * len(indices) 
    for name, index in indices.iteritems(): 
        l[index] = d[name] 
    return l 

def listToDict(names, l): 
    d = {} 
    for x in enumerate(names): 
        d[x[1]] = l[x[0]] 
    return d 

def print_array(oarray):
    print json.dumps(oarray, sort_keys=True, indent=4)

def handleDetailsCrits(details):
    if type(details) is dict and len(details) > 0: 
        for vehicleid, detail_values in details.items(): 
            details[vehicleid]['critsDestroyedTankmenList'] = getDestroyedTankmen(detail_values)
            details[vehicleid]['critsCriticalDevicesList'] = getCriticalDevicesList(detail_values)
            details[vehicleid]['critsDestroyedDevicesList'] = getDestroyedDevicesList(detail_values)
            details[vehicleid]['critsCount'] = len(details[vehicleid]['critsDestroyedTankmenList']) + len(details[vehicleid]['critsDestroyedTankmenList']) + len(details[vehicleid]['critsDestroyedTankmenList'])
    return details

def getDestroyedTankmen(detail_values):
    destroyedTankmenList = [] 
    if detail_values['crits']  >0: 
        destroyedTankmen = detail_values['crits'] >> 24 & 255
          
        for shift in range(len(VEHICLE_TANKMAN_TYPE_NAMES)): 
            if 1 << shift & destroyedTankmen: 
                destroyedTankmenList.append(VEHICLE_TANKMAN_TYPE_NAMES[shift]) 
    return destroyedTankmenList  

def getCriticalDevicesList(detail_values):
    criticalDevicesList = [] 
    if detail_values['crits'] > 0: 
        criticalDevices = detail_values['crits'] & 4095
          
        for shift in range(len(VEHICLE_DEVICE_TYPE_NAMES)): 
            if 1 << shift & criticalDevices: 
                criticalDevicesList.append(VEHICLE_DEVICE_TYPE_NAMES[shift]) 
    return criticalDevicesList
        
def getDestroyedDevicesList(detail_values):
    destroyedDevicesList = [] 
    if detail_values['crits'] > 0: 
        destroyedDevices = detail_values['crits'] >> 12 & 4095
        for shift in range(len(VEHICLE_DEVICE_TYPE_NAMES)): 
        
            if 1 << shift & destroyedDevices: 
                destroyedDevicesList.append(VEHICLE_DEVICE_TYPE_NAMES[shift]) 
    return destroyedDevicesList 
  
def printmessage(logtext, to_console): 
    #import datetime, os 

    if to_console == 1:
        print str(logtext)
        
    if option_logging == 1:
        now = datetime.datetime.now() 
        message = str(now.strftime("%Y-%m-%d %H:%M:%S")) + " - " + str(logtext) + "\r\n"
        logFile = open(log_file, "a+b") 
        logFile.write(message) 
        logFile.close() 
    
class _VehicleInteractionDetailsItem(object): 
    
    def __init__(self, values, offset): 
        self.__values = values 
        self.__offset = offset 
    def __getitem__(self, key): 
        return self.__values[self.__offset + VEH_INTERACTION_DETAILS_INDICES[key]] 
    def __setitem__(self, key, value): 
        self.__values[self.__offset + VEH_INTERACTION_DETAILS_INDICES[key]] = min(int(value), VEH_INTERACTION_DETAILS_MAX_VALUES[key]) 
    def __iter__(self): 
        return izip(VEH_INTERACTION_DETAILS_NAMES, self.__values[self.__offset:]) 


class VehicleInteractionDetails(object): 
  
    def __init__(self, vehicleIDs, values): 
        self.__vehicleIDs = vehicleIDs 
        self.__values = values 
        size = len(VEH_INTERACTION_DETAILS) 
        self.__offsets = dict(((x[1], x[0] * size) for x in enumerate(self.__vehicleIDs))) 
  
    @staticmethod
    def fromPacked(packed): 
        count = len(packed) / struct.calcsize(''.join(['<I', VEH_INTERACTION_DETAILS_LAYOUT])) 
        packedVehIDsLayout = '<%dI' % (count,) 
        packedVehIDsLen = struct.calcsize(packedVehIDsLayout) 
        vehicleIDs = struct.unpack(packedVehIDsLayout, packed[:packedVehIDsLen]) 
        values = struct.unpack('<' + VEH_INTERACTION_DETAILS_LAYOUT * count, packed[packedVehIDsLen:]) 
        return VehicleInteractionDetails(vehicleIDs, values) 
  
    def __getitem__(self, vehicleID): 
        offset = self.__offsets.get(vehicleID, None) 
        if offset is None: 
            self.__vehicleIDs.append(vehicleID) 
            offset = len(self.__values) 
            self.__values += VEH_INTERACTION_DETAILS_INIT_VALUES 
  
            self.__offsets[vehicleID] = offset 
        return _VehicleInteractionDetailsItem(self.__values, offset) 
  
    def __contains__(self, vehicleID): 
        return vehicleID in self.__offsets 
  
    def __str__(self): 
        return str(self.toDict()) 
  
    def pack(self): 
        count = len(self.__vehicleIDs) 
        packed = struct.pack(('<%dI' % count), *self.__vehicleIDs) + struct.pack(('<' + VEH_INTERACTION_DETAILS_LAYOUT * count), *self.__values) 
  
        return packed 
  
    def toDict(self): 
        return dict([ (vehID, dict(_VehicleInteractionDetailsItem(self.__values, offset))) for vehID, offset in self.__offsets.iteritems() ]) 
  
class _VehicleInteractionDetailsItem_LEGACY(object): 
  
    def __init__(self, values, offset): 
        self.__values = values 
        self.__offset = offset 
  
    def __getitem__(self, key): 
        return self.__values[self.__offset + VEH_INTERACTION_DETAILS_INDICES_LEGACY[key]] 
  
    def __setitem__(self, key, value): 
        self.__values[self.__offset + VEH_INTERACTION_DETAILS_INDICES_LEGACY[key]] = min(int(value), 65535) 
  
    def __str__(self): 
        return str(dict(self)) 
  
    def __iter__(self): 
        return izip(VEH_INTERACTION_DETAILS_LEGACY, self.__values[self.__offset:]) 
  
  
class VehicleInteractionDetails_LEGACY(object): 
  
    def __init__(self, vehicleIDs, values): 
        self.__vehicleIDs = vehicleIDs 
        self.__values = values 
        size = len(VEH_INTERACTION_DETAILS_LEGACY) 
        self.__offsets = dict(((x[1], x[0] * size) for x in enumerate(self.__vehicleIDs))) 
  
    @staticmethod
    def fromPacked(packed): 
        size = len(VEH_INTERACTION_DETAILS_LEGACY) 
        count = len(packed) / struct.calcsize('I%dH' % size) 
        unpacked = struct.unpack('%dI%dH' % (count, count * size), packed) 
        vehicleIDs = unpacked[:count] 
        values = unpacked[count:] 
        return VehicleInteractionDetails_LEGACY(vehicleIDs, values) 
  
    def __getitem__(self, vehicleID): 
        offset = self.__offsets.get(vehicleID, None) 
        if offset is None: 
            self.__vehicleIDs.append(vehicleID) 
            offset = len(self.__values) 
            size = len(VEH_INTERACTION_DETAILS_LEGACY) 
            self.__values += [0] * size 
            self.__offsets[vehicleID] = offset 
        return _VehicleInteractionDetailsItem_LEGACY(self.__values, offset) 
  
    def __contains__(self, vehicleID): 
        return vehicleID in self.__offsets 
  
    def __str__(self): 
        return str(self.toDict()) 
  
    def pack(self): 
        count = len(self.__vehicleIDs) 
        size = len(VEH_INTERACTION_DETAILS_LEGACY) 
        packed = struct.pack(('%dI' % count), *self.__vehicleIDs) + struct.pack(('%dH' % count * size), *self.__values) 
        return packed 
  
    def toDict(self): 
        return dict([ (vehID, dict(_VehicleInteractionDetailsItem_LEGACY(self.__values, offset))) for vehID, offset in self.__offsets.iteritems() ]) 


class Unpickler(object): 
    @classmethod
    def loads(cls, pickle_string):
        try:
            unpickler = cPickle.Unpickler(StringIO.StringIO(pickle_string))
            return unpickler.load()
        except Exception, e:
            cachefile.close()
            raise cPickle.UnpicklingError('Unpickler Error: ' + e.message)
            
    @classmethod
    def load(cls, pickle_file):
        try:
            unpickler = cPickle.Unpickler(pickle_file)
            return unpickler.load()
        
        except EOFError, er:
            # IRONPYTHON MODIFIED: close dossier input file
            cachefile.close()
            raise cPickle.UnpicklingError('Unpickler EOF Error: ' + er.message)
        
        except Exception, e:
            # IRONPYTHON MODIFIED: close dossier input file
            cachefile.close()
            raise cPickle.UnpicklingError('Unpickler Error: ' + e.message)

if __name__ == '__main__': 
    main() 

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.