Skip to content

Instantly share code, notes, and snippets.

@NathaTerrien
Created January 18, 2016 19:34
Show Gist options
  • Save NathaTerrien/2f825727a0a52d131473 to your computer and use it in GitHub Desktop.
Save NathaTerrien/2f825727a0a52d131473 to your computer and use it in GitHub Desktop.
/* ---- PMT ---------------------- */
var PMT = PMT || (function(){
'use strict';
var version = '1.0',
majEncombrement = function () {
getAttrs(["encombrement"], function (values) {
var enc = parseFloat(values.encombrement) || 0.0;
var explo = 0;
var combat = 0;
if (enc>=71) {
explo = 3;
combat = 1;
} else if (enc>=41 && enc<71) {
explo = 9;
combat = 3;
} else if (enc>=31 && enc<41) {
explo = 18;
combat = 6;
} else if (enc>=21 && enc<31) {
explo = 24;
combat = 9;
} else {
explo = 36;
combat = 12;
}
setAttrs({
mvtexploration: explo,
mvtcombat: combat,
mvtcourse: explo,
mvtexterieur: explo*3
});
});
};
return {
majEncombrement: majEncombrement
};
}());
/* ---- BEGIN: TheAaronSheet.js ---- */
// Github: https://github.com/shdwjk/TheAaronSheet/blob/master/TheAaronSheet.js
// By: The Aaron, Arcane Scriptomancer
// Contact: https://app.roll20.net/users/104025/the-aaron
var TAS = TAS || (function(){
'use strict';
var version = '0.2.0',
lastUpdate = 1448523710,
queuedUpdates = {}, //< Used for delaying saves till the last momment.
log = function(){
_.each(arguments,function(m){
switch(typeof m){
case 'string':
console.log('<TAS> :: '+m);
break;
case 'number':
console.log('<TAS> :: [number]: '+m);
break;
default:
console.log('<TAS> :: ['+typeof m +']:');
console.log(m);
console.log('<TAS> :: =========================================================');
}
});
},
prepareUpdate = function( attribute, value ){
queuedUpdates[attribute]=value;
},
applyQueuedUpdates = function() {
setAttrs(queuedUpdates);
queuedUpdates = {};
},
namesFromArgs = function(args,base){
return _.chain(args)
.reduce(function(memo,attr){
if('string' === typeof attr) {
memo.push(attr);
} else if(_.isArray(args) || _.isArguments(args)){
memo = namesFromArgs(attr,memo);
}
return memo;
},(_.isArray(base) && base) || [])
.uniq()
.value();
},
addId = function(obj,value){
Object.defineProperty(obj,'id',{
value: value,
writeable: false,
enumerable: false
});
},
addProp = function(obj,prop,value,fullname){
(function(){
var pname=(_.contains(['S','F','I','D'],prop) ? '_'+prop : prop),
full_pname = fullname || prop,
pvalue=value;
_.each(['S','I','F'],function(p){
if( !_.has(obj,p)){
Object.defineProperty(obj, p, {
value: {},
enumerable: false,
readonly: true
});
}
});
if( !_.has(obj,'D')){
Object.defineProperty(obj, 'D', {
value: _.reduce(_.range(10),function(m,d){
Object.defineProperty(m, d, {
value: {},
enumerable: true,
readonly: true
});
return m;
},{}),
enumerable: false,
readonly: true
});
}
// Raw value
Object.defineProperty(obj, pname, {
enumerable: true,
set: function(v){
pvalue=v;
prepareUpdate(full_pname,v);
},
get: function(){
return pvalue;
}
});
// string value
Object.defineProperty(obj.S, pname, {
enumerable: true,
set: function(v){
var val=v.toString();
pvalue=val;
prepareUpdate(full_pname,val);
},
get: function(){
return pvalue.toString();
}
});
// int value
Object.defineProperty(obj.I, pname, {
enumerable: true,
set: function(v){
var val=parseInt(v,10) || 0;
pvalue=val;
prepareUpdate(full_pname,val);
},
get: function(){
return parseInt(pvalue,10) || 0;
}
});
// float value
Object.defineProperty(obj.F, pname, {
enumerable: true,
set: function(v){
var val=parseFloat(v) || 0;
pvalue=val;
prepareUpdate(full_pname,val);
},
get: function(){
return parseFloat(pvalue) || 0;
}
});
_.each(_.range(10),function(d){
Object.defineProperty(obj.D[d], pname, {
enumerable: true,
set: function(v){
var val=(parseFloat(v) || 0).toFixed(d);
pvalue=val;
prepareUpdate(full_pname,val);
},
get: function(){
return (parseFloat(pvalue) || 0).toFixed(d);
}
});
});
}());
},
repeating = function( section ) {
return (function(s){
var sectionName = s,
attrNames = [],
fieldNames = [],
operations = [],
repAttrs = function(){
attrNames = namesFromArgs(arguments,attrNames);
return this;
},
repFields = function(){
fieldNames = namesFromArgs(arguments,fieldNames);
return this;
},
repReduce = function(func, initial, final, context) {
operations.push({
type: 'reduce',
func: (func && _.isFunction(func) && func) || _.noop,
memo: (_.isUndefined(initial) && 0) || initial,
final: (final && _.isFunction(final) && final) || _.noop,
context: context || {}
});
return this;
},
repMap = function(func, final, context) {
operations.push({
type: 'map',
func: (func && _.isFunction(func) && func) || _.noop,
final: (final && _.isFunction(final) && final) || _.noop,
context: context || {}
});
return this;
},
repEach = function(func, final, context) {
operations.push({
type: 'each',
func: (func && _.isFunction(func) && func) || _.noop,
final: (final && _.isFunction(final) && final) || _.noop,
context: context || {}
});
return this;
},
repTap = function(final, context) {
operations.push({
type: 'tap',
final: (final && _.isFunction(final) && final) || _.noop,
context: context || {}
});
return this;
},
repExecute = function(){
var rowSet = {},
attrSet = {},
fieldIds = [],
fullFieldNames = [];
// call each operation per row.
// call each operation's final
getSectionIDs("repeating_"+sectionName,function(ids){
fieldIds = ids;
fullFieldNames = _.reduce(fieldIds,function(memo,id){
return memo.concat(_.map(fieldNames,function(name){
return 'repeating_'+sectionName+'_'+id+'_'+name;
}));
},[]);
getAttrs( _.uniq(attrNames.concat(fullFieldNames)), function(values){
_.each(attrNames,function(aname){
if(values.hasOwnProperty(aname)){
addProp(attrSet,aname,values[aname]);
}
});
rowSet = _.reduce(fieldIds,function(memo,id){
var r={};
addId(r,id);
_.each(fieldNames,function(name){
var fn = 'repeating_'+sectionName+'_'+id+'_'+name;
addProp(r,name,values[fn],fn);
});
memo[id]=r;
return memo;
},{});
_.each(operations,function(op){
var res;
switch(op.type){
case 'tap':
_.bind(op.final,op.context,rowSet,attrSet)();
break;
case 'each':
_.each(rowSet,function(r){
_.bind(op.func,op.context,r,attrSet,r.id,rowSet)();
});
_.bind(op.final,op.context,rowSet,attrSet)();
break;
case 'map':
res = _.map(rowSet,function(r){
return _.bind(op.func,op.context,r,attrSet,r.id,rowSet)();
});
_.bind(op.final,op.context,res,rowSet,attrSet)();
break;
case 'reduce':
res = op.memo;
_.each(rowSet,function(r){
res = _.bind(op.func,op.context,res,r,attrSet,r.id,rowSet)();
});
_.bind(op.final,op.context,res,rowSet,attrSet)();
break;
}
});
// finalize attrs
applyQueuedUpdates();
});
});
};
return {
attrs: repAttrs,
attr: repAttrs,
column: repFields,
columns: repFields,
field: repFields,
fields: repFields,
reduce: repReduce,
inject: repReduce,
foldl: repReduce,
map: repMap,
collect: repMap,
each: repEach,
forEach: repEach,
tap: repTap,
'do': repTap,
execute: repExecute,
go: repExecute,
run: repExecute
};
}(section));
},
repeatingSimpleSum = function(section, field, destination){
repeating(section)
.attr(destination)
.field(field)
.reduce(function(m,r){
return m + (r.F[field]);
},0,function(t,r,a){
a[destination]=t;
})
.execute();
};
console.log('-=> TheAaronSheet v'+version+' <=- ['+(new Date(lastUpdate*1000))+']');
return {
repeatingSimpleSum: repeatingSimpleSum,
repeating: repeating
};
}());
/* ---- END: TheAaronSheet.js ---- */
/* ---- FIN PMT ---------------------- */
// Création perso -- Ouverture de la fiche
on('sheet:opened',function(){
// **** Gestion 1ière ouverture après création du personnage
getAttrs(["todoequip"], function(values) {
//Equipements
if(values.todoequip.length > 0){
var newrowid ="";
var newrowattrs = {};
var TabEq = [];
var arrayLength = 0;
var obj = {};
TabEq = values.todoequip.split("|");
arrayLength = TabEq.length;
if (arrayLength > 0){
setAttrs({todoequip: ""});
for (var i = 0; i < arrayLength; i++) {
obj=JSON.parse(TabEq[i]);
newrowid = generateRowID();
newrowattrs = {};
newrowattrs["repeating_equipement_" + newrowid + "_equipnom"] = obj.nom;
newrowattrs["repeating_equipement_" + newrowid + "_equipqte"] = obj.qte;
newrowattrs["repeating_equipement_" + newrowid + "_equippoids"] = obj.poids;
newrowattrs["repeating_equipement_" + newrowid + "_equippoidstot"] = ((parseFloat(obj.qte) || 1.0) * (parseFloat(obj.poids) || 0.0)).toFixed(1);
setAttrs(newrowattrs);
}
setAttrs({encombrement: totpoids});
TAS.repeating('equipement') /* declares the repeating set to work on */
.fields('equippoidstot') /* declares the fields to be available for each row */
.attrs('encombrement') /* declares the attributes to be available */
.reduce(function(m,r){ /* the reduce function executed for each row, */
/* m is the memo returned from each iteration */
m.total += r.F.equippoidstot; /* adds the floating point value to the memo's total */
return m; /* returns the memo for the next iteration */
},
{ total: 0}, /* the initial value of the memo, an object with total= 0 */
function(m,r,a){ /* the final function called with the results of the reduce */
a.D[2].encombrement = m.total; /* set the total encombrement with 2 decimal places */
PMT.majEncombrement(); /* call the other function */
})
.execute(); /* causes all of the above to be run. */
}
}
});
getAttrs(["todosort"], function(values) {
//Sorts
if(values.todosort.length > 0){
var newrowid ="";
var newrowattrs = {};
var TabEq = [];
var arrayLength = 0;
TabEq = values.todosort.split("|");
arrayLength = TabEq.length;
if (arrayLength > 0){
for (var i = 0; i < arrayLength; i++) {
newrowid = generateRowID();
newrowattrs = {};
newrowattrs["repeating_sorts_" + newrowid + "_sort-nom"] = TabEq[i];
newrowattrs["repeating_sorts_" + newrowid + "_sort-niveau"] = "1";
setAttrs(newrowattrs);
}
}
setAttrs({todosort: ""});
}
});
getAttrs(["todoatk"], function(values) {
//Attaques
if(values.todoatk.length > 0){
var newrowid ="";
var newrowattrs = {};
var TabAtk = [];
var arrayLength = 0;
var obj = {};
TabAtk = values.todoatk.split("|");
arrayLength = TabAtk.length;
if (arrayLength > 0){
for (var i = 0; i < arrayLength; i++) {
obj=JSON.parse(TabAtk[i]);
newrowid = generateRowID();
newrowattrs = {};
newrowattrs["repeating_attaques_" + newrowid + "_attaque-nom"] = obj.nom;
newrowattrs["repeating_attaques_" + newrowid + "_attaque-carac"] = obj.mod;
newrowattrs["repeating_attaques_" + newrowid + "_attaque-bonus"] = obj.bonus;
newrowattrs["repeating_attaques_" + newrowid + "_attaque-degats-nbde"] = obj.nbde;
newrowattrs["repeating_attaques_" + newrowid + "_attaque-degats-de"] = obj.de;
newrowattrs["repeating_attaques_" + newrowid + "_attaque-degats-carac"] = obj.moddgt;
newrowattrs["repeating_attaques_" + newrowid + "_attaque-degats-bonus"] = obj.bonusdgt;
setAttrs(newrowattrs);
}
}
setAttrs({todoatk: ""});
}
});
});
// Inventaire / équipement
on("change:repeating_equipement:equipqte change:repeating_equipement:equippoids", function() {
getAttrs(["repeating_equipement_equipqte", "repeating_equipement_equippoids"], function(values) {
setAttrs({
repeating_equipement_equippoidstot: ((parseFloat(values.repeating_equipement_equipqte) || 1.0)*(parseFloat(values.repeating_equipement_equippoids) || 0.0)).toFixed(1)
});
TAS.repeatingSimpleSum('equipement','equippoidstot','encombrement');
});
});
on("change:encombrement", function() {
PMT.majEncombrement();
});
on('remove:repeating_equipement',function(){
TAS.repeatingSimpleSum('equipement','equippoidstot','encombrement');
});
// Gestion CA
on("change:sheetoptionca", function() {
getAttrs(["sheetoptionca","atk_bonus"], function(values) {
var bonusatk=parseInt(values.atk_bonus);
var option = parseInt(values.sheetoptionca);
if ( (option > 0) && (bonusatk < 1) ) {
setAttrs({
ATK_Bonus:1
});
} else if ( (option == 0) && (bonusatk > 0) ) {
setAttrs({
ATK_Bonus:0
});
}
});
});
// Caractéristiques et modificateurs
on("change:force", function() {
getAttrs(["force"], function(values) {
var Force=parseInt(values.force);
var MOD_FOR=0;
if (Force <4) {MOD_FOR=-3;}
else if (Force>3 && Force<6) {MOD_FOR=-2;}
else if (Force>5 && Force<9) {MOD_FOR=-1;}
else if (Force>12 && Force<16) {MOD_FOR=1;}
else if (Force>15 && Force<18) {MOD_FOR=2;}
else if (Force>17) {MOD_FOR=3;}
else {MOD_FOR=0;}
setAttrs({
MOD_FOR:MOD_FOR
});
});
});
on("change:dexterite", function() {
getAttrs(["dexterite"], function(values) {
var Dexterite=parseInt(values.dexterite);
var MOD_DEX=0;
if (Dexterite <4) {MOD_DEX=-3;}
else if (Dexterite>3 && Dexterite<6) {MOD_DEX=-2;}
else if (Dexterite>5 && Dexterite<9) {MOD_DEX=-1;}
else if (Dexterite>12 && Dexterite<16) {MOD_DEX=1;}
else if (Dexterite>15 && Dexterite<18) {MOD_DEX=2;}
else if (Dexterite>17) {MOD_DEX=3;}
else {MOD_DEX=0;}
setAttrs({
MOD_DEX:MOD_DEX
});
});
});
on("change:constitution", function() {
getAttrs(["constitution"], function(values) {
var Constitution=parseInt(values.constitution);
var MOD_CON=0;
if (Constitution <4) {MOD_CON=-3;}
else if (Constitution>3 && Constitution<6) {MOD_CON=-2;}
else if (Constitution>5 && Constitution<9) {MOD_CON=-1;}
else if (Constitution>12 && Constitution<16) {MOD_CON=1;}
else if (Constitution>15 && Constitution<18) {MOD_CON=2;}
else if (Constitution>17) {MOD_CON=3;}
else {MOD_CON=0;}
setAttrs({
MOD_CON:MOD_CON
});
});
});
on("change:intelligence", function() {
getAttrs(["intelligence"], function(values) {
var Intelligence=parseInt(values.intelligence);
var MOD_INT=0;
if (Intelligence>12 && Intelligence<16) {MOD_INT=1;}
else if (Intelligence>15 && Intelligence<18) {MOD_INT=2;}
else if (Intelligence>17) {MOD_INT=3;}
else {MOD_INT=0;}
setAttrs({
MOD_INT:MOD_INT
});
});
});
on("change:sagesse", function() {
getAttrs(["sagesse"], function(values) {
var Sagesse=parseInt(values.sagesse);
var MOD_SAG=0;
if (Sagesse <4) {MOD_SAG=-3;}
else if (Sagesse>3 && Sagesse<6) {MOD_SAG=-2;}
else if (Sagesse>5 && Sagesse<9) {MOD_SAG=-1;}
else if (Sagesse>12 && Sagesse<16) {MOD_SAG=1;}
else if (Sagesse>15 && Sagesse<18) {MOD_SAG=2;}
else if (Sagesse>17) {MOD_SAG=3;}
else {MOD_SAG=0;}
setAttrs({
MOD_SAG:MOD_SAG
});
});
});
on("change:charisme", function() {
getAttrs(["charisme"], function(values) {
var Charisme=parseInt(values.charisme);
var MOD_CHA=0;
var MaxCompagnons=0;
var MoralCompagnons=0;
if (Charisme <4) {
MOD_CHA=2;
MaxCompagnons=1;
MoralCompagnons=4;
}
else if (Charisme>3 && Charisme<6) {
MOD_CHA=1;
MaxCompagnons=2;
MoralCompagnons=5;
}
else if (Charisme>5 && Charisme<9) {
MOD_CHA=1;
MaxCompagnons=3;
MoralCompagnons=6;
}
else if (Charisme>12 && Charisme<16) {
MOD_CHA=-1;
MaxCompagnons=5;
MoralCompagnons=8;
}
else if (Charisme>15 && Charisme<18) {
MOD_CHA=-1;
MaxCompagnons=6;
MoralCompagnons=9;
}
else if (Charisme>17) {
MOD_CHA=-2;
MaxCompagnons=7;
MoralCompagnons=10;
}
else {
MOD_CHA=0;
MaxCompagnons=4;
MoralCompagnons=7;
}
setAttrs({
MOD_CHA:MOD_CHA,
MaxCompagnons:MaxCompagnons,
MoralCompagnons:MoralCompagnons
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment