Created
May 10, 2015 21:08
-
-
Save BaldarSilveraxe/c9ae82d9592c2330597f 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
var activeMap = activeMap || (function(){ | |
'use strict'; | |
var version = '0.2.0', | |
lastUpdate = 1430550687, | |
schemaVersion = 0.3, | |
objExtractKeys = ['id','pageid','stroke','rotation','layer','width','height','top','left','controlledby', | |
'imgsrc', 'flipv', 'fliph'], | |
clearURL = 'https://s3.amazonaws.com/files.d20.io/images/9171667/y4knGAdmSvLt4bjqQjgtHQ/thumb.png?1430503896', | |
gmPingUrl = 'https://s3.amazonaws.com/files.d20.io/images/9191101/pylh4RYgJPrwEuE0ZI5pVg/thumb.png?1430578296', | |
parityProps = ['left', 'top', 'width', 'height', 'rotation', 'flipv', 'fliph', 'isdrawing', | |
'controlledby', 'represents', 'gmnotes', 'name', 'pageid', 'lastmove', | |
'bar1_link', 'bar2_link', 'bar3_link', 'bar1_value', 'bar1_max', 'bar2_value', 'bar2_max', 'bar3_value', 'bar3_max', | |
'aura1_radius', 'aura1_square', 'aura1_color', 'aura2_radius', 'aura2_square', 'aura2_color', 'tint_color', 'statusmarkers', | |
'light_radius', 'light_dimradius', 'light_otherplayers', 'light_hassight', 'light_angle', 'light_losangle', 'light_multiplier', | |
'showname', 'showplayers_name', 'showplayers_bar1', 'showplayers_bar2', 'showplayers_bar3', 'showplayers_aura1', 'showplayers_aura2', | |
'playersedit_name', 'playersedit_bar1', 'playersedit_bar2', 'playersedit_bar3', 'playersedit_aura1', 'playersedit_aura2', 'lastmove'], | |
getObjValue = function(obj, keys) { | |
return _.reduce( keys || objExtractKeys, function(m,prop){ | |
m[prop] = obj.get(prop); | |
return m; | |
}, {}); | |
}, | |
getMirroredPair = function(id) { | |
var mirrored; | |
_.find(state.ActiveMap.foreground,function(slaveid,masterid){ | |
if(id === masterid || id === slaveid) { | |
mirrored = { | |
master: getObj('graphic',masterid), | |
slave: getObj('graphic',slaveid) | |
}; | |
return true; | |
} | |
return false; | |
}); | |
return mirrored; | |
}, | |
getMirrored = function(id) { | |
var mirrored; | |
_.find(state.ActiveMap.foreground,function(slaveid,masterid){ | |
if(id === masterid){ | |
mirrored = getObj('graphic',slaveid); | |
return true; | |
} | |
if(id === slaveid) { | |
mirrored = getObj('graphic',masterid); | |
return true; | |
} | |
return false; | |
}); | |
return mirrored; | |
}, | |
createClone = function(obj) { | |
var objValues = getObjValue(obj), | |
baseObj = {imgsrc: clearURL, layer: 'objects'}, | |
clone; | |
_.each(parityProps,function(p){ | |
baseObj[p]=obj.get(p); | |
}); | |
clone = createObj('graphic',baseObj); | |
obj.set('layer', 'map'); | |
state.ActiveMap.foreground[clone.id]=objValues.id; | |
}, | |
gmPing = function() { | |
var currentPageGraphics = findObjs({ | |
_pageid: Campaign().get('playerpageid'), | |
_type: 'graphic', | |
layer: 'objects', | |
}); | |
_.each(currentPageGraphics, function(obj) { | |
gmPingeer(obj); | |
}); | |
}, | |
gmPingeer = function(obj) { | |
var objValues = getObjValue(obj), | |
ping = createObj('graphic',{ | |
imgsrc: gmPingUrl, | |
layer: 'gmlayer', | |
pageid: Campaign().get("playerpageid"), | |
top: objValues.top, | |
left: objValues.left, | |
height: 70, | |
width: 70 | |
}); | |
(function(){ | |
var size = 140, | |
delta = -10, | |
threshold = 70, | |
rate = 100, | |
interval = setInterval(function(){ | |
ping.set({ | |
height: size, | |
width: size | |
}); | |
size += delta; | |
if(size <= threshold) { | |
clearInterval(interval); | |
} | |
}, rate); | |
}()); | |
setTimeout(function() {ping.remove(); }, 1000); | |
}, | |
cloneParity = function(clone) { | |
var cloneValues = getObjValue(clone), | |
prime = getObj('graphic', state.ActiveMap.foreground[clone.id]), | |
baseObj = {layer: 'map'}; | |
_.each(parityProps,function(p){ | |
baseObj[p]=clone.get(p); | |
}); | |
prime.set(baseObj) | |
}, | |
refreshData = function(){ | |
}, | |
checkInstall = function() { | |
log('-=> AtiveMap v'+version+' <=- ['+(new Date(lastUpdate*1000))+']'); | |
if( ! _.has(state,'AtiveMap') || state.AtiveMap.version !== schemaVersion) { | |
log(' > Updating Schema to v'+schemaVersion+' <'); | |
switch(state.ActiveMap && state.ActiveMap.version) { | |
case 0: | |
default: | |
state.ActiveMap = { | |
version: schemaVersion, | |
config: { | |
}, | |
foreground: {} | |
}; | |
break; | |
} | |
} | |
}, | |
handleInput = function(msg) { | |
if (msg.type !== "api" ) { | |
return; | |
} | |
gmPing(); | |
}, | |
handleGraphicChange = function(obj) { | |
var objValues = getObjValue(obj), | |
pageName = getObj('page', objValues.pageid).get('name'), | |
playerBookMark = Campaign().get('playerpageid'); | |
if( (objValues.pageid !== playerBookMark) || ('objects' !== objValues.layer) || ('ActiveMapKeep' !== pageName) ) { | |
return; | |
} | |
if( (undefined === state.ActiveMap.foreground[objValues.id]) && (clearURL !== objValues.imgsrc) ) { | |
state.ActiveMap.foreground[objValues.id]=objValues.id; | |
setTimeout(function() {createClone(obj); }, 100); | |
return; | |
} | |
if( (undefined !== state.ActiveMap.foreground[objValues.id]) && (clearURL === objValues.imgsrc) ) { | |
setTimeout(function() {cloneParity(obj); }, 100); | |
return; | |
} | |
}, | |
handleGraphicDestroy = function(obj) { | |
var objValues = getObjValue(obj), | |
pair=getMirroredPair(objValues.id); | |
if(pair) { | |
if(objValues.id === pair.master.id ) { | |
pair.slave.remove(); | |
} | |
delete state.ActiveMap.foreground[pair.master.id]; | |
} | |
}, | |
handlePathAdd = function(obj) { | |
}, | |
handleGraphicAdd = function(obj) { | |
handleGraphicChange(obj); | |
}, | |
handlePageChange = function(obj) { | |
}, | |
registerEventHandlers = function() { | |
on('change:campaign:playerpageid', handlePageChange); | |
on('chat:message', handleInput); | |
on('change:graphic', handleGraphicChange); | |
on('add:graphic', handleGraphicAdd); | |
on('add:path', handlePathAdd); | |
on('destroy:graphic', handleGraphicDestroy); | |
checkInstall(); | |
}; | |
return { | |
CheckInstall: checkInstall, | |
RegisterEventHandlers: registerEventHandlers | |
}; | |
}()); | |
on('ready',function(){ | |
'use strict'; | |
activeMap.RegisterEventHandlers(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment