Created
September 22, 2019 11:49
-
-
Save benjaminbhollon/03334c2d63e2e7f11e8ba437d2c097f7 to your computer and use it in GitHub Desktop.
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Challenge Creation Tool</title> | |
<style> | |
body { | |
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif; | |
font-size: 1.1em; | |
text-align: center | |
} | |
.warning { | |
color: red; | |
} | |
#challengeData { | |
text-align: center; | |
width: 100%; | |
max-width: 500px; | |
height: 300px; | |
} | |
</style> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js" defer async></script> | |
</head> | |
<body> | |
<h1>Challenge Creation Tool</h1> | |
<form> | |
<h3>Please enter your user information:</h3> | |
<label>User ID: <input id="uid" type="text" placeholder="User ID here..."></label><br> | |
<label>API Token: <input id="apitoken"type="password" placeholder="API Token here..."><br><span class="warning">Warning! Your API Token is a password! See Habitica's warnings about sharing this token with anyone.</span></label><br> | |
<h3>Please enter the compressed challenge data:</h3> | |
<textarea id="challengeData" placeholder="Challenge Name | |
Short Name | |
Summary | |
Challenge Description | |
Add To | |
Categories | |
Prize | |
Tasks | |
Task Type;Task Name;Notes;Difficulty;Due Date/StartDate/Reset Streak;Repeats;Repeat Every;Repeat On | |
End Tasks | |
Dates are in format DD/MM/YYYY | |
~ indicates a new line | |
"></textarea> | |
</form> | |
<button onclick="submit()">Submit</button> | |
<a id="finalResult" style="display:none">View Created Challenge</a> | |
<h2 class="warning" id="err"></h2> | |
<script> | |
var apiHeaders = {}; | |
var categories = [ | |
"Academics", | |
"Advocacy + Causes", | |
"Creativity", | |
"Entertainment", | |
"Finance", | |
"Health + Fitness", | |
"Hobbies + Occupations", | |
"Location-based", | |
"Mental Health + Self-Care", | |
"Getting Organized", | |
"Self-Improvement", | |
"Spirituality", | |
"Time-Management + Accountability" | |
]; | |
var guilds = []; | |
function E(id) { | |
return document.getElementById(id); | |
} | |
function decompressInput(input) { | |
for (i=0;i<categories.length;i++) { | |
input = input.replace(categories[i] + "\n", categories[i] + ";"); | |
} | |
input = input.split("\n"); | |
var output = { | |
"title":"", | |
"shortName": "", | |
"summary": "", | |
"description": "", | |
"addTo": "", | |
"categories": "", | |
"prize": "", | |
"tasks": [] | |
}; | |
var fields; | |
for (i=0;i<input.length;i++) { | |
input[i] = input[i].replace(/~/g, "\n"); | |
switch(i) { | |
case 0: | |
output.name = input[i]; | |
break; | |
case 1: | |
output.shortName = input[i]; | |
break; | |
case 2: | |
output.summary = input[i]; | |
break; | |
case 3: | |
output.description = input[i]; | |
break; | |
case 4: | |
output.addTo = input[i]; | |
break; | |
case 5: | |
var challengeCategories = input[i].split(";"); | |
output.prize = parseInt(challengeCategories.pop()); | |
output.categories = challengeCategories; | |
break; | |
default: | |
if (input[i] == "Tasks" || input[i] == "End Tasks") break; | |
fields = input[i].split(";"); | |
output.tasks.push({ | |
"type": fields[0], | |
"name": fields[1], | |
"notes": fields[2], | |
"difficulty": fields[3], | |
"date": fields[4], | |
"repeats": fields[5], | |
"repeatEvery": fields[6], | |
"repeatOn": fields[7] | |
}); | |
break; | |
} | |
} | |
return output; | |
} | |
function submit() { | |
E("err").innerText = "Working..."; | |
var apiHeaders = { | |
"x-client": "fc6bd8d5-94a2-462b-ac7f-9b03bc446d45-Challenge Creation Tool", | |
"x-api-user": E("uid").value, | |
"x-api-key": E("apitoken").value | |
} | |
var challengeData = decompressInput(E("challengeData").value); | |
function addGuild(id, name, noguilds) { | |
guilds.push({"id":id,"name":name}); | |
if (noguilds == guilds.length) { | |
for (i=0;i<guilds.length;i++) { | |
if (guilds[i]["name"] == challengeData.addTo) { | |
challengeData.addTo = guilds[i]["id"]; | |
break; | |
} | |
} | |
$.ajax({ | |
dataType: "json", | |
url: "https://habitica.com/api/v3/challenges", | |
type: "POST", | |
headers: apiHeaders, | |
data: { | |
"group": challengeData.addTo, | |
"name": challengeData.name, | |
"shortName": challengeData.shortName, | |
"summary": challengeData.summary, | |
"description": challengeData.description, | |
"prize": challengeData.prize | |
}, | |
success: function(response) { | |
var data = response.data; | |
var challengeId = data.id; | |
E("finalResult").setAttribute("href", "https://habitica.com/challenges/" + challengeId); | |
E("finalResult").style.display = "block"; | |
E("err").innerText = ""; | |
var taskDifficulties = { | |
"Trivial": 0.1, | |
"Easy": 1, | |
"Medium": 1.5, | |
"Hard": 2 | |
}; | |
var taskData = {}; | |
var date, datearray, repeatsOn, habiticandays, thesedays; | |
for (i=0;i<challengeData.tasks.length;i++) { | |
date = datearray = repeatsOn = habiticandays = thesedays = ""; | |
switch (challengeData.tasks[i].type) { | |
case "Habit": | |
taskData = { | |
"text": challengeData.tasks[i].name, | |
"type": "habit", | |
"notes": challengeData.tasks[i].notes, | |
"priority": taskDifficulties[challengeData.tasks[i].difficulty] | |
}; | |
break; | |
case "Daily": | |
date = challengeData.tasks[i].date; | |
datearray = date.split("/"); | |
date = datearray[1] + '/' + datearray[0] + '/' + datearray[2]; | |
repeatsOn = {"su": false, "m": false, "t": false, "w": false, "th": false, "f": false, "s": false}; | |
days = ["S", "M", "T", "W", "T", "F", "S"]; | |
habiticandays = ["su", "m", "t", "w", "th", "f", "s"]; | |
thesedays = challengeData.tasks[i].repeatOn; | |
if (thesedays) { | |
for (x=0;x<days.length;x++) { | |
if (thesedays[0] == days[i]) { | |
thesedays[0] = ""; | |
repeatsOn[habiticandays[i]] = true; | |
} | |
} | |
} | |
taskData = { | |
"text": challengeData.tasks[i].name, | |
"type": "daily", | |
"notes": challengeData.tasks[i].notes, | |
"priority": taskDifficulties[challengeData.tasks[i].difficulty], | |
"startDate": date, | |
"frequency": challengeData.tasks[i].repeats.toLowerCase(), | |
"everyX": challengeData.tasks[i].repeatEvery, | |
"repeat": repeatsOn | |
}; | |
break; | |
case "To-Do": | |
date = challengeData.tasks[i].date; | |
var datearray = date.split("/"); | |
date = datearray[1] + '/' + datearray[0] + '/' + datearray[2]; | |
taskData = { | |
"text": challengeData.tasks[i].name, | |
"type": "todo", | |
"notes": challengeData.tasks[i].notes, | |
"priority": taskDifficulties[challengeData.tasks[i].difficulty], | |
"date": date | |
}; | |
break; | |
case "Reward": | |
taskData = { | |
"text": challengeData.tasks[i].name, | |
"type": "reward", | |
"notes": challengeData.tasks[i].notes, | |
"value": challengeData.tasks[i].difficulty | |
}; | |
break; | |
} | |
console.log(taskData); | |
$.ajax({ | |
dataType: "json", | |
url: "https://habitica.com/api/v3/tasks/challenge/" + challengeId, | |
type: "POST", | |
headers: apiHeaders, | |
data: taskData, | |
success: function(response) { | |
console.log(response.data); | |
console.log("Successfully added task."); | |
}, | |
error: function (response) { | |
E("err").innerText = "Failed adding task: " + response.responseJSON.message; | |
} | |
}); | |
} | |
}, | |
error: function (response) { | |
E("err").innerText = response.responseJSON.message; | |
} | |
}); | |
} | |
} | |
$.ajax({ | |
dataType: "json", | |
url: "https://habitica.com/api/v3/user/", | |
headers: apiHeaders, | |
success: function(response) { | |
var data = response.data; | |
var noguilds = data.guilds.length; | |
for (i=0;i<data.guilds.length;i++) { | |
$.ajax({ | |
dataType: "json", | |
url: "https://habitica.com/api/v3/groups/" + data.guilds[i], | |
headers: apiHeaders, | |
success: function(response) { | |
var data = response.data; | |
addGuild(data.id, data.name, noguilds); | |
}, | |
error: function (response) { | |
E("err").innerText = response.responseJSON.message; | |
} | |
}); | |
} | |
}, | |
error: function (response) { | |
E("err").innerText = response.responseJSON.message; | |
} | |
}); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment