1
2 import GameKeys
3 from random import randint
4 import os.path
5
7 """
8 singleton class to parse the config file
9
10 @author: Niner
11 @contact: Admiral.Niner@googlemail.com
12
13 @cvar __instance: private class variable to check if singleteon is initilized
14 @type __instance: object
15
16 @ivar ram: True if config file is not writable
17 @type ram: Boolean
18 @ivar keyList: a list of the ascii values for the specific keystrokes
19 @type keyList: List
20 @ivar powerupsList: a list of the number of collected powerups
21 @type powerupsList: List
22 @ivar collectionList: a list of the collected merhcandise objects
23 @type collectionList: List
24 @ivar loaded: True if config file was loaded and set
25 @type loaded: Boolean
26 """
27
28 __instance = None
29
31 if self.__instance is None:
32 self.__instance = object.__new__(self, *args, **kargs)
33
34
35 self.ram = False
36 self.keyList = []
37 self.powerupsList = []
38 self.collectionList = []
39 self.loaded = False
40
41 return self.__instance
42
45
47 """
48 checks if file exists and is writable
49 otherwise create it with default values
50 or throw exception when user has wrong privileges
51
52 @rtype: Boolean
53 @return: True if config was loaded, otherwise False
54 """
55 result = False
56 try:
57 configfile = open(os.path.expanduser('~/stairs.cfg'), "a+")
58
59 error = False
60 configfile.close()
61 configfile = open(os.path.expanduser('~/stairs.cfg'), "r")
62 self.setDefault()
63 self.ram = False
64 option = configfile.readline()
65
66 while (option != ""):
67 result = True
68 if option[:-1] == "[keycontrol]":
69 key_count = 0
70 option = configfile.readline()
71 while (option != "" and option[0] != "["):
72 if key_count >= 6:
73 error = True
74 values = option[:-1].split("=")
75 self.setControlKeyValue(values[0], int(values[1]))
76 option = configfile.readline()
77 key_count += 1
78
79 if option[:-1] == "[powerups]":
80 key_count = 0
81 option = configfile.readline()
82 while (option != "" and option[0] != "["):
83 if key_count >= 6:
84 error = True
85 values = option[:-1].split("=")
86 self.setPowerUps(values[0], int(values[1]))
87 option = configfile.readline()
88 key_count += 1
89
90 if option[:-1] == "[collecteditems]":
91 option = configfile.readline()
92 while (option != "" and option[0] != "["):
93 self.setMerch(option[:-1])
94 option = configfile.readline()
95
96 configfile.close()
97
98 except IOError:
99 print "wrong privileges to create config file"
100 self.setDefault()
101 self.ram = True
102 if result:
103 self.loaded = True
104
105 return result
106
108 """
109 set input keys to default (awsd) and fills properties with random values
110 """
111
112 self.keyList = []
113 self.keyList.append(['fwd', GameKeys.WKEY])
114 self.keyList.append(['bwd', GameKeys.SKEY])
115 self.keyList.append(['dxr', GameKeys.DKEY])
116 self.keyList.append(['snl', GameKeys.AKEY])
117 self.keyList.append(['uwd', GameKeys.SPACEKEY])
118 self.keyList.append(['dwd', GameKeys.LEFTSHIFTKEY])
119
120 self.powerupsList = []
121 amount = randint(1,4)
122 self.powerupsList.append(['health', amount])
123 amount = randint(1,4)
124 self.powerupsList.append(['newplatf', amount])
125 amount = randint(1,4)
126 self.powerupsList.append(['power', amount])
127 amount = randint(1,4)
128 self.powerupsList.append(['ammo', amount])
129 amount = randint(1,4)
130 self.powerupsList.append(['oneighty', amount])
131 amount = randint(1,4)
132
133 self.powerupsList.append(['low_grav', 0])
134 amount = randint(1,4)
135
136 self.powerupsList.append(['high_grav', 0])
137 amount = randint(1,4)
138
139 self.powerupsList.append(['delay_view', 0])
140 amount = randint(1,4)
141
142 self.powerupsList.append(['transwarp', 0])
143 self.powerupsList.append(['sniper', 0])
144
145 self.loaded = True
146
148 """
149 writes config values to file
150 """
151 wFile = open(os.path.expanduser('~/stairs.cfg'), 'w')
152 wString = "[keycontrol]\n"
153 for i in self.keyList:
154 wString += (i[0]+"="+str(i[1])+"\n")
155 wString += "[powerups]\n"
156 for i in self.powerupsList:
157 wString += (i[0]+"="+str(i[1])+"\n")
158 wString += "[collecteditems]\n"
159 for i in self.collectionList:
160 wString += (i+"\n")
161 wFile.write(wString)
162
164 """
165 fills specific options in category 'keycontrol' with new values
166 (dependent on the players orientation)
167
168 @type key: String
169 @param key: represents the key in the in the control key section of the config file
170 @type value: Integer
171 @param value: the value of the given key
172 """
173
174 for i in self.keyList:
175 if i[0] == key:
176 i[1] = value
177
179 """
180 returns the value to the given key in the list of user's input keys
181
182 key = String
183 @type key: String
184 @param key: the key out of the control key section
185 @rtype: Integer
186 @return: returns the value of the given key
187 """
188
189 for i in self.keyList:
190 if i[0] == key:
191 return i[1]
192 return -1
193
195 """
196 fills specific options in category 'powerups' with new values
197 (collected player properties)
198
199 @type key: String
200 @param key: represents the key in the in the powerups section of the config file
201 @type value: Integer
202 @param value: the value of the the given key
203 """
204
205 for i in self.powerupsList:
206 if i[0] == key:
207 i[1] = value
208
210 """
211 returns the value to the given key in the list of user's powerups
212
213 @type key: String
214 @param key: the name of the powerup
215
216 @rtype: Integer
217 @return: returns the number of the given key
218 """
219
220 for i in self.powerupsList:
221 if i[0] == key:
222 return i[1]
223 return 0
224
226 """
227 fills specific options in category 'collecteditems' with new values
228 (collected player merchandise)
229
230 @type merch_name: String
231 @param merch_name: the name of the merchandise object
232 """
233
234 exist = False
235 for i in self.collectionList:
236 if i == merch_name:
237 exist = True
238
239 if not exist:
240 self.collectionList.append(merch_name)
241