Skip to content

Instantly share code, notes, and snippets.

View NachoToast's full-sized avatar

Liam Douglas NachoToast

View GitHub Profile
@NachoToast
NachoToast / cursorUtil.ts
Created September 24, 2023 23:04
NDC to XYZ Coordinates (ThreeJS)
/*
Translates normalised device coordinates (NCD) to world coordinates.
Calculate NCD using mouseMove/pointerMove event listeners:
mouseX = (e.clientX / window.innerWidth) * 2 - 1;
mouseY = -(e.clientY / window.innerHeight) * 2 + 1;
You can then test if it works by setting up a camera and a cursor mesh object in the scene,
copying its position from the return value every animation frame:
cursorMesh.position.copy(getCursorWorldCoordinates());
@NachoToast
NachoToast / extendNumber.ts
Created January 10, 2023 03:50
Existing Class Extending in Typescript
// typing of implementation is not checked by the compiler, only calls to it
declare global {
interface Number {
between: (min: number, max: number) => boolean;
}
}
Object.defineProperty(Number.prototype, 'between', {
value(min: number, max: number): boolean {
return this >= min && this <= max;
@NachoToast
NachoToast / CookieManager.ts
Created October 4, 2022 03:38
Cookie Manager
/** Flags we put at the start of each cookie value so we know how to parse them. */
enum CookieTypeMap {
Boolean = `b`,
Number = `n`,
Object = `o`,
String = `s`,
}
/**
* Options when setting this cookie.
@NachoToast
NachoToast / activityGetter.py
Last active January 29, 2022 08:09
Gets a GitHub user's contributions using the graphql API.
from filecmp import cmp
from requests import post
from json import loads
from datetime import datetime
# generate your token at https://github.com/settings/tokens (i'm not sure what scopes you will need)
token = 'api_token'
username = 'NachoToast' # doesn't have to be your username
url = 'https://api.github.com/graphql'
export type JoinVerification = 'token' | 'ip' | 'username' | 'gameCode';
import { JwtPayload, verify } from 'jsonwebtoken';
import { Socket } from 'socket.io';
import { CONNECTION_SYSTEM } from '../constants/logging';
import {
EMITTED_PLAYER_EVENTS,
RECEIVED_PLAYER_EVENTS,
} from '../constants/socketEvent';
import {
@NachoToast
NachoToast / levenshtein.ts
Last active January 10, 2023 19:30
Levenshtein distance calculator for TypeScript.
/** Compares the similarity between 2 strings using Levenshtein distance.
* @param {string} s1 The first string.
* @param {string} s2 The second string.
* @returns {number} Similarity between the 2 strings.
*/
export function levenshteinDistance(s1: string, s2: string): number {
let longerString = s1;
let shorterString = s2;
if (s1.length < s2.length) {
longerString = s2;
@NachoToast
NachoToast / get_folders_recursive.php
Created May 21, 2021 04:35
Create an array of folders in a directory, recursively.
<?php
function add_folders($root) {
$local_folders_array = array();
foreach (glob("$root/*", GLOB_ONLYDIR) as $folder) {
array_push($local_folders_array, ['foldername' => $folder]);
$arr2 = add_folders($folder);
if (count($arr2) > 0) foreach($arr2 as $subfolder) array_push($local_folders_array, ['foldername' => $subfolder['foldername']]);
}
return $local_folders_array;
}
@NachoToast
NachoToast / files.php
Created May 14, 2021 08:10
Get array of files and multidimensional array of folders and files (1 directory deep) in a directory.
var files =
<?php
$files = array();
foreach (glob('*.html') as $filename) {
$files[] = pathinfo($filename)['filename'] . "." . pathinfo($filename)['extension'];
}
echo json_encode($files);
?>,
folders =
<?php
@NachoToast
NachoToast / captcha.php
Created April 27, 2021 06:43
Verify a recaptcha v3 token.
<?php
function verify_captcha($token) {
$threshold = 0.5; // Score must be > threshold to pass captcha.
// Default is 0.5, although the majority of users will get 0.9
$sites = ["localhost", "nachotoast.com", "ntgc.ddns.net"]; // Site names string, e.g. sub.domain.com:8080
$secret = "Put your client secret here.";
$url = "https://www.google.com/recaptcha/api/siteverify";
$data = array("secret" => $secret, "response" => $token);
@NachoToast
NachoToast / make_circle_coordinates.js
Created April 24, 2021 01:48
Create an array of coordinate objects {x, y} of points on a circle within set bounds.
function make_circle_coordinates(top = 0, left = 0, bottom = 300, right = 300, points = 20, visual = true) {
// e.g. top: 20, left: 20, right: 1900, bottom: 800
let center = {
x: (left + right) / 2,
y: (top + bottom) / 2
},
radius = {
x: right - center.x,
y: bottom - center.y
},