๐Ÿ“ฆ sleepyfran / duets

๐Ÿ“„ Character.fs ยท 57 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57module Duets.Entities.Character

open Duets.Common
open System

type CharacterNameValidationError =
    | NameTooShort
    | NameTooLong

type CharacterAgeValidationError =
    | AgeTooYoung
    | AgeTooOld

let allAttributes = Union.allCasesOf<CharacterAttribute> ()

let defaultAttributes =
    [ (CharacterAttribute.Energy, 100)
      (CharacterAttribute.Health, 100)
      (CharacterAttribute.Hunger, 100)
      (CharacterAttribute.Mood, 100) ]
    |> Map.ofList

/// Base character that has no real properties. Only to be used while
/// populating a character during a transformation.
let empty =
    { Id = CharacterId <| Identity.create ()
      Name = ""
      Birthday = Calendar.gameBeginning |> Calendar.Ops.addYears -25<years>
      Gender = Gender.Other
      Attributes = defaultAttributes
      Moodlets = Set.empty }

/// Creates a character from the given parameters, generating a random
/// ID for it.
let from name gender birthday =
    { Id = CharacterId <| Identity.create ()
      Name = name
      Birthday = birthday
      Gender = gender
      Attributes = defaultAttributes
      Moodlets = Set.empty }

/// Validates whether the name of the character is valid or not.
let validateName (name: string) =
    if String.IsNullOrEmpty name then Error NameTooShort
    else if name.Length > 50 then Error NameTooLong
    else Ok name

/// Validates whether the age of the character is valid or not.
let validateBirthday year =
    let birthday = Calendar.Date.fromYear year
    let age = Calendar.Query.yearsBetween Calendar.gameBeginning birthday

    if age < 18<years> then Error AgeTooYoung
    else if age > 80<years> then Error AgeTooOld
    else Ok birthday