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

159 lines
4.2 KiB
Plaintext
Raw Normal View History

2019-10-19 09:25:09 +00:00
{{
const dotUtils = require('./dotUtils.js');
2019-10-19 09:25:09 +00:00
const champions = require('./.champion.json')
.filter(({ id }) => id > 0)
.sortBy(({ name }) => name);
2021-06-30 23:29:39 +00:00
const constName = name => dotUtils.changeCase.constantCase(name).replace(/[^_A-Z0-9]+/g, '');
const constNamePad = 12;
2019-10-19 09:25:09 +00:00
const hashFactor = 256;
2021-06-30 23:29:39 +00:00
const strHash = (str) => {
2019-10-19 09:25:09 +00:00
let h = 0;
for (let c of str)
h = hashFactor * h + c.charCodeAt(0);
return h;
};
}}{{= dotUtils.preamble() }}
2019-10-19 09:25:09 +00:00
2021-06-30 23:29:39 +00:00
use serde::{ Serialize, Deserialize };
2019-10-19 09:25:09 +00:00
2021-06-30 23:29:39 +00:00
/// League of Legends champions.
2019-10-23 01:21:27 +00:00
///
2021-06-30 23:29:39 +00:00
/// The documentation of each const field specifies:<br>
2019-10-23 01:21:27 +00:00
/// NAME (`IDENTIFIER`, ID).
///
/// Implements [IntoEnumIterator](super::IntoEnumIterator).
2021-06-30 23:29:39 +00:00
#[derive(Serialize, Deserialize)]
#[derive(Copy, Clone)]
#[derive(PartialEq, Eq, PartialOrd, Ord)]
#[serde(transparent)]
#[repr(transparent)]
pub struct Champion(pub i16);
2019-10-26 05:19:00 +00:00
2021-06-30 23:29:39 +00:00
impl Champion {
2019-10-19 09:25:09 +00:00
{{
2019-10-23 01:21:27 +00:00
for (let { id, alias, name } of champions) {
2019-10-19 09:25:09 +00:00
}}
/// {{= name }} (`{{= alias }}`, {{= id }}).
2021-06-30 23:29:39 +00:00
pub const {{= constName(name) }}: Self = Self({{= id }});
2019-10-19 09:25:09 +00:00
{{
}
}}
2021-06-30 23:29:39 +00:00
pub const fn is_known(self) -> bool {
match self {
{{
for (let { name, alias } of champions) {
}}
Self::{{= constName(name).padEnd(constNamePad) }} => true,
{{
}
}}
_ => false,
}
}
/// The champion's name (`en_US` localization).
pub const fn name(self) -> Option<&'static str> {
match self {
{{
for (let { name } of champions) {
}}
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,
2021-06-30 23:29:39 +00:00
/// capitalization preserved, however the follow are exceptions:
2019-11-05 07:29:20 +00:00
///
2021-06-30 23:29:39 +00:00
/// Field | Name | Identifier
2019-11-05 07:29:20 +00:00
/// --------|------|-----------
2019-10-19 09:25:09 +00:00
{{
for (let { name, alias } of champions) {
if (name.replace(/[^a-zA-Z0-9]+/, '') !== alias) {
2019-10-19 09:25:09 +00:00
}}
2021-06-30 23:29:39 +00:00
/// `{{= constName(name) }}` | "{{= name }}" | "{{= alias }}"
2019-10-19 09:25:09 +00:00
{{
}
}
}}
2021-06-30 23:29:39 +00:00
pub const fn identifier(self) -> Option<&'static str> {
2019-10-19 09:25:09 +00:00
match self {
{{
for (let { name, alias } of champions) {
}}
2021-06-30 23:29:39 +00:00
Self::{{= constName(name).padEnd(constNamePad) }} => Some("{{= alias }}"),
2019-10-19 09:25:09 +00:00
{{
}
}}
2021-06-30 23:29:39 +00:00
_ => None,
2019-10-19 09:25:09 +00:00
}
}
}
2021-06-30 23:29:39 +00:00
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)
}
}
impl std::convert::From<i16> for Champion {
fn from(value: i16) -> Self {
Self(value)
}
}
impl std::convert::From<Champion> for i16 {
fn from(value: Champion) -> Self {
value.0
}
}
impl std::fmt::Display for Champion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl std::fmt::Debug for Champion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Champion({} {})", self.0, self.identifier().unwrap_or("UNKNOWN"))
}
}