#!/usr/bin/env python
#    Copyright 2008, Carl Gherardi
#    
#    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 2 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, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
########################################################################
#    Standard Library modules
import Configuration
import traceback
import sys
import re
import xml.dom.minidom
from xml.dom.minidom import Node
from HandHistoryConverter import HandHistoryConverter
# Carbon format looks like:
# 1) 
# 2) 
# 3)  
#                
#                ...
# 4) 
#                
#                
# 5) 
#                
# 6) 
#           
#           ....
#	    
# The full sequence for a NHLE cash game is:
# BLINDS, PREFLOP, POSTFLOP, POSTTURN, POSTRIVER, SHOWDOWN, END_OF_GAME
# This sequence can be terminated after BLINDS at any time by END_OF_FOLDED_GAME
class CarbonPoker(HandHistoryConverter): 
	def __init__(self, config, filename):
		print "Initialising Carbon Poker converter class"
		HandHistoryConverter.__init__(self, config, filename, "Carbon") # Call super class init
		self.setFileType("xml")
        self.siteId   = 4 # Needs to match id entry in Sites database
	def readSupportedGames(self): 
		pass
	def determineGameType(self):
		gametype = []
		desc_node = self.doc.getElementsByTagName("description")
		#TODO: no examples of non ring type yet
		gametype = gametype + ["ring"]
		type = desc_node[0].getAttribute("type")
		if(type == "Holdem"):
			gametype = gametype + ["hold"]
		else:
			print "Unknown gametype: '%s'" % (type)
		stakes = desc_node[0].getAttribute("stakes")
		#TODO: no examples of anything except nlhe
		m = re.match('(?PNo Limit)\s\(\$?(?P[.0-9]+)/\$?(?P[.0-9]+)\)', stakes)
		if(m.group('LIMIT') == "No Limit"):
			gametype = gametype + ["nl"]
		gametype = gametype + [self.float2int(m.group('SB'))]
		gametype = gametype + [self.float2int(m.group('BB'))]
		return gametype
	def readPlayerStacks(self):
		pass
	def readBlinds(self):
		pass
	def readAction(self):
		pass
	# Override read function as xml.minidom barfs on the Carbon layout
        # This is pretty dodgy
	def readFile(self, filename):
		print "Carbon: Reading file: '%s'" %(filename)
		infile=open(filename, "rU")
		self.obs = infile.read()
		infile.close()
		self.obs = "\n" + self.obs + ""
		try:
			doc = xml.dom.minidom.parseString(self.obs)
			self.doc = doc
		except:
			traceback.print_exc(file=sys.stderr)
if __name__ == "__main__":
	c = Configuration.Config()
	e = CarbonPoker(c, "regression-test-files/carbon-poker/Niagara Falls (15245216).xml") 
	e.processFile()
	print str(e)