Total Complexity | 43 |
Total Lines | 302 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like osci.dialog.BookingDlg often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # |
||
2 | # Copyright 2001 - 2016 Ludek Smid [http://www.ospace.net/] |
||
3 | # |
||
4 | # This file is part of Outer Space. |
||
5 | # |
||
6 | # Outer Space is free software; you can redistribute it and/or modify |
||
7 | # it under the terms of the GNU General Public License as published by |
||
8 | # the Free Software Foundation; either version 2 of the License, or |
||
9 | # (at your option) any later version. |
||
10 | # |
||
11 | # Outer Space is distributed in the hope that it will be useful, |
||
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
14 | # GNU General Public License for more details. |
||
15 | # |
||
16 | # You should have received a copy of the GNU General Public License |
||
17 | # along with Outer Space; if not, write to the Free Software |
||
18 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
||
19 | # |
||
20 | import time |
||
21 | |||
22 | import pygameui as ui |
||
23 | |||
24 | import ige |
||
25 | from osci import client |
||
26 | from osci import gdata |
||
27 | from PasswordDlg import PasswordDlg |
||
28 | |||
29 | |||
30 | class BookingDlg: |
||
31 | """ There you subscribe to the new galaxy.""" |
||
32 | |||
33 | def __init__(self, app): |
||
34 | self.app = app |
||
35 | self.createUI() |
||
36 | self.passwordDlg = PasswordDlg(app, hide = False) |
||
37 | self.displayGoal = False |
||
38 | self.displayPublic = True |
||
39 | self.offering = client.cmdProxy.getBookingOffers() |
||
40 | self.bookingInfo = client.cmdProxy.getBookingAnswers() |
||
41 | self.selectionID = None |
||
42 | |||
43 | def display(self, caller = None): |
||
44 | self.selectionID = None |
||
45 | self.caller = caller |
||
46 | if self.show(): |
||
47 | self.win.show() |
||
48 | |||
49 | def hide(self): |
||
50 | self.win.hide() |
||
51 | |||
52 | def show(self): |
||
53 | self._setButtons() |
||
54 | self._showMenu() |
||
55 | self._showSelection() |
||
56 | return True |
||
57 | |||
58 | def _setButtons(self): |
||
59 | self.win.vBookingPublic.visible = self.displayPublic |
||
60 | self.win.vBookingPrivate.visible = not self.displayPublic |
||
61 | self.win.vCreatePrivate.visible = self.displayPublic |
||
62 | self.win.vDeletePrivate.visible = not self.displayPublic |
||
63 | if self.displayPublic: |
||
64 | self.win.vPublicToggle.text = _("Show private bookings") |
||
1 ignored issue
–
show
|
|||
65 | else: |
||
66 | self.win.vPublicToggle.text = _("Show public bookings") |
||
67 | if self.selectionID: |
||
68 | book = self.bookingInfo[self.selectionID] |
||
69 | scenario = self.offering[book.gal_type].scenario |
||
70 | self.win.vCreatePrivate.enabled = scenario != ige.ospace.Const.SCENARIO_SINGLE |
||
71 | self.win.vDeletePrivate.enabled = book.owner_nick == client.account.nick |
||
72 | self.win.vToggle.enabled = book.owner_nick != client.account.nick |
||
73 | else: |
||
74 | self.win.vCreatePrivate.enabled = False |
||
75 | self.win.vDeletePrivate.enabled = False |
||
76 | |||
77 | def _showMenu(self): |
||
78 | itemsPublic = [] |
||
79 | itemsPrivate = [] |
||
80 | for bookID in self.bookingInfo: |
||
81 | if bookID < ige.Const.BID_FREESTART: |
||
82 | continue |
||
83 | book = self.bookingInfo[bookID] |
||
84 | tPos = book.capacity |
||
85 | tCur = book.bookings |
||
86 | rawTime = book.last_creation |
||
87 | tScenario = gdata.gameScenarios[self.offering[book.gal_type].scenario] |
||
88 | isSelected = book.is_booked |
||
89 | if rawTime: |
||
90 | tTime = time.strftime(_("%m-%d-%y %H:%M"), time.localtime(rawTime)) |
||
1 ignored issue
–
show
|
|||
91 | else: |
||
92 | tTime = _('N/A') |
||
93 | if isSelected: |
||
94 | tChoice = '*' |
||
95 | else: |
||
96 | tChoice = '' |
||
97 | item = ui.Item(book.gal_type, tPos = tPos, tCur = tCur, tTime = tTime,tChoice = tChoice, tScenario = tScenario, tOwner = book.owner_nick, tID = bookID) |
||
98 | if book.owner_nick: |
||
99 | itemsPrivate.append(item) |
||
100 | else: |
||
101 | itemsPublic.append(item) |
||
102 | self.win.vBookingPublic.items = itemsPublic |
||
103 | self.win.vBookingPublic.itemsChanged() |
||
104 | self.win.vBookingPrivate.items = itemsPrivate |
||
105 | self.win.vBookingPrivate.itemsChanged() |
||
106 | |||
107 | def _showSelection(self): |
||
108 | if self.selectionID: |
||
109 | book = self.bookingInfo[self.selectionID] |
||
110 | selection = self.offering[book.gal_type] |
||
111 | self._displayText(book.gal_type, selection.scenario) |
||
112 | self.win.vInfo.offsetRow = 0 |
||
113 | self.win.vInfo.vertScrollbar.slider.position = 0 |
||
114 | |||
115 | self.win.vPlanets.text = _('{0} - {1}'.format(selection.minPlanets, selection.maxPlanets)) |
||
1 ignored issue
–
show
|
|||
116 | self.win.vRadius.text = selection.radius |
||
117 | self.win.vPlayerGroup.text = selection.playerGroup |
||
118 | self.win.vResources.text = [", ".join(map(lambda x: gdata.stratRes[x], selection.resources))] |
||
119 | self.win.vResources.offsetRow = 0 |
||
120 | if selection.challenges: |
||
121 | self.win.vChallenges.text = ", ".join(map(lambda x: gdata.gameChallenges[x], selection.challenges)) |
||
122 | else: |
||
123 | self.win.vChallenges.text = "" |
||
124 | |||
125 | def _displayText(self, gal_type, scenario): |
||
126 | try: |
||
127 | if self.displayGoal: |
||
128 | self.win.vInfo.text = [gdata.gameScenarioDescriptions[scenario]] |
||
129 | else: |
||
130 | self.win.vInfo.text = [gdata.galaxyTypeDescriptions[gal_type]] |
||
131 | except KeyError: |
||
132 | # this shouldn't happen |
||
133 | self.win.vInfo.text = [_("Description missing.")] |
||
134 | |||
135 | def onSelect(self, widget, action, data): |
||
136 | self.selectionID = data.tID |
||
137 | self._setButtons() |
||
138 | self._showSelection() |
||
139 | |||
140 | def onToggleBooking(self, widget, action, data): |
||
141 | if self.selectionID is None: |
||
142 | self.win.setStatus(_('Select booking entry to toggle.')) |
||
1 ignored issue
–
show
|
|||
143 | return |
||
144 | if self.displayPublic: |
||
145 | self._onToggleBooking() |
||
146 | else: |
||
147 | self.passwordDlg.display(self._onToggleBooking) |
||
148 | |||
149 | def _handleResult(self, result): |
||
150 | triggerID = ige.Const.BID_TRIGGERED |
||
151 | if not triggerID in result or not result[triggerID]: |
||
152 | # booking change is logged, no galaxy creation triggered |
||
153 | self.bookingInfo = result |
||
154 | self.show() |
||
155 | else: |
||
156 | # galaxy creation has been triggered |
||
157 | self.hide() |
||
158 | self.caller.display() |
||
159 | self.caller.win.setStatus(_('New galaxy creation has been triggered.')) |
||
1 ignored issue
–
show
|
|||
160 | |||
161 | def _onToggleBooking(self, password = None): |
||
162 | if self.selectionID: |
||
163 | try: |
||
164 | result = client.cmdProxy.toggleBooking(self.selectionID, password) |
||
165 | self._handleResult(result) |
||
166 | except ige.BookingMngrException: |
||
167 | self.win.setStatus(_('Wrong password.')) |
||
1 ignored issue
–
show
|
|||
168 | |||
169 | def onPublicToggle(self, widget, action, data): |
||
170 | self.selectionID = None |
||
171 | self.displayPublic = not self.displayPublic |
||
172 | self.show() |
||
173 | |||
174 | def onGoalToggle(self, widget, action, data): |
||
175 | self.displayGoal = not self.displayGoal |
||
176 | if self.displayGoal: |
||
177 | self.win.vGoalToggle.text = _("Show galaxy info") |
||
1 ignored issue
–
show
|
|||
178 | else: |
||
179 | self.win.vGoalToggle.text = _("Show goal info") |
||
180 | self.show() |
||
181 | |||
182 | def onCreatePrivate(self, widget, action, data): |
||
183 | if self.selectionID is None: |
||
184 | self.win.setStatus(_('Select galaxy type to create private booking.')) |
||
1 ignored issue
–
show
|
|||
185 | return |
||
186 | self.passwordDlg.display(self.onCreatePrivateConfirmed) |
||
187 | |||
188 | def onCreatePrivateConfirmed(self, password): |
||
189 | result = client.cmdProxy.createPrivateBooking(self.selectionID, password) |
||
190 | self._handleResult(result) |
||
191 | |||
192 | def onDeletePrivate(self, widget, action, data): |
||
193 | if self.selectionID is None: |
||
194 | self.win.setStatus(_('Select galaxy type to create private booking.')) |
||
1 ignored issue
–
show
|
|||
195 | return |
||
196 | result = client.cmdProxy.deletePrivateBooking(self.selectionID) |
||
197 | self.selectionID = None |
||
198 | self._handleResult(result) |
||
199 | |||
200 | def onCancel(self, widget, action, data): |
||
201 | self.win.hide() |
||
202 | if self.caller: |
||
203 | self.caller.display() |
||
204 | else: |
||
205 | self.app.exit() |
||
206 | |||
207 | def createUI(self): |
||
208 | w, h = gdata.scrnSize |
||
209 | self.win = ui.Window(self.app, |
||
210 | modal = 1, |
||
211 | movable = 0, |
||
212 | title = _('Select, which galaxy types do you want to play'), |
||
1 ignored issue
–
show
|
|||
213 | rect = ui.Rect((w - 564) / 2, (h - 404) / 2, 564, 404), |
||
214 | layoutManager = ui.SimpleGridLM(), |
||
215 | tabChange = True |
||
216 | ) |
||
217 | ui.Listbox(self.win, layout = (0, 0, 28, 7), id = 'vBookingPublic', |
||
218 | sortedBy = ('text', 1), |
||
219 | columns = ( |
||
220 | ('', 'tChoice', 1, ui.ALIGN_NONE), |
||
221 | (_('Galaxy type'), 'text', 6, ui.ALIGN_NONE), |
||
222 | (_('Scenario'), 'tScenario', 6, ui.ALIGN_NONE), |
||
223 | (_('Queue'), 'tCur', 3, ui.ALIGN_NONE), |
||
224 | (_('Capacity'), 'tPos', 3, ui.ALIGN_NONE), |
||
225 | (_('Last start'), 'tTime', 8, ui.ALIGN_E) |
||
226 | ), |
||
227 | columnLabels = 1, |
||
228 | action = 'onSelect', |
||
229 | ) |
||
230 | ui.Listbox(self.win, layout = (0, 0, 28, 7), id = 'vBookingPrivate', |
||
231 | sortedBy = ('tOwner', 1), |
||
232 | columns = ( |
||
233 | ('', 'tChoice', 1, ui.ALIGN_NONE), |
||
234 | (_('Galaxy type'), 'text', 6, ui.ALIGN_NONE), |
||
235 | (_('Scenario'), 'tScenario', 6, ui.ALIGN_NONE), |
||
236 | (_('Queue'), 'tCur', 3, ui.ALIGN_NONE), |
||
237 | (_('Capacity'), 'tPos', 3, ui.ALIGN_NONE), |
||
238 | (_('Owner'), 'tOwner', 8, ui.ALIGN_E) |
||
239 | ), |
||
240 | columnLabels = 1, |
||
241 | action = 'onSelect', |
||
242 | ) |
||
243 | self.win.subscribeAction('*', self) |
||
244 | |||
245 | ui.Button(self.win, layout = (0, 7, 8, 1), id = "vPublicToggle", text = _('Show personal bookings'), action = 'onPublicToggle', |
||
246 | tooltipTitle = _("Booking types"), |
||
247 | tooltip = _("Public Bookings\nPublic bookings are recommended way how to jump into new game.\nWhen queue is full, galaxy creation is triggered, and you can start adventure with group of strangers.\n\nPrivate Bookings\nPrivate bookings are the way to start game with group of your friends.\nEvery private booking requires password chosen by the owner.\nSingle player games cannot be privately booked (as they are private by definition).") |
||
248 | ) |
||
249 | ui.Button(self.win, layout = (20, 7, 8, 1), id = "vCreatePrivate", text = _('Create private booking'), action = 'onCreatePrivate', |
||
250 | tooltipTitle = _("Create Private Booking"), |
||
251 | tooltip = _("Private bookings are way how to create games for group of friends.\n\ |
||
252 | Every booking has to be created password protected, so you have to tell others\n\ |
||
253 | what the password is.\n\n\ |
||
254 | Account has limit of {0} private bookings at the time. Created galaxies\n\ |
||
255 | no longers counts into the limit.".format(ige.ospace.Const.BOOKING_PRIVATE_LIMIT)) |
||
256 | ) |
||
257 | |||
258 | ui.Button(self.win, layout = (20, 7, 8, 1), id = "vDeletePrivate", text = _('Delete private booking'), action = 'onDeletePrivate', |
||
259 | tooltipTitle = _("Delete Private Booking"), |
||
260 | tooltip = _("As the author of the booking, you can delete it at any time. No further warning will be issued.") |
||
261 | ) |
||
262 | |||
263 | ui.Button(self.win, layout = (7, 8.5, 14, 1.5), id = "vToggle", text = _('Toggle booking'), action = 'onToggleBooking') |
||
264 | scrollBarInfo = ui.Scrollbar(self.win, layout = (27, 10.3, 1, 4.7)) |
||
265 | textBox = ui.Text(self.win, layout = (9, 10.3, 18, 4.7), id = "vInfo", editable = 0) |
||
266 | textBox.attachVScrollbar(scrollBarInfo) |
||
267 | |||
268 | ui.Button(self.win, layout = (0.5, 10.5, 8, 1), id = "vGoalToggle", text = _('Display Goals'), action = 'onGoalToggle') |
||
269 | ui.Label(self.win, layout = (0, 12, 4, 1), text = _("Planets:"), align = ui.ALIGN_W, |
||
270 | tooltipTitle = _("Planets"), |
||
271 | tooltip = _("Range of number of planets. About half of them is not colonizable at the beginning.") |
||
272 | ) |
||
273 | ui.Label(self.win, layout = (4, 12, 4.5, 1), id = "vPlanets", align = ui.ALIGN_E, |
||
274 | tooltipTitle = _("Planets"), |
||
275 | tooltip = _("Range of number of planets. About half of them is not colonizable at the beginning.") |
||
276 | ) |
||
277 | ui.Label(self.win, layout = (0, 13, 4, 1), text = _("Radius:"), align = ui.ALIGN_W, |
||
278 | tooltipTitle = _("Radius"), |
||
279 | tooltip = _("Galaxy radius, implies speed of game.") |
||
280 | ) |
||
281 | ui.Label(self.win, layout = (4, 13, 4.5, 1), id = "vRadius", align = ui.ALIGN_E, |
||
282 | tooltipTitle = _("Radius"), |
||
283 | tooltip = _("Galaxy radius, implies speed of game.") |
||
284 | ) |
||
285 | ui.Label(self.win, layout = (0, 14, 4, 1), text = _("Grouping:"), align = ui.ALIGN_W, |
||
286 | tooltipTitle = _("Grouping"), |
||
287 | tooltip = _("How many starting positions are grouped together in vicinity.") |
||
288 | ) |
||
289 | ui.Label(self.win, layout = (4, 14, 4.5, 1), id = "vPlayerGroup", align = ui.ALIGN_E, |
||
290 | tooltipTitle = _("Grouping"), |
||
291 | tooltip = _("How many starting positions are grouped together.") |
||
292 | ) |
||
293 | |||
294 | ui.Label(self.win, layout = (0, 15.2, 4, 1), text = _("Resources:"), align = ui.ALIGN_W) |
||
295 | ui.Text (self.win, layout = (4, 15.2, 23, 1.6), id = "vResources", editable = 0) |
||
296 | ui.Label(self.win, layout = (0, 17, 4, 1), text = _("Challenges:"), align = ui.ALIGN_W) |
||
297 | ui.Label(self.win, layout = (4, 17, 24, 1), id = "vChallenges", align = ui.ALIGN_W) |
||
298 | |||
299 | ui.Title(self.win, layout = (0, 18, 24, 1), id = 'vStatusBar', align = ui.ALIGN_W) |
||
300 | ui.TitleButton(self.win, layout = (24, 18, 4, 1), text = _('Exit'), action = 'onCancel') |
||
301 | self.win.statusBar = self.win.vStatusBar |
||
302 |