File: wizardutil.py

package info (click to toggle)
pysolfc 3.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 94,856 kB
  • sloc: python: 82,020; tcl: 4,529; makefile: 65; sh: 57; perl: 48
file content (494 lines) | stat: -rw-r--r-- 15,391 bytes parent folder | download | duplicates (2)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/usr/bin/env python
# -*- mode: python; coding: utf-8; -*-
# ---------------------------------------------------------------------------
#
# Copyright (C) 1998-2003 Markus Franz Xaver Johannes Oberhumer
# Copyright (C) 2003 Mt. Hood Playing Card Co.
# Copyright (C) 2005-2009 Skomoroh
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# ---------------------------------------------------------------------------

import os

from pysollib.gamedb import GI, hideGame, loadGame
from pysollib.layout import Layout
from pysollib.mygettext import _, n_
from pysollib.stack import AC_FoundationStack, \
        AC_RowStack, \
        BO_FoundationStack, \
        BO_RowStack, \
        BasicRowStack, \
        DealFirstRowRedealTalonStack, \
        DealReserveRedealTalonStack, \
        DealRowRedealTalonStack, \
        GroundsForADivorceTalonStack, \
        InitialDealTalonStack, \
        RK_FoundationStack, \
        RK_RowStack, \
        SC_FoundationStack, \
        SC_RowStack, \
        SS_FoundationStack, \
        SS_RowStack, \
        SpiderTalonStack, \
        Spider_AC_Foundation, \
        Spider_AC_RowStack, \
        Spider_BO_Foundation, \
        Spider_BO_RowStack, \
        Spider_RK_Foundation, \
        Spider_SC_Foundation, \
        Spider_SC_RowStack, \
        Spider_SS_Foundation, \
        Spider_SS_RowStack, \
        UD_AC_RowStack, \
        UD_BO_RowStack, \
        UD_RK_RowStack, \
        UD_SC_RowStack, \
        UD_SS_RowStack, \
        WasteTalonStack, \
        Yukon_AC_RowStack, \
        Yukon_BO_RowStack, \
        Yukon_RK_RowStack, \
        Yukon_SC_RowStack, \
        Yukon_SS_RowStack
from pysollib.util import ACE, ANY_RANK, KING, NO_RANK, UNLIMITED_MOVES
from pysollib.wizardpresets import presets

# ************************************************************************
# *
# ************************************************************************


class WizSetting:
    def __init__(self, values_map, default, var_name,
                 label, widget='menu'):
        self.values_map = values_map
        self.default = default
        # self.values_dict = dict(self.values_map)
        self.translation_map = {}       # for backward translation
        if widget == 'menu':
            self.values = []
            for k, v in self.values_map:
                self.values.append(k)
                self.translation_map[_(k)] = k
            assert self.default in self.values
        elif widget == 'preset':
            self.values = []
            for v in self.values_map:
                self.values.append(v)
                self.translation_map[_(v)] = v
            assert self.default in self.values
        else:
            self.values = self.values_map
        self.var_name = var_name
        self.label = label
        self.widget = widget
        self.variable = None            # Tk variable
        self.current_value = None


WizardPresets = WizSetting(
    values_map=list(presets.keys()),
    default='None',
    widget='preset',
    label=_('Initial setting:'),
    var_name='preset',
    )
GameName = WizSetting(
    values_map=(),
    default=_('My Game'),
    widget='entry',
    label=_('Name:'),
    var_name='name',
    )
SkillLevel = WizSetting(
    values_map=((n_('Luck only'),    GI.SL_LUCK),
                (n_('Mostly luck'),  GI.SL_MOSTLY_LUCK),
                (n_('Balanced'),     GI.SL_BALANCED),
                (n_('Mostly skill'), GI.SL_MOSTLY_SKILL),
                (n_('Skill only'),   GI.SL_SKILL),
                ),
    default=n_('Balanced'),
    label=_('Skill level:'),
    var_name='skill_level',
    )
NumDecks = WizSetting(
    values_map=((n_('One'),   1),
                (n_('Two'),   2),
                (n_('Three'), 3),
                (n_('Four'),  4)),
    default=n_('One'),
    label=_('Number of decks:'),
    var_name='decks',
    )
LayoutType = WizSetting(
    values_map=((n_('FreeCell'), Layout.freeCellLayout),
                (n_('Klondike'), Layout.klondikeLayout),
                (n_('Gypsy'),    Layout.gypsyLayout),
                (n_('Harp'),     Layout.harpLayout),
                ),
    default=n_('FreeCell'),
    label=_('Layout:'),
    var_name='layout',
    )
TalonType = WizSetting(
    values_map=((n_('Deal all cards at the beginning'), InitialDealTalonStack),
                (n_('Deal to waste'),         WasteTalonStack),
                (n_('Deal to tableau'),       DealRowRedealTalonStack),
                (n_('Deal to first tableau'), DealFirstRowRedealTalonStack),
                (n_('Deal to reserves'),      DealReserveRedealTalonStack),
                (n_('Spider'),                SpiderTalonStack),
                (n_('Grounds for a Divorce'), GroundsForADivorceTalonStack),
                ),
    default=n_('Deal all cards at the beginning'),
    label=_('Type:'),
    var_name='talon',
    )
Redeals = WizSetting(
    values_map=((n_('No redeals'), 0),
                (n_('One redeal'), 1),
                (n_('Two redeals'), 2),
                (n_('Three redeals'), 3),
                (n_('Unlimited redeals'), -1),
                ),
    default=n_('No redeals'),
    label=_('Number of redeals:'),
    var_name='redeals',
    )
DealToWaste = WizSetting(
    values_map=(1, 5),
    default=1,
    widget='spin',
    label=_('# of cards dealt to the waste:'),
    var_name='deal_to_waste',
    )
TalonShuffle = WizSetting(
    values_map=(0, 1),
    default=0,
    label=_('Shuffle during redeal:'),
    var_name='talon_shuffle',
    widget='check',
    )
FoundType = WizSetting(
    values_map=((n_('Same suit'),                       SS_FoundationStack),
                (n_('Alternate color'),                 AC_FoundationStack),
                (n_('Same color'),                      SC_FoundationStack),
                (n_('Rank'),                            RK_FoundationStack),
                (n_('Any suit but the same'),           BO_FoundationStack),
                (n_('Spider same suit'),                Spider_SS_Foundation),
                (n_('Spider alternate color'),          Spider_AC_Foundation),
                (n_('Spider same color'),               Spider_SC_Foundation),
                (n_('Spider rank'),                     Spider_RK_Foundation),
                (n_('Spider any suit but the same'),    Spider_BO_Foundation),
                ),
    default=n_('Same suit'),
    label=_('Type:'),
    var_name='found_type',
    )
FoundBaseCard = WizSetting(
    values_map=((n_('Ace'),  ACE),
                (n_('King'), KING),
                (n_('Any'),  ANY_RANK),
                ),
    default=n_('Ace'),
    label=_('Base card:'),
    var_name='found_base_card',
    )
FoundDir = WizSetting(
    values_map=((n_('Up'), 1), (n_('Down'), -1)),
    default=n_('Up'),
    label=_('Direction:'),
    var_name='found_dir',
    )
FoundMaxMove = WizSetting(
    values_map=((n_('None'), 0,), (n_('Top card'), 1)),
    default=n_('Top card'),
    label=_('Move:'),
    var_name='found_max_move',
    )
FoundEqual = WizSetting(
    values_map=(0, 1),
    default=1,
    label=_('First card sets base cards:'),
    var_name='found_equal',
    widget='check',
    )
RowsNum = WizSetting(
    values_map=(1, 20),
    default=8,
    widget='spin',
    label=_('Number of tableau piles:'),
    var_name='rows_num',
    )
RowsType = WizSetting(
    values_map=((n_('No building'),                        BasicRowStack),

                (n_('Same suit'),                          SS_RowStack),
                (n_('Alternate color'),                    AC_RowStack),
                (n_('Same color'),                         SC_RowStack),
                (n_('Rank'),                               RK_RowStack),
                (n_('Any suit but the same'),              BO_RowStack),

                (n_('Up or down by same suit'),            UD_SS_RowStack),
                (n_('Up or down by alternate color'),      UD_AC_RowStack),
                (n_('Up or down by same color'),           UD_SC_RowStack),
                (n_('Up or down by rank'),                 UD_RK_RowStack),
                (n_('Up or down by any suit but the same'), UD_BO_RowStack),

                (n_('Spider same suit'),                   Spider_SS_RowStack),
                (n_('Spider alternate color'),             Spider_AC_RowStack),
                (n_('Spider same color'),                  Spider_SC_RowStack),
                (n_('Spider any suit but the same'),       Spider_BO_RowStack),

                (n_('Yukon same suit'),                    Yukon_SS_RowStack),
                (n_('Yukon alternate color'),              Yukon_AC_RowStack),
                (n_('Yukon same color'),                   Yukon_SC_RowStack),
                (n_('Yukon rank'),                         Yukon_RK_RowStack),
                (n_('Yukon any suit but the same'),        Yukon_BO_RowStack),
                ),
    default=n_('Alternate color'),
    label=_('Type:'),
    var_name='rows_type',
    )
RowsBaseCard = WizSetting(
    values_map=((n_('Ace'),  ACE),
                (n_('King'), KING),
                (n_('Any'),  ANY_RANK),
                (n_('None'), NO_RANK),
                ),
    default=n_('Any'),
    label=_('Base card:'),
    var_name='rows_base_card',
    )
RowsDir = WizSetting(
    values_map=((n_('Up'), 1), (n_('Down'), -1), (n_('Same rank'), 0)),
    default=n_('Down'),
    label=_('Direction:'),
    var_name='rows_dir',
    )
RowsMaxMove = WizSetting(
    values_map=((n_('Top card'), 1), (n_('Sequence'), UNLIMITED_MOVES)),
    default=n_('Sequence'),
    label=_('Move:'),
    var_name='rows_max_move',
    )
RowsWrap = WizSetting(
    values_map=(0, 1),
    default=0,
    label=_('Wrapping:'),
    var_name='rows_wrap',
    widget='check',
    )
RowsSuperMove = WizSetting(
    values_map=(0, 1),
    default=0,
    label=_('Use "Super Move" feature:'),
    var_name='rows_super_move',
    widget='check',
    )
ReservesNum = WizSetting(
    values_map=(0, 20),
    default=4,
    widget='spin',
    label=_('Number of reserves:'),
    var_name='reserves_num',
    )
ReservesMaxAccept = WizSetting(
    values_map=(0, 20),
    default=1,
    widget='spin',
    label=_('Max # of accepted cards:'),
    var_name='reserves_max_accept',
    )
DealType = WizSetting(
    values_map=((n_('Triangle'),  'triangle'),
                (n_('Rectangle'), 'rectangle'),
                (n_('Pyramid'), 'pyramid')
                ),
    default=n_('Rectangle'),
    label=_('Type:'),
    var_name='deal_type',
    )
DealFaceDown = WizSetting(
    values_map=(0, 20),
    default=0,
    widget='spin',
    label=_('# of face-down cards dealt to the tableau pile:'),
    var_name='deal_face_down',
    )
DealFaceUp = WizSetting(
    values_map=(0, 20),
    default=8,
    widget='spin',
    label=_('# of face-up cards dealt to the tableau pile:'),
    var_name='deal_face_up',
    )
DealToReseves = WizSetting(
    values_map=(0, 208),
    default=0,
    widget='spin',
    label=_('# of cards dealt to the reserve:'),
    var_name='deal_to_reserves',
    )
