Created
September 16, 2014 12:13
-
-
Save arturaz/ab2776d3df5b6ee3daf1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit class OrderingOps[T](val ord: Ordering[T]) extends AnyVal { | |
def orElse(ord2: Ordering[T]) = new CompositeOrdering[T](ord, ord2) | |
} | |
final class CompositeOrdering[T]( | |
val ord1: Ordering[T], val ord2: Ordering[T] | |
) extends Ordering[T] { | |
def compare( x: T, y: T ) = { | |
val comp = ord1.compare( x, y ) | |
if ( comp != 0 ) comp else ord2.compare( x, y ) | |
} | |
} | |
object CompositeOrdering { | |
def apply[T](orderings: Ordering[T]*) = orderings reduceLeft (_ orElse _) | |
} | |
private[this] def ord[A](f: FactionObj => A)(implicit ord: Ordering[A]) | |
: Ordering[FactionObj] = Ordering.by(f) | |
implicit val attackOrdering = | |
/* Fighters must be dealt with first */ | |
ord(_.isInstanceOf[Fighter]).reverse orElse | |
/* Warping in objects must be dealt with first */ | |
ord { | |
case w: Warpable => w.isWarpingIn | |
case _ => false | |
}.reverse orElse | |
/* Attack ones with least HP first. */ | |
ord(_.hp) orElse | |
/* Attack ones with biggest damage output first. */ | |
ord { | |
case f: Fighter => f.fighterStats.attack.end | |
case _ => 0 | |
}.reverse orElse | |
/* Random order after that. */ | |
ord(_ => Random.nextInt()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment