2016-05-11 23:45:42 -07:00
|
|
|
import { compose } from 'redux';
|
2016-06-07 20:41:42 -07:00
|
|
|
import { bonfire, html, js } from '../../utils/challengeTypes';
|
2016-05-11 23:45:42 -07:00
|
|
|
|
|
|
|
export function encodeScriptTags(value) {
|
|
|
|
return value
|
|
|
|
.replace(/<script>/gi, 'fccss')
|
|
|
|
.replace(/<\/script>/gi, 'fcces');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function decodeSafeTags(value) {
|
|
|
|
return value
|
|
|
|
.replace(/fccss/gi, '<script>')
|
|
|
|
.replace(/fcces/gi, '</script>');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function encodeFormAction(value) {
|
|
|
|
return value.replace(
|
|
|
|
/<form[^>]*>/,
|
|
|
|
val => val.replace(/action(\s*?)=/, 'fccfaa$1=')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function decodeFccfaaAttr(value) {
|
|
|
|
return value.replace(
|
|
|
|
/<form[^>]*>/,
|
|
|
|
val => val.replace(/fccfaa(\s*?)=/, 'action$1=')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-05-14 16:46:20 -07:00
|
|
|
export function arrayToString(seedData = ['']) {
|
2016-05-11 23:45:42 -07:00
|
|
|
seedData = Array.isArray(seedData) ? seedData : [seedData];
|
|
|
|
return seedData.reduce((seed, line) => '' + seed + line + '\n', '\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildSeed({ challengeSeed = [] } = {}) {
|
|
|
|
return compose(
|
|
|
|
decodeSafeTags,
|
2016-05-14 16:46:20 -07:00
|
|
|
arrayToString
|
2016-05-11 23:45:42 -07:00
|
|
|
)(challengeSeed);
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathsMap = {
|
2016-06-07 20:41:42 -07:00
|
|
|
[html]: 'html',
|
|
|
|
[js]: 'js',
|
|
|
|
[bonfire]: 'js'
|
2016-05-11 23:45:42 -07:00
|
|
|
};
|
|
|
|
|
2016-05-20 12:42:26 -07:00
|
|
|
export function getPreFile({ challengeType }) {
|
|
|
|
return {
|
|
|
|
name: 'index',
|
|
|
|
ext: pathsMap[challengeType] || 'html',
|
|
|
|
key: getFileKey({ challengeType })
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getFileKey({ challengeType }) {
|
|
|
|
return 'index' + (pathsMap[challengeType] || 'html');
|
2016-05-11 23:45:42 -07:00
|
|
|
}
|
|
|
|
|
2016-05-13 20:04:56 -07:00
|
|
|
export function createTests({ tests = [] }) {
|
|
|
|
return tests
|
|
|
|
.map(test => ({
|
2016-06-09 16:02:51 -07:00
|
|
|
text: ('' + test).split('message: ').pop().replace(/\'\);/g, ''),
|
2016-05-25 18:28:20 -07:00
|
|
|
testString: test
|
2016-05-13 20:04:56 -07:00
|
|
|
}));
|
|
|
|
}
|
2016-05-27 17:11:25 -07:00
|
|
|
|
|
|
|
export function loggerToStr(args) {
|
|
|
|
args = Array.isArray(args) ? args : [args];
|
|
|
|
return args
|
|
|
|
.map(arg => typeof arg === 'undefined' ? 'undefined' : arg)
|
|
|
|
.map(arg => {
|
|
|
|
if (typeof arg !== 'string') {
|
|
|
|
return JSON.stringify(arg);
|
|
|
|
}
|
|
|
|
return arg;
|
|
|
|
})
|
|
|
|
.reduce((str, arg) => str + arg + '\n', '');
|
|
|
|
}
|
2016-06-01 15:52:08 -07:00
|
|
|
|
|
|
|
export function getFirstChallenge(
|
|
|
|
{ superBlock, block, challenge },
|
|
|
|
result
|
|
|
|
) {
|
|
|
|
return challenge[
|
|
|
|
block[
|
|
|
|
superBlock[
|
|
|
|
result[0]
|
|
|
|
].blocks[0]
|
|
|
|
].challenges[0]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-06-10 14:01:13 -07:00
|
|
|
export function getNextChallenge(current, entites) {
|
2016-06-01 15:52:08 -07:00
|
|
|
const { challenge: challengeMap, block: blockMap } = entites;
|
|
|
|
// find current challenge
|
|
|
|
// find current block
|
|
|
|
// find next challenge in block
|
|
|
|
const currentChallenge = challengeMap[current];
|
2016-06-10 14:01:13 -07:00
|
|
|
if (!currentChallenge) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const block = blockMap[currentChallenge.block];
|
|
|
|
const index = block.challenges.indexOf(currentChallenge.dashedName);
|
|
|
|
return challengeMap[block.challenges[index + 1]];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getFirstChallengeOfNextBlock(current, entites) {
|
|
|
|
const {
|
|
|
|
challenge: challengeMap,
|
|
|
|
block: blockMap,
|
|
|
|
superBlock: SuperBlockMap
|
|
|
|
} = entites;
|
|
|
|
const currentChallenge = challengeMap[current];
|
|
|
|
if (!currentChallenge) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const block = blockMap[currentChallenge.block];
|
|
|
|
if (!block) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const superBlock = SuperBlockMap[block.superBlock];
|
|
|
|
const index = superBlock.blocks.indexOf(block.dashedName);
|
2016-06-21 09:52:16 -07:00
|
|
|
const newBlock = blockMap[superBlock.blocks[ index + 1 ]];
|
2016-06-10 14:01:13 -07:00
|
|
|
if (!newBlock) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return challengeMap[newBlock.challenges[0]];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getFirstChallengeOfNextSuperBlock(
|
|
|
|
current,
|
|
|
|
entites,
|
|
|
|
superBlocks
|
|
|
|
) {
|
|
|
|
const {
|
|
|
|
challenge: challengeMap,
|
|
|
|
block: blockMap,
|
|
|
|
superBlock: SuperBlockMap
|
|
|
|
} = entites;
|
|
|
|
const currentChallenge = challengeMap[current];
|
|
|
|
if (!currentChallenge) {
|
|
|
|
return null;
|
2016-06-01 15:52:08 -07:00
|
|
|
}
|
2016-06-10 14:01:13 -07:00
|
|
|
const block = blockMap[currentChallenge.block];
|
|
|
|
if (!block) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const superBlock = SuperBlockMap[block.superBlock];
|
|
|
|
const index = superBlocks.indexOf(superBlock.dashedName);
|
|
|
|
const newSuperBlock = SuperBlockMap[superBlocks[ index + 1]];
|
|
|
|
if (!newSuperBlock) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const newBlock = blockMap[newSuperBlock.blocks[0]];
|
|
|
|
return challengeMap[newBlock.challenges[0]];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCurrentBlockName(current, entities) {
|
|
|
|
const { challenge: challengeMap } = entities;
|
|
|
|
const challenge = challengeMap[current];
|
|
|
|
return challenge.block;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCurrentSuperBlockName(current, entities) {
|
|
|
|
const { challenge: challengeMap, block: blockMap } = entities;
|
|
|
|
const challenge = challengeMap[current];
|
|
|
|
const block = blockMap[challenge.block];
|
|
|
|
return block.superBlock;
|
2016-06-01 15:52:08 -07:00
|
|
|
}
|
2016-06-13 12:26:30 -07:00
|
|
|
|
|
|
|
// gets new mouse position
|
|
|
|
// getMouse(
|
|
|
|
// e: MouseEvent|TouchEvent,
|
|
|
|
// [ dx: Number, dy: Number ]
|
|
|
|
// ) => [ Number, Number ]
|
|
|
|
export function getMouse(e, [dx, dy]) {
|
|
|
|
let { pageX, pageY, touches, changedTouches } = e;
|
|
|
|
|
|
|
|
// touches can be empty on touchend
|
|
|
|
if (touches || changedTouches) {
|
|
|
|
e.preventDefault();
|
|
|
|
// these re-assigns the values of pageX, pageY from touches
|
|
|
|
({ pageX, pageY } = touches[0] || changedTouches[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [pageX - dx, pageY - dy];
|
|
|
|
}
|