"use strict";
/* Helper functions */
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
/* Persistent data store */
var dataStore={
groupAName: "Group A",
groupBName: "Group B",
entities: {},
groupA: [],
groupB: [],
assignments: [],
options: {
remFromB: false
}
}
function loadDataStore(){
try{
var ser=localStorage.getItem("dataStore");
if(ser){
dataStore=JSON.parse(ser);
}
//loading to UI
//load group names
$(".groupAName").text(dataStore.groupAName);
$(".groupBName").text(dataStore.groupBName);
//load group entities
$.each(dataStore.groupA, (i, val) => {
var el=dataStore.entities[val];
$("
"+el.val+"
").hide().appendTo("#groupAList").slideDown();
})
$.each(dataStore.groupB, (i, val) => {
var el=dataStore.entities[val];
$("
"+el.val+"
").hide().appendTo("#groupBList").slideDown();
})
//load options
$("#remFromBOnMatch").prop("checked", dataStore.options.remFromB);
}
catch(e){
console.log(e);
}
}
function saveDataStore(){
try{
var ser=JSON.stringify(dataStore);
localStorage.setItem("dataStore", ser);
}
catch(e){
console.log(e);
}
}
/* Edit group name */
function renameGroupA(){
$("#groupANameDisplay").hide();
$("#groupANameEdit").show();
$("#groupANameField").val(dataStore.groupAName);
}
function renameGroupB(){
$("#groupBNameDisplay").hide();
$("#groupBNameEdit").show();
$("#groupBNameField").val(dataStore.groupBName);
}
function saveGroupA(){
dataStore.groupAName=$("#groupANameField").val();
$(".groupAName").text(dataStore.groupAName);
$("#groupANameEdit").hide();
$("#groupANameDisplay").show();
saveDataStore();
}
function saveGroupB(){
dataStore.groupBName=$("#groupBNameField").val();
$(".groupBName").text(dataStore.groupBName);
$("#groupBNameEdit").hide();
$("#groupBNameDisplay").show();
saveDataStore();
}
/* Element manipulation */
function addGroupA(fromfile){
var inp=fromfile?fromfile:$("#groupAInput").val();
var id=guid();
var el={id: id, val: inp};
Object.assign(dataStore.entities, {[id]: el});
dataStore.groupA.push(id);
saveDataStore();
$("#groupAInput").val("");
//add to ui
$("
"+inp+"
").hide().appendTo("#groupAList").slideDown();
}
function addGroupB(fromfile){
var inp=fromfile?fromfile:$("#groupBInput").val();
var id=guid();
var el={id: id, val: inp};
Object.assign(dataStore.entities, {[id]: el});
dataStore.groupB.push(id);
saveDataStore();
$("#groupBInput").val("");
//add to ui
$("
"+inp+"
").hide().appendTo("#groupBList").slideDown();
}
function remGroupA(el, id){
dataStore.groupA=dataStore.groupA.filter((el) => el!==id);
saveDataStore();
$(el).parent().slideUp(() => $(el).parent().remove());
}
function remGroupB(el, id){
console.log(id);
dataStore.groupB=dataStore.groupB.filter((el) => el!==id);
saveDataStore();
$(el).parent().slideUp(() => $(el).parent().remove());
}
/* Options */
function updateOptions(){
dataStore.options.remFromB=$("#remFromBOnMatch").prop("checked")?true:false;
saveDataStore();
}
/* actual rolling */
var baseRotateA=0;
var baseRotateB=0;
var countA=0;
var countB=0;
var degA=0;
var degB=0;
function setupRoll(){
$("#roller").slideUp(() => {
//set up roller A
$("#rollerA").html("");
$("#rollerA").css("transform", "rotate(0)");
countA=dataStore.groupA.length;
degA=360/countA;
var dataA=dataStore.groupA.map((id) => dataStore.entities[id]);
$.each(dataA, (i, val) => {
var rotSlicer=i*degA;
var rotLabel=((i-1)*degA)+(degA/2);
$("").appendTo("#rollerA");
$("
"+val.val+"
").appendTo("#rollerA");
});
$("#rollerA").css("background", "#674B6F");
//set up roller B
$("#rollerB").html("");
$("#rollerB").css("transform", "rotate(0)");
countB=dataStore.groupB.length;
degB=360/countB;
var dataB=dataStore.groupB.map((id) => dataStore.entities[id]);
$.each(dataB, (i, val) => {
var rotSlicer=i*degB;
var rotLabel=((i-1)*degB)+(degB/2);
$("").appendTo("#rollerB");
$("
"+val.val+"
").appendTo("#rollerB");
});
$("#rollerB").css("background", "#9AA05B");
if(countA%2!==0){
baseRotateA=-degA/2;
}
if(countB%2!==0){
baseRotateB=degB/2;
}
//$("#rollerA").css("transform", "rotate("+baseRotateA.toString()+"deg)");
//$("#rollerB").css("transform", "rotate("+baseRotateB.toString()+"deg)");
$("#editor").slideUp();
$("#roller").slideDown();
$("#roller_button").slideDown();
})
}
var idA="";
var idB="";
function roll(){
$("#roller_button").slideUp();
var randA=Math.floor(Math.random()*countA);
var randB=Math.floor(Math.random()*countB);
idA=dataStore.groupA[randA];
idB=dataStore.groupB[randB];
var rotA=(360*10)-((randA-1)*degA)-(degA/2);
var rotB=(360*10)-((randB-1)*degB)-(degB/2);
dataStore.assignments.push({a: idA, b: idB});
dataStore.groupA=dataStore.groupA.filter((id) => id!=idA);
if(dataStore.options.remFromB){
dataStore.groupB=dataStore.groupB.filter((id) => id!=idB);
}
$("#rollerA").removeClass("roller__rotate");
$("#rollerB").removeClass("roller__rotate");
$("#rollerA").css("transform", "rotate("+rotA.toString()+"deg)");
$("#rollerB").css("transform", "rotate("+rotB.toString()+"deg)");
$("#rollerA").addClass("roller__rotate");
$("#rollerB").addClass("roller__rotate");
setTimeout(finishRoll, 11500);
saveDataStore();
}
function finishRoll(){
$("#resA").text(dataStore.entities[idA].val);
$("#resB").text(dataStore.entities[idB].val);
$("#roll_results").show();
}
function reroll(){
if(dataStore.groupA.length>1){
$("#roll_results").slideUp();
setupRoll();
}
else{
dataStore.assignments.push({"a": dataStore.groupA[0], "b": dataStore.groupB[0]});
dataStore.groupA=[];
dataStore.groupB=[];
saveDataStore();
showResults();
}
}
function showResults(){
$("#editor").slideUp();
$("#roller").slideUp();
$.each(dataStore.assignments, (i, val) => {
var labelA=dataStore.entities[val.a].val;
var labelB=dataStore.entities[val.b].val;
$("
"+labelA+"
"+labelB+"
").appendTo("#resultsTable");
});
$("#results").slideDown();
}
function reset(){
localStorage.removeItem("dataStore");
location.reload();
}
function importFile(){
var file=$("#fileImport")[0].files[0];
var fr=new FileReader();
fr.onload=function(){
//load string
var map=JSON.parse(fr.result);
$.each(map.a, (i, val) => {
addGroupA(val);
});
$.each(map.b, (i, val) => {
addGroupB(val);
});
saveDataStore();
};
fr.readAsText(file, "UTF-8");
}