-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyracedash.py
97 lines (82 loc) · 3.24 KB
/
pyracedash.py
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
#!/usr/bin/env python3
import configparser
import os
import pygame
import requests
import sys
import json
def main(args):
# Get configurations
config = configparser.ConfigParser()
config.read(os.path.dirname(os.path.realpath(__file__)) + '/conf.ini')
# Initialization
pygame.init()
clock = pygame.time.Clock()
# Hide mouse cursor
pygame.mouse.set_visible(False)
# Set game display
if config.get('global', 'FULLSCREEN') == '1':
fullscreen = pygame.FULLSCREEN
else:
fullscreen = 0
screen = pygame.display.set_mode((int(config.get('global', 'X_RES')),
int(config.get('global', 'Y_RES'))),
fullscreen)
# Set window caption
pygame.display.set_caption(config.get('global', 'TITLE'))
# Get theme
try:
theme_name = config.get('global', 'THEME')
theme_class = getattr(__import__('themes.' + theme_name + '.' + theme_name, fromlist=[theme_name]), theme_name)
except (ModuleNotFoundError, configparser.NoOptionError):
theme_class = getattr(__import__('themes.Fallback.Fallback', fromlist=['Fallback']), 'Fallback')
theme = theme_class(pygame, screen, (int(config.get('global', 'X_RES')), int(config.get('global', 'Y_RES'))))
# Create request url
url = 'http://' + config.get('host', 'IP_ADDRESS') + ':' + config.get('host', 'PORT') + '/crest'
if config.get('host', 'PORT') == '8180':
url += '2'
url += '/v1/api'
if len(theme.needed_modules) > 0:
url += '?'
for mod in theme.needed_modules:
url += mod + '=true&'
# Game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
return
# Get game data
try:
# If debug mode...
if len(args) > 1 and args[1] == 'debug':
with open(os.path.dirname(os.path.realpath(__file__)) + '/crest2.json') as json_data:
theme.refresh(json.load(json_data))
else:
theme.refresh(requests.get(url, timeout=0.1).json())
except requests.exceptions.ConnectionError:
# Cannot connect to host
# TODO: Make it prettier, and use relative font size?
screen.fill((0, 0, 0))
screen.blit(pygame.font.SysFont(None, 32).render(
'Connection error! Is Host IP address correct? Is CREST running?',
True,
(255, 255, 255)),
(10, 10))
except requests.exceptions.ReadTimeout:
pass
except KeyError:
# PCARS is not running or Shared Memory is disabled
# TODO: Make it prettier, and use relative font size?
screen.fill((0, 0, 0))
screen.blit(pygame.font.SysFont(None, 28).render(
'Connection successful! Please run Project CARS with Shared Memory enabled.',
True,
(255, 255, 255)),
(10, 10))
pygame.display.update()
clock.tick(int(config.get('global', 'FPS')))
if __name__ == '__main__':
main(sys.argv)