annotate libervia/frontends/tools/games.py @ 4326:5fd6a4dc2122

cli (output/std): use `rich` to output JSON.
author Goffi <goffi@goffi.org>
date Wed, 20 Nov 2024 11:38:44 +0100
parents 26b7ed2817da
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
1 #!/usr/bin/env python3
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
141
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
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
5 # Copyright (C) 2009-2021 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
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
22 SUITS_ORDER = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
23 "pique",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
24 "coeur",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
25 "trefle",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
26 "carreau",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
27 "atout",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
28 ] # I have switched the usual order 'trefle' and 'carreau' because card are more easy to see if suit colour change (black, red, black, red)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
29 VALUES_ORDER = [str(i) for i in range(1, 11)] + ["valet", "cavalier", "dame", "roi"]
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
30
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
31
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
32 class TarotCard(object):
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
33 """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
34
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
35 def __init__(self, tuple_card):
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
36 """@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
37 self.suit, self.value = tuple_card
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
38 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
39 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
40 self.points = 4.5
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
41 elif self.value == "dame":
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
42 self.points = 3.5
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
43 elif self.value == "cavalier":
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
44 self.points = 2.5
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
45 elif self.value == "valet":
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
46 self.points = 1.5
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
47 else:
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
48 self.points = 0.5
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
49
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
50 def get_tuple(self):
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
51 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
52
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
53 @staticmethod
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def from_tuples(tuple_list):
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
55 result = []
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
56 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
57 result.append(TarotCard(card_tuple))
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
58 return result
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
59
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
60 def __cmp__(self, other):
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
61 if other is None:
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
62 return 1
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
63 if self.suit != other.suit:
590
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
64 idx1 = SUITS_ORDER.index(self.suit)
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
65 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
66 return idx1.__cmp__(idx2)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67 if self.suit == "atout":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
68 if self.value == other.value == "excuse":
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
69 return 0
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
70 if self.value == "excuse":
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
71 return -1
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
72 if other.value == "excuse":
141
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
73 return 1
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
74 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
75 # 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
76 idx1 = VALUES_ORDER.index(self.value)
56531f9e9ac7 Fix pep8 support in src/tools.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
77 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
78 return idx1.__cmp__(idx2)
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
79
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
80 def __str__(self):
8c80d4dec7a8 mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
diff changeset
81 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
82
30eb49e4e05d frontends tools: added symbols for MUC user activity identification
souliane <souliane@mailoo.org>
parents: 682
diff changeset
83
30eb49e4e05d frontends tools: added symbols for MUC user activity identification
souliane <souliane@mailoo.org>
parents: 682
diff changeset
84 # These symbols are diplayed by Libervia next to the player's nicknames
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
85 SYMBOLS = {"Radiocol": ["♬"], "Tarot": ["♠", "♣", "♥", "♦"]}