Skip to content

Instantly share code, notes, and snippets.

View endel's full-sized avatar

Endel Dreyer endel

View GitHub Profile
@endel
endel / index.ts
Created July 17, 2023 17:29
Colyseus Schema - Listen for all changes and print them as they come
room.onStateChange.once(function() {
function registerCallbacksOnStructure (schemaInstance, path) {
const schema = schemaInstance['_definition'].schema;
for (let field in schema) {
const schemaType = typeof(schema[field]);
if (schemaType === "object") {
// on item added to collection
schemaInstance[field].onAdd(function (item, key) {
onItemAdd([...path, field], item, key);
@endel
endel / README.md
Last active June 13, 2022 15:29
Node.js Package Authors: Please support both CJS and ESM: https://dev.to/endel/nodejs-package-authors-please-support-both-cjs-and-esm-1oj3
@endel
endel / SchemaCallbacks.md
Last active August 22, 2021 13:26
New Schema Callback API Proposal

Hi everybody! Regarding the possible new schema callback API, so far this is what I could came up so far, feedback highly appreciated! Do let me know if you feel I need to clarify anything!

I understand this may be long and hard to grasp, I can provide a side-by-side comparison between the new API and previous one so you can clearly see the difference.

The new API would allow to create all the callbacks at once, after the connection with the room has been established (even on deep structures) - rather than binding callbacks as Schema instances that are created on-demand on the client-side.

Structures:

class Point extends Schema {
@endel
endel / dungeon-generator.js
Created November 6, 2020 18:45
Simple Dungeon Generation Algorithm. Used in https://github.com/endel/mazmorra/
// Based on: http://breinygames.blogspot.com/2011/07/random-map-generation.html
// Needs improvment:
// - It is possible with small rooms for it to not be closed (i.e. a wall tile missing)
// - Walls often double up (more room spacing?)
var RoomMaze = srm = {
generate: function(gridSize, minRoomSize, maxRoomSize, maxRooms) {
// 1) Create the grid
var grid = [];
@endel
endel / lua-ternary-operator.lua
Created September 24, 2020 21:16
LUA Ternary Operator
local x = (y > 10) and "condition is true" or "condition is false"
@endel
endel / ecosystem.config.js
Last active June 28, 2020 00:30
Sample PM2 ecosystem file for deployment
module.exports = {
apps : [{
name : 'my-app',
script : 'lib/index.js',
watch : false,
instances : 1,
exec_mode : 'fork',
env: {
NODE_ENV: 'development'
}
@endel
endel / extract-parameters-typeof-typescript.ts
Created March 28, 2020 15:21
Extract parameters of an instance method using TypeScript
// Playground link: https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=19&pc=49#code/C4TwDgpgBAKuEB4YD4oF4oDsIHcoAoA6YgQwCcBzAZwC4oTMQBtAXQEp1UYBuAKF4DGAGxJUqUAEoB7KQFsk6eo1QBvXlChVgJYBAD8dHuqhTMAYTIQdEfFLDAAlqdpKQHNRoC+vb4JFioAFkQaTkoCAAPXUwAE3FQ+RUYhwAzFIhLTGAAcQhsMgcBOi0CzApPVWNTCytdW3snTBcVLBJZCGLgUooAGighCAA3CCE6TABXWQAjDKhPd29fFPHMAUdTKBiIFIdsBUjouNh4BATkZHxjMhlZOEhDHqqG5zoABXI2iF0yKgQASSa2lWEDuiBQTAA5NVLNYISxkEwAAwsXjuHz8LY7bD4YIJPotTCfOgAIj+wCgOCkZAA1lQAITEvoDYajKAARjmbG4QA
type Type<T> = new (...args: any[]) => T;
class Room<T = any> {
state?: T;
onCreate(options: any) {
}
}
@endel
endel / ssl-fullchain-tutorial.md
Created December 18, 2019 02:56
Manually generating and using SSL fullchain on Node.js (purchased from ssls.com - Comodo PositiveSSL)

Before purchasing the SSL certificate, you'll generate a CSR and PEM files:

  • STAR_domainname_com.csr
  • STAR_domainname_com.pem

When purchasing a SSL from ssls.com, they give you these files:

  • STAR_domainname_com.ca-bundle
  • STAR_domainname_com.crt

To generate the fullchain.pem

@endel
endel / aws-ec2-root-node-port-80.sh
Created December 4, 2019 18:58
Allow non-root node to use ports 80 (HTTP) and 443 (HTTPS) (AWS EC2)
# Allow non-root node to use ports 80 (HTTP) and 443 (HTTPS)
sudo setcap 'cap_net_bind_service=+ep' $(which node)
@endel
endel / parse-patreon-csv.js
Created October 9, 2019 20:55
Parse Patreon Members.csv file to output active patron's names (NodeJS)
const csv = require('csv-parser')
const fs = require('fs')
const patrons = [];
const GENEROUS_PLEDGE = 30;
const GENEROUS_LIFETIME = 200;
fs.createReadStream(process.argv[2])
.pipe(csv())
.on('data', (data) => patrons.push(data))