Suppenbiatch

CSGO's Game Coordinator

Dec 8th, 2020
3,875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.07 KB | None | 0 0
  1. from csgo.client import CSGOClient
  2. from steam.client import SteamClient
  3.  
  4. import discord_channels as credentials
  5. from write import write
  6.  
  7.  
  8. def to_steam_id(account_id):
  9.     return int(account_id) + 76561197960265728
  10.  
  11.  
  12. def to_account_id(steam_id):
  13.     return int(steam_id) - 76561197960265728
  14.  
  15.  
  16. def parse_match(response, match_number: int = 0):
  17.     matches = str(response).split('}\n, ')
  18.     if len(matches) - 1 < match_number:
  19.         print(f'Found only {len(matches)}')
  20.         match_number = 0
  21.     infos = matches[match_number].split('\n')
  22.     result = {}
  23.     item_list = [info.split(': ') for info in infos if ': ' in info]
  24.  
  25.     for _key, _value in item_list:
  26.         key = _key.replace(' ', '').replace('[', '').replace(']', '')
  27.         value = _value.replace('"', '')
  28.         if key not in result:
  29.             result[key] = [value]
  30.         else:
  31.             result[key].append(value)
  32.  
  33.     # remove single item lists, convert all possible items to integers
  34.     for key, _lists in result.items():
  35.         try:
  36.             lists = list(map(int, _lists))
  37.         except ValueError:
  38.             lists = _lists
  39.  
  40.         if len(lists) == 1:
  41.             result[key] = lists[0]
  42.         else:
  43.             result[key] = lists
  44.     return result
  45.  
  46.  
  47. client = SteamClient()
  48. cs = CSGOClient(client)
  49.  
  50.  
  51. def get_current_player(steam_id):
  52.     cs.request_live_game_for_user(to_account_id(steam_id))
  53.     r, = cs.wait_event('live_game_for_user')
  54.     if r is not None:
  55.         data = parse_match(r)
  56.         if 'account_ids' in data:
  57.             return list(map(to_steam_id, data['account_ids']))
  58.     return None
  59.  
  60.  
  61. @client.on('logged_on')
  62. def start_csgo():
  63.     write('Logged in to Steam')
  64.     cs.launch()
  65.     write('CS:GO game coordinator starting')
  66.  
  67.  
  68. @cs.on('ready')
  69. def gc_ready():
  70.     write('CS:GO game coordinator running')
  71.     # ready to call get_current_player() here
  72.     # a discord bot should now listen for a eg. !steamid and call get_current_player()
  73.  
  74. client.cli_login(username=credentials.username, password=credentials.password)
  75. client.run_forever()
  76.  
  77.  
Add Comment
Please, Sign In to add comment