New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@cardcore/util

Package Overview
Dependencies
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cardcore/util - npm Package Compare versions

Comparing version 0.0.1-1f746c46 to 0.0.1-289cb4df

dist/box.js

37

dist/constants.js

@@ -6,16 +6,25 @@ "use strict";

});
var LOCATION_FIELD = exports.LOCATION_FIELD = "field";
var LOCATION_DECK = exports.LOCATION_DECK = "deck";
var LOCATION_HAND = exports.LOCATION_HAND = "hand";
var LOCATION_GRAVEYARD = exports.LOCATION_GRAVEYARD = "graveyard";
var ALL_LOCATIONS = exports.ALL_LOCATIONS = [LOCATION_FIELD, LOCATION_DECK, LOCATION_HAND, LOCATION_GRAVEYARD];
var TYPE_CREATURE = exports.TYPE_CREATURE = "creature";
var TYPE_FACE = exports.TYPE_FACE = "face";
var ALL_TYPES = exports.ALL_TYPES = [TYPE_CREATURE, TYPE_FACE];
var PLAYER_SELF = exports.PLAYER_SELF = "self";
var PLAYER_ENEMY = exports.PLAYER_ENEMY = "enemy";
exports.PLAYER_ENEMY = exports.PLAYER_SELF = exports.ALL_TYPES = exports.TYPE_FACE = exports.TYPE_CREATURE = exports.ALL_LOCATIONS = exports.LOCATION_GRAVEYARD = exports.LOCATION_HAND = exports.LOCATION_DECK = exports.LOCATION_FIELD = exports.REMOTE_ACTION = void 0;
const REMOTE_ACTION = "__REMOTE_ACTION";
exports.REMOTE_ACTION = REMOTE_ACTION;
const LOCATION_FIELD = "field";
exports.LOCATION_FIELD = LOCATION_FIELD;
const LOCATION_DECK = "deck";
exports.LOCATION_DECK = LOCATION_DECK;
const LOCATION_HAND = "hand";
exports.LOCATION_HAND = LOCATION_HAND;
const LOCATION_GRAVEYARD = "graveyard";
exports.LOCATION_GRAVEYARD = LOCATION_GRAVEYARD;
const ALL_LOCATIONS = [LOCATION_FIELD, LOCATION_DECK, LOCATION_HAND, LOCATION_GRAVEYARD];
exports.ALL_LOCATIONS = ALL_LOCATIONS;
const TYPE_CREATURE = "creature";
exports.TYPE_CREATURE = TYPE_CREATURE;
const TYPE_FACE = "face";
exports.TYPE_FACE = TYPE_FACE;
const ALL_TYPES = [TYPE_CREATURE, TYPE_FACE];
exports.ALL_TYPES = ALL_TYPES;
const PLAYER_SELF = "self";
exports.PLAYER_SELF = PLAYER_SELF;
const PLAYER_ENEMY = "enemy";
exports.PLAYER_ENEMY = PLAYER_ENEMY;
//# sourceMappingURL=constants.js.map

@@ -6,11 +6,47 @@ "use strict";

});
var _exportNames = {
Keys: true,
Box: true,
hashState: true,
makeSchema: true
};
Object.defineProperty(exports, "Box", {
enumerable: true,
get: function () {
return _box.default;
}
});
Object.defineProperty(exports, "hashState", {
enumerable: true,
get: function () {
return _hashState.default;
}
});
Object.defineProperty(exports, "makeSchema", {
enumerable: true,
get: function () {
return _makeSchema.default;
}
});
exports.Keys = void 0;
var _targetHelper = require("./target-helper");
var Keys = _interopRequireWildcard(require("./keys"));
Object.keys(_targetHelper).forEach(function (key) {
exports.Keys = Keys;
var _box = _interopRequireDefault(require("./box"));
var _hashState = _interopRequireDefault(require("./hash-state"));
var _makeSchema = _interopRequireDefault(require("./make-schema"));
var _constants = require("./constants");
Object.keys(_constants).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _targetHelper[key];
get: function () {
return _constants[key];
}

@@ -20,2 +56,28 @@ });

var _debug = require("./debug");
Object.keys(_debug).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _debug[key];
}
});
});
var _nextActions = require("./next-actions");
Object.keys(_nextActions).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _nextActions[key];
}
});
});
var _randomUtil = require("./random-util");

@@ -25,5 +87,6 @@

if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
get: function () {
return _randomUtil[key];

@@ -34,10 +97,11 @@ }

var _util = require("./util");
var _targetHelper = require("./target-helper");
Object.keys(_util).forEach(function (key) {
Object.keys(_targetHelper).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _util[key];
get: function () {
return _targetHelper[key];
}

@@ -47,13 +111,18 @@ });

var _constants = require("./constants");
var _util = require("./util");
Object.keys(_constants).forEach(function (key) {
Object.keys(_util).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _constants[key];
get: function () {
return _util[key];
}
});
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
//# sourceMappingURL=index.js.map

@@ -6,75 +6,67 @@ "use strict";

});
exports.rando = exports.RandomUtil = exports.shuffle = undefined;
exports.shuffle = shuffle;
exports.rando = exports.RandomUtil = void 0;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); // very simple PRNG from here https://gist.github.com/blixt/f17b47c62508be59987b
var _ssbKeys = _interopRequireDefault(require("@streamplace/ssb-keys"));
var _ssbKeys = require("ssb-keys");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _ssbKeys2 = _interopRequireDefault(_ssbKeys);
// very simple PRNG from here https://gist.github.com/blixt/f17b47c62508be59987b
function shuffle(arr, func = Math.random) {
// often times values from Object.keys and such can be nondeterministic, so
arr = [...arr].sort();
const randos = arr.map(() => func());
return Object.keys(arr).sort((a, b) => randos[a] - randos[b]).map(k => arr[k]);
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
class RandomUtil {
constructor(seed) {
if (seed) {
this.setSeed(seed);
}
}
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
setSeed(seed) {
// todo: broken and insecure
if (typeof seed === "string") {
let seedInt = 0;
function _shuffle(arr) {
var func = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Math.random;
for (let i = 0; i < seed.length; i += 1) {
seedInt = (seedInt + seed.charCodeAt(i)) % 2147483647;
}
var randos = arr.map(function () {
return func();
});
return Object.keys(arr).sort(function (a, b) {
return randos[a] - randos[b];
}).map(function (k) {
return arr[k];
});
}
seed = seedInt;
} else if (typeof seed !== "number") {
throw new Error("seed must be a string or a number");
}
exports.shuffle = _shuffle;
this.seed = seed % 2147483647;
if (this.seed <= 0) this.seed += 2147483646;
}
var RandomUtil = exports.RandomUtil = function () {
function RandomUtil(seed) {
_classCallCheck(this, RandomUtil);
shuffle(arr) {
return shuffle(arr, this.next.bind(this));
}
if (!seed) {
seed = _ssbKeys2.default.hash("" + Math.random());
next() {
if (!this.seed) {
throw new Error("tried to use RNG without providing a seed!");
}
this.setSeed(seed);
return this.seed = this.seed * 16807 % 2147483647;
}
_createClass(RandomUtil, [{
key: "setSeed",
value: function setSeed(seed) {
if (typeof seed === "string") {
var seedInt = 0;
for (var i = 0; i < seed.length; i += 1) {
seedInt = (seedInt + seed.charCodeAt(i)) % 2147483647;
}
seed = seedInt;
} else if (typeof seed !== "number") {
throw new Error("seed must be a string or a number");
}
this.seed = seed % 2147483647;
if (this.seed <= 0) this.seed += 2147483646;
}
}, {
key: "shuffle",
value: function shuffle(arr) {
return _shuffle(arr, this.next.bind(this));
}
}, {
key: "next",
value: function next() {
return this.seed = this.seed * 16807 % 2147483647;
}
}, {
key: "nextFloat",
value: function nextFloat() {
return (this.next() - 1) / 2147483646;
}
}]);
nextFloat() {
return (this.next() - 1) / 2147483646;
}
return RandomUtil;
}();
clearSeed() {
this.seed = null;
}
var rando = exports.rando = new RandomUtil();
}
exports.RandomUtil = RandomUtil;
const rando = new RandomUtil();
exports.rando = rando;
//# sourceMappingURL=random-util.js.map

@@ -6,5 +6,2 @@ "use strict";

});
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
exports.target = target;

@@ -17,6 +14,11 @@ exports.targetArray = targetArray;

var noop = function noop(x) {
return x;
};
var _box = _interopRequireDefault(require("./box"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const noop = x => x;
/**

@@ -26,3 +28,3 @@ * Returns an object of {unitId: unit}

* @export
* @param {object} state
* @param {object} game
* @param {object} target

@@ -35,22 +37,28 @@ * @property {string} target.player

*/
function target(state, target) {
var func = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : noop;
var units = {};
var owners = {};
function target(state, target, func = noop) {
const hasFullState = !!state.game;
let game = hasFullState ? state.game : state;
let units = {};
const owners = {};
if (target.unitId) {
units[target.unitId] = state.units[target.unitId];
owners[target.unitId] = Object.keys(state.players).find(function (playerId) {
return state.players[playerId].field.includes(target.unitId);
units[target.unitId] = game.units[target.unitId];
owners[target.unitId] = Object.keys(game.players).find(playerId => {
return game.players[playerId].field.map(boxId => _box.default.traverse(state, boxId)).includes(target.unitId);
});
if (!owners[target.unitId]) {
throw new Error(`couldn't find who owns ${target.unitId}`);
}
} else {
var players = void 0;
let players;
if (!target.player) {
players = state.playerOrder;
players = game.playerOrder;
} else if (target.player === _constants.PLAYER_SELF) {
players = [state.turn];
players = [game.turn];
} else if (target.player === _constants.PLAYER_ENEMY) {
players = state.playerOrder.filter(function (p) {
return p !== state.turn;
});
players = game.playerOrder.filter(p => p !== game.turn);
} else {

@@ -60,90 +68,44 @@ players = [target.player];

var locations = target.location ? [target.location] : _constants.ALL_LOCATIONS;
var types = target.type ? [target.type] : _constants.ALL_TYPES;
var _owners = {};
const locations = target.location ? [target.location] : _constants.ALL_LOCATIONS;
const types = target.type ? [target.type] : _constants.ALL_TYPES;
const owners = {};
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
for (const [playerId, player] of Object.entries(game.players)) {
if (!players.includes(playerId)) {
continue;
}
try {
for (var _iterator = Object.entries(state.players)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _ref = _step.value;
if (types.includes(_constants.TYPE_FACE)) {
units[player.unitId] = game.units[player.unitId];
owners[player.unitId] = playerId;
}
var _ref2 = _slicedToArray(_ref, 2);
if (!types.includes(_constants.TYPE_CREATURE)) {
continue;
}
var playerId = _ref2[0];
var player = _ref2[1];
for (const location of locations) {
for (const boxId of player[location]) {
if (!hasFullState) {
throw new Error("Deprecated call to target(), need full state");
}
if (!players.includes(playerId)) {
continue;
}
if (types.includes(_constants.TYPE_FACE)) {
units[player.unitId] = state.units[player.unitId];
_owners[player.unitId] = playerId;
}
if (!types.includes(_constants.TYPE_CREATURE)) {
continue;
}
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
const unitId = _box.default.traverse(state, boxId);
try {
for (var _iterator2 = locations[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var location = _step2.value;
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
if (!unitId) {
// TODO FIXME this should be an error condition
// console.warn(
// `Tried to target something I can't decrypt, ${JSON.stringify({
// location,
// boxId,
// playerId
// })}`
// );
continue;
}
try {
for (var _iterator3 = player[location][Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var unitId = _step3.value;
units[unitId] = state.units[unitId];
_owners[unitId] = playerId;
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
units[unitId] = game.units[unitId];
owners[unitId] = playerId;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}

@@ -153,34 +115,20 @@ }

if (target.random) {
var newArray = Object.keys(units).sort();
var newUnitIds = _randomUtil.rando.shuffle(newArray);
var newUnitObject = {};
for (var i = 0; i < target.count; i++) {
let newArray = Object.keys(units).sort();
let newUnitIds = _randomUtil.rando.shuffle(newArray);
let newUnitObject = {};
for (let i = 0; i < target.count; i++) {
newUnitObject[newUnitIds[i]] = units[newUnitIds[i]];
}
units = newUnitObject;
}
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator4 = Object.keys(units)[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var _unitId = _step4.value;
units[_unitId] = func(units[_unitId], { playerId: owners[_unitId], unitId: _unitId });
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
for (const unitId of Object.keys(units)) {
units[unitId] = func(units[unitId], {
playerId: owners[unitId],
unitId
});
}

@@ -191,7 +139,9 @@

function targetArray(state, action) {
return Object.values(target(state, action, function (unit, details) {
return Object.assign({}, details, { unit: unit });
function targetArray(game, action) {
return Object.values(target(game, action, (unit, details) => {
return _objectSpread({}, details, {
unit
});
}));
}
//# sourceMappingURL=target-helper.js.map

@@ -7,28 +7,33 @@ "use strict";

exports.range = range;
exports.uid = exports.traverseSecret = exports.getRightPlayer = exports.getLeftPlayer = exports.rotateArray = void 0;
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var _randomUtil = require("./random-util");
var _ssbKeys = _interopRequireDefault(require("@streamplace/ssb-keys"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Like Python's range. Get an array of numbers from 0 to n.
*/
function range(size) {
var startAt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
return [].concat(_toConsumableArray(Array(size).keys())).map(function (i) {
return i + startAt;
});
function range(size, startAt = 0) {
return [...Array(size).keys()].map(i => i + startAt);
}
/**
* Return an array rotated so elem is first. Useful for encrypt-decrypt order and such.
*/
var rotateArray = exports.rotateArray = function rotateArray(arr, startElem) {
var output = [];
var idx = arr.indexOf(startElem);
const rotateArray = (arr, startElem) => {
const output = [];
let idx = arr.indexOf(startElem);
if (idx === -1) {
throw new Error(startElem + " is not in " + JSON.stringify(arr));
throw new Error(`${startElem} is not in ${JSON.stringify(arr)}`);
}
while (output.length < arr.length) {
output.push(arr[idx]);
idx += 1;
if (idx >= arr.length) {

@@ -38,25 +43,59 @@ idx = 0;

}
return output;
};
/**
* Get the player to the left of this one
*/
var traverseSecret = exports.traverseSecret = function traverseSecret(secret, secrets) {
exports.rotateArray = rotateArray;
const getLeftPlayer = (playerId, players) => {
const index = players.indexOf(playerId);
if (index === 0) {
return players[players.length - 1];
}
return players[index - 1];
};
/**
* Get the player to the right of this one
*/
exports.getLeftPlayer = getLeftPlayer;
const getRightPlayer = (playerId, players) => {
const index = players.indexOf(playerId);
if (index === players.length - 1) {
return players[0];
}
return players[index + 1];
};
exports.getRightPlayer = getRightPlayer;
const traverseSecret = (secret, secrets) => {
if (!secret || !secret.secret) {
return secret;
}
if (secrets[secret.id]) {
return traverseSecret(secrets[secret.id].contents, secrets);
}
return null;
};
/**
* id-generation function that assumes that all users will execute it in the _exact_ same order.
* this is.... hopefully reasonable.
*/
var cur = 0;
var uid = exports.uid = function uid() {
var res = "id-" + cur;
cur += 1;
return res;
exports.traverseSecret = traverseSecret;
const uid = () => {
return `id-${_randomUtil.rando.next()}`;
};
exports.uid = uid;
//# sourceMappingURL=util.js.map
{
"name": "@cardcore/util",
"version": "0.0.1-1f746c46",
"version": "0.0.1-289cb4df",
"description": "various streamcards utilities",

@@ -13,3 +13,9 @@ "main": "dist/index.js",

},
"gitHead": "1f746c46c9911e36a1e9be89edf6da902e8ff20f"
"dependencies": {
"@streamplace/ssb-keys": "7.0.21",
"debug": "^4.1.0",
"isomorphic-fetch": "^2.2.1",
"json-stable-stringify": "^1.0.1"
},
"gitHead": "289cb4dfbb9f3a2d4a956bbfce4a7d8bfb079ef9"
}

@@ -0,1 +1,3 @@

export const REMOTE_ACTION = "__REMOTE_ACTION";
export const LOCATION_FIELD = "field";

@@ -2,0 +4,0 @@ export const LOCATION_DECK = "deck";

@@ -0,4 +1,12 @@

import * as Keys from "./keys";
export { default as Box } from "./box";
export { default as hashState } from "./hash-state";
export { default as makeSchema } from "./make-schema";
export { Keys };
export * from "./constants";
export * from "./debug";
export * from "./next-actions";
export * from "./random-util";
export * from "./target-helper";
export * from "./random-util";
export * from "./util";
export * from "./constants";
// very simple PRNG from here https://gist.github.com/blixt/f17b47c62508be59987b
import ssbKeys from "ssb-keys";
import ssbKeys from "@streamplace/ssb-keys";
export function shuffle(arr, func = Math.random) {
// often times values from Object.keys and such can be nondeterministic, so
arr = [...arr].sort();
const randos = arr.map(() => func());

@@ -14,9 +16,9 @@ return Object.keys(arr)

constructor(seed) {
if (!seed) {
seed = ssbKeys.hash(`${Math.random()}`);
if (seed) {
this.setSeed(seed);
}
this.setSeed(seed);
}
setSeed(seed) {
// todo: broken and insecure
if (typeof seed === "string") {

@@ -40,2 +42,5 @@ let seedInt = 0;

next() {
if (!this.seed) {
throw new Error("tried to use RNG without providing a seed!");
}
return (this.seed = (this.seed * 16807) % 2147483647);

@@ -47,4 +52,8 @@ }

}
clearSeed() {
this.seed = null;
}
}
export const rando = new RandomUtil();

@@ -10,2 +10,3 @@ import {

import { rando } from "./random-util";
import Box from "./box";

@@ -18,3 +19,3 @@ const noop = x => x;

* @export
* @param {object} state
* @param {object} game
* @param {object} target

@@ -28,17 +29,24 @@ * @property {string} target.player

export function target(state, target, func = noop) {
const hasFullState = !!state.game;
let game = hasFullState ? state.game : state;
let units = {};
const owners = {};
if (target.unitId) {
units[target.unitId] = state.units[target.unitId];
owners[target.unitId] = Object.keys(state.players).find(playerId => {
return state.players[playerId].field.includes(target.unitId);
units[target.unitId] = game.units[target.unitId];
owners[target.unitId] = Object.keys(game.players).find(playerId => {
return game.players[playerId].field
.map(boxId => Box.traverse(state, boxId))
.includes(target.unitId);
});
if (!owners[target.unitId]) {
throw new Error(`couldn't find who owns ${target.unitId}`);
}
} else {
let players;
if (!target.player) {
players = state.playerOrder;
players = game.playerOrder;
} else if (target.player === PLAYER_SELF) {
players = [state.turn];
players = [game.turn];
} else if (target.player === PLAYER_ENEMY) {
players = state.playerOrder.filter(p => p !== state.turn);
players = game.playerOrder.filter(p => p !== game.turn);
} else {

@@ -52,3 +60,3 @@ players = [target.player];

for (const [playerId, player] of Object.entries(state.players)) {
for (const [playerId, player] of Object.entries(game.players)) {
if (!players.includes(playerId)) {

@@ -58,3 +66,3 @@ continue;

if (types.includes(TYPE_FACE)) {
units[player.unitId] = state.units[player.unitId];
units[player.unitId] = game.units[player.unitId];
owners[player.unitId] = playerId;

@@ -66,4 +74,19 @@ }

for (const location of locations) {
for (const unitId of player[location]) {
units[unitId] = state.units[unitId];
for (const boxId of player[location]) {
if (!hasFullState) {
throw new Error("Deprecated call to target(), need full state");
}
const unitId = Box.traverse(state, boxId);
if (!unitId) {
// TODO FIXME this should be an error condition
// console.warn(
// `Tried to target something I can't decrypt, ${JSON.stringify({
// location,
// boxId,
// playerId
// })}`
// );
continue;
}
units[unitId] = game.units[unitId];
owners[unitId] = playerId;

@@ -92,5 +115,5 @@ }

export function targetArray(state, action) {
export function targetArray(game, action) {
return Object.values(
target(state, action, (unit, details) => {
target(game, action, (unit, details) => {
return { ...details, unit };

@@ -97,0 +120,0 @@ })

@@ -0,1 +1,4 @@

import { rando } from "./random-util";
import ssbKeys from "@streamplace/ssb-keys";
/**

@@ -27,2 +30,24 @@ * Like Python's range. Get an array of numbers from 0 to n.

/**
* Get the player to the left of this one
*/
export const getLeftPlayer = (playerId, players) => {
const index = players.indexOf(playerId);
if (index === 0) {
return players[players.length - 1];
}
return players[index - 1];
};
/**
* Get the player to the right of this one
*/
export const getRightPlayer = (playerId, players) => {
const index = players.indexOf(playerId);
if (index === players.length - 1) {
return players[0];
}
return players[index + 1];
};
export const traverseSecret = (secret, secrets) => {

@@ -38,11 +63,4 @@ if (!secret || !secret.secret) {

/**
* id-generation function that assumes that all users will execute it in the _exact_ same order.
* this is.... hopefully reasonable.
*/
let cur = 0;
export const uid = () => {
let res = `id-${cur}`;
cur += 1;
return res;
return `id-${rando.next()}`;
};

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc