]> git.lizzy.rs Git - go-anidb.git/commitdiff
misc: CountEpisodes and convenience conversion functions
authorDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Tue, 16 Jul 2013 17:06:13 +0000 (14:06 -0300)
committerDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Tue, 16 Jul 2013 17:06:13 +0000 (14:06 -0300)
misc/episodelist.go
misc/episoderange.go

index a22bef488b942af3fc17003914185b660c500bce..7f7d0abd8e2edc752587cbb36919c6ad9885575a 100644 (file)
@@ -17,6 +17,14 @@ type EpisodeCount struct {
 
 type EpisodeList []*EpisodeRange
 
+func EpisodeToList(ep *Episode) EpisodeList {
+       return RangesToList(EpisodeToRange(ep))
+}
+
+func RangesToList(ranges ...*EpisodeRange) EpisodeList {
+       return EpisodeList(ranges)
+}
+
 // Converts the EpisodeList into the AniDB API list format.
 func (el EpisodeList) String() string {
        scales := map[EpisodeType]int{}
@@ -134,6 +142,37 @@ func (el EpisodeList) Simplify() EpisodeList {
        return nl
 }
 
+func (el EpisodeList) CountEpisodes() (ec EpisodeCount) {
+       for _, er := range el {
+               var c *int
+               switch er.Type {
+               case EpisodeTypeRegular:
+                       c = &ec.RegularCount
+               case EpisodeTypeSpecial:
+                       c = &ec.SpecialCount
+               case EpisodeTypeCredits:
+                       c = &ec.CreditsCount
+               case EpisodeTypeOther:
+                       c = &ec.OtherCount
+               case EpisodeTypeTrailer:
+                       c = &ec.TrailerCount
+               case EpisodeTypeParody:
+                       c = &ec.ParodyCount
+               default:
+                       continue
+               }
+               if *c < 0 {
+                       continue
+               }
+               if er.End == nil {
+                       *c = -1
+                       continue
+               }
+               *c += er.End.Number - er.Start.Number
+       }
+       return
+}
+
 func (el EpisodeList) Len() int {
        return len(el)
 }
index 0f11b0cbc671037b7d236923cd95e3ce28394f8d..fb91caab9fc2c041acc31f5c12f5265a5f3d8ec5 100644 (file)
@@ -12,6 +12,14 @@ type EpisodeRange struct {
        End   *Episode    // The end of the range; may be nil, which represents an endless range
 }
 
+func EpisodeToRange(ep *Episode) *EpisodeRange {
+       return &EpisodeRange{
+               Type:  ep.Type,
+               Start: ep,
+               End:   ep,
+       }
+}
+
 // Converts the EpisodeRange into AniDB API range format.
 func (er *EpisodeRange) String() string {
        return er.Format(er.scale())