DealMaxCards = WizSetting(
    values_map=(0, 208),
    default=52,
    widget='spin',
    label=_('Max # of dealt cards:'),
    var_name='deal_max_cards',
    )
DealToFound = WizSetting(
    values_map=(0, 1),
    default=0,
    label=_('Deal first cards to the foundations:'),
    var_name='deal_found',
    widget='check',
    )

WizardWidgets = (
    _('General'),
    WizardPresets,
    GameName,
    SkillLevel,
    NumDecks,
    LayoutType,
    _('Talon'),
    TalonType,
    Redeals,
    DealToWaste,
    TalonShuffle,
    _('Foundations'),
    FoundType,
    FoundBaseCard,
    FoundDir,
    FoundMaxMove,
    FoundEqual,
    _('Tableau'),
    RowsNum,
    RowsType,
    RowsBaseCard,
    RowsDir,
    RowsMaxMove,
    RowsWrap,
    RowsSuperMove,
    _('Reserves'),
    ReservesNum,
    ReservesMaxAccept,
    _('Opening deal'),
    DealType,
    DealFaceDown,
    DealFaceUp,
    DealToReseves,
    DealMaxCards,
    DealToFound,
    )


def write_game(app, game=None):
    import pysollib.customgame                   # for py2exe
    pysollib.customgame.no_use()
    if game is None:
        # new game
        d = app.dn.plugins
        n = 1
        while True:
            fn = os.path.join(d, 'customgame%d.py' % n)  # file name
            mn = 'customgame%d' % n         # module name
            gameid = 200000+n
            if not os.path.exists(fn):
                break
            n += 1
        check_game = True
    else:
        # edit current game
        fn = game.MODULE_FILENAME
        mn = game.__module__
        gameid = game.SETTINGS['gameid']
        check_game = False

    # print '===>', fn
    with open(fn, 'w') as fd:

        fd.write('''\
## -*- coding: utf-8 -*-
## THIS FILE WAS GENERATED AUTOMATICALLY BY THE SOLITAIRE WIZARD
## DO NOT EDIT

from pysollib.customgame import CustomGame, registerCustomGame

class MyCustomGame(CustomGame):
    WIZARD_VERSION = 1
    SETTINGS = {
''')

        for w in WizardWidgets:
            if isinstance(w, str):
                continue
            v = w.variable.get()
            if w.widget in ('menu', 'preset'):
                v = w.translation_map[v]
            if v == w.default:
                # save only unique values
                continue
            if isinstance(v, int):
                fd.write("        '%s': %i,\n" % (w.var_name, v))
            else:
                if w.var_name == 'name':
                    # escape
                    v = v.replace('\\', '\\\\')
                    v = v.replace("'", "\\'")
                    v = v.replace("\n", "\\n")
                    v = v.replace("\r", "\\r")
                    v = v.replace("\t", "\\t")
                    # See: https://github.com/shlomif/PySolFC/issues/177
                    # if isinstance(v, str):
                    #     v = v.encode('utf-8')
                    if not v:
                        v = 'Invalid Game Name'
                fd.write("        '{}': '{}',\n".format(w.var_name, v))
        fd.write("        'gameid': %i,\n" % gameid)

        fd.write('''\
            }

registerCustomGame(MyCustomGame)
''')

    loadGame(mn, fn, check_game=check_game)

    return gameid


def delete_game(app, game):
    hideGame(game)
    app.stats.deleteGameStats(game.id)
    fn = game.MODULE_FILENAME
    os.remove(fn)


def reset_wizard(game):
    for w in WizardWidgets:
        if isinstance(w, str):
            continue
        if game is None:
            # set to default
            v = w.default
        else:
            # set from current game
            if w.var_name in game.SETTINGS:
                v = game.SETTINGS[w.var_name]
            else:
                v = w.default
        w.current_value = v