Guest User

Untitled

a guest
Jan 19th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. .
  2. ├── config.py (global configuration)
  3. ├── core
  4. │   ├── client.py (generic requests class)
  5. │   └── persist.py (mongodb layer class)
  6. ├── pubg
  7. │   ├── controller.py (handler for pubg)
  8. │   └── models.py (models for pubg)
  9. └── main.py
  10.  
  11. class Client:
  12. ...
  13. def request(self, endpoint, params=None:
  14. response = self.session.get(endpoint, timeout=TIMEOUT, params=params)
  15. return response.json()
  16.  
  17. class Pubg(Client):
  18. # Which includes the calls to the official endpoints {palyer, match, telemtry}
  19. ...
  20. def players(self, params, shard):
  21. res = []
  22. url = f'{SHARD_URL}/{shard}/players'
  23. response = self.request(url, params)
  24. res.append(Player(item))
  25. return res
  26.  
  27. class PubgManager(Pubg):
  28. # Which is used to transform the data in the shape as I really want to be stored (either in mongo or disk)
  29. # I kept in the class the list of matches to process (matches_to_process), shard and if I want to use ids or real names (use_ids var)
  30. def __init__(self, shard, players, mongo):
  31. super().__init__(api_key)
  32. # Keep mongo instance in order to persist data later
  33. self.mongo = mongo
  34. self.mongo_pubg = mongo.get_client().pubg
  35. ...
  36.  
  37. def process_matches():
  38. ...
  39.  
  40. def process_matches(self, list_matches, telemetry=True, store_mongo=True, store_disk=False, return_data=False):
  41. # Create the Data Structure to return
  42. res = dict({
  43. 'match_data': list(),
  44. 'participant_data': list(),
  45. 'telemetry_users': list(),
  46. 'telemetry_match': list(),
  47. })
  48.  
  49. # Iterate over the matches id
  50. for m_id in list_matches:
  51.  
  52. # get info from matches
  53. m_info = self.match(m_id, self.shard)
  54. m_data = m_info.get_data()
  55. participants = {
  56. m_id: m_data.get('participants')
  57. }
  58. # Store info
  59. if store_mongo:
  60. self.mongo.insert_item('matches', m_data)
  61. self.mongo.insert_item('participants', participants)
  62.  
  63. # Update response
  64. if return_data:
  65. res.get('match_data').append(m_data)
  66. res.get('participant_data').append(participants)
  67.  
  68. if telemetry:
  69. t_url = m_data.get('telemetry')
  70. t_data = self.telemetry(t_url, m_id).get_data().get(
  71. 'telemetry')
  72.  
  73. user_activity = list()
  74. match_activity = list()
  75. other_activity = list()
  76.  
  77. for action in t_data:
  78. # We are only interested in the events AFTER lift off
  79. # The only event without `common` is matchStart which it's
  80. # been just pop out the list
  81. is_game = action.get('common').get('isGame') >= 0.1
  82. # if is_game:
  83. # tele_ingame_data.append(action)
  84.  
  85. action_type = action.get('_T')
  86. pl = self.players
  87. if is_game and action_type in EVENTS_USER:
  88. # Is the player attacking somebody?
  89. try:
  90. attacks = action.get('attacker').get('name') in pl
  91. if attacks:
  92. user_activity.append(action)
  93. except AttributeError:
  94. pass
  95. # Is the player beign attacked?
  96. try:
  97. is_victim = action.get('victim').get('name') in pl
  98. if is_victim:
  99. user_activity.append(action)
  100. except AttributeError:
  101. pass
  102. # Is the player interacting with something ?
  103. try:
  104. user = action.get('character').get('name') in pl
  105. if user:
  106. user_activity.append(action)
  107. except AttributeError:
  108. pass
  109. elif is_game and action_type in EVENTS_GAME:
  110. match_activity.append(action)
  111.  
  112. elif is_game:
  113. other_activity.append(action)
  114. else:
  115. pass
  116. user_info = {
  117. '_id': m_id,
  118. 'id': m_id,
  119. 'data': user_activity
  120. }
  121. match_info = {
  122. '_id': m_id,
  123. 'id': m_id,
  124. 'match_details': match_info,
  125. 'data': match_activity
  126. }
  127. # Update values
  128. if return_data:
  129. res.get('telemetry_users').append(user_info)
  130. res.get('telemetry_match').append(match_info)
  131. # Store info
  132. if store_mongo:
  133. self.mongo.insert_item('telemetry_users', user_info)
  134. self.mongo.insert_item('telemetry_match', match_info)
  135. if store_disk:
  136. path = 'output/'
  137. filename = f'{m_id}.json'
  138. data = {
  139. 'match_info': match_info,
  140. 'telemetry': t_data,
  141. }
  142. store_json(path, filename, data)
Add Comment
Please, Sign In to add comment