2021-06-30 16:34:34 -07:00
|
|
|
{{
|
|
|
|
const dotUtils = require('./dotUtils.js');
|
|
|
|
const champions = require('./.champion.json')
|
|
|
|
.filter(({ id }) => id > 0)
|
|
|
|
.sortBy(({ name }) => name);
|
2021-06-30 17:06:33 -07:00
|
|
|
|
2021-06-30 16:34:34 -07:00
|
|
|
const constName = name => dotUtils.changeCase.constantCase(name).replace(/[^_A-Z0-9]+/g, '');
|
|
|
|
const constNamePad = 12;
|
|
|
|
}}{{= dotUtils.preamble() }}
|
|
|
|
|
2021-06-30 18:50:08 -07:00
|
|
|
newtype_enum! {
|
|
|
|
/// A League of Legends champion.
|
|
|
|
///
|
|
|
|
/// This newtype acts as a C-like enum; each variant corresponds to an
|
|
|
|
/// integer value. Using a newtype allows _unknown_ variants to be
|
|
|
|
/// represented. This is important when Riot adds new champions.
|
|
|
|
///
|
|
|
|
/// Field | Name | Identifier | Id
|
|
|
|
/// ---|---|---|---
|
2022-06-20 20:43:15 -07:00
|
|
|
/// `NONE` | None (no ban) | | -1
|
2021-06-30 16:34:34 -07:00
|
|
|
{{
|
2021-06-30 17:06:33 -07:00
|
|
|
for (const { id, alias, name } of champions) {
|
|
|
|
}}
|
2021-06-30 18:50:08 -07:00
|
|
|
/// `{{= constName(name) }}` | "{{= name }}" | "{{= alias }}" | {{= id }}
|
2021-06-30 17:06:33 -07:00
|
|
|
{{
|
|
|
|
}
|
|
|
|
}}
|
2021-06-30 18:50:08 -07:00
|
|
|
pub newtype_enum Champion(i16) {
|
2022-06-20 20:43:15 -07:00
|
|
|
/// `-1`, none. Appears when a champion ban is not used in champ select.
|
|
|
|
NONE = -1,
|
|
|
|
|
2021-06-30 17:06:33 -07:00
|
|
|
{{
|
2021-06-30 18:50:08 -07:00
|
|
|
for (const { id, alias, name } of champions) {
|
2021-06-30 16:34:34 -07:00
|
|
|
}}
|
2021-09-09 22:51:15 -07:00
|
|
|
/// `{{= id }}`.
|
2021-06-30 18:50:08 -07:00
|
|
|
{{= constName(name) }} = {{= id }},
|
2021-06-30 16:34:34 -07:00
|
|
|
{{
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
}
|
2021-06-30 18:50:08 -07:00
|
|
|
}
|
2021-06-30 16:34:34 -07:00
|
|
|
|
2021-06-30 18:50:08 -07:00
|
|
|
impl Champion {
|
2021-06-30 16:34:34 -07:00
|
|
|
/// The champion's name (`en_US` localization).
|
|
|
|
pub const fn name(self) -> Option<&'static str> {
|
|
|
|
match self {
|
|
|
|
{{
|
2021-06-30 17:06:33 -07:00
|
|
|
for (const { name } of champions) {
|
2021-06-30 16:34:34 -07:00
|
|
|
}}
|
|
|
|
Self::{{= constName(name).padEnd(constNamePad) }} => Some("{{= name }}"),
|
|
|
|
{{
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The champion's identifier key. Somtimes called "key", "identifier", or "alias".
|
|
|
|
/// This is mainly used in DDragon paths.
|
|
|
|
///
|
|
|
|
/// This is generally the `en_US` name with spaces and punctuation removed,
|
|
|
|
/// capitalization preserved, however the follow are exceptions:
|
|
|
|
///
|
2021-06-30 18:50:08 -07:00
|
|
|
/// Field | Name | Identifier | Id
|
|
|
|
/// ---|---|---|---
|
2021-06-30 16:34:34 -07:00
|
|
|
{{
|
2021-06-30 18:50:08 -07:00
|
|
|
for (const { id, alias, name } of champions) {
|
2021-06-30 16:34:34 -07:00
|
|
|
if (name.replace(/[^a-zA-Z0-9]+/, '') !== alias) {
|
|
|
|
}}
|
2021-06-30 18:50:08 -07:00
|
|
|
/// `{{= constName(name) }}` | "{{= name }}" | "{{= alias }}" | {{= id }}
|
2021-06-30 16:34:34 -07:00
|
|
|
{{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
pub const fn identifier(self) -> Option<&'static str> {
|
|
|
|
match self {
|
|
|
|
{{
|
2021-06-30 17:06:33 -07:00
|
|
|
for (const { name, alias } of champions) {
|
2021-06-30 16:34:34 -07:00
|
|
|
}}
|
|
|
|
Self::{{= constName(name).padEnd(constNamePad) }} => Some("{{= alias }}"),
|
|
|
|
{{
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2021-12-29 09:34:12 -08:00
|
|
|
|
|
|
|
/// https://github.com/MingweiSamuel/Riven/issues/36
|
|
|
|
pub(crate) fn serialize_result<S>(
|
|
|
|
val: &Result<Self, std::num::TryFromIntError>,
|
|
|
|
serializer: S,
|
|
|
|
) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::ser::Serializer,
|
|
|
|
{
|
|
|
|
use serde::ser::Serialize;
|
|
|
|
val.unwrap_or(Champion(-1)).serialize(serializer)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// https://github.com/MingweiSamuel/Riven/issues/36
|
|
|
|
pub(crate) fn deserialize_result<'de, D>(
|
|
|
|
deserializer: D,
|
|
|
|
) -> Result<Result<Self, std::num::TryFromIntError>, D::Error>
|
|
|
|
where
|
|
|
|
D: serde::de::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
use std::convert::TryInto;
|
|
|
|
<i64 as serde::de::Deserialize>::deserialize(deserializer).map(|id| id.try_into().map(Self))
|
|
|
|
}
|
2021-06-30 16:34:34 -07:00
|
|
|
}
|
|
|
|
|
2022-02-08 22:28:29 -08:00
|
|
|
/// The error used for failures in [`Champion`]'s
|
|
|
|
/// [`FromStr`](std::str::FromStr) implementation.
|
2022-02-08 17:17:47 -08:00
|
|
|
///
|
|
|
|
/// Currently only internally stores the four characters used to parse the
|
|
|
|
/// champion, but may change in the future.
|
2021-12-29 11:50:45 -08:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ParseChampionError([char; 4]);
|
|
|
|
impl std::fmt::Display for ParseChampionError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
let s: String = self.0.iter().copied().take_while(|&c| '\0' != c).collect();
|
|
|
|
write!(f, "Failed to parse unknown champion prefix: {:?}", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::error::Error for ParseChampionError {}
|
|
|
|
|
2021-06-30 16:34:34 -07:00
|
|
|
impl std::str::FromStr for Champion {
|
2021-12-29 11:50:45 -08:00
|
|
|
type Err = ParseChampionError;
|
2021-06-30 16:34:34 -07:00
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
2021-12-29 11:50:45 -08:00
|
|
|
let mut chars = ['\0'; 4];
|
|
|
|
s.chars()
|
2021-06-30 16:34:34 -07:00
|
|
|
.take(4)
|
|
|
|
.filter(|c| c.is_ascii_alphanumeric())
|
2021-12-29 11:50:45 -08:00
|
|
|
.map(|c| c.to_ascii_uppercase())
|
|
|
|
.enumerate()
|
|
|
|
.for_each(|(i, c)| chars[i] = c);
|
|
|
|
match chars {
|
2021-06-30 16:34:34 -07:00
|
|
|
{{
|
|
|
|
const keyStrings = (name, alias) => new Set([].concat(...[ name, alias ].map(s => s.toUpperCase())
|
|
|
|
.map(s => [
|
|
|
|
s.replace(/[^A-Z0-9]+/, '').substring(0, 4),
|
|
|
|
s.split(/[^A-Z0-9]/, 1)[0].substring(0, 4),
|
|
|
|
s.split(/[^A-Z]/, 1)[0].substring(0, 4),
|
|
|
|
])));
|
|
|
|
for (const { id, alias, name } of champions) {
|
|
|
|
for (const prefix of keyStrings(name, alias)) {
|
2021-12-29 11:50:45 -08:00
|
|
|
const chars = Object.assign(Array(4).fill('\\0'), Array.from(prefix))
|
|
|
|
.map(c => `'${c}'`)
|
|
|
|
.map(c => c.padStart(4));
|
2021-06-30 16:34:34 -07:00
|
|
|
}}
|
2021-12-29 11:50:45 -08:00
|
|
|
/* {{= prefix.padEnd(4) }} */ [{{= chars.join(', ') }}] => Ok(Champion::{{= constName(name) }}),
|
2021-06-30 16:34:34 -07:00
|
|
|
{{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
2021-12-29 11:50:45 -08:00
|
|
|
unknown => Err(ParseChampionError(unknown)),
|
2021-06-30 16:34:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::convert::TryFrom<&str> for Champion {
|
|
|
|
type Error = <Self as std::str::FromStr>::Err;
|
|
|
|
fn try_from(value: &str) -> Result<Self, Self::Error> {
|
|
|
|
<Self as std::str::FromStr>::from_str(value)
|
|
|
|
}
|
|
|
|
}
|