Advertisement
csmit195

Templating KillSay

Dec 5th, 2019
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /*
  3.                            _   _     __    ___    _____                             __  
  4.                           (_) | |   /_ |  / _ \  | ____|                           / /
  5.    ___   ___   _ __ ___    _  | |_   | | | (_) | | |__         _ __ ___   ___     / /
  6.   / __| / __| | '_ ` _ \  | | | __|  | |  \__, | |___ \       | '_ ` _ \ / _ \   / /  
  7.  | (__  \__ \ | | | | | | | | | |_   | |    / /   ___) |  _   | | | | | |  __/  / /  
  8.   \___| |___/ |_| |_| |_| |_|  \__|  |_|   /_/   |____/  (_)  |_| |_| |_|\___| /_/    
  9.    
  10.    
  11.     Script Name: Super Customizable Killsay
  12.     Script Author: csmit195
  13.     Script Version: 1.0
  14.     Script Description: Literally gives you the ability to create basically any killsay you want. Suggest stuff to me, I got you.
  15. */
  16.  
  17. // This is Mustache.js, a great minimalistic templating engine, if the admins or mods feel odd about it, feel free to head to the github https://github.com/janl/mustache.js, and verify that the below mustache.min.js snippet is the source.
  18. (function defineMustache(global,factory){if(typeof exports==="object"&&exports&&typeof exports.nodeName!=="string"){factory(exports)}else if(typeof define==="function"&&define.amd){define(["exports"],factory)}else{global.Mustache={};factory(global.Mustache)}})(this,function mustacheFactory(mustache){var objectToString=Object.prototype.toString;var isArray=Array.isArray||function isArrayPolyfill(object){return objectToString.call(object)==="[object Array]"};function isFunction(object){return typeof object==="function"}function typeStr(obj){return isArray(obj)?"array":typeof obj}function escapeRegExp(string){return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g,"\\$&")}function hasProperty(obj,propName){return obj!=null&&typeof obj==="object"&&propName in obj}function primitiveHasOwnProperty(primitive,propName){return primitive!=null&&typeof primitive!=="object"&&primitive.hasOwnProperty&&primitive.hasOwnProperty(propName)}var regExpTest=RegExp.prototype.test;function testRegExp(re,string){return regExpTest.call(re,string)}var nonSpaceRe=/\S/;function isWhitespace(string){return!testRegExp(nonSpaceRe,string)}var entityMap={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;","/":"&#x2F;","`":"&#x60;","=":"&#x3D;"};function escapeHtml(string){return String(string).replace(/[&<>"'`=\/]/g,function fromEntityMap(s){return entityMap[s]})}var whiteRe=/\s*/;var spaceRe=/\s+/;var equalsRe=/\s*=/;var curlyRe=/\s*\}/;var tagRe=/#|\^|\/|>|\{|&|=|!/;function parseTemplate(template,tags){if(!template)return[];var lineHasNonSpace=false;var sections=[];var tokens=[];var spaces=[];var hasTag=false;var nonSpace=false;var indentation="";var tagIndex=0;function stripSpace(){if(hasTag&&!nonSpace){while(spaces.length)delete tokens[spaces.pop()]}else{spaces=[]}hasTag=false;nonSpace=false}var openingTagRe,closingTagRe,closingCurlyRe;function compileTags(tagsToCompile){if(typeof tagsToCompile==="string")tagsToCompile=tagsToCompile.split(spaceRe,2);if(!isArray(tagsToCompile)||tagsToCompile.length!==2)throw new Error("Invalid tags: "+tagsToCompile);openingTagRe=new RegExp(escapeRegExp(tagsToCompile[0])+"\\s*");closingTagRe=new RegExp("\\s*"+escapeRegExp(tagsToCompile[1]));closingCurlyRe=new RegExp("\\s*"+escapeRegExp("}"+tagsToCompile[1]))}compileTags(tags||mustache.tags);var scanner=new Scanner(template);var start,type,value,chr,token,openSection;while(!scanner.eos()){start=scanner.pos;value=scanner.scanUntil(openingTagRe);if(value){for(var i=0,valueLength=value.length;i<valueLength;++i){chr=value.charAt(i);if(isWhitespace(chr)){spaces.push(tokens.length);indentation+=chr}else{nonSpace=true;lineHasNonSpace=true;indentation+=" "}tokens.push(["text",chr,start,start+1]);start+=1;if(chr==="\n"){stripSpace();indentation="";tagIndex=0;lineHasNonSpace=false}}}if(!scanner.scan(openingTagRe))break;hasTag=true;type=scanner.scan(tagRe)||"name";scanner.scan(whiteRe);if(type==="="){value=scanner.scanUntil(equalsRe);scanner.scan(equalsRe);scanner.scanUntil(closingTagRe)}else if(type==="{"){value=scanner.scanUntil(closingCurlyRe);scanner.scan(curlyRe);scanner.scanUntil(closingTagRe);type="&"}else{value=scanner.scanUntil(closingTagRe)}if(!scanner.scan(closingTagRe))throw new Error("Unclosed tag at "+scanner.pos);if(type==">"){token=[type,value,start,scanner.pos,indentation,tagIndex,lineHasNonSpace]}else{token=[type,value,start,scanner.pos]}tagIndex++;tokens.push(token);if(type==="#"||type==="^"){sections.push(token)}else if(type==="/"){openSection=sections.pop();if(!openSection)throw new Error('Unopened section "'+value+'" at '+start);if(openSection[1]!==value)throw new Error('Unclosed section "'+openSection[1]+'" at '+start)}else if(type==="name"||type==="{"||type==="&"){nonSpace=true}else if(type==="="){compileTags(value)}}stripSpace();openSection=sections.pop();if(openSection)throw new Error('Unclosed section "'+openSection[1]+'" at '+scanner.pos);return nestTokens(squashTokens(tokens))}function squashTokens(tokens){var squashedTokens=[];var token,lastToken;for(var i=0,numTokens=tokens.length;i<numTokens;++i){token=tokens[i];if(token){if(token[0]==="text"&&lastToken&&lastToken[0]==="text"){lastToken[1]+=token[1];lastToken[3]=token[3]}else{squashedTokens.push(token);lastToken=token}}}return squashedTokens}function nestTokens(tokens){var nestedTokens=[];var collector=nestedTokens;var sections=[];var token,section;for(var i=0,numTokens=tokens.length;i<numTokens;++i){token=tokens[i];switch(token[0]){case"#":case"^":collector.push(token);sections.push(token);collector=token[4]=[];break;case"/":section=sections.pop();section[5]=token[2];collector=sections.length>0?sections[sections.length-1][4]:nestedTokens;break;default:collector.push(token)}}return nestedTokens}function Scanner(string){this.string=string;this.tail=string;this.pos=0}Scanner.prototype.eos=function eos(){return this.tail===""};Scanner.prototype.scan=function scan(re){var match=this.tail.match(re);if(!match||match.index!==0)return"";var string=match[0];this.tail=this.tail.substring(string.length);this.pos+=string.length;return string};Scanner.prototype.scanUntil=function scanUntil(re){var index=this.tail.search(re),match;switch(index){case-1:match=this.tail;this.tail="";break;case 0:match="";break;default:match=this.tail.substring(0,index);this.tail=this.tail.substring(index)}this.pos+=match.length;return match};function Context(view,parentContext){this.view=view;this.cache={".":this.view};this.parent=parentContext}Context.prototype.push=function push(view){return new Context(view,this)};Context.prototype.lookup=function lookup(name){var cache=this.cache;var value;if(cache.hasOwnProperty(name)){value=cache[name]}else{var context=this,intermediateValue,names,index,lookupHit=false;while(context){if(name.indexOf(".")>0){intermediateValue=context.view;names=name.split(".");index=0;while(intermediateValue!=null&&index<names.length){if(index===names.length-1)lookupHit=hasProperty(intermediateValue,names[index])||primitiveHasOwnProperty(intermediateValue,names[index]);intermediateValue=intermediateValue[names[index++]]}}else{intermediateValue=context.view[name];lookupHit=hasProperty(context.view,name)}if(lookupHit){value=intermediateValue;break}context=context.parent}cache[name]=value}if(isFunction(value))value=value.call(this.view);return value};function Writer(){this.cache={}}Writer.prototype.clearCache=function clearCache(){this.cache={}};Writer.prototype.parse=function parse(template,tags){var cache=this.cache;var cacheKey=template+":"+(tags||mustache.tags).join(":");var tokens=cache[cacheKey];if(tokens==null)tokens=cache[cacheKey]=parseTemplate(template,tags);return tokens};Writer.prototype.render=function render(template,view,partials,tags){var tokens=this.parse(template,tags);var context=view instanceof Context?view:new Context(view);return this.renderTokens(tokens,context,partials,template,tags)};Writer.prototype.renderTokens=function renderTokens(tokens,context,partials,originalTemplate,tags){var buffer="";var token,symbol,value;for(var i=0,numTokens=tokens.length;i<numTokens;++i){value=undefined;token=tokens[i];symbol=token[0];if(symbol==="#")value=this.renderSection(token,context,partials,originalTemplate);else if(symbol==="^")value=this.renderInverted(token,context,partials,originalTemplate);else if(symbol===">")value=this.renderPartial(token,context,partials,tags);else if(symbol==="&")value=this.unescapedValue(token,context);else if(symbol==="name")value=this.escapedValue(token,context);else if(symbol==="text")value=this.rawValue(token);if(value!==undefined)buffer+=value}return buffer};Writer.prototype.renderSection=function renderSection(token,context,partials,originalTemplate){var self=this;var buffer="";var value=context.lookup(token[1]);function subRender(template){return self.render(template,context,partials)}if(!value)return;if(isArray(value)){for(var j=0,valueLength=value.length;j<valueLength;++j){buffer+=this.renderTokens(token[4],context.push(value[j]),partials,originalTemplate)}}else if(typeof value==="object"||typeof value==="string"||typeof value==="number"){buffer+=this.renderTokens(token[4],context.push(value),partials,originalTemplate)}else if(isFunction(value)){if(typeof originalTemplate!=="string")throw new Error("Cannot use higher-order sections without the original template");value=value.call(context.view,originalTemplate.slice(token[3],token[5]),subRender);if(value!=null)buffer+=value}else{buffer+=this.renderTokens(token[4],context,partials,originalTemplate)}return buffer};Writer.prototype.renderInverted=function renderInverted(token,context,partials,originalTemplate){var value=context.lookup(token[1]);if(!value||isArray(value)&&value.length===0)return this.renderTokens(token[4],context,partials,originalTemplate)};Writer.prototype.indentPartial=function indentPartial(partial,indentation,lineHasNonSpace){var filteredIndentation=indentation.replace(/[^ \t]/g,"");var partialByNl=partial.split("\n");for(var i=0;i<partialByNl.length;i++){if(partialByNl[i].length&&(i>0||!lineHasNonSpace)){partialByNl[i]=filteredIndentation+partialByNl[i]}}return partialByNl.join("\n")};Writer.prototype.renderPartial=function renderPartial(token,context,partials,tags){if(!partials)return;var value=isFunction(partials)?partials(token[1]):partials[token[1]];if(value!=null){var lineHasNonSpace=token[6];var tagIndex=token[5];var indentation=token[4];var indentedValue=value;if(tagIndex==0&&indentation){indentedValue=this.indentPartial(value,indentation,lineHasNonSpace)}return this.renderTokens(this.parse(indentedValue,tags),context,partials,indentedValue)}};Writer.prototype.unescapedValue=function unescapedValue(token,context){var value=context.lookup(token[1]);if(value!=null)return value};Writer.prototype.escapedValue=function escapedValue(token,context){var value=context.lookup(token[1]);if(value!=null)return mustache.escape(value)};Writer.prototype.rawValue=function rawValue(token){return token[1]};mustache.name="mustache.js";mustache.version="3.1.0";mustache.tags=["{{","}}"];var defaultWriter=new Writer;mustache.clearCache=function clearCache(){return defaultWriter.clearCache()};mustache.parse=function parse(template,tags){return defaultWriter.parse(template,tags)};mustache.render=function render(template,view,partials,tags){if(typeof template!=="string"){throw new TypeError('Invalid template! Template should be a "string" '+'but "'+typeStr(template)+'" was given as the first '+"argument for mustache#render(template, view, partials)")}return defaultWriter.render(template,view,partials,tags)};mustache.to_html=function to_html(template,view,partials,send){var result=mustache.render(template,view,partials);if(isFunction(send)){send(result)}else{return result}};mustache.escape=escapeHtml;mustache.Scanner=Scanner;mustache.Context=Context;mustache.Writer=Writer;return mustache});
  19.  
  20. // And this is my code.
  21.  
  22. const script = {hide:[]};
  23. const colours = {
  24.     white: '',
  25.     green: '',
  26.     blue: ' ',
  27.     darkblue: ' ',
  28.     darkred: '',
  29.     gold: '',
  30.     grey: '',
  31.     lightgreen: '',
  32.     lightred: '',
  33.     lime: '',
  34.     orchid: '',
  35.     yellow: '   ',
  36.     palered: ''
  37. }
  38.  
  39. script.init = function(){
  40.     UI.AddCheckbox('Kill Message')
  41.     UI.AddTextbox('Template');
  42.     script.hide.push('Template')
  43.     var HelpLabels = [
  44.         'Template Tags:',
  45.         '{n} Your name',
  46.         '{v} Victims Name',
  47.         '{a} Assists Name',
  48.         '{kda.k} Victims Kills',
  49.         '{kda.d} Victims Deaths',
  50.         '{kda.a} Victims Assists',
  51.         '{w} Weapon Name',
  52.         '{s} Suicide (Boolean)',
  53.         '{ivb} Is Victim Bot? (Boolean)',
  54.         '{iab} Is Assist Bot? (Boolean)',
  55.         '{hs} Headshot (Boolean)',
  56.         '{p} Penetrated (Boolean)',
  57.         'COLORS:',
  58.         '{#c.g}text{/c.g} - Green',
  59.         '{#c.b}text{/c.b} - Blue',
  60.         '{#c.db}text{/c.db} - Dark Blue',
  61.         '{#c.dr}text{/c.dr} - Dark Red',
  62.         '{#c.gl}text{/c.gl} - Gold',
  63.         '{#c.gr}text{/c.gr} - Grey',
  64.         '{#c.lg}text{/c.lg} - Light Green',
  65.         '{#c.lr}text{/c.lr} - Light Red',
  66.         '{#c.l}text{/c.l} - Lime',
  67.         '{#c.o}text{/c.o} - Orchid',
  68.         '{#c.y}text{/c.y} - Yellow',
  69.         '{#c.pr}text{/c.pr} - Pale Red'
  70.         //'{d} Dominated Count(broken?)',
  71.         //'{r} Revenge (broken?)',
  72.     ];
  73.     for ( i = 0; i < HelpLabels.length; i++ ) {
  74.         var Label = HelpLabels[i];
  75.         script.hide.push(Label);
  76.         UI.AddLabel(Label);
  77.     }
  78.    
  79.     // Disable Mustache.JS HTML Escape:
  80.     Mustache.escape = function (value){
  81.         return value;
  82.     };
  83.    
  84.     Mustache.tags = ['{', '}'];
  85.  
  86.     // Register Events
  87.     Global.RegisterCallback('player_death', 'script.player_death');
  88.     Global.RegisterCallback('Draw', 'script.updateVisibility');
  89. }
  90.  
  91. script.get = {
  92.     value(v) {
  93.         return UI.GetValue('Misc', 'JAVASCRIPT', 'Script items', v);
  94.     },
  95.     string(s) {
  96.         return UI.GetString('Misc', 'JAVASCRIPT', 'Script items', s);
  97.     }
  98. }
  99.  
  100. script.set = function(s,v){
  101.     UI.SetValue('Misc', 'JAVASCRIPT', 'Script items', s, v);
  102. }
  103.  
  104. script.visible = function(s,v){
  105.     UI.SetEnabled('Misc', 'JAVASCRIPT', 'Script items', s, v);
  106. }
  107.  
  108. script.say = function(msg){
  109.     Global.ExecuteCommand('say ' + msg)
  110. }
  111.  
  112. script.colourFunction = function(color){
  113.     // Nested Functions? Don't ask me, its how Mustache likes it...
  114.     return function(){
  115.         return function(text, render){
  116.             return colours[color] + render(text) + colours.white
  117.         }
  118.     }
  119. }
  120.  
  121. script.player_death = function() {
  122.     if ( !script.get.value('Kill Message') )
  123.         return
  124.    
  125.     var AttackerEntity = Entity.GetEntityFromUserID(Event.GetInt('attacker')),
  126.         VictimEntity = Entity.GetEntityFromUserID(Event.GetInt('userid')),
  127.         AssisterEntity = Entity.GetEntityFromUserID(Event.GetInt('assister'));
  128.    
  129.     if ( !Entity.IsLocalPlayer(AttackerEntity) )
  130.         return
  131.    
  132.     var eData = {
  133.         n: Entity.GetName(Entity.GetLocalPlayer()), //attacker
  134.         v: Entity.GetName(VictimEntity), //victim
  135.         a: Entity.GetName(AssisterEntity), //assister
  136.         kda: {k: Entity.GetProp(VictimEntity, 'CPlayerResource', 'm_iKills'), d: Entity.GetProp(VictimEntity, 'CPlayerResource', 'm_iDeaths') + 1, a: Entity.GetProp(VictimEntity, 'CPlayerResource', 'm_iAssists')},
  137.         w: Event.GetString('weapon'), //weapon
  138.         s: (AttackerEntity == VictimEntity), //suicide
  139.         ivb: Entity.IsBot(VictimEntity), //isVictimBot
  140.         iab: Entity.IsBot(AssisterEntity), //isAssisterBot
  141.         hs: Event.GetInt('headshot'), //headshot
  142.         d: Event.GetInt('dominated'), //dominated
  143.         r: Event.GetInt('revenge'), //revenge
  144.         p: (Event.GetInt('penetrated') > 0), //penetrated
  145.         c: {
  146.             g: script.colourFunction('green'),
  147.             b: script.colourFunction('blue'),
  148.             db: script.colourFunction('darkblue'),
  149.             dr: script.colourFunction('darkred'),
  150.             gl: script.colourFunction('gold'),
  151.             gr: script.colourFunction('grey'),
  152.             lg: script.colourFunction('lightgreen'),
  153.             lr: script.colourFunction('lightred'),
  154.             l: script.colourFunction('lime'),
  155.             o: script.colourFunction('orchid'),
  156.             y: script.colourFunction('yellow'),
  157.             pr: script.colourFunction('palered')
  158.         }
  159.     };
  160.    
  161.     var output = Mustache.render(script.get.string('Template'), eData, {}, ['{', '}']);
  162.     if ( output ) {
  163.         script.say(output);
  164.     } else {
  165.         Global.Print('KILLSAY: Malformed Template, try again\n');
  166.     }
  167. }
  168.  
  169. script.updateVisibility = function(){
  170.     var Toggle = (script.get.value('Kill Message') == 1) && true || false;
  171.     for ( i = 0; i < script.hide.length; i++ ) {
  172.         script.visible(script.hide[i], Toggle);
  173.     }
  174. }
  175.  
  176. script.init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement