-
Notifications
You must be signed in to change notification settings - Fork 3
/
HabiticaMagic-v2.1.0.min.js
31 lines (27 loc) · 6.95 KB
/
HabiticaMagic-v2.1.0.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
* HabiticaMagic.js
* https://github.com/delightedCrow/HabiticaMagic
*
* A convenient way to interact with the Habitica API
* (https://habitica.com/apidoc/).
*
* Copyright © 2019 JSC (@delightedCrow) & PJM (@ArrayOfFrost)
*
* MIT Licensed
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.
*/
class HabiticaUserTasksManager{constructor(t){this.apiData=t}get taskList(){return this.apiData}get todosDueToday(){return this.todosDueOnDate(moment().endOf("day"))}todosDueOnDate(t){for(var e=[],a=0;a<this.taskList.length;a++){let s=this.taskList[a];if("todo"==s.type)if(s.date)moment(s.date).isBefore(t)&&e.push(s)}return e}calculateDailyStatsFor(t){var e={dueCount:0,totalDamageToSelf:0,dailyDamageToSelf:0,bossDamage:0,dailiesEvaded:0};var a=t.stealth;let s=t.constitutionBonus;for(var i=0;i<this.taskList.length;i++){let l=this.taskList[i];if("daily"==l.type&&(l.isDue&&!l.completed))if(a>0)a--,e.dailiesEvaded++;else{e.dueCount++;var r=l.value<-47.27?-47.27:l.value;if(r=r>21.27?21.27:r,r=Math.abs(Math.pow(.9747,r)),l.checklist.length>0)for(var n=r/l.checklist.length,o=0;o<l.checklist.length;o++)l.checklist[o].completed&&(r-=n);var h=r*s*l.priority*2;if(e.dailyDamageToSelf+=Math.round(10*h)/10,t.isOnBossQuest){var u=l.priority<1?r*l.priority:r;u*=t.quest.data.boss.str,e.bossDamage+=u}}}e.totalDamageToSelf=e.dailyDamageToSelf+e.bossDamage,e.totalDamageToSelf=Math.ceil(10*e.totalDamageToSelf)/10,e.bossDamage=Math.ceil(10*e.bossDamage)/10,this.dailyStats=e}}class HabiticaUser{constructor(t){this.apiData=t,this.stats=this._calculateStats()}get gems(){return 4*this.apiData.balance}get hourglasses(){return this.apiData.purchased.plan.consecutive.trinkets}get gold(){return Math.round(this.apiData.stats.gp)}get goldCompact(){return new Intl.NumberFormat("lookup",{notation:"compact",compactDisplay:"short"}).format(this.gold)}get level(){return this.apiData.stats.lvl}get displayName(){return this.apiData.profile.name}get className(){return"wizard"==this.apiData.stats.class?"mage":this.apiData.stats.class}get bio(){return this.apiData.profile.blurb}get experience(){return Math.floor(this.apiData.stats.exp)}get experienceToLevel(){return Math.round(this.apiData.stats.toNextLevel)}get mana(){return Math.floor(this.apiData.stats.mp)}get manaMax(){return Math.round(this.apiData.stats.maxMP)}get health(){return Math.floor(this.apiData.stats.hp)}get healthMax(){return Math.round(this.apiData.stats.maxHealth)}get stealth(){return this.apiData.stats.buffs.stealth}get armor(){return this.apiData.items.gear.equipped}get costume(){return this.apiData.items.gear.costume}get outfit(){return 1==this.apiData.preferences.costume?this.costume:this.armor}get isSleeping(){return this.apiData.preferences.sleep}_calculateStats(){var t={totals:{str:0,con:0,int:0,per:0},armor:{str:0,con:0,int:0,per:0},buffs:{str:this.apiData.stats.buffs.str,con:this.apiData.stats.buffs.con,int:this.apiData.stats.buffs.int,per:this.apiData.stats.buffs.per},points:{str:this.apiData.stats.str,con:this.apiData.stats.con,int:this.apiData.stats.int,per:this.apiData.stats.per}};for(var e in this.armor){let s=this.armor[e];for(var a in t.armor)t.armor[a]+=s[a],this.className!==s.klass&&this.className!==s.specialClass||(t.armor[a]+=.5*s[a])}let s=Math.floor(this.level/2);for(var a in t.totals)t.totals[a]=t.armor[a]+t.buffs[a]+t.points[a]+s;return t}get constitutionBonus(){let t=1-this.stats.totals.con/250;return t<.1?.1:t}get quest(){return this.apiData.party.quest}get isOnQuest(){return null!=this.quest.data}get isOnBossQuest(){return!(!this.isOnQuest||null==this.quest.data.boss)}set tasks(t){this._taskManager=t,this.tasks.calculateDailyStatsFor(this)}get tasks(){return this._taskManager}}class HabiticaAPIManager{constructor(t,e="en"){this.language=e,this.xclient=t,this.content={}}fetchContentData(){return this.getRequest("https://habitica.com/api/v3/content",{language:this.language}).then(t=>{var e=JSON.parse(t).data;this.content=e})}fetchUser(t){const e="https://habitica.com/api/v3/members/"+t;return this.getRequest(e).then(t=>{var e=JSON.parse(t).data;return new HabiticaUser(this.replaceKeysWithContent(e))})}fetchAuthenticatedUser(t,e){return this.authGetRequest("https://habitica.com/api/v3/user",t,e).then(t=>{var e=JSON.parse(t).data;return new HabiticaUser(this.replaceKeysWithContent(e))})}fetchUserTasks(t,e){return this.authGetRequest("https://habitica.com/api/v3/tasks/user",t,e).then(t=>{var e=JSON.parse(t).data;return new HabiticaUserTasksManager(e)})}fetchUserWithTasks(t,e){var a;return this.fetchAuthenticatedUser(t,e).then(s=>(a=s,this.fetchUserTasks(t,e))).then(t=>(a.tasks=t,a))}authGetRequest(t,e,a,s={}){let i=this.getQueryStringURL(t,s);return new Promise((t,s)=>{let r=new XMLHttpRequest;r.open("GET",i),r.onerror=function(){s(this.statusText)},r.onload=function(){200==this.status?t(this.responseText):s(this.responseText)},r.setRequestHeader("x-api-user",e),r.setRequestHeader("x-api-key",a),r.setRequestHeader("x-client",this.xclient),r.send()})}getRequest(t,e={}){let a=this.getQueryStringURL(t,e);return new Promise((t,e)=>{let s=new XMLHttpRequest;s.open("GET",a),s.onerror=function(){e(this.statusText)},s.onload=function(){200==this.status?t(this.responseText):e(this.responseText)},s.setRequestHeader("x-client",this.xclient),s.send()})}postRequest(t,e,a,s={}){return new Promise((i,r)=>{let n=new XMLHttpRequest;n.open("POST",t),n.onerror=function(){r(this.responseText)},n.onload=function(){201===n.status||200===n.status?i(this.responseText):r(this.responseText)},n.setRequestHeader("Content-Type","application/json;charset=UTF-8"),n.setRequestHeader("x-client",this.xclient),n.setRequestHeader("x-api-user",e),n.setRequestHeader("x-api-key",a),n.send(JSON.stringify(s))})}replaceKeysWithContent(t){if(0==Object.entries(this.content).length)return t;for(var e of[t.items.gear.equipped,t.items.gear.costume])for(var a in e){let t=e[a],s=this.content.gear.flat[t];e[a]=s}return t.party.quest.key&&(t.party.quest.data=this.content.quests[t.party.quest.key]),t}getQueryStringURL(t,e){let a=Object.entries(e);return a.length<1?t:t+"?"+a.map(t=>t.map(encodeURIComponent).join("=")).join("&")}}