annotate frontends/src/tools/games.py @ 2391:07e1543d6992

template: new "adv_format" filter which use Python's format and return whole value if template is None
author Goffi <goffi@goffi.org>
date Fri, 20 Oct 2017 08:49:39 +0200
parents 2daf7b4c6756
children 8b37a62336c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
1 #!/usr/bin/env python2
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
4 # SAT: a jabber client
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
10 # (at your option) any later version.
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
15 # GNU Affero General Public License for more details.
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
19
682
2805fa3f4bdf tools: moved src/tools/games.py to src/tools/frontends/
souliane <souliane@mailoo.org>
parents: 609
diff changeset
20 """This library help manage general games (e.g. card games) and it is shared by the frontends"""
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
21
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
22 SUITS_ORDER = ['pique', 'coeur', 'trefle', 'carreau', 'atout'] # I have switched the usual order 'trefle' and 'carreau' because card are more easy to see if suit colour change (black, red, black, red)
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
23 VALUES_ORDER = [str(i) for i in xrange(1, 11)] + ["valet", "cavalier", "dame", "roi"]
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
24
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
25
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
26 class TarotCard(object):
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
27 """This class is used to represent a car logically"""
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
28
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
29 def __init__(self, tuple_card):
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
30 """@param tuple_card: tuple (suit, value)"""
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
31 self.suit, self.value = tuple_card
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
32 self.bout = self.suit == "atout" and self.value in ["1", "21", "excuse"]
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
33 if self.bout or self.value == "roi":
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
34 self.points = 4.5
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
35 elif self.value == "dame":
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
36 self.points = 3.5
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
37 elif self.value == "cavalier":
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
38 self.points = 2.5
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
39 elif self.value == "valet":
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
40 self.points = 1.5
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
41 else:
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
42 self.points = 0.5
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
43
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
44 def get_tuple(self):
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
45 return (self.suit, self.value)
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
46
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
47 @staticmethod
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
48 def from_tuples(tuple_list):
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
49 result = []
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
50 for card_tuple in tuple_list:
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
51 result.append(TarotCard(card_tuple))
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
52 return result
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
53
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def __cmp__(self, other):
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
55 if other is None:
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
56 return 1
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
57 if self.suit != other.suit:
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
58 idx1 = SUITS_ORDER.index(self.suit)
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
59 idx2 = SUITS_ORDER.index(other.suit)
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
60 return idx1.__cmp__(idx2)
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
61 if self.suit == 'atout':
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
62 if self.value == other.value == 'excuse':
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
63 return 0
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
64 if self.value == 'excuse':
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
65 return -1
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
66 if other.value == 'excuse':
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
67 return 1
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
68 return int(self.value).__cmp__(int(other.value))
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
69 # at this point we have the same suit which is not 'atout'
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
70 idx1 = VALUES_ORDER.index(self.value)
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
71 idx2 = VALUES_ORDER.index(other.value)
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
72 return idx1.__cmp__(idx2)
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
73
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
74 def __str__(self):
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
75 return "[%s,%s]" % (self.suit, self.value)
716
30eb49e4e05d frontends tools: added symbols for MUC user activity identification
souliane <souliane@mailoo.org>
parents: 682
diff changeset
76
30eb49e4e05d frontends tools: added symbols for MUC user activity identification
souliane <souliane@mailoo.org>
parents: 682
diff changeset
77
30eb49e4e05d frontends tools: added symbols for MUC user activity identification
souliane <souliane@mailoo.org>
parents: 682
diff changeset
78 # These symbols are diplayed by Libervia next to the player's nicknames
1379
da2ea16fabc6 quick_frontend: display MUC games symbols
souliane <souliane@mailoo.org>
parents: 811
diff changeset
79 SYMBOLS = {"Radiocol": [u"♬"], "Tarot": [u"♠", u"♣", u"♥", u"♦"]}