Skip to content

Instantly share code, notes, and snippets.

@jpegzilla
Last active May 2, 2020 12:59
Show Gist options
  • Save jpegzilla/ec5a8fd93435d5f064d2b306b4033f04 to your computer and use it in GitHub Desktop.
Save jpegzilla/ec5a8fd93435d5f064d2b306b4033f04 to your computer and use it in GitHub Desktop.
const parseDayZProfile = async e => {
// asynchronous event listener for input change.
// get the uploaded file and...
const f = e.target.files[0];
//...parse it to some text
let t = await f.text();
// set divider and make sure reserved words are used correctly
const divider = "Survivor";
const reserved = [divider];
// body parts that the gear is equipped on.
// I don't play this game so I don't know if
// these are all actual body parts in the game. lol
// you can add some more or take some away if you have to
const parts = [
"Body",
"Legs",
"Feet",
"Arms",
"Hands",
"Head",
"Chest",
"Back"
];
// regular expression that matches character names in data
const nameRegex = /Survivor[M | F]_[A-Za-z]+/gim;
// structure to hold character equip data
const structure = {};
// remove control characters, excess spaces, items that are not valid words,
// items that are fucked up in some way
t = t
.replace(/[\x00-\x1F\x7F-\x9F\W]/gm, " ")
.replace(/\s+/gim, " ")
.trim()
.split(" ")
.filter(item => item.length > 3)
.filter(item => !/^[a-z]/gm.test(item[0]))
.filter(item => {
const b = item
.split("")
.sort()
.join("")
.replace(/\d/gm, "");
return !/[A-Z]{4,}/gm.test(b);
});
// add body parts to character objects
t.forEach(item => {
if (item === divider) {
return;
}
const surv = structure;
// if current item is a character, then add on all the body part arrays
if (nameRegex.test(item)) {
surv[item] = {};
parts.forEach(p => (surv[item][p] = []));
reserved.push(item);
}
});
// filter out extraneous names in data
t = t.filter(n => !nameRegex.test(n));
// set up temp array for equipment
let temp = [];
// initialize flags to hold the current operating character, and current body part
let currentPerson, currentPart;
// for each item in the array of text,
t.forEach(item => {
const s = structure;
// if the item is not a body part or a reserved keyword,
if (!parts.includes(item) && !reserved.includes(item)) {
// add the item into the array. it should be an equipment piece
temp.push(item);
} else {
// otherwise, set the current body part.
if (parts.includes(item)) {
currentPart = item;
// go through the temp array, which is now full of equipment,
temp.forEach(t => {
// and add on equipment to the current body part on the current person.
s[currentPerson][currentPart].push(t);
});
// empty out the temporary 'gear holder'
temp = [];
} else if (reserved.includes(item) && item !== divider) {
// if a new name is found, set the current person to the new name
currentPerson = item;
}
}
});
// return the created object
return structure;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment