Skip to content

Instantly share code, notes, and snippets.

@demipixel
demipixel / redditbuilds.js
Last active Nov 6, 2015
Reddit Builds a House (flying-squid pseudo code)
View redditbuilds.js
module.exports = {
player: playerInject,
server: serverInject,
disable: disable
}
function playerInject(serv, player, self) {
var pData = player.plugin(self); // Player Data
pData.blocklist = [];
@demipixel
demipixel / factorioScreen.js
Created Jun 25, 2016
60x60 Factorio Screen made of lamps
View factorioScreen.js
const Blueprint = require('factorio-blueprint');
const inputs = Object.keys(Blueprint.getEntityData()).filter(key => !Blueprint.getEntityData()[key].combinator).slice(0, 60);
const carries = Object.keys(Blueprint.getEntityData()).filter(key => !Blueprint.getEntityData()[key].combinator).slice(60, 90);
const bp = new Blueprint();
for (let i = 0; i < 2; i++) {
const start = i == 0 ? 0 : 59;
const dir = i == 0 ? 1 : -1;
let prevCombinator = null;
@demipixel
demipixel / factorioRickRoll.js
Created Jun 25, 2016
Generate the rick roll animation for the Factorio screen!
View factorioRickRoll.js
const Blueprint = require('factorio-blueprint');
const PNG = require('png-js');
const inputs = Object.keys(Blueprint.getEntityData()).filter(key => !Blueprint.getEntityData()[key].combinator).slice(0, 60);
const bp = new Blueprint();
const FRAMES = 4230; // Number of images I had
const COL = 30; // Max height = COL * 4 or something
const LEFT = true; // Change so you can get the left and the right side
@demipixel
demipixel / FactorioReminder.js
Created Jun 27, 2016
Wake me up when it's out!
View FactorioReminder.js
/*
HOW TO RUN:
- Download and Install node.js. Check by running: node -v
- Create a folder "wakeup" and stick this index.js inside
- cd to the folder. On a mac, this might look like: cd Desktop/wakeup
no idea on a windows, I think it's something like: cd C:\Users\username\Desktop\wakeup
- Run: npm install request
- Run: npm install sfx
@demipixel
demipixel / change.js
Last active Aug 7, 2016
Exact Change Solution by DemiPixel
View change.js
function drawer(price, cash, cid) { // cid == denominations array
price *= 100; cash *= 100; // Convert to pennies
// Really we're just trying to get the coins for cash-price
var vals = [1,5,10,25,100,500,1000,2000,10000]; // How much each denomation is worth (in pennies)
var cidHave = cid.map(x=>Math.round(x[1]*100)); // Convert each denomination we have into pennies (e.g. $1.05 in nickels to 105)
var cidAmts = cidHave.map((x,i)=>Math.floor(x/vals[i])); // e.g. 105 pennies worth in nickels to 21 nickels or 20000 pennies worth in quarters to 80 quarters
var best = [[0,0,0,0,0,0,0,0,0]]; // Start with $0.00 = 0 of each coin
var bestScoreList = [0]; // # of coins for $0.00 is 0
for (var i = 1; i <= cash-price; i++) { // For $0.00 to amt
best[i] = [...Array(cid.length)].map(x=>0); // Empty array of denominations
@demipixel
demipixel / blueprintText.js
Created Oct 2, 2016
Create Factorio blueprint strings for any text you'd like in any material!
View blueprintText.js
const Blueprint = require('factorio-blueprint');
const alphabet = `
~AA BBB CCC DDD EEEE FFFF GGG H H IIIII JJJJJ K K L MMM MMM NN N OOO PPP QQQ RRR SSSS TTTTT U U V V W W X X Y Y ZZZZZ ( ) * * * ! ??
A A B B C D D E F G H H I J K K L M M M N N N O O P P Q Q R R S T U U V V W W X X Y Y Z ( ) *** ! ?
AAAA BBB C D D EEEE FFFF G GGG HHHH I J KK L M M M N N N O O PPP Q Q RRR SSS T U U V V W W W X Y Z ( ) ***** ! ?
A A B B C D D E F G G H H I J J K K L M M N NN O O P Q QQ R R S T U U V V W W W X X Y Z ( ) *** .. ? ,
A A BBB CCC DDD EEEE F GGG H H IIIII JJJ K K LLLLL M M N N OOO P QQQQ R R SSSS T UUU V WWW X X Y ZZZZZ ( ) * * * .. ! ? ,~~
`.trim().replace(/\~/g, ' ').split('\n');
View datadog.sh
#!/bin/sh
# Datadog Agent installation from source script (should support most Unix OSes):
# download the source of the Agent, install its dependencies and set it up.
# Bail on errors
set -e
# We shouldn't have unbounded vars
set -u
#######################################################################
@demipixel
demipixel / redis-memolock-example.ts
Created Oct 25, 2021
Redis Memolock, implemented in TypeScript (with example usage)
View redis-memolock-example.ts
import Redis from 'ioredis';
const { HOST: host, PORT: port } = process.env;
const redis = new Redis({ host, port: parseInt(port || '6379', 10) });
const subRedis = new Redis({ host, port: parseInt(port || '6379', 10) });
// Write a class that checks a redis key for a cache.
// If the source is not locked, lock it and fetch it.
// If the source is already locked, wait for the lock to expire.
View getRelations.ts
export type RelationDepth = {
[key: string]: RelationDepth;
};
export function getRelations(depth: RelationDepth = {}) {
const relations: string[] = [];
const recurse = (obj: RelationDepth, path = '') => {
Object.keys(obj).forEach((key) => {
relations.push(path + key);
recurse(obj[key], path + key + '.');
View MakeRequired.ts
type FilterOut =
| string
| number
| Date
| undefined
| null
// eslint-disable-next-line @typescript-eslint/ban-types
| Function
| FilterOut[];