Skip to content

Commit

Permalink
Added script to lookup skill values over multiple chars
Browse files Browse the repository at this point in the history
  • Loading branch information
Bathtor committed Jul 14, 2018
1 parent 197f664 commit 4f594f2
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 57 deletions.
2 changes: 1 addition & 1 deletion script/epapiscript.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name := "EP API Script"

organization := "com.lkroll.ep"

version := "0.7.1"
version := "0.7.2"

scalaVersion := "2.12.4"

Expand Down
2 changes: 1 addition & 1 deletion script/src/main/scala/com/lkroll/ep/api/EPScripts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import fastparse.all._
import util.{ Try, Success, Failure }

object EPScripts extends APIScriptRoot {
override def children: Seq[APIScript] = Seq(RollsScript, TokensScript, GroupRollsScript, compendium.CompendiumScript, BattleManagerScript, CharCleanerScript);
override def children: Seq[APIScript] = Seq(RollsScript, TokensScript, GroupRollsScript, GMTools, compendium.CompendiumScript, BattleManagerScript, CharCleanerScript);

val version = BuildInfo.version;
val author = "Lars Kroll";
Expand Down
125 changes: 125 additions & 0 deletions script/src/main/scala/com/lkroll/ep/api/GMTools.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.lkroll.ep.api

import com.lkroll.roll20.core._
import com.lkroll.roll20.api.{ Character => Roll20Char, _ }
import com.lkroll.roll20.api.conf._
import com.lkroll.roll20.api.templates._
import scalajs.js
import scalajs.js.JSON
import util.{ Try, Success, Failure }
import com.lkroll.ep.model.{ EPCharModel => epmodel, ActiveSkillSection, KnowledgeSkillSection }

object GMTools extends APIScript {
override def apiCommands: Seq[APICommand[_]] = Seq(GMToolsCommand);
}

class GMToolsConf(args: Seq[String]) extends ScallopAPIConf(args) {

val bestMod = opt[Boolean]("best-mod", descr = "List selected characters sorted by best modifier.");

val skillName = opt[String](
"skill-name",
descr = "Select skills by name for selected characters.")(
ScallopUtils.singleArgSpacedConverter(identity));

val characterNames = opt[List[String]](
"char-name",
descr = "Use character of this name instead of selected tokens. (Can be specified multiple times.)")(
ScallopUtils.singleListArgConverter(identity));

val charIds = opt[Boolean]("char-ids", descr = "Read character ids from trailing args.");

val trailing = trailArg[List[String]]("trailing", hidden = true, required = false);

dependsOnAll(bestMod, List(skillName));
requireOne(bestMod);
verify();
}

object GMToolsCommand extends APICommand[GMToolsConf] {
import CoreImplicits._;
override def command = "epgmtools";
override def options = (args) => new GMToolsConf(args);
override def apply(config: GMToolsConf, ctx: ChatContext): Unit = {
val skill = config.skillName();
val res = if (config.characterNames.isSupplied || config.charIds()) {
var chars = List.empty[Roll20Char];
if (config.characterNames.isSupplied) {
val charNames = config.characterNames();
chars ++= charNames.flatMap { charName =>
val r = Roll20Char.find(charName);
if (r.isEmpty) {
ctx.reply(s"No character found for name $charName");
}
r
}
}
if (config.charIds()) {
val charIds = config.trailing();
chars ++= charIds.flatMap { charId =>
val r = Roll20Char.get(charId);
if (r.isEmpty) {
ctx.reply(s"No character found for ID $charId");
}
r
}
}
chars.map(skillTotals(_, skill))
} else ctx.forChar(skillTotals(_, skill));
if (config.bestMod()) {
val bySkillName = res.flatMap {
case (char, mods) => {
mods.map(t => (char.name, t._1, t._2))
}
}.groupBy(_._2).map {
case (skillName, l) => {
val l2 = l.map(t => (t._1, t._3));
(skillName -> l2)
}
};
val organised = bySkillName.map(t => (t._1 -> t._2.sortBy(_._2)));
val results = organised.map(_ match {
case (skill, mods) => {
val title = s"<b>$skill</b>";
val modsS = mods.map{
case (char, total) => s"<em>$char</em> has [[$total]]"
}.mkString("<ul><li>", "</li><li>", "</li></ul>");
title + modsS
}
}).mkString("<ul><li>", "</li><li>", "</li></ul>");
debug(s"Results: $results");
ctx.reply(s"Best Modifiers (incl. wounds and traumas):$results");
}
}

private def skillTotals(char: Roll20Char, skill: String): (Roll20Char, List[(String, Int)]) = {
val woundMod = char.attribute(epmodel.woundMod)();
val traumaMod = char.attribute(epmodel.traumaMod)();
val aSkills = char.repeating(ActiveSkillSection.skillName).filter(_.current.equalsIgnoreCase(skill));
val aSkillMods: List[(String, Int)] = aSkills.flatMap { skillNameAttr =>
val Some(rowId) = skillNameAttr.getRowId;
val field = char.repeatingAt(rowId)(ActiveSkillSection.field).map(_.apply).
map(f => if (f.isEmpty) "" else s" ($f)").getOrElse("");
val name = s"${skillNameAttr()}$field";
val totalO = char.repeatingAt(rowId)(ActiveSkillSection.total).map(_.apply);
totalO.map { total =>
val totalModded = total - woundMod - traumaMod;
(name -> totalModded)
}
};
val kSkills = char.repeating(KnowledgeSkillSection.skillName).filter(_.current.equalsIgnoreCase(skill));
val kSkillMods: List[(String, Int)] = kSkills.flatMap { skillNameAttr =>
val Some(rowId) = skillNameAttr.getRowId;
val field = char.repeatingAt(rowId)(KnowledgeSkillSection.field).map(_.apply).
map(f => if (f.isEmpty) "" else s" ($f)").getOrElse("");
val name = s"${skillNameAttr()}$field";
val totalO = char.repeatingAt(rowId)(KnowledgeSkillSection.total).map(_.apply);
totalO.map { total =>
val totalModded = total - woundMod - traumaMod;
(name -> totalModded)
}
};
val skillMods: List[(String, Int)] = aSkillMods ++ kSkillMods;
(char -> skillMods)
}
}
94 changes: 39 additions & 55 deletions script/src/main/scala/com/lkroll/ep/api/TokensScript.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,63 +84,47 @@ object EPTokensCommand extends APICommand[EPTokensConf] {
override def command = "eptoken";
override def options = (args) => new EPTokensConf(args);
override def apply(config: EPTokensConf, ctx: ChatContext): Unit = {
val graphicTokens = ctx.selected;
if (graphicTokens.isEmpty) {
ctx.reply("No tokens selected. Nothing to do...");
} else {
val tokens = graphicTokens.flatMap {
case t: Token => Some(t)
case c => debug(s"Ignoring non-Token $c"); None
};
val updatedCharacters = tokens.flatMap { token =>
debug(s"Working on token: ${token.name} (${token.id})");
token.represents match {
case Some(char) => {
var updates = List.empty[String];
debug(s"Token represents $char");
if (config.clear()) {
val existing = char.abilities;
debug(s"Found existing abilities to be removed for char=${char.name}:\n${existing.mkString("\n");}", true);
existing.foreach(_.remove());
updates ::= config.clear.name;
}
if (config.ini()) {
if (createAbility(SupportedAbilities.ini, char, config.force())) {
updates ::= config.ini.name;
}
}
if (config.fray()) {
if (createAbility(SupportedAbilities.fray, char, config.force())) {
updates ::= config.fray.name;
}
}
if (config.frayHalved()) {
if (createAbility(SupportedAbilities.frayHalved, char, config.force())) {
updates ::= config.frayHalved.name;
}
}
if (config.skill.isSupplied) {
// if (createAbility(SupportedAbilities.fromSkill(config.skill()), char, config.force())) {
// updates ::= config.skill.name + s"(${config.skill()})";
// }
config.skill().foreach { skillName =>
if (createAbility(SupportedAbilities.fromSkill(skillName), char, config.force())) {
updates ::= config.skill.name + s"($skillName)";
}
}
}
Some(char.name -> updates)
val updatedCharacters = ctx.forChar { char =>
var updates = List.empty[String];
debug(s"Token represents $char");
if (config.clear()) {
val existing = char.abilities;
debug(s"Found existing abilities to be removed for char=${char.name}:\n${existing.mkString("\n");}", true);
existing.foreach(_.remove());
updates ::= config.clear.name;
}
if (config.ini()) {
if (createAbility(SupportedAbilities.ini, char, config.force())) {
updates ::= config.ini.name;
}
}
if (config.fray()) {
if (createAbility(SupportedAbilities.fray, char, config.force())) {
updates ::= config.fray.name;
}
}
if (config.frayHalved()) {
if (createAbility(SupportedAbilities.frayHalved, char, config.force())) {
updates ::= config.frayHalved.name;
}
}
if (config.skill.isSupplied) {
// if (createAbility(SupportedAbilities.fromSkill(config.skill()), char, config.force())) {
// updates ::= config.skill.name + s"(${config.skill()})";
// }
config.skill().foreach { skillName =>
if (createAbility(SupportedAbilities.fromSkill(skillName), char, config.force())) {
updates ::= config.skill.name + s"($skillName)";
}
case None => ctx.reply(s"Token ${token.name}(${token.id}) does not represent any character!"); None
}

};
val updates = updatedCharacters.map(_ match {
case (char, ups) => char + ups.mkString("<ul><li>", "</li><li>", "</li></ul>")
}).mkString("<ul><li>", "</li><li>", "</li></ul>");
debug(s"Updates: $updates")
ctx.reply(s"Updated Characters $updates");
}
}
(char.name -> updates)
};
val updates = updatedCharacters.map(_ match {
case (char, ups) => char + ups.mkString("<ul><li>", "</li><li>", "</li></ul>")
}).mkString("<ul><li>", "</li><li>", "</li></ul>");
debug(s"Updates: $updates")
ctx.reply(s"Updated Characters $updates");
}

private def createAbility(ability: AbilityTemplate, char: Character, force: Boolean): Boolean = {
Expand Down
69 changes: 69 additions & 0 deletions script/src/main/scala/com/lkroll/ep/api/package.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,80 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Lars Kroll <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package com.lkroll.ep

import com.lkroll.ep.compendium.ChatRenderable
import com.lkroll.roll20.core.{ Rolls, APIButton, RollExpression }
import com.lkroll.ep.model.{ EPCharModel => epmodel }
import com.lkroll.roll20.api._
import com.lkroll.roll20.api.templates._

package object api {
implicit class EnhancedContext(ctx: ChatContext) {
import com.lkroll.roll20.core._
def withChars(f: List[Character] => Unit): Unit = {
val graphicTokens = ctx.selected;
if (graphicTokens.isEmpty) {
ctx.reply("No tokens selected. Nothing to do...");
} else {
val tokens = graphicTokens.flatMap {
case t: Token => Some(t)
case c => APILogger.debug(s"Ignoring non-Token $c"); None
};
val chars = tokens.flatMap { token =>
APILogger.debug(s"Working on token: ${token.name} (${token.id})");
val tO = token.represents;
if (tO.isEmpty) {
ctx.reply(s"Token ${token.name}(${token.id}) does not represent any character!");
}
tO
}
f(chars)
}
}

def forChar[T](f: Character => T): List[T] = {
val graphicTokens = ctx.selected;
if (graphicTokens.isEmpty) {
ctx.reply("No tokens selected. Nothing to do...");
List.empty
} else {
val tokens = graphicTokens.flatMap {
case t: Token => Some(t)
case c => APILogger.debug(s"Ignoring non-Token $c"); None
};
tokens.flatMap { token =>
APILogger.debug(s"Working on token: ${token.name} (${token.id})");
token.represents match {
case Some(char) => Some(f(char))
case None => ctx.reply(s"Token ${token.name}(${token.id}) does not represent any character!"); None
}
}
}
}
}

import TemplateImplicits._;

def asInfoTemplate(title: String, subtitle: String, importButton: Option[APIButton], keys: List[(String, String)], description: String): String = {
Expand Down

0 comments on commit 4f594f2

Please sign in to comment.