Riven/srcgen/consts/champion.rs.dt

108 lines
2.9 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);
2019-10-19 09:25:09 +00:00
const hashFactor = 256;
const enumName = name => name.replace(/[^a-z]+/i, '');
const strHash = function(str) {
let h = 0;
for (let c of str)
h = hashFactor * h + c.charCodeAt(0);
return h;
};
const padId = function(id) { return ('' + id).padEnd(3); };
}}{{= dotUtils.preamble() }}
2019-10-19 09:25:09 +00:00
use std::fmt;
use num_enum::{ IntoPrimitive, TryFromPrimitive };
2019-10-26 05:19:00 +00:00
use serde_repr::{ Serialize_repr, Deserialize_repr };
2019-10-19 09:25:09 +00:00
2019-10-23 01:21:27 +00:00
/// League of Legend's champions.
///
/// The documentation of each variant specifies:<br>
/// NAME (`IDENTIFIER`, ID).
2019-10-30 20:29:23 +00:00
#[non_exhaustive]
#[derive(Debug, Copy, Clone)]
#[derive(IntoPrimitive, TryFromPrimitive)]
2019-10-26 05:19:00 +00:00
#[derive(Serialize_repr, Deserialize_repr)]
#[repr(i16)]
2019-10-19 09:25:09 +00:00
pub enum Champion {
2019-10-26 05:19:00 +00:00
/// A champion that doesn't exist. Used in TeamBans when no ban occured.
None = -1,
2019-10-19 09:25:09 +00:00
{{
2019-10-23 01:21:27 +00:00
for (let { id, alias, name } of champions) {
const comment = `${name.padEnd(14)} (\`${alias}\`, ${id}).`.padEnd(36);
const ename = enumName(name).padEnd(12);
2019-10-19 09:25:09 +00:00
}}
2019-10-23 01:21:27 +00:00
/** {{= comment }} */ {{= ename }} = {{= id }},
2019-10-19 09:25:09 +00:00
{{
}
}}
2019-10-23 01:21:27 +00:00
}
2019-10-19 09:25:09 +00:00
impl Champion {
pub fn name(self) -> &'static str {
match self {
2019-10-26 05:19:00 +00:00
Self::None => "None",
2019-10-19 09:25:09 +00:00
{{
for (let { id, name } of champions) {
}}
Self::{{= enumName(name).padEnd(12) }} => "{{= name }}",
{{
}
}}
}
}
pub fn identifier(self) -> &'static str {
match self {
2019-10-26 05:19:00 +00:00
Self::None => "None",
2019-10-19 09:25:09 +00:00
{{
for (let { name, alias } of champions) {
}}
Self::{{= enumName(name).padEnd(12) }} => "{{= alias }}",
{{
}
}}
}
}
}
impl std::str::FromStr for Champion {
type Err = ();
fn from_str(val: &str) -> Result<Self, Self::Err> {
// 4 characters encoded as an int.
match val.chars()
.filter(|c| c.is_ascii_alphabetic())
.take(4)
.map(|c| c.to_ascii_uppercase() as u32)
.fold(0u32, |hash, next| hash * {{= hashFactor }} + next)
{
{{
let keyStrings = (name, alias) => new Set([].concat(...[ name, alias ].map(s => s.toUpperCase())
.map(s => [
s.replace(/[^A-Z]+/, '').substring(0, 4),
s.split(/[^A-Z]/, 1)[0].substring(0, 4)
])));
for (let { id, alias, name } of champions) {
for (let prefix of keyStrings(name, alias)) {
}}
{{= ('' + strHash(prefix)).padEnd(10) }} /* {{= prefix.padEnd(4) }} */ => Ok(Self::{{= enumName(name) }}),
{{
}
}
}}
_ => Err(()),
}
}
}
impl fmt::Display for Champion {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}