Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeremysmitherman/966d7454878e57b2efefe92a48814d17 to your computer and use it in GitHub Desktop.
Save jeremysmitherman/966d7454878e57b2efefe92a48814d17 to your computer and use it in GitHub Desktop.
package com.hoggit.missionhub.mission.data
import java.io.File
import java.util.UUID
import com.hoggit.mizery.core.mission.json._
import com.typesafe.scalalogging.StrictLogging
import scala.collection.mutable.ListBuffer
object MissionData extends StrictLogging {
/**
* Goes through a list of VehicleGroups in a mission and removes all of the groups that don't contain a Client skill.
* @return A list of VehicleGroups
*/
def getPlayerPlanes(mission: Mission, coalitionColor: String): List[VehicleGroup] = {
val targetCoalition = mission.coalitions.filter(_.name == coalitionColor)
val planeGroupsList: List[VehicleGroups] = for {
coalition <- targetCoalition
country <- coalition.countries
planes <- country.planes
helis <- country.helicopters
} yield planes.merge(helis)
val vehicleList = for {
planeGroups <- planeGroupsList
planeGroup <- planeGroups.groups
} yield planeGroup
vehicleList.filter(grp => {
//TODO: This leaves in the whole group if there's one client. Do we want that?
grp.units.exists(v => (v.skill == Client || v.skill == Player))
})
}
/**
* Formats the countries on a mission into a List of countries we can easily organize.
*/
def getCountries(mission: Mission): List[Country] = {
val countries = new ListBuffer[Country]()
mission.coalitions.foreach(coalition => {
coalition.countries.foreach(country => {
countries += Country(country.name, coalition.name)
})
})
countries.toList
}
/**
* List of taskings available to all vehicles.
* Ignores tasks of "Nothing".
*/
def getTaskings(mission: Mission): Set[String] = {
val vehicleGroupsList: List[VehicleGroups] = for {
coalitions <- mission.coalitions
countries <- coalitions.countries
planes <- countries.planes
helis <- countries.helicopters
} yield planes.merge(helis)
val vehicleGroups: List[VehicleGroup] = for {
vehicles <- vehicleGroupsList
vehicleGroup <- vehicles.groups
} yield vehicleGroup
vehicleGroups.foldLeft(Set.empty[String])((tasks, group) => {
if (group.task != "Nothing") tasks ++ Set(group.task) else tasks
})
}
/**
* Determines how many Client slots there are in a mission and returns true if it's greater than 1
*/
def isMultiplayer(playerGroups: List[VehicleGroup]): Boolean = {
playerGroups.toStream.scanLeft(0) { (sum, grp) =>
{
sum + grp.units.count(_.skill == Client)
}
}.dropWhile(_ < 1).headOption.isDefined
}
def apply(mission: Mission): MissionData = {
val redPlayerPlanes = getPlayerPlanes(mission, "red")
val bluePlayerPlanes = getPlayerPlanes(mission, "blue")
val coalitions = getCountries(mission)
val multiplayer: Boolean = isMultiplayer(bluePlayerPlanes)
val tasks: Set[String] = getTaskings(mission)
MissionData(mission.name, mission.description, multiplayer, redPlayerPlanes, bluePlayerPlanes, coalitions, tasks, "", "")
}
def emptyMissionData() = MissionData("", "", multiplayer = false, List.empty[VehicleGroup], List.empty[VehicleGroup], List.empty[Country])
}
/**
* This is the wrapper for the data we get back from briefing room.
* Used in the creation of MissionInfo objects.
* TODO: I think we can get rid of this and just use MissionInfo.
*/
case class MissionData(name: String, description: String = "foo", multiplayer: Boolean, redPlayerPlanes: List[VehicleGroup], bluePlayerPlanes: List[VehicleGroup], coalitions: List[Country], tasks: Set[String] = Set.empty[String], descriptionBlueTask: String, descriptionRedTask: String)
object MissionInfo {
def apply(
id: UUID = UUID.randomUUID(),
name: String = "",
path: String = "",
tags: List[String],
misData: MissionData
): MissionInfo = {
MissionInfo(id, misData.name, misData.description, path, tags, misData.multiplayer, misData.redPlayerPlanes, misData.bluePlayerPlanes, misData.coalitions, misData.tasks, misData.descriptionBlueTask, misData.descriptionRedTask)
}
}
/**
* This is what we store in ES and return to users who search for missions.
*/
final case class MissionInfo(
id: UUID,
name: String,
description: String,
path: String,
tags: List[Tag],
multiplayer: Boolean,
redPlayerVehicles: List[VehicleGroup],
bluePlayerVehicles: List[VehicleGroup],
countries: List[Country],
tasks: Set[String],
descriptionBlueTask: String,
descriptionRedTask: String
)
final case class Country(name: String, side: String)
final case class MissionList(missions: Set[MissionInfo])
final case class MissionFile(name: String, file: File)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment