io.github.pidoveproject.showdown.battle.RelativePosition.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scala-showdown-api_3 Show documentation
Show all versions of scala-showdown-api_3 Show documentation
A Scala wrapper of Pokemon Showdown's API
The newest version!
package io.github.pidoveproject.showdown.battle
import io.github.pidoveproject.showdown.protocol.{MessageDecoder, MessageEncoder, ProtocolError}
import io.github.pidoveproject.showdown.protocol.MessageDecoder.toInvalidInput
/**
* The relative position of an active pokemon.
*
* @param side the relative side of the pokemon
* @param slot the slot of the pokemon
*/
case class RelativePosition(side: RelativeSide, slot: PokemonSlot)
object RelativePosition:
given MessageEncoder[RelativePosition] = MessageEncoder.string.contramap: target =>
val side =
if target.side == RelativeSide.Ally then "-"
else "+"
s"$side${target.slot}"
given MessageDecoder[RelativePosition] = MessageDecoder.string.mapEither:
case s"+$slot" =>
for
intSlot <- slot.toIntOption.toRight(ProtocolError.InvalidInput(slot, "Invalid int"))
pokemonSlot <- PokemonSlot.either(intSlot).toInvalidInput(slot)
yield RelativePosition(RelativeSide.Enemy, pokemonSlot)
case s"-$slot" =>
for
intSlot <- slot.toIntOption.toRight(ProtocolError.InvalidInput(slot, "Invalid int"))
pokemonSlot <- PokemonSlot.either(intSlot).toInvalidInput(slot)
yield RelativePosition(RelativeSide.Ally, pokemonSlot)
case value => Left(ProtocolError.InvalidInput(value, "Invalid relative position"))