Renamed the root package to fmon and added paralysis, burn, and sleep status effects
This commit is contained in:
12
FakeMon/src/fmon/util/Dice.scala
Normal file
12
FakeMon/src/fmon/util/Dice.scala
Normal file
@@ -0,0 +1,12 @@
|
||||
package fmon.util
|
||||
|
||||
import scala.util.Random
|
||||
|
||||
class Dice(val rng : Random) extends AnyVal {
|
||||
def nextInt(min : Int, max : Int) = rng.nextInt(max - min + 1) + min
|
||||
|
||||
def pick[T](seq : IndexedSeq[T]) : T = seq(rng.nextInt(seq.size))
|
||||
|
||||
def chance(frac : Fraction) :Boolean = chance(frac.num, frac.denom)
|
||||
def chance(num : Int, denom : Int) : Boolean = rng.nextInt(denom) < num
|
||||
}
|
||||
18
FakeMon/src/fmon/util/Fraction.scala
Normal file
18
FakeMon/src/fmon/util/Fraction.scala
Normal file
@@ -0,0 +1,18 @@
|
||||
package fmon.util
|
||||
|
||||
case class Fraction(val num : Int, val denom : Int) extends Ordered[Fraction] {
|
||||
def *(f : Fraction) = Fraction(num * f.num, denom * f.denom)
|
||||
def *(x : Int) : Int = x * num / denom
|
||||
|
||||
def unary_-() : Fraction = Fraction(-num, denom)
|
||||
|
||||
override def compare(f : Fraction) : Int = {
|
||||
(num * f.denom) compare (f.num * denom)
|
||||
}
|
||||
}
|
||||
|
||||
class IntFraction(val x : Int) extends AnyVal {
|
||||
def %% = Fraction(x, 100)
|
||||
def \\ (denom : Int) = Fraction(x, denom)
|
||||
def frac = Fraction(x, 1)
|
||||
}
|
||||
52
FakeMon/src/fmon/util/JsonHelper.scala
Normal file
52
FakeMon/src/fmon/util/JsonHelper.scala
Normal file
@@ -0,0 +1,52 @@
|
||||
package fmon.util
|
||||
|
||||
import org.json4s._
|
||||
import org.json4s.jackson.JsonMethods._
|
||||
import org.json4s.ext.EnumNameSerializer
|
||||
|
||||
import java.io.InputStream
|
||||
|
||||
import scala.io.Source
|
||||
|
||||
object JsonHelper {
|
||||
|
||||
// Maybe Look at Jackson-YAML & Jackson-Scala
|
||||
|
||||
implicit val formats = fmon.stat.formats
|
||||
|
||||
def extract[T](text : String)(implicit mf : Manifest[T]) : T = {
|
||||
val json = parse(text)
|
||||
json.extract[T]
|
||||
}
|
||||
|
||||
def extract[T](source : Source)(implicit mf : Manifest[T]) : T = {
|
||||
val text = source.getLines().mkString("\n")
|
||||
extract(text)
|
||||
}
|
||||
|
||||
def extractFromFile[T](filename : String)(implicit mf : Manifest[T]) : T = {
|
||||
using(filename : InputStream)(reader => extract(Source.fromInputStream(filename)))
|
||||
}
|
||||
|
||||
def toScala(jvalue : JValue): Any = jvalue match {
|
||||
case JNothing => null
|
||||
case JNull => null
|
||||
case JString(s) => s
|
||||
case JDouble(num) => num.toDouble
|
||||
case JDecimal(num) => num.toInt
|
||||
case JInt(num) => num.toInt
|
||||
case JLong(num) => num.toLong
|
||||
case JBool(value) => value
|
||||
case JObject(obj) => obj.map{ case (s, o) => {
|
||||
(s, toScala(o))
|
||||
}}.toMap
|
||||
case JArray(arr) => arr.map(toScala)
|
||||
case JSet(data) => data.map(toScala)
|
||||
}
|
||||
|
||||
def loadFromSource(source : Source) : List[Map[String, Any]] = {
|
||||
val text = source.getLines().mkString("\n")
|
||||
val array = parse(text).asInstanceOf[JArray]
|
||||
array.arr.map(jo => toScala(jo).asInstanceOf[Map[String, Any]])
|
||||
}
|
||||
}
|
||||
45
FakeMon/src/fmon/util/YamlHelper.scala
Normal file
45
FakeMon/src/fmon/util/YamlHelper.scala
Normal file
@@ -0,0 +1,45 @@
|
||||
package fmon.util
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
|
||||
import com.fasterxml.jackson.module.scala.DefaultScalaModule
|
||||
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
|
||||
|
||||
import java.io.InputStream
|
||||
|
||||
import scala.io.Source
|
||||
|
||||
import fmon.util._
|
||||
|
||||
object YamlHelper {
|
||||
val mapper = new ObjectMapper(new YAMLFactory()) with ScalaObjectMapper
|
||||
mapper.registerModule(DefaultScalaModule)
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
||||
|
||||
def extract[T](text : String)(implicit m : Manifest[T]) : T = {
|
||||
mapper.readValue[T](text)
|
||||
}
|
||||
|
||||
def extract[T](source : Source)(implicit mf : Manifest[T]) : T = {
|
||||
val text = source.getLines().mkString("\n")
|
||||
extract(text)
|
||||
}
|
||||
|
||||
def extract[T](source : InputStream)(implicit m : Manifest[T]) : T = {
|
||||
mapper.readValue[T](source)
|
||||
}
|
||||
|
||||
def extractSeq[T](text : String)(implicit m : Manifest[T]) : IndexedSeq[T] = {
|
||||
mapper.readValue[IndexedSeq[T]](text)
|
||||
}
|
||||
|
||||
def extractSeq[T](source : Source)(implicit mf : Manifest[T]) : IndexedSeq[T] = {
|
||||
val text = source.getLines().mkString("\n")
|
||||
extractSeq(text)
|
||||
}
|
||||
|
||||
def extractSeq[T](source : InputStream)(implicit m : Manifest[T]) : IndexedSeq[T] = {
|
||||
mapper.readValue[IndexedSeq[T]](source)
|
||||
}
|
||||
}
|
||||
46
FakeMon/src/fmon/util/package.scala
Normal file
46
FakeMon/src/fmon/util/package.scala
Normal file
@@ -0,0 +1,46 @@
|
||||
package fmon
|
||||
|
||||
import java.io._
|
||||
|
||||
import scala.language.implicitConversions
|
||||
|
||||
package object util {
|
||||
type TypeReference[A] = com.fasterxml.jackson.core.`type`.TypeReference[A]
|
||||
|
||||
def using[T <: Closeable, A](resource : T)(block : T => A) : A = {
|
||||
try {
|
||||
block(resource)
|
||||
} finally {
|
||||
resource.close()
|
||||
}
|
||||
}
|
||||
|
||||
def makeParentDirs(filename : String) : Unit = {
|
||||
val file = new File(filename)
|
||||
file.getParentFile().mkdirs()
|
||||
}
|
||||
|
||||
def getFilename(file : String) : String = {
|
||||
file.split("\\.").head
|
||||
}
|
||||
|
||||
implicit def fileName2Writer(filename : String) : PrintWriter = {
|
||||
makeParentDirs(filename)
|
||||
new PrintWriter(new BufferedWriter(new FileWriter(filename)))
|
||||
}
|
||||
|
||||
implicit def fileName2Reader(filename : String) : BufferedReader = {
|
||||
new BufferedReader(new FileReader(filename))
|
||||
}
|
||||
|
||||
implicit def filename2OutputStream(filename : String) : PrintStream = {
|
||||
makeParentDirs(filename)
|
||||
new PrintStream(new BufferedOutputStream(new FileOutputStream(filename)))
|
||||
}
|
||||
|
||||
implicit def filename2InputStream(filename : String) : BufferedInputStream = {
|
||||
new BufferedInputStream(new FileInputStream(filename))
|
||||
}
|
||||
|
||||
implicit def int2Helper(x : Int) = new IntFraction(x)
|
||||
}
|
||||
Reference in New Issue
Block a user