# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - RockPaperScissors

    @copyright: 2004 by Jos Yule <jos@theorganization.net>
    @license: GNU GPL, see COPYING for details.

	Usage:
	[[RockPaperScissors( pageName )]]

	pageName is optional, works the same way that the MonthCalendar Macro does. You could have a game being played on page GameOne, and want to include that game on another page. Simply use the macro like so:
	
	[[RockPaperScissors(GameOne)]]


"""


#import re, string, time
#from MoinMoin import config, wikiutil
from MoinMoin.Page import Page
import re


missingHomePage = """
RockPaperScissors Info: You have entered the page <b>%s</b> as the root for this Rock Paper Scissors game. Unfortunatly, it can not be found. Please try a different page.
"""

playerPagesMissing = """
RockPaperScissors Info: You seem to be missing the player pages for the game on page %s. You need to create the subpages %s/PlayerOne and %s/PlayerTwo. As this game requries some secrecy, the two players might want to use the ACLs to make their respective pages private. 
"""

noPlays = """
RockPaperScissors Info: There are no valid moves entered yet. Make sure that on the two player pages (/PlayerOne and /PlayerTwo) you have a list of the form:
<pre>
 * paper or rock or scissors</pre>
 Each line is a "play" of the game. Anything else will be ignored. The case is not important.
"""

playMatrix = {'paper':{'scissors':-1,'rock':1,'paper':0},
	'rock':{'scissors':1,'rock':0,'paper':-1},
	'scissors':{'scissors':0,'rock':-1,'paper':1}}

def execute(macro, args):
	
	if args == '':
		RPShome = macro.formatter.page.page_name
	else:
		RPShome = args.split(',')[0]


	RPShomePage = Page(RPShome)

	if not RPShomePage.exists():
		return missingHomePage % RPShome
	
	# get the 2 subpages

	listOfAllPages = macro.request.getPageList()
	pageToMatch = "^" + RPShome + "/PlayerOne"
	cond = re.compile(pageToMatch).match 
	playerOnePage = filter(cond, listOfAllPages)

	if len(playerOnePage) == 0:
		return playerPagesMissing % (RPShome, RPShome, RPShome)

	listOfAllPages = macro.request.getPageList()
	pageToMatch = "^" + RPShome + "/PlayerTwo"
	cond = re.compile(pageToMatch).match 
	playerTwoPage = filter(cond, listOfAllPages)

	if len(playerTwoPage) == 0:
		return playerPagesMissing % (RPShome, RPShome, RPShome)

	playerOneText = Page(playerOnePage[0]).get_raw_body()
	playerTwoText = Page(playerTwoPage[0]).get_raw_body()

	moveRegex = re.compile(r' [*] (paper|rock|scissors)',re.I)
	playerOneMoves = moveRegex.findall(playerOneText)
	playerTwoMoves = moveRegex.findall(playerTwoText)

	numberOfPlays = min(len(playerOneMoves),len(playerTwoMoves))

	if not numberOfPlays > 0:
		return noPlays
	
	playOutput = ''
	score = [0,0]

	for aPlay in range(numberOfPlays):
		p1 = playerOneMoves[aPlay].lower()
		p2 = playerTwoMoves[aPlay].lower()

		if playMatrix[p1][p2] == -1:
			#player one looses
			score[1] += 1;
			playOutput += '<br> Player One\'s <b>%s</b> looses to Player Two\'s <b>%s</b>.' % (p1.capitalize(),p2.capitalize())
		elif playMatrix[p1][p2] == 0:
			# draw
			playOutput += '<br> Player One\'s <b>%s</b> draws against Player Two\'s <b>%s</b>.' % (p1.capitalize(),p2.capitalize())
		elif playMatrix[p1][p2] == 1:
			#player one wins
			score[0] += 1;
			playOutput += '<br> Player One\'s <b>%s</b> defeats Player Two\'s <b>%s</b>.' % (p1.capitalize(),p2.capitalize())

	if score[0] > score[1]:
		playOutput += '<br> The Score is %s to %s for Player One.' % (score[0],score[1])
	elif score[1] > score[0]:
		playOutput += '<br> The Score is %s to %s for Player Two.' % (score[1],score[0])
	elif score[1] == score[0]:
		playOutput += '<br> The Score is tied at %s to %s.' % (score[1],score[0])

	return playOutput

	
	
