forked from mirror/Riven
1
0
Fork 0
Riven/srcgen/consts/champion.rs.dt

130 lines
3.5 KiB
Plaintext
Raw Normal View History

2021-06-30 23:34:34 +00:00
{{
const dotUtils = require('./dotUtils.js');
const champions = require('./.champion.json')
.filter(({ id }) => id > 0)
.sortBy(({ name }) => name);
2021-07-01 00:06:33 +00:00
2021-06-30 23:34:34 +00:00
const constName = name => dotUtils.changeCase.constantCase(name).replace(/[^_A-Z0-9]+/g, '');
const constNamePad = 12;
const hashFactor = 256;
const strHash = (str) => {
let h = 0;
2021-07-01 00:06:33 +00:00
for (const c of str)
2021-06-30 23:34:34 +00:00
h = hashFactor * h + c.charCodeAt(0);
return h;
};
}}{{= dotUtils.preamble() }}
use serde::{ Serialize, Deserialize };
2021-07-01 01:50:08 +00: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
/// ---|---|---|---
2021-06-30 23:34:34 +00:00
{{
2021-07-01 00:06:33 +00:00
for (const { id, alias, name } of champions) {
}}
2021-07-01 01:50:08 +00:00
/// `{{= constName(name) }}` | "{{= name }}" | "{{= alias }}" | {{= id }}
2021-07-01 00:06:33 +00:00
{{
}
}}
2021-07-01 01:50:08 +00:00
#[derive(Serialize, Deserialize)]
#[serde(transparent)]
pub newtype_enum Champion(i16) {
2021-07-01 00:06:33 +00:00
{{
2021-07-01 01:50:08 +00:00
for (const { id, alias, name } of champions) {
2021-06-30 23:34:34 +00:00
}}
2021-07-01 01:50:08 +00:00
{{= constName(name) }} = {{= id }},
2021-06-30 23:34:34 +00:00
{{
}
}}
}
2021-07-01 01:50:08 +00:00
}
2021-06-30 23:34:34 +00:00
2021-07-01 01:50:08 +00:00
impl Champion {
2021-06-30 23:34:34 +00:00
/// The champion's name (`en_US` localization).
pub const fn name(self) -> Option<&'static str> {
match self {
{{
2021-07-01 00:06:33 +00:00
for (const { name } of champions) {
2021-06-30 23:34:34 +00: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-07-01 01:50:08 +00:00
/// Field | Name | Identifier | Id
/// ---|---|---|---
2021-06-30 23:34:34 +00:00
{{
2021-07-01 01:50:08 +00:00
for (const { id, alias, name } of champions) {
2021-06-30 23:34:34 +00:00
if (name.replace(/[^a-zA-Z0-9]+/, '') !== alias) {
}}
2021-07-01 01:50:08 +00:00
/// `{{= constName(name) }}` | "{{= name }}" | "{{= alias }}" | {{= id }}
2021-06-30 23:34:34 +00:00
{{
}
}
}}
pub const fn identifier(self) -> Option<&'static str> {
match self {
{{
2021-07-01 00:06:33 +00:00
for (const { name, alias } of champions) {
2021-06-30 23:34:34 +00:00
}}
Self::{{= constName(name).padEnd(constNamePad) }} => Some("{{= alias }}"),
{{
}
}}
_ => None,
}
}
}
impl std::str::FromStr for Champion {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.chars()
.take(4)
.filter(|c| c.is_ascii_alphanumeric())
.fold(0_u32, |hash, next| hash * {{= hashFactor }} + u32::from(next))
{
{{
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)) {
}}
0x{{= strHash(prefix).toString(16).padEnd(8) }} /* {{= prefix.padEnd(4) }} */ => Ok(Champion::{{= constName(name) }}),
{{
}
}
}}
_ => Err(()),
}
}
}
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)
}
}