-- This module implements [[Template:Energy meter]].locallang=mw.language.getContentLanguage()localyesno=require('Module:Yesno')localp={}localfunctionround(n)returnmath.floor(n+0.5)endlocalfunctionsanitizePercentage(n)ifn<0thenn=0elseifn>100thenn=100endreturnnendlocalfunctiongetUnixTime(date)returntonumber(lang:formatDate('U',date))endlocalfunctioncalculateHumanPercentage(energyleft)ifnotenergyleftthenerror("the 'energyleft' parameter must be specified for all non-bot users",3)endenergyleft=tonumber(energyleft)ifnotenergyleftthenerror("the 'energyleft' parameter was not a valid number",3)endreturnsanitizePercentage(energyleft)endlocalfunctioncalculateBotPercentage(expiry)ifnotexpirythenerror("the 'expiry' parameter must be specified for all bot users",3)endlocalnow=getUnixTime()locallifespan=getUnixTime('now + 6 months')-nowlocaltimeLeft=getUnixTime(expiry)-nowlocalpercentage=timeLeft/lifespan*100returnsanitizePercentage(percentage)endlocalfunctioncalculateDaysLeft(expiry)localseconds=getUnixTime(expiry)-getUnixTime()localdays=seconds/60/60/24ifdays<0thendays=0endreturnmath.floor(days)endfunctionp._main(args,frame)frame=frameormw.getCurrentFrame()localisBot=yesno(args.isbot)orfalselocalisHorizontal=yesno(args.ishorizontal)orfalselocalisTopIcon=yesno(args.istopicon)orfalselocalisThumb=notyesno(args.nothumb)-- PercentagelocalpercentageifisBotthenpercentage=calculateBotPercentage(args.expiry)elsepercentage=calculateHumanPercentage(args.energyleft)end-- Power levellocalpowerLevel=math.ceil(percentage/100*6)-- Image namelocalimagedolocalimages={'Empty','Almost Empty','Partially Empty','Half','Partially Full','Almost Full','Full'}image=images[powerLevel+1]image='Battery '..imageifisHorizontalthenimage='Horizontal '..imageendimage=image..'.png'end-- CaptionlocalcaptionifisBotthencaption='This bot has '..round(percentage)..'% power left.'ifpowerLevel==2thencaption=caption..'<br>This bot is running low on energy.'elseifpowerLevel==1thencaption=caption..'<br>This bot has almost no energy left.'..'<br>It will die in '..calculateDaysLeft(args.expiry)..' day(s).'..'<br>Contact operator.'elseifpowerLevel==0thencaption=caption..'<br>This bot has died. Contact the operator.'endelse-- Is a humancaption='This user has '..round(percentage)..'% energy left.'ifpowerLevel==2thencaption=caption..'<br>This user is running low on energy.'..'<br>They may not be very active on Wikipedia.'elseifpowerLevel==1thencaption=caption..'<br>This user has almost no energy left.'..'<br>They may retire soon.'elseifpowerLevel==0thencaption=caption..'<br>This user retired.'endend-- WidthlocalwidthifisTopIconthenwidth='25'elsewidth='200'endwidth=width..'px'-- PositionlocalpositionifnotisTopIconthenposition=args.positionor'right'end-- File linklocalfileLink=string.format('[[File:%s|%s|%s%s%s]]',image,caption,width,positionand'|'..positionor'',notisTopIconandisThumband'|thumb'or'')-- OutputifisTopIconthenlocalname='energy-meter'ifargs.sortkeythenname=args.sortkey..'-'..nameendreturnframe:extensionTag{name='indicator',content=fileLink,args={name=name}}elsereturnfileLinkendendfunctionp.main(frame)localargs=require('Module:Arguments').getArgs(frame,{wrappers='Template:Energy meter'})returnp._main(args,frame)endreturnp