Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Parse Stardew Valley event conditions
var input = "95/e 93/k 94/t 1800 1950/i 136/y 2";
(function(conditionString) {
"use strict";
let conditions = conditionString.split("/");
// define parsers
let parsers = [
// event ID
// <id>
{
match: /^\d+$/,
parse: (type, values) => "event ID"
},
// between times
// a <x> <y>
{
match: /^a /,
parse: (type, values) => `at tile position (${values[0]}, ${values[1]})`
},
// day of week (not one of)
// d [<day name>]+
{
match: /^d /,
parse: (type, values) => {
let days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat","Sun"]
.filter(day => values.indexOf(day) == -1)
.map(day => { let x = { "Mon": "Monday", "Tue": "Tuesday", "Wed": "Wednesday", "Thu": "Thursday", "Fri": "Friday", "Sat": "Saturday", "Sun": "Sunday" }; return x[day] || day; });
return "must be " + days.join(", ");
}
},
// seen event
// e [<id>]+
{
match: /^e /,
parse: (type, values) => "player has seen event" + (values.length > 1 ? "s " : " ") + values.join(', ')
},
// friendship points
// f [<name> <number>]+
{
match: /^f /,
parse: (type, values) => {
let result = "requires ";
for(let i = 0; i < values.length; i += 2) {
result += (values[i + 1] / 250) + " hearts with " + values[i];
if(i + 2 < values.length)
result += ", ";
}
return result;
}
},
// player has item
// i <id>
{
match: /^i /,
parse: (type, values) => "player has item " + values.join(', ')
},
// didn't see event
// k [<id>]+
{
match: /^k /,
parse: (type, values) => "player has NOT seen event" + (values.length > 1 ? "s " : " ") + values.join(', ')
},
// received letter (or set flag)
// l <letter name>
// n <letter name>
{
match: /^[ln] /,
parse: (type, values) => `received '${values[0]}' letter (or set that flag)`
},
// character is in same location
// p <NPC name>
{
match: /^p /,
parse: (type, values) => `${values[0]} must be here too`
},
// between times
// t <min time> <max time>
{
match: /^t /,
parse: (type, values) => `between times of ${values[0]} and ${values[1]}`
},
// weather
// w <name>
{
match: /^w /,
parse: (type, values) => `must be ${values[0]}`
},
// year
// y <year>
{
match: /^y /,
parse: (type, values) => values[0] == "1" ? "must be year 1" : `must be at least year ${values[0]}`
},
// season (not one of)
// z [<season>]+
{
match: /^z /,
parse: (type, values) => {
let seasons = ["spring", "summer", "fall", "winter"]
.filter(season => values.indexOf(season) == -1)
.map(season => season[0].toUpperCase() + season.slice(1));
return "must be " + seasons.join(", ");
}
}
];
// padding function
function padRight(str, len, character)
{
if(str.length >= len)
return str;
return str + new Array(len - str.length + 1).join(character || ' ');
}
// print header
let labelWidth = Math.max("condition".length, ...conditions.map(v => v.length));
console.log(conditionString);
console.log(padRight("condition", labelWidth) + " | summary");
console.log(padRight("", labelWidth, "-") + ' | ' + padRight("", labelWidth, "-"));
for(let condition of conditions) {
let type, values;
[type, ...values] = condition.split(' ');
let parsed = false;
for(let parser of parsers) {
if(parser.match.test(condition)) {
console.log(padRight(condition, labelWidth) + " | " + parser.parse(type, values));
parsed = true;
break;
}
}
if(!parsed)
console.log(padRight(condition, labelWidth) + " | <unknown>")
}
})(input);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment