import cgi
import webapp2
import random
import time
from google.appengine.api import users
##BEGIN POKEMON CLASSES & FUNCTIONS
class pokemon(object):
def __init__(self, name, pokemonType, health, moves, attack, defense, speed):
self.name = name
self.type = pokemonType
self.health = health
self.moves = moves
self.attack = attack
self.defense = defense
self.speed = speed
self.accuracy = 1
def isAlive(self):
if self.health > 0:
return True
else:
return False
def useMove(self, opponent, move):
chance = random.random()
chance *= self.accuracy
if chance > move.accuracy:
return ['missed']
elif chance <= move.accuracy:
#hit
if type(move.effect) == list:
if move.effect[0] == 'self':
stat = move.effect[1]
if stat == 'attack':
self.attack *= move.effect[2]
return ['hit', 'status', 'self', 'attack']
if stat == 'defense':
self.defense *= move.effect[2]
return ['hit', 'status', 'self', 'defense']
if stat == 'speed':
self.speed *= move.effect[2]
return ['hit', 'status', 'self', 'speed']
if stat == 'accuracy':
self.accuracy *= move.effect[2]
return ['hit', 'status', 'self', 'accuracy']
elif move.effect[0] == 'other':
stat = move.effect[1]
if stat == 'attack':
opponent.active.attack *= move.effect[2]
return ['hit', 'status', 'other', 'attack']
if stat == 'defense':
opponent.active.attack *= move.effect[2]
return ['hit', 'status', 'other', 'defense']
if stat == 'speed':
opponent.active.attack *= move.effect[2]
return ['hit', 'status', 'other', 'speed']
if stat == 'accuracy':
return ['hit', 'status', 'other', 'accuracy']
elif move.effect == False:
toReturn = ['hit', 'physical']
#apply attack and def stats
dmgDone = ((float(move.damage) / opponent.active.defense) * self.attack)
#critical hits
critRate = self.speed * 100.0 / 512
critRate = 100 - critRate
critRoll = random.random()
if critRoll > critRate:
toReturn.append('crit')
dmgDone *= 2
elif critRoll < critRate:
toReturn.append('noCrit')
#check for super-effectiveness
if isWeakTo(opponent.active, move):
toReturn.append('super')
dmgDone *= 2
if isStrongTo(opponent.active, move):
toReturn.append('ineffective')
dmgDone *= .5
else:
toReturn.append('normal')
opponent.active.health -= dmgDone
toReturn.append(dmgDone)
return toReturn
class move(object):
def __init__(self, name, moveType, damage, accuracy, effect=False):
self.name = name
self.type = moveType
self.damage = damage
self.accuracy = accuracy
self.effect = effect
class character(object):
def __init__(self, name, roster):
self.name = name
self.roster = roster
self.active = 'nothing'
self.living = self.listLiving()
self.alive = len(self.living)
def checkLiving(self):
self.alive = len(self.roster)
for pokemon in self.roster:
if not pokemon.isAlive():
self.alive -= 1
self.living = self.listLiving()
if self.active != 'nothing':
if self.active.health <= 0:
print self.active.name, 'has died!'
self.active = 'nothing'
def hasActive(self):
if self.active != 'nothing':
return True
else:
return False
def listLiving(self):
living = []
for pokemon in self.roster:
if pokemon.health > 0:
living.append(pokemon)
return living
def printLiving(self):
index = 0
for pokemon in self.living:
print index, pokemon.name
index += 1
def isWeakTo(pkmn, move):
if pkmn.type in weakTo.keys():
if move.type in weakTo[pkmn.type]:
return True
else:
return False
def isStrongTo(pkmn, move):
if pkmn.type in strongTo.keys():
if move.type in strongTo[pkmn.type]:
return True
else:
return False
def getHeroChoice(character):
global gamePlaying
character.checkLiving()
if character.alive <= 0:
print
print
print 'You have no more pokemon!'
print 'You lose.'
print
print
gamePlaying = False
else:
if character.hasActive():
while True:
choice = raw_input('Do you want to (l)ist pokemon, (c)hange pokemon, or (u)se a move?')
if choice == 'l':
character.printLiving()
elif choice == 'c':
#SELECT A POKEMON
index = 0
for pokemon in character.living:
print index, ':', pokemon.name
index += 1
while True:
choice = raw_input('Choose a pokemon to play.')
try:
choice = int(choice)
except ValueError:
print 'Not a valid choice.'
if choice in range(0, len(character.living)):
print character.name, 'chose', character.living[choice].name
return ['c', character.living[choice]]
elif choice == 'u':
#SELECT A MOVE
index = 0
for move in character.active.moves:
print index, move.name
index += 1
while True:
choice = raw_input('Choose a move to use. (1, 2, 3, or 4)')
choice = int(choice)
if choice in range(0, 4):
return ['u', character.active.moves[choice]]
else:
print 'Not a valid choice'
elif not character.hasActive():
#SELECT A POKEMON
index = 0
for pokemon in character.living:
print index, ':', pokemon.name
index += 1
while True:
choice = raw_input('Choose a pokemon to play.')
try:
choice = int(choice)
except ValueError:
print 'Not a valid choice'
if choice in range(0, len(character.living)):
print character.name, 'chose', character.living[choice].name
return ['c', character.living[choice]]
def getAntagChoice(character):
global gamePlaying
character.checkLiving()
if character.alive <= 0:
print
print
print 'Your opponent has no more pokemon!'
print 'You win.'
print
print
gamePlaying = False
else:
if character.hasActive():
moveRoll = random.randint(0, len(character.active.moves) - 1)
return ['u', character.active.moves[moveRoll]]
else:
monsterRoll = random.randint(0, (len(character.living) - 1))
return ['c', character.living[monsterRoll]]
### END POKEMON CLASSES AND FUNCTIONS
# BEGIN POKEMON DATA AND CLASS INSTANTIATIONS
weakTo = {'fire': ['water'], 'water': ['grass'], 'grass': ['fire', 'flying'], 'normal': ['none']}
strongTo = {'fire': ['grass'], 'water': ['fire'], 'grass': ['water', 'flying'], 'normal': ['none']}
#Moves: Name, type, power, accuracy, effect[self/other, stat, amount]=False
Tackle = move('tackle', 'normal', 50, .85)
Ember = move('ember', 'fire', 40, 1)
Bubble = move('bubble', 'water', 20, 1)
Scratch = move('scratch', 'normal', 40, 1)
Gust = move('gust', 'flying', 40, 1)
Growl = move('growl', 'normal', 0, 1, ['other', 'attack', .5])
TailWhip = move('tail whip', 'normal', 0, 1, ['other', 'defense', .5])
SandAttack = move('sand attack', 'normal', 0, 1, ['other', 'accuracy', .5])
#Pokemon: Name, type, health, moves(list), attack, defense, speed
AshPidgey = pokemon('Pidgey', 'flying', 40, [Tackle, Gust, SandAttack], 45, 40, 56)
AshRattata = pokemon('Rattata', 'normal', 30, [Tackle, TailWhip], 56, 35, 72)
AshCharmander = pokemon('Charmander', 'fire', 39, [Ember, Scratch, Growl], 52, 43, 65)
ashRoster = [AshCharmander, AshPidgey, AshRattata]
GaryPidgey = pokemon('Pidgey', 'flying', 40, [Tackle, Gust, SandAttack], 45, 40, 56)
GaryRattata = pokemon('Rattata', 'normal', 30, [Tackle, TailWhip], 56, 35, 72)
GarySquirtle = pokemon('Squirtle', 'water', 44, [Bubble, Tackle, TailWhip], 48, 65, 43)
garyRoster = [GaryPidgey, GaryRattata, GarySquirtle]
Ash = character('Ash', ashRoster)
Gary = character('Gary', garyRoster)
hero = Ash
antag = Gary
pokeList = [AshPidgey, AshRattata]
### END POKEMONE DATA AND CLASS INSTANTIATIONS
### BEGIN GOOGLE APP ENGINE HANDLER CLASSES
#Selection variable determines which request is used. Can be 'main', 'change', 'use', or 'buffer'
selection = ''
class MainPage(webapp2.RequestHandler):
def get(self):
global selection
hero.checkLiving()
pokeIndex = 0
self.response.out.write("""<html><body>""")
self.response.out.write('Hello ')
self.response.out.write(hero.name)
self.response.out.write('<br>')
self.response.out.write('Living pokemon: ')
self.response.out.write(hero.alive)
self.response.out.write('<br>')
self.response.out.write('Your available pokemon:')
self.response.out.write('<br>')
selection = 'main'
for pkmn in hero.living:
self.response.out.write(pokeIndex)
self.response.out.write(' ')
self.response.out.write(pkmn.name)
self.response.out.write('<br>')
pokeIndex += 1
self.response.out.write('Choose a pokemon to play. (1, 2, or 3)')
self.response.out.write("""
<form action="/change" method="post">
<div>
<textarea name="mainChoice" rows="3" cols="60"></textarea>
</div>
<div>
<input type="submit" value="Enter selection">
</div>
</form>
</body>
</html>""")
class ChangePage(webapp2.RequestHandler):
def doMove(self, char, opponent, move):
moveData = char.active.useMove(opponent, move)
if moveData[0] == 'missed':
self.response.out.write(char.active.name)
self.response.out.write(' tried to use ')
self.response.out.write(move.name)
self.response.out.write(' but it missed.')
self.response.out.write('<br>')
if moveData[0] == 'hit':
self.response.out.write(char.active.name)
self.response.out.write(' used ')
self.response.out.write(move.name)
if moveData[1] == 'status':
if moveData[2] == 'self':
self.response.out.write("It's ")
self.response.out.write(moveData[3])
self.response.out.write(' increases.')
self.response.out.write('<br>')
if moveData[2] == 'other':
self.response.out.write('It lowered the ')
self.response.out.write(moveData[3])
self.response.out.write(' of ')
self.response.out.write(opponent.active.name)
self.response.out.write('<br>')
if moveData[1] == 'physical':
if moveData[2] == 'crit':
self.response.out.write('Crital hit!')
self.response.out.write('<br>')
if moveData[3] == 'super':
self.response.out.write("It's super-effective!")
self.response.out.write('<br>')
if moveData[3] == 'ineffective':
self.response.out.write("It's not very effective...")
self.response.out.write('<br>')
self.response.out.write('It does ')
self.response.out.write(moveData[4])
self.response.out.write(' damage.')
def post(self):
global selction
if selection == 'main':
heroChoice = str(self.request.get('mainChoice'))
if selection == 'buffer':
heroChoice = str(self.request.get('bufferChoice'))
hero.checkLiving()
self.response.out.write('<html><body>')
self.response.out.write(hero.name)
self.response.out.write('<br>')
if hero.active != 'nothing':
if hero.active.isAlive():
self.response.out.write('Your active pokemon is ')
self.response.out.write(hero.active.name)
self.response.out.write('<br>')
self.response.out.write("It's health is: ")
self.response.out.write(hero.active.health)
self.response.out.write('<br>')
if hero.alive <= 0:
self.response.out.write('You lose!')
self.response.out.write('<br>')
elif antag.alive <= 0:
self.response.out.write('You win!')
self.response.out.write('<br>')
else:
EM = getAntagChoice(antag)
if EM[0] == 'c':
hero.active = hero.living[int(heroChoice)]
self.response.out.write('You chose ')
self.response.out.write(hero.active.name)
self.response.out.write('<br>')
antag.active = EM[1]
self.response.out.write(antag.name)
self.response.out.write(' chose ')
self.response.out.write(antag.active.name)
self.response.out.write('<br>')
if EM[0] == 'u':
hero.active = hero.living[int(heroChoice)]
self.response.out.write('You chose ')
self.response.out.write(hero.active.name)
self.response.out.write('<br>')
#antag does move EM[1] == a randomly selected move object
self.doMove(antag, hero, EM[1])
hero.checkLiving()
self.response.out.write('Your living pokemon: ')
self.response.out.write(hero.alive)
self.response.out.write('<br>')
if hero.active != 'nothing':
if hero.active.isAlive():
self.response.out.write('Your active pokemon is ')
self.response.out.write(hero.active.name)
self.response.out.write('<br>')
self.response.out.write("It's health is: ")
self.response.out.write(hero.active.health)
self.response.out.write('<br>')
if hero.alive <= 0:
self.response.out.write('You lose!')
self.response.out.write('<br>')
elif antag.alive <= 0:
self.response.out.write('You win!')
self.response.out.write('<br>')
else:
#Ask to change pkmn or use move
selection = 'change'
self.response.out.write('Do you want to (c)hange pokemon or do you want to (u)se a move?')
self.response.out.write("""
<form action="/buffer" method="post">
<div>
<textarea name="chooseChoice" rows="3" cols="60"></textarea>
</div>
<div>
<input type="submit" value="Enter selection">
</div>
</form>
</body>
</html>""")
class BufferPage(webapp2.RequestHandler):
def post(self):
global selection
if selection == 'change':
chooseChoice = self.request.get('chooseChoice')
if chooseChoice == 'c':
self.response.out.write('<html><body>')
pokeIndex = 0
self.response.out.write('Select a pokemon.')
for pkmn in hero.living:
self.response.out.write('<br>')
self.response.out.write(pokeIndex)
self.response.out.write(' ')
self.response.out.write(pkmn.name)
self.response.out.write('<br>')
pokeIndex += 1
selection = 'buffer'
self.response.out.write("""
<form action="/change" method="post">
<div>
<textarea name="bufferChoice" rows="3" cols="60"></textarea>
</div>
<div>
<input type="submit" value="Enter selection">
</div>
</form>
</body>
</html>""")
elif chooseChoice == 'u':
self.response.out.write('<html><body>')
pokeIndex = 0
self.response.out.write('Select a move.')
for move in hero.active.moves:
self.response.out.write('<br>')
self.response.out.write(pokeIndex)
self.response.out.write(' ')
self.response.out.write(move.name)
self.response.out.write('<br>')
pokeIndex += 1
selection = 'buffer'
self.response.out.write("""
<form action="/use" method="post">
<div>
<textarea name="bufferChoice" rows="3" cols="60"></textarea>
</div>
<div>
<input type="submit" value="Enter selection">
</div>
</form>
</body>
</html>""")
if selection == 'use':
useChoice = self.request.get('useChoice')
if useChoice == 'c':
self.response.out.write('<html><body>')
pokeIndex = 0
self.response.out.write('Select a pokemon.')
for pkmn in hero.living:
self.response.out.write('<br>')
self.response.out.write(pokeIndex)
self.response.out.write(' ')
self.response.out.write(pkmn.name)
self.response.out.write('<br>')
pokeIndex += 1
selection = 'buffer'
self.response.out.write("""
<form action="/change" method="post">
<div>
<textarea name="bufferChoice" rows="3" cols="60"></textarea>
</div>
<div>
<input type="submit" value="Enter selection">
</div>
</form>
</body>
</html>""")
elif useChoice == 'u':
self.response.out.write('<html><body>')
pokeIndex = 0
self.response.out.write('Select a move.')
for move in hero.active.moves:
self.response.out.write('<br>')
self.response.out.write(pokeIndex)
self.response.out.write(' ')
self.response.out.write(move.name)
self.response.out.write('<br>')
pokeIndex += 1
selection = 'buffer'
self.response.out.write("""
<form action="/use" method="post">
<div>
<textarea name="bufferChoice" rows="3" cols="60"></textarea>
</div>
<div>
<input type="submit" value="Enter selection">
</div>
</form>
</body>
</html>""")
class UsePage(webapp2.RequestHandler):
def doMove(self, char, opponent, move):
moveData = char.active.useMove(opponent, move)
if moveData[0] == 'missed':
self.response.out.write(char.active.name)
self.response.out.write(' tried to use ')
self.response.out.write(move.name)
self.response.out.write(' but it missed.')
self.response.out.write('<br>')
if moveData[0] == 'hit':
self.response.out.write(char.active.name)
self.response.out.write(' used ')
self.response.out.write(move.name)
if moveData[1] == 'status':
if moveData[2] == 'self':
self.response.out.write("It's ")
self.response.out.write(moveData[3])
self.response.out.write(' increases.')
self.response.out.write('<br>')
if moveData[2] == 'other':
self.response.out.write('It lowered the ')
self.response.out.write(moveData[3])
self.response.out.write(' of ')
self.response.out.write(opponent.active.name)
self.response.out.write('<br>')
if moveData[1] == 'physical':
if moveData[2] == 'crit':
self.response.out.write('Crital hit!')
self.response.out.write('<br>')
if moveData[3] == 'super':
self.response.out.write("It's super-effective!")
self.response.out.write('<br>')
if moveData[3] == 'ineffective':
self.response.out.write("It's not very effective...")
self.response.out.write('<br>')
self.response.out.write('It does ')
self.response.out.write(moveData[4])
self.response.out.write(' damage.')
def post(self):
global selection
choice = str(self.request.get('bufferChoice'))
choice = int(choice)
moveChoice = hero.active.moves[choice]
hero.checkLiving()
self.response.out.write('<html><body>')
self.response.out.write(hero.name)
self.response.out.write('<br>')
if hero.active != 'nothing':
if hero.active.isAlive():
self.response.out.write('Your active pokemon is ')
self.response.out.write(hero.active.name)
self.response.out.write('<br>')
self.response.out.write("It's health is: ")
self.response.out.write(hero.active.health)
self.response.out.write('<br>')
if hero.alive <= 0:
self.response.out.write('You lose!')
self.response.out.write('<br>')
elif antag.alive <= 0:
self.response.out.write('You win!')
self.response.out.write('<br>')
EM = getAntagChoice(antag)
if EM[0] == 'c':
antag.active = EM[1]
self.response.out.write(antag.name)
self.response.out.write(' chose ')
self.response.out.write(antag.active.name)
self.response.out.write('<br>')
self.doMove(hero, antag, moveChoice)
if EM[0] == 'u':
if antag.active.speed > hero.active.speed:
self.doMove(antag, hero, EM[1])
hero.checkLiving
if hero.active.isAlive():
self.doMove(hero, antag, moveChoice)
antag.checkLiving
# if not antag.active.isAlive():
### END GOOGLE APP ENGING HANDLER CLASSES
app = webapp2.WSGIApplication([
('/', MainPage),
('/buffer', BufferPage),
('/change', ChangePage),
('/use', UsePage)
],
debug=True)