2010-07-08 20:01:03 +02:00
|
|
|
#!/usr/bin/python
|
2010-07-04 03:05:16 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2009-05-21 05:26:00 +02:00
|
|
|
|
2010-07-04 03:05:16 +02:00
|
|
|
#Copyright 2008-2010 Carl Gherardi
|
2009-05-21 05:26:00 +02:00
|
|
|
#This program is free software: you can redistribute it and/or modify
|
|
|
|
#it under the terms of the GNU Affero General Public License as published by
|
|
|
|
#the Free Software Foundation, version 3 of the License.
|
2010-01-21 07:12:17 +01:00
|
|
|
#
|
2009-05-21 05:26:00 +02:00
|
|
|
#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.
|
2010-01-21 07:12:17 +01:00
|
|
|
#
|
2009-05-21 05:26:00 +02:00
|
|
|
#You should have received a copy of the GNU Affero General Public License
|
|
|
|
#along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2010-07-04 03:05:16 +02:00
|
|
|
#In the "official" distribution you can find the license in agpl-3.0.txt.
|
2009-05-21 05:26:00 +02:00
|
|
|
|
2010-08-15 20:50:49 +02:00
|
|
|
import locale
|
|
|
|
lang=locale.getdefaultlocale()[0][0:2]
|
|
|
|
if lang=="en":
|
|
|
|
def _(string): return string
|
|
|
|
else:
|
|
|
|
import gettext
|
|
|
|
try:
|
|
|
|
trans = gettext.translation("fpdb", localedir="locale", languages=[lang])
|
|
|
|
trans.install()
|
|
|
|
except IOError:
|
|
|
|
def _(string): return string
|
2009-05-21 05:26:00 +02:00
|
|
|
|
2009-12-22 10:20:07 +01:00
|
|
|
# From fpdb_simple
|
|
|
|
card_map = { "0": 0, "2": 2, "3" : 3, "4" : 4, "5" : 5, "6" : 6, "7" : 7, "8" : 8,
|
|
|
|
"9" : 9, "T" : 10, "J" : 11, "Q" : 12, "K" : 13, "A" : 14}
|
|
|
|
|
|
|
|
# FIXME: the following is a workaround until switching to newimport.
|
|
|
|
# This should be moved into DerivedStats
|
|
|
|
# I'd also like to change HandsPlayers.startCards to a different datatype
|
|
|
|
# so we can 'trivially' add different start card classifications
|
|
|
|
|
|
|
|
def calcStartCards(hand, player):
|
|
|
|
if hand.gametype['category'] == 'holdem':
|
|
|
|
hcs = hand.join_holecards(player, asList=True)
|
|
|
|
#print "DEBUG: hcs: %s" % hcs
|
|
|
|
value1 = card_map[hcs[0][0]]
|
|
|
|
value2 = card_map[hcs[1][0]]
|
|
|
|
return twoStartCards(value1, hcs[0][1], value2, hcs[1][1])
|
|
|
|
else:
|
|
|
|
# FIXME: Only do startCards value for holdem at the moment
|
|
|
|
return 0
|
2009-05-21 05:26:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
def twoStartCards(value1, suit1, value2, suit2):
|
|
|
|
""" Function to convert 2 value,suit pairs into a Holdem style starting hand e.g. AQo
|
2010-01-12 23:10:59 +01:00
|
|
|
Incoming values should be ints 2-14 (2,3,...K,A), suits are 'd'/'h'/'c'/'s'
|
|
|
|
Hand is stored as an int 13 * x + y + 1 where (x+2) represents rank of 1st card and
|
2009-05-21 05:26:00 +02:00
|
|
|
(y+2) represents rank of second card (2=2 .. 14=Ace)
|
2010-01-12 23:10:59 +01:00
|
|
|
If x > y then pair is suited, if x < y then unsuited
|
|
|
|
Examples:
|
|
|
|
0 Unknown / Illegal cards
|
|
|
|
1 22
|
|
|
|
2 32o
|
|
|
|
3 42o
|
|
|
|
...
|
|
|
|
14 32s
|
|
|
|
15 33
|
|
|
|
16 42o
|
|
|
|
...
|
|
|
|
170 AA
|
|
|
|
"""
|
|
|
|
if value1 is None or value1 < 2 or value1 > 14 or value2 is None or value2 < 2 or value2 > 14:
|
2009-07-27 00:00:12 +02:00
|
|
|
ret = 0
|
2010-01-12 23:10:59 +01:00
|
|
|
elif value1 == value2: # pairs
|
|
|
|
ret = (13 * (value2-2) + (value2-2) ) + 1
|
2009-07-27 00:00:12 +02:00
|
|
|
elif suit1 == suit2:
|
|
|
|
if value1 > value2:
|
2010-01-21 07:12:17 +01:00
|
|
|
ret = 13 * (value1-2) + (value2-2) + 1
|
2009-07-27 00:00:12 +02:00
|
|
|
else:
|
2010-01-12 23:10:59 +01:00
|
|
|
ret = 13 * (value2-2) + (value1-2) + 1
|
2009-05-21 05:26:00 +02:00
|
|
|
else:
|
2009-07-27 00:00:12 +02:00
|
|
|
if value1 > value2:
|
2010-01-12 23:10:59 +01:00
|
|
|
ret = 13 * (value2-2) + (value1-2) + 1
|
2009-07-27 00:00:12 +02:00
|
|
|
else:
|
2010-01-12 23:10:59 +01:00
|
|
|
ret = 13 * (value1-2) + (value2-2) + 1
|
2010-01-21 07:12:17 +01:00
|
|
|
|
2009-07-27 00:00:12 +02:00
|
|
|
# print "twoStartCards(", value1, suit1, value2, suit2, ")=", ret
|
|
|
|
return ret
|
2009-05-21 05:26:00 +02:00
|
|
|
|
|
|
|
def twoStartCardString(card):
|
|
|
|
""" Function to convert an int representing 2 holdem hole cards (as created by twoStartCards)
|
|
|
|
into a string like AQo """
|
2009-07-27 00:00:12 +02:00
|
|
|
ret = 'xx'
|
|
|
|
if card > 0:
|
2009-05-21 05:26:00 +02:00
|
|
|
s = ('2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A')
|
2010-01-12 23:10:59 +01:00
|
|
|
x = (card-1) / 13
|
|
|
|
y = (card-1) - 13 * x
|
2009-07-27 00:00:12 +02:00
|
|
|
if x == y: ret = s[x] + s[y]
|
|
|
|
elif x > y: ret = s[x] + s[y] + 's'
|
|
|
|
else: ret = s[y] + s[x] + 'o'
|
|
|
|
# print "twoStartCardString(", card ,") = " + ret
|
|
|
|
return ret
|
2009-05-21 05:26:00 +02:00
|
|
|
|
2009-05-21 16:14:49 +02:00
|
|
|
def fourStartCards(value1, suit1, value2, suit2, value3, suit3, value4, suit4):
|
2009-05-21 19:02:11 +02:00
|
|
|
""" Function to convert 4 value,suit pairs into a Omaha style starting hand,
|
|
|
|
haven't decided how to encode this yet """
|
2009-05-21 16:14:49 +02:00
|
|
|
# This doesn't actually do anything yet - CG
|
2009-05-22 03:23:10 +02:00
|
|
|
|
2009-05-21 19:02:11 +02:00
|
|
|
# What combinations do we need to store? just cards: AA23? some suits as well e.g. when
|
|
|
|
# double suited ATcKTd? Lots more possible combos than holdem :-( 270K vs 1326? not sure
|
|
|
|
# Probably need to use this field as a key into some other table - sc
|
2009-05-22 03:23:10 +02:00
|
|
|
|
2009-05-21 17:02:50 +02:00
|
|
|
#AAKKds
|
|
|
|
#AAKKs
|
|
|
|
#AAKKr
|
|
|
|
# Is probably what we are looking for
|
2009-06-19 08:04:57 +02:00
|
|
|
|
|
|
|
# mct:
|
|
|
|
# my maths says there are 4 classes of suitedness
|
|
|
|
# SSSS SSSx SSxy SSHH
|
|
|
|
# encode them as follows:
|
|
|
|
# SSSS (K, J, 6, 3)
|
|
|
|
# - 13C4 = 715 possibilities
|
|
|
|
# SSSx (K, J, 6),(3)
|
2010-01-21 07:12:17 +01:00
|
|
|
# - 13C3 * 13 = 3718 possibilities
|
2009-06-19 08:04:57 +02:00
|
|
|
# SSxy (K, J),(6),(3)
|
|
|
|
# - 13C2 * 13*13 = 13182 possibilities
|
|
|
|
# SSHH (K, J),(6, 3)
|
|
|
|
# - 13C2 * 13C2 = 6084 possibilities
|
|
|
|
# Needless to say they won't fit on a 13x13 grid.
|
|
|
|
# The actual number of hands in each class is far greater
|
2009-05-21 16:14:49 +02:00
|
|
|
return(0)
|
|
|
|
|
2009-05-21 05:26:00 +02:00
|
|
|
def cardFromValueSuit(value, suit):
|
|
|
|
""" 0=none, 1-13=2-Ah 14-26=2-Ad 27-39=2-Ac 40-52=2-As """
|
|
|
|
if suit == 'h': return(value-1)
|
|
|
|
elif suit == 'd': return(value+12)
|
|
|
|
elif suit == 'c': return(value+25)
|
|
|
|
elif suit == 's': return(value+38)
|
|
|
|
else: return(0)
|
|
|
|
|
2009-07-31 06:40:31 +02:00
|
|
|
suitFromCardList = ['', '2h', '3h', '4h', '5h', '6h', '7h', '8h', '9h', 'Th', 'Jh', 'Qh', 'Kh', 'Ah'
|
|
|
|
, '2d', '3d', '4d', '5d', '6d', '7d', '8d', '9d', 'Td', 'Jd', 'Qd', 'Kd', 'Ad'
|
|
|
|
, '2c', '3c', '4c', '5c', '6c', '7c', '8c', '9c', 'Tc', 'Jc', 'Qc', 'Kc', 'Ac'
|
|
|
|
, '2s', '3s', '4s', '5s', '6s', '7s', '8s', '9s', 'Ts', 'Js', 'Qs', 'Ks', 'As'
|
|
|
|
]
|
2009-05-21 05:26:00 +02:00
|
|
|
def valueSuitFromCard(card):
|
2010-01-21 07:12:17 +01:00
|
|
|
""" Function to convert a card stored in the database (int 0-52) into value
|
2009-05-21 05:26:00 +02:00
|
|
|
and suit like 9s, 4c etc """
|
2009-07-31 06:40:31 +02:00
|
|
|
global suitFromCardList
|
2009-06-19 08:04:57 +02:00
|
|
|
if card < 0 or card > 52 or not card:
|
2009-05-21 05:26:00 +02:00
|
|
|
return('')
|
|
|
|
else:
|
2009-07-31 06:40:31 +02:00
|
|
|
return suitFromCardList[card]
|
2009-05-21 05:26:00 +02:00
|
|
|
|
2009-07-31 06:40:31 +02:00
|
|
|
encodeCardList = {'2h': 1, '3h': 2, '4h': 3, '5h': 4, '6h': 5, '7h': 6, '8h': 7, '9h': 8, 'Th': 9, 'Jh': 10, 'Qh': 11, 'Kh': 12, 'Ah': 13,
|
2010-01-30 20:54:46 +01:00
|
|
|
'2d': 14, '3d': 15, '4d': 16, '5d': 17, '6d': 18, '7d': 19, '8d': 20, '9d': 21, 'Td': 22, 'Jd': 23, 'Qd': 24, 'Kd': 25, 'Ad': 26,
|
|
|
|
'2c': 27, '3c': 28, '4c': 29, '5c': 30, '6c': 31, '7c': 32, '8c': 33, '9c': 34, 'Tc': 35, 'Jc': 36, 'Qc': 37, 'Kc': 38, 'Ac': 39,
|
|
|
|
'2s': 40, '3s': 41, '4s': 42, '5s': 43, '6s': 44, '7s': 45, '8s': 46, '9s': 47, 'Ts': 48, 'Js': 49, 'Qs': 50, 'Ks': 51, 'As': 52,
|
|
|
|
' ': 0
|
2009-07-31 06:40:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def encodeCard(cardString):
|
|
|
|
"""Take a card string (Ah) and convert it to the db card code (1)."""
|
|
|
|
global encodeCardList
|
|
|
|
if cardString not in encodeCardList: return 0
|
|
|
|
return encodeCardList[cardString]
|
2009-05-21 05:26:00 +02:00
|
|
|
|
2010-09-22 10:35:26 +02:00
|
|
|
def encodeRazzStartHand(cards):
|
|
|
|
"""No idea how this is actually going to work, figured i'd record the top 10
|
|
|
|
starting hands anyway
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
#A, 2, 3
|
|
|
|
#A, 2, 4
|
|
|
|
#A, 2, 5
|
|
|
|
#A, 3, 4
|
|
|
|
#2, 3, 4
|
|
|
|
#A, 3, 5
|
|
|
|
#A, 4, 5
|
|
|
|
#2, 3, 5
|
|
|
|
#2, 4, 5
|
|
|
|
#A, 2, 6
|
|
|
|
|
2009-07-25 17:58:43 +02:00
|
|
|
if __name__ == '__main__':
|
2010-08-15 07:31:32 +02:00
|
|
|
print _("fpdb card encoding(same as pokersource)")
|
2009-07-25 17:58:43 +02:00
|
|
|
for i in xrange(1, 14):
|
|
|
|
print "card %2d = %s card %2d = %s card %2d = %s card %2d = %s" % \
|
|
|
|
(i, valueSuitFromCard(i), i+13, valueSuitFromCard(i+13), i+26, valueSuitFromCard(i+26), i+39, valueSuitFromCard(i+39))
|
2009-07-29 16:33:42 +02:00
|
|
|
|
2010-01-21 07:12:17 +01:00
|
|
|
print
|
2009-12-22 10:20:07 +01:00
|
|
|
print encodeCard('7c')
|