-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
102 lines (91 loc) · 2.66 KB
/
config.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
98
99
100
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pygame
class ConfigManager(object):
def __init__(self, filename="playersettings.txt"):
self.filename = filename
self.keys = {}
def load(self):
try:
fp = open(self.filename, "r")
except:
self.initConfigFile(self.filename)
return
for line in fp:
#skipping comments
pos = line.find('#')
if pos > -1:
line = line[:pos]
try:
key, value = map(lambda x: x.strip(), line.split("="))
except:
#skip empty lines
continue
try:
#may need to check for values < 0 and > 255
value = int(value)
except:
#skipping every param that is not the server address
if key != 'server-address':
continue
if key == 'screen-width':
#print "Setting GLOBAL Screen Width : %s" % (value)
self.keys["SCREEN_WIDTH"] = value
elif key == 'screen-height':
#print "Setting GLOBAL Screen Height : %s" % (value)
self.keys["SCREEN_HEIGHT"] = value
elif key == 'server-address':
self.keys["SERVER_ADDRESS"] = value
elif key == 'server-port':
self.keys["SERVER_PORT"] = value
elif key == 'key-up':
self.keys["KEY_UP"] = value
elif key == 'key-down':
self.keys["KEY_DOWN"] = value
elif key == 'key-left':
self.keys["KEY_LEFT"] = value
elif key == 'key-right':
self.keys["KEY_RIGHT"] = value
elif key == 'key-select':
self.keys["KEY_SELECT_TARGET"] = value
elif key == 'key-attack':
self.keys["KEY_ATTACK"] = value
elif key == 'key-sit':
self.keys["KEY_SIT"] = value
fp.close()
def initConfigFile(self, filename):
print "Invalid configuration... resetting..."
configdata = [ ('screen-width', 800),\
('screen-height', 600),\
('server-address', "88.173.217.230"),\
('server-port', 18647),\
('key-up', pygame.K_UP),\
('key-down', pygame.K_DOWN),\
('key-left', pygame.K_LEFT),\
('key-right', pygame.K_RIGHT),\
('key-select', pygame.K_a),\
('key-attack', pygame.K_e),\
('key-sit', pygame.K_c)
]
lines = []
fp = open(filename, "w")
for key, value in configdata:
lines.append("%s = %s\n" % (key, str(value)))
fp.writelines(lines)
fp.close()
self.load()
cm = ConfigManager()
cm.load()
SCREEN_WIDTH = cm.keys["SCREEN_WIDTH"]
SCREEN_HEIGHT = cm.keys["SCREEN_HEIGHT"]
SERVER_ADDRESS = cm.keys["SERVER_ADDRESS"]
SERVER_PORT = cm.keys["SERVER_PORT"]
# controls
KEY_UP = cm.keys["KEY_UP"]
KEY_DOWN = cm.keys["KEY_DOWN"]
KEY_LEFT = cm.keys["KEY_LEFT"]
KEY_RIGHT = cm.keys["KEY_RIGHT"]
KEY_SELECT_TARGET = cm.keys["KEY_SELECT_TARGET"]
KEY_ATTACK = cm.keys["KEY_ATTACK"]
KEY_SIT = cm.keys["KEY_SIT"]
CONTROL_KEY_LIST = [KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_SELECT_TARGET, KEY_ATTACK, KEY_SIT]