puzzle/common/Util.js

174 lines
3 KiB
JavaScript
Raw Normal View History

2020-12-21 18:34:57 +01:00
import { Rng } from './Rng.js'
2020-11-08 14:49:34 +01:00
// get a unique id
export const uniqId = () => Date.now().toString(36) + Math.random().toString(36).substring(2)
2020-11-07 11:35:29 +01:00
// get a random int between min and max (inclusive)
2020-12-21 18:34:57 +01:00
export const randomInt = (
/** @type Rng */ rng,
min,
max
) => rng.random(min, max)
2020-11-07 11:35:29 +01:00
// get one random item from the given array
2020-12-21 18:34:57 +01:00
export const choice = (
/** @type Rng */ rng,
array
) => array[randomInt(rng, 0, array.length - 1)]
2020-11-07 11:35:29 +01:00
export const throttle = (fn, delay) => {
let canCall = true
return (...args) => {
if (canCall) {
fn.apply(null, args)
canCall = false
setTimeout(() => {
canCall = true
}, delay)
}
}
}
2020-11-07 11:35:29 +01:00
// return a shuffled (shallow) copy of the given array
2020-12-21 18:34:57 +01:00
export const shuffle = (
/** @type Rng */ rng,
array
) => {
2020-11-07 11:35:29 +01:00
let arr = array.slice()
for (let i = 0; i <= arr.length - 2; i++)
{
2020-12-21 18:34:57 +01:00
const j = randomInt(rng, i, arr.length -1);
2020-11-07 11:35:29 +01:00
const tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
return arr
}
2020-11-25 22:03:35 +01:00
export const timestamp = () => {
const d = new Date();
return Date.UTC(
d.getUTCFullYear(),
d.getUTCMonth(),
d.getUTCDate(),
d.getUTCHours(),
d.getUTCMinutes(),
d.getUTCSeconds(),
d.getUTCMilliseconds(),
)
}
2020-12-05 19:45:34 +01:00
function encodeShape(data) {
if (typeof data === 'number') {
return data
}
/* encoded in 1 byte:
00000000
^^ top
^^ right
^^ bottom
^^ left
*/
return ((data.top + 1) << 0)
| ((data.right + 1) << 2)
| ((data.bottom + 1) << 4)
| ((data.left + 1) << 6)
}
function decodeShape(data) {
if (typeof data !== 'number') {
return data
}
return {
top: (data >> 0 & 0b11) - 1,
right: (data >> 2 & 0b11) - 1,
bottom: (data >> 4 & 0b11) - 1,
left: (data >> 6 & 0b11) - 1,
}
}
function encodeTile(data) {
if (Array.isArray(data)) {
return data
}
return [data.idx, data.pos.x, data.pos.y, data.z, data.owner, data.group]
}
function decodeTile(data) {
if (!Array.isArray(data)) {
return data
}
return {
idx: data[0],
pos: {
x: data[1],
y: data[2],
},
z: data[3],
owner: data[4],
group: data[5],
}
}
function encodePlayer(data) {
if (Array.isArray(data)) {
return data
}
return [
data.id,
data.x,
data.y,
data.d,
data.name,
data.color,
data.bgcolor,
data.points,
data.ts,
]
}
function decodePlayer(data) {
if (!Array.isArray(data)) {
return data
}
return {
id: data[0],
x: data[1],
y: data[2],
d: data[3], // mouse down
name: data[4],
color: data[5],
bgcolor: data[6],
points: data[7],
ts: data[8],
}
}
function coordByTileIdx(info, tileIdx) {
const wTiles = info.width / info.tileSize
return {
x: tileIdx % wTiles,
y: Math.floor(tileIdx / wTiles),
}
}
2020-11-07 11:35:29 +01:00
export default {
2020-11-08 14:49:34 +01:00
uniqId,
randomInt,
choice,
throttle,
2020-11-08 14:49:34 +01:00
shuffle,
2020-11-25 22:03:35 +01:00
timestamp,
2020-12-05 19:45:34 +01:00
encodeShape,
decodeShape,
encodeTile,
decodeTile,
encodePlayer,
decodePlayer,
coordByTileIdx,
2020-11-08 12:56:54 +01:00
}