1
0
mirror of https://gitlab.com/Deukhoofd/BattleSim.git synced 2025-10-29 02:30:05 +00:00

Lots of stuff

This commit is contained in:
Deukhoofd
2016-02-01 23:19:30 +01:00
commit d7316d5799
6681 changed files with 527969 additions and 0 deletions

View File

@@ -0,0 +1,199 @@
# eventName should be one of "challenge" or "find battle"
# opts may include whether to enable clauses, for example
@createChallengePane = (opts) ->
$wrapper = opts.populate
$button = opts.button
$accept = opts.acceptButton || $()
$reject = opts.rejectButton || $()
$buttons = $button.add($accept).add($reject)
eventName = opts.eventName
capitalizedEventName = "#{eventName[0].toUpperCase()}#{eventName.substr(1)}"
acceptEventName = "accept#{capitalizedEventName}"
rejectEventName = "reject#{capitalizedEventName}"
cancelEventName = "cancel#{capitalizedEventName}"
generation = opts.generation
personId = opts.personId
defaultClauses = opts.defaultClauses || []
blockedClauses = opts.blockedClauses ? false
selectedTeamId = null
selectedAlt = null
getSelectedTeam = ->
PokeBattle.TeamStore.get(selectedTeamId) || PokeBattle.TeamStore.at(0)
renderCurrentTeam = ($context) ->
$selectTeam = $context.find('.select-team')
if PokeBattle.TeamStore.length > 0
currentTeam = getSelectedTeam()
html = JST['team_dropdown'](window: window, team: currentTeam)
$selectTeam.html(html)
else
$selectTeam.html("You have no teams!")
cancelChallenge = ->
enableButtons()
if personId
PokeBattle.primus.send(cancelEventName, personId)
else
format = $selectFormat.data('format')
PokeBattle.primus.send(cancelEventName, format)
$button.trigger('cancelChallenge')
disableButtons = ->
$wrapper.find('.select').addClass('disabled')
$buttons.addClass('disabled')
# Enable buttons
enableButtons = ->
$buttons.removeClass('disabled')
toggleAltInput = (visible) ->
$wrapper.find('.alt-input').toggleClass("hidden", !visible)
$wrapper.find('.alt-dropdown-section').toggleClass("hidden", visible)
$wrapper.find('.alt-input input').focus() if visible
isAttachedToDom = ->
$.contains(document, $wrapper.get(0))
altCreatedEvent = ->
return PokeBattle.primus.off('altCreated', altCreatedEvent) unless isAttachedToDom()
$wrapper.find('.alt-input input').val("")
toggleAltInput(false)
PokeBattle.primus.on 'altCreated', altCreatedEvent
enableButtons()
$wrapper.html(JST['new_battle']({window, defaultClauses}))
$selectFormat = $wrapper.find(".select-format")
# Implement finding battle/challenging
$button.on 'click.challenge', ->
# Start requesting for notify permission here
PokeBattle.requestNotifyPermission()
format = $selectFormat.data('format')
# Toggle state when you press the button.
if !$button.hasClass('disabled')
team = getSelectedTeam()
unless team
alert("You need to create a team using the Teambuilder before you can battle.")
PokeBattle.navigation.showTeambuilder()
return
disableButtons()
teamJSON = team.toNonNullJSON().pokemon
# Send the event
if personId
$clauses = $wrapper.find('input:checked[type="checkbox"]')
clauses = []
$clauses.each(-> clauses.push(parseInt($(this).val(), 10)))
PokeBattle.primus.send(eventName, personId, format, teamJSON, clauses, selectedAlt)
else
PokeBattle.primus.send(eventName, format, teamJSON, selectedAlt)
$button.addClass('disabled').trigger('challenge')
else
cancelChallenge()
# Implement accept/reject buttons.
$accept.on 'click.challenge', ->
return if $(this).hasClass('disabled')
team = getSelectedTeam()
unless team
alert("You need to create a team using the Teambuilder before you can battle.")
PokeBattle.navigation.showTeambuilder()
return
disableButtons()
teamJSON = team.toNonNullJSON().pokemon
PokeBattle.primus.send(acceptEventName, personId, teamJSON, selectedAlt)
$reject.on 'click.challenge', ->
return if $(this).hasClass('disabled')
disableButtons()
PokeBattle.primus.send(rejectEventName, personId)
# Clicking the alts dropdown brings down an alt selection dropdown menu
$wrapper.find('.select-alt').click (e) ->
html = JST['alt_dropdown'](alts: PokeBattle.alts.list, username: PokeBattle.username)
$wrapper.find('.alt-dropdown').html(html)
# Selecting an alt from the dropdown
$wrapper.find('.alt-dropdown').on 'click', '.select-alt-dropdown-item', (e) ->
selectedAlt = $(this).data('alt-name')
$wrapper.find('.select-alt').html($(this).html())
# When add alt is clicked, show the alt input form
$wrapper.find('.alt-dropdown').on 'click', '.add-alt-dropdown-item', (e) ->
toggleAltInput(true)
# Clicking the Add Alt Button
$wrapper.find('.alt-input .add-button').click (e) ->
altName = $wrapper.find('.alt-input input').val().trim()
PokeBattle.alts.createAlt(altName)
# Clicking the Cancel Add Alt Button
$wrapper.find('.alt-input .cancel-button').click (e) ->
toggleAltInput(false)
# Clicking the team dropdown brings down a team selection menu.
# Also updates the allTeams collection
$wrapper.find('.select-team').click (e) ->
allTeams = PokeBattle.TeamStore.models || []
html = JST['team_dropdown'](window: window, teams: allTeams)
$wrapper.find('.team-dropdown').html(html)
# Selecting a team from the menu
$wrapper.find('.team-dropdown').on 'click', '.select-team-dropdown-item', (e) ->
slot = $(e.currentTarget).data('slot')
selectedTeamId = PokeBattle.TeamStore.at(slot).id
renderCurrentTeam($wrapper)
# Selecting build team from the menu
$wrapper.find('.team-dropdown').on 'click', '.build-team-option', (e) ->
PokeBattle.navigation.showTeambuilder()
# Selecting the format changes the dropdown.
$wrapper.find('.format-dropdown').on 'click', '.select-format-dropdown-item', (e) ->
$target = $(e.currentTarget)
format = $target.data('format')
$selectFormat.text($target.text())
$selectFormat.data('format', format)
# Select non-alt option
$wrapper.find('.select-alt').html(JST['alt_dropdown'](alt: null, username: PokeBattle.username))
# Auto-select format.
if generation
# If a generation is passed, auto-select it.
$format = $wrapper.find(".format-dropdown a[data-format='#{generation}']")
$format.first().click()
$wrapper.find('.select-format').addClass('disabled')
else
# Auto-select first available format.
$wrapper.find('.format-dropdown a').first().click()
if blockedClauses
$checkboxes = $wrapper.find('input[type="checkbox"]')
if blockedClauses != true
$checkboxes = $checkboxes.filter ->
clause = Number($(this).data('clause'))
clause in blockedClauses
$checkboxes.prop('disabled', true)
$checkboxes.closest('label').addClass('disabled')
renderCurrentTeam($wrapper)
# Called when a team has been updated
teamUpdated = ->
# If this challenge panel no longer exists, remove the callback
if not isAttachedToDom()
PokeBattle.TeamStore.off 'add remove reset saved', teamUpdated
return
# Rerender the current team
renderCurrentTeam($wrapper)
# Start listening for team updated events
PokeBattle.TeamStore.on 'add remove reset saved', teamUpdated

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,15 @@
# domId is optional:
#
# PokeBattle.modal(modalPath, [domId], options, initialize)
PokeBattle.modal = (modalPath, domId, options, initialize) ->
[domId, options, initialize] = [null, domId, options] if !_.isString(domId)
[options, initialize] = [{}, options] if _.isFunction(options)
$modal = $(JST[modalPath](options))
id = '#' + (domId || $modal.prop('id'))
if $(id).length == 0
$modal.appendTo($('body'))
initialize?($modal)
else
$modal = $(id).last()
$modal.modal('show')
return $modal

View File

@@ -0,0 +1,262 @@
HiddenPower = (if module? then require('../../../../shared/hidden_power') else window.HiddenPower ?= {})
@PokeBattle ?= {}
@PokeBattle.parseTeam = (teamString) ->
text = teamString.split('\n')
team = []
pokemonRegex = /^(.*?)\s*(\(M\)|\(F\)|)?(?:\s*@\s*(.*))?$/
pokemon = null
for line in text
line = line.trim()
if line.length == 0
pokemon = null
else if !pokemon
[ all, pokemonLine, gender, item ] = line.match(pokemonRegex)
pokemon = {}
team.push(pokemon)
if pokemonLine.match(/(.*?)\s*\((.*)\)/)
pokemon.name = RegExp.$1
pokemonLine = RegExp.$2
convertNameToSpeciesAndForme(pokemon, pokemonLine.trim())
pokemon.gender = gender[1] if gender # (M) and (F)
pokemon.item = item if item
for olditem, newitem of Aliases.items
if olditem is pokemon.item
pokemon.item = newitem
else if line.match(/^(?:Trait|Ability):\s+(.*)$/i)
pokemon.ability = RegExp.$1
for oldability, newability of Aliases.abilities
if pokemon.ability is oldability
pokemon.ability = newability
else if line.match(/^Level:\s+(.*)$/i)
pokemon.level = Number(RegExp.$1) || 100
else if line.match(/^Happiness:\s+(.*)$/i)
pokemon.happiness = Number(RegExp.$1) || 0
else if line.match(/^Shiny: Yes$/i)
pokemon.shiny = true
else if line.match(/^EVs: (.*)$/i)
evs = RegExp.$1.split(/\//g)
pokemon.evs = {}
for ev in evs
ev = ev.trim()
[ numberString, rawStat ] = ev.split(/\s+/)
pokemon.evs[statsHash[rawStat]] = Number(numberString) || 0
else if line.match(/^IVs: (.*)$/i)
ivs = RegExp.$1.split(/\//g)
pokemon.ivs = {}
for iv in ivs
iv = iv.trim()
[ numberString, rawStat ] = iv.split(/\s+/)
pokemon.ivs[statsHash[rawStat]] = Number(numberString) || 0
else if line.match(/^([A-Za-z]+) nature/i)
pokemon.nature = RegExp.$1
else if line.match(/^[\-\~]\s*(.*)/)
moveName = RegExp.$1
for oldmove, newmove of Aliases.moves
if moveName is oldmove
moveName = newmove
if /Hidden Power /.test(moveName)
if !pokemon.ivs
moveName.match(/Hidden Power (.*)/i)
hiddenPowerType = RegExp.$1.trim().toLowerCase().replace(/\W+/g, '')
pokemon.ivs = HiddenPower.BW.ivs[hiddenPowerType] || {}
moveName = 'Hidden Power'
pokemon.moves ?= []
pokemon.moves.push(moveName)
return team
@PokeBattle.exportTeam = (json) ->
s = []
for pokemon in json
s.push("")
species = pokemon.species
if pokemon.forme && pokemon.forme != "default"
species += "-#{pokemon.forme[0].toUpperCase()}"
mainLine = []
if pokemon.name
mainLine.push(pokemon.name)
mainLine.push("(#{species})")
else
mainLine.push(species)
mainLine.push("(#{pokemon.gender})") if pokemon.gender
mainLine.push("@ #{pokemon.item}") if pokemon.item
s.push(mainLine.join(' '))
# Ability
s.push("Ability: #{pokemon.ability}") if pokemon.ability
# EVs
if pokemon.evs
evArray = for stat, amount of pokemon.evs when amount > 0
"#{amount} #{reverseStatsHash[stat]}"
s.push("EVs: #{evArray.join(" / ")}") if evArray.length > 0
# IVs
if pokemon.ivs
ivArray = for stat, amount of pokemon.ivs when amount < 31
"#{amount} #{reverseStatsHash[stat]}"
s.push("IVs: #{ivArray.join(" / ")}") if ivArray.length > 0
# Nature
s.push("#{pokemon.nature} nature") if pokemon.nature
# Level
s.push("Level: #{pokemon.level}") if pokemon.level && pokemon.level != 100
# Shiny
s.push("Shiny: Yes") if pokemon.shiny
# Happiness
if pokemon.happiness && pokemon.happiness != 100
s.push("Happiness: #{pokemon.happiness}")
# Moves
if pokemon.moves
s.push("- #{moveName}") for moveName in pokemon.moves
s.push("\n") # Trailing newlines, just in case.
s.join("\n")
Aliases =
moves:
"Ancient Power" : "AncientPower"
"Bubble Beam" : "BubbleBeam"
"Double Slap" : "DoubleSlap"
"Dragon Breath" : "DragonBreath"
"Dynamic Punch" : "DynamicPunch"
"Extreme Speed" : "ExtremeSpeed"
"Feint Attack" : "Faint Attack"
"Feather Dance" : "FeatherDance"
"Grass Whistle" : "GrassWhistle"
"High Jump Kick" : "Hi Jump Kick"
"Poison Powder" : "PoisonPowder"
"Sand Attack" : "Sand-Attack"
"Self-Destruct" : "Selfdestruct"
"Smelling Salts" : "SmellingSalt"
"Smokescreen" : "SmokeScreen"
"Soft-Boiled" : "Softboiled"
"Solar Beam" : "SolarBeam"
"Sonic Boom" : "SonicBoom"
"Thunder Punch" : "ThunderPunch"
"Thunder Shock" : "ThunderShock"
"Vice Grip" : "ViceGrip"
abilities:
"Compound Eyes" : "Compoundeyes"
"Lightning Rod" : "Lightningrod"
items:
"Balm Mushroom" : "BalmMushroom"
"Black Glasses" : "BlackGlasses"
"Bright Powder" : "BrightPowder"
"Deep Sea Scale" : "DeepSeaScale"
"Deep Sea Tooth" : "DeepSeaTooth"
"Energy Powder" : "EnergyPowder"
"Never-Melt Ice" : "NeverMeltIce"
"Paralyze Heal" : "Parlyz Heal"
"Rage Candy Bar" : "RageCandyBar"
"Silver Powder" : "SilverPowder"
"Thunder Stone" : "Thunderstone"
"Tiny Mushroom" : "TinyMushroom"
"Twisted Spoon" : "TwistedSpoon"
"X Defense" : "X Defend"
"X Sp. Atk" : "X Special"
statsHash =
'hp' : 'hp'
'Hp' : 'hp'
'HP' : 'hp'
'Atk' : 'attack'
'Def' : 'defense'
'SAtk' : 'specialAttack'
'SpA' : 'specialAttack'
'SDef' : 'specialDefense'
'SpD' : 'specialDefense'
'Spe' : 'speed'
'Spd' : 'speed'
reverseStatsHash =
'hp' : 'HP'
'attack' : 'Atk'
'defense' : 'Def'
'specialAttack' : 'SAtk'
'specialDefense' : 'SDef'
'speed' : 'Spe'
convertNameToSpeciesAndForme = (pokemon, species) ->
if species.match(/(Thundurus|Landorus|Tornadus)\-T(herian)?/i)
pokemon.species = RegExp.$1
pokemon.forme = 'therian'
else if species.match(/Shaymin-S(ky)?/i)
pokemon.species = "Shaymin"
pokemon.forme = 'sky'
else if species.match(/Giratina-O(rigin)?/i)
pokemon.species = "Giratina"
pokemon.forme = 'origin'
else if species.match(/Arceus(\-.*)?/)
pokemon.species = "Arceus"
else if species.match(/Kyurem-B(lack)?/)
pokemon.species = "Kyurem"
pokemon.forme = "black"
else if species.match(/Kyurem-W(hite)?/)
pokemon.species = "Kyurem"
pokemon.forme = "white"
else if species.match(/Rotom-W|Rotom-Wash/)
pokemon.species = "Rotom"
pokemon.forme = "wash"
else if species.match(/Rotom-S|Rotom-Fan/)
pokemon.species = "Rotom"
pokemon.forme = "fan"
else if species.match(/Rotom-H|Rotom-Heat/)
pokemon.species = "Rotom"
pokemon.forme = "heat"
else if species.match(/Rotom-F|Rotom-Frost/)
pokemon.species = "Rotom"
pokemon.forme = "frost"
else if species.match(/Rotom-C|Rotom-Mow/)
pokemon.species = "Rotom"
pokemon.forme = "mow"
else if species.match(/Deoxys-A|Deoxys-Attack/)
pokemon.species = "Deoxys"
pokemon.forme = "attack"
else if species.match(/Deoxys-D|Deoxys-Defense/)
pokemon.species = "Deoxys"
pokemon.forme = "defense"
else if species.match(/Deoxys-S|Deoxys-Speed/)
pokemon.species = "Deoxys"
pokemon.forme = "speed"
else if species.match(/Basculin-Blue-Striped|Basculin-A/)
pokemon.species = "Basculin"
pokemon.forme = "blue-striped"
else if species.match(/Keldeo-Resolute|Keldeo-R/)
pokemon.species = "Keldeo"
pokemon.forme = "resolute"
else if species.match(/Shellos-East/)
pokemon.species = "Shellos"
# TODO: Read east forme
pokemon.forme = "default"
else if species.match(/Gastrodon-East/)
pokemon.species = "Gastrodon"
# TODO: Read east forme
pokemon.forme = "default"
else if species.match(/Wormadam-Sandy|Wormadam-G/)
pokemon.species = "Wormadam"
pokemon.forme = "sandy"
else if species.match(/Wormadam-Trash|Wormadam-S/)
pokemon.species = "Wormadam"
pokemon.forme = "trash"
else if species.match(/Deerling-.*/)
pokemon.species = "Deerling"
# TODO: Read other formes
pokemon.forme = null
else if species.match(/Sawsbuck-.*/)
pokemon.species = "Sawsbuck"
# TODO: Read other formes
pokemon.forme = null
else if species.match(/Unown-.*/)
pokemon.species = "Unown"
# TODO: Read other formes
pokemon.forme = null
else
pokemon.species = species

View File

@@ -0,0 +1,17 @@
# TODO: Move more timer functionality into here
# TODO: Encapsulate this better. Maybe put humanizeTime in a time object with more functions?
PokeBattle.humanizeTime = (unixTime) =>
unixTime = 0 if !unixTime? || unixTime < 0
seconds = Math.floor(unixTime / 1000) % 60
minutes = Math.floor(unixTime / 1000 / 60)
seconds = String(seconds)
return minutes + ":" + "00".substr(seconds.length) + seconds
$ ->
window.setInterval( ->
$(".elapsed-time").each ->
$el = $(this)
elapsedTime = Date.now() - $el.data("time-start")
$el.text(PokeBattle.humanizeTime(elapsedTime))
, 500)