@cardcore/util
Advanced tools
Comparing version 0.0.1-dc755b0c to 0.0.1-dd07e215
130
dist/box.js
@@ -6,21 +6,25 @@ "use strict"; | ||
}); | ||
exports.default = void 0; | ||
var _ssbKeys = require("@streamplace/ssb-keys"); | ||
var _ssbKeys = _interopRequireDefault(require("@streamplace/ssb-keys")); | ||
var _ssbKeys2 = _interopRequireDefault(_ssbKeys); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var Box = { | ||
open: function open(boxId, box, me) { | ||
var privateKey = void 0; | ||
const boxCache = {}; | ||
const Box = { | ||
open(state, boxId) { | ||
const box = state.game.boxes[boxId]; | ||
let privateKey; | ||
if (box.privateKey) { | ||
privateKey = box.privateKey; | ||
} else if (box.keys[me.id]) { | ||
privateKey = Box.getPrivate(box, me); | ||
} else if (state.client && state.client.keys && box.keys[state.client.keys.id]) { | ||
privateKey = Box.getPrivate(state, boxId); | ||
} | ||
if (!privateKey) { | ||
return null; | ||
} | ||
return _ssbKeys2.default.unbox(box.contents, { | ||
return _ssbKeys.default.unbox(box.contents, { | ||
id: boxId, | ||
@@ -31,10 +35,34 @@ public: boxId.slice(1), | ||
}, | ||
getPrivate: function getPrivate(box, me) { | ||
return _ssbKeys2.default.unbox(box.keys[me.id], me); | ||
getPrivate(state, boxId) { | ||
const box = state.game.boxes[boxId]; | ||
const me = state.client.keys; | ||
return _ssbKeys.default.unbox(box.keys[me.id], me); | ||
}, | ||
traverse: function traverse(boxId, boxes, me) { | ||
if (!boxes[boxId]) { | ||
traverse(state, boxId) { | ||
if (typeof state === "string") { | ||
throw new Error("deprecated call to Box.traverse"); | ||
} | ||
if (boxCache[boxId]) { | ||
return boxCache[boxId]; | ||
} | ||
const result = this._traverse(state, boxId); | ||
if (result) { | ||
boxCache[boxId] = result; | ||
} | ||
return result; | ||
}, | ||
_traverse(state, boxId) { | ||
if (!state.game.boxes[boxId]) { | ||
return boxId; // idk maybe a unitId or something | ||
} | ||
var boxContents = Box.open(boxId, boxes[boxId], me); | ||
const boxContents = Box.open(state, boxId); | ||
if (!boxContents) { | ||
@@ -44,57 +72,38 @@ // dang, couldn't open it. done! | ||
} | ||
if (boxes[boxContents]) { | ||
if (state.game.boxes[boxContents]) { | ||
// hey, this box had a box in it! keep going! | ||
return Box.traverse(boxContents, boxes, me); | ||
} | ||
// got something that wasn't a box — we're done! | ||
return Box._traverse(state, boxContents); | ||
} // got something that wasn't a box — we're done! | ||
return boxContents; | ||
}, | ||
// contents can be anything JSON-able. owners is an array of ssb ids | ||
new(contents, ...ownerIds) { | ||
const keys = _ssbKeys.default.generate(); | ||
// contents can be anything JSON-able. owners is an array of ssb ids | ||
new: function _new(contents) { | ||
var keys = _ssbKeys2.default.generate(); | ||
var box = { | ||
contents: _ssbKeys2.default.box(contents, [keys]), | ||
const box = { | ||
contents: _ssbKeys.default.box(contents, [keys]), | ||
keys: {} | ||
}; | ||
for (var _len = arguments.length, ownerIds = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
ownerIds[_key - 1] = arguments[_key]; | ||
for (const ownerId of ownerIds) { | ||
box.keys[ownerId] = _ssbKeys.default.box(keys.private, [{ | ||
id: ownerId, | ||
public: ownerId.slice(1), | ||
curve: "ed25519" | ||
}]); | ||
} | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
return { | ||
boxId: keys.id, | ||
box | ||
}; | ||
}, | ||
try { | ||
for (var _iterator = ownerIds[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var ownerId = _step.value; | ||
box.keys[ownerId] = _ssbKeys2.default.box(keys.private, [{ | ||
id: ownerId, | ||
public: ownerId.slice(1), | ||
curve: "ed25519" | ||
}]); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
return { boxId: keys.id, box: box }; | ||
}, | ||
addKey: function addKey(box, me, ownerId) { | ||
var boxMasterPrivateKey = _ssbKeys2.default.unbox(box.keys[me.id], me); | ||
return _ssbKeys2.default.box(boxMasterPrivateKey, [{ | ||
addKey(state, boxId, ownerId) { | ||
const boxMasterPrivateKey = this.getPrivate(state, boxId); | ||
return _ssbKeys.default.box(boxMasterPrivateKey, [{ | ||
id: ownerId, | ||
@@ -105,5 +114,6 @@ public: ownerId.slice(1), | ||
} | ||
}; | ||
exports.default = Box; | ||
var _default = Box; | ||
exports.default = _default; | ||
//# sourceMappingURL=box.js.map |
@@ -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 |
@@ -10,6 +10,4 @@ "use strict"; | ||
var _jsonStableStringify = require("json-stable-stringify"); | ||
var _jsonStableStringify = _interopRequireDefault(require("json-stable-stringify")); | ||
var _jsonStableStringify2 = _interopRequireDefault(_jsonStableStringify); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -21,8 +19,11 @@ | ||
} | ||
return (0, _ssbKeys.hash)((0, _jsonStableStringify2.default)(game)) | ||
// switch up the two url-unsafe characters in base64 | ||
.replace(/\+/g, "-").replace(/\//g, "~") | ||
// and putting 256 bygit tes in base64 means there's always 1 trailing =. lose it. | ||
if (game.game) { | ||
game = game.game; | ||
} | ||
return (0, _ssbKeys.hash)((0, _jsonStableStringify.default)(game)) // switch up the two url-unsafe characters in base64 | ||
.replace(/\+/g, "-").replace(/\//g, "~") // and putting 256 bygit tes in base64 means there's always 1 trailing =. lose it. | ||
.replace(/=/g, ""); | ||
} | ||
//# sourceMappingURL=hash-state.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,15 @@ }); | ||
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 +74,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 +84,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,10 +98,11 @@ }); | ||
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]; | ||
} | ||
@@ -60,21 +112,5 @@ }); | ||
var _box = require("./box"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
Object.defineProperty(exports, "Box", { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_box).default; | ||
} | ||
}); | ||
var _hashState = require("./hash-state"); | ||
Object.defineProperty(exports, "hashState", { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_hashState).default; | ||
} | ||
}); | ||
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,33 +6,19 @@ "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("@streamplace/ssb-keys"); | ||
var _ssbKeys2 = _interopRequireDefault(_ssbKeys); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _shuffle(arr) { | ||
var func = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Math.random; | ||
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]; | ||
}); | ||
// 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]); | ||
} | ||
exports.shuffle = _shuffle; | ||
var RandomUtil = exports.RandomUtil = function () { | ||
function RandomUtil(seed) { | ||
_classCallCheck(this, RandomUtil); | ||
class RandomUtil { | ||
constructor(seed) { | ||
if (seed) { | ||
@@ -43,46 +29,45 @@ this.setSeed(seed); | ||
_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"); | ||
setSeed(seed) { | ||
// todo: broken and insecure | ||
if (typeof seed === "string") { | ||
let seedInt = 0; | ||
for (let i = 0; i < seed.length; i += 1) { | ||
seedInt = (seedInt + seed.charCodeAt(i)) % 2147483647; | ||
} | ||
this.seed = seed % 2147483647; | ||
if (this.seed <= 0) this.seed += 2147483646; | ||
seed = seedInt; | ||
} else if (typeof seed !== "number") { | ||
throw new Error("seed must be a string or a number"); | ||
} | ||
}, { | ||
key: "shuffle", | ||
value: function shuffle(arr) { | ||
return _shuffle(arr, this.next.bind(this)); | ||
this.seed = seed % 2147483647; | ||
if (this.seed <= 0) this.seed += 2147483646; | ||
} | ||
shuffle(arr) { | ||
return shuffle(arr, this.next.bind(this)); | ||
} | ||
next() { | ||
if (!this.seed) { | ||
throw new Error("tried to use RNG without providing a seed!"); | ||
} | ||
}, { | ||
key: "next", | ||
value: function next() { | ||
if (!this.seed) { | ||
throw new Error("tried to use RNG without providing a seed!"); | ||
} | ||
return this.seed = this.seed * 16807 % 2147483647; | ||
} | ||
}, { | ||
key: "nextFloat", | ||
value: function nextFloat() { | ||
return (this.next() - 1) / 2147483646; | ||
} | ||
}, { | ||
key: "clearSeed", | ||
value: function clearSeed() { | ||
this.seed = null; | ||
} | ||
}]); | ||
return RandomUtil; | ||
}(); | ||
return this.seed = this.seed * 16807 % 2147483647; | ||
} | ||
var rando = exports.rando = new RandomUtil(); | ||
nextFloat() { | ||
return (this.next() - 1) / 2147483646; | ||
} | ||
clearSeed() { | ||
this.seed = null; | ||
} | ||
} | ||
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,41 @@ 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) { | ||
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 +112,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 +136,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 |
@@ -6,38 +6,34 @@ "use strict"; | ||
}); | ||
exports.uid = exports.traverseSecret = exports.getRightPlayer = exports.getLeftPlayer = exports.rotateArray = undefined; | ||
exports.range = range; | ||
exports.uid = exports.traverseSecret = exports.getRightPlayer = exports.getLeftPlayer = exports.rotateArray = void 0; | ||
var _randomUtil = require("./random-util"); | ||
var _ssbKeys = require("@streamplace/ssb-keys"); | ||
var _ssbKeys = _interopRequireDefault(require("@streamplace/ssb-keys")); | ||
var _ssbKeys2 = _interopRequireDefault(_ssbKeys); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
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); } } | ||
/** | ||
* 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) { | ||
@@ -47,40 +43,59 @@ idx = 0; | ||
} | ||
return output; | ||
}; | ||
/** | ||
* Get the player to the left of this one | ||
*/ | ||
var getLeftPlayer = exports.getLeftPlayer = function getLeftPlayer(playerId, players) { | ||
var index = players.indexOf(playerId); | ||
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 | ||
*/ | ||
var getRightPlayer = exports.getRightPlayer = function getRightPlayer(playerId, players) { | ||
var index = players.indexOf(playerId); | ||
exports.getLeftPlayer = getLeftPlayer; | ||
const getRightPlayer = (playerId, players) => { | ||
const index = players.indexOf(playerId); | ||
if (index === players.length - 1) { | ||
return players[0]; | ||
} | ||
return players[index + 1]; | ||
}; | ||
var traverseSecret = exports.traverseSecret = function traverseSecret(secret, secrets) { | ||
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; | ||
}; | ||
var uid = exports.uid = function uid() { | ||
return "id-" + _randomUtil.rando.next(); | ||
exports.traverseSecret = traverseSecret; | ||
const uid = () => { | ||
return `id-${_randomUtil.rando.next()}`; | ||
}; | ||
exports.uid = uid; | ||
//# sourceMappingURL=util.js.map |
{ | ||
"name": "@cardcore/util", | ||
"version": "0.0.1-dc755b0c", | ||
"version": "0.0.1-dd07e215", | ||
"description": "various streamcards utilities", | ||
@@ -14,6 +14,7 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@streamplace/ssb-keys": "^7.0.16", | ||
"@streamplace/ssb-keys": "7.0.21", | ||
"isomorphic-fetch": "^2.2.1", | ||
"json-stable-stringify": "^1.0.1" | ||
}, | ||
"gitHead": "dc755b0c606b39d50628875d503dad70e9c8ad6d" | ||
"gitHead": "dd07e2158a438e97578b5a86f5e1de940e837c96" | ||
} |
import ssbKeys from "@streamplace/ssb-keys"; | ||
const boxCache = {}; | ||
const Box = { | ||
open(boxId, box, me) { | ||
open(state, boxId) { | ||
const box = state.game.boxes[boxId]; | ||
let privateKey; | ||
if (box.privateKey) { | ||
privateKey = box.privateKey; | ||
} else if (box.keys[me.id]) { | ||
privateKey = Box.getPrivate(box, me); | ||
} else if ( | ||
state.client && | ||
state.client.keys && | ||
box.keys[state.client.keys.id] | ||
) { | ||
privateKey = Box.getPrivate(state, boxId); | ||
} | ||
@@ -21,11 +28,28 @@ if (!privateKey) { | ||
getPrivate(box, me) { | ||
getPrivate(state, boxId) { | ||
const box = state.game.boxes[boxId]; | ||
const me = state.client.keys; | ||
return ssbKeys.unbox(box.keys[me.id], me); | ||
}, | ||
traverse(boxId, boxes, me) { | ||
if (!boxes[boxId]) { | ||
traverse(state, boxId) { | ||
if (typeof state === "string") { | ||
throw new Error("deprecated call to Box.traverse"); | ||
} | ||
if (boxCache[boxId]) { | ||
return boxCache[boxId]; | ||
} | ||
const result = this._traverse(state, boxId); | ||
if (result) { | ||
boxCache[boxId] = result; | ||
} | ||
return result; | ||
}, | ||
_traverse(state, boxId) { | ||
if (!state.game.boxes[boxId]) { | ||
return boxId; // idk maybe a unitId or something | ||
} | ||
const boxContents = Box.open(boxId, boxes[boxId], me); | ||
const boxContents = Box.open(state, boxId); | ||
if (!boxContents) { | ||
@@ -35,5 +59,5 @@ // dang, couldn't open it. done! | ||
} | ||
if (boxes[boxContents]) { | ||
if (state.game.boxes[boxContents]) { | ||
// hey, this box had a box in it! keep going! | ||
return Box.traverse(boxContents, boxes, me); | ||
return Box._traverse(state, boxContents); | ||
} | ||
@@ -63,4 +87,4 @@ // got something that wasn't a box — we're done! | ||
addKey(box, me, ownerId) { | ||
const boxMasterPrivateKey = ssbKeys.unbox(box.keys[me.id], me); | ||
addKey(state, boxId, ownerId) { | ||
const boxMasterPrivateKey = this.getPrivate(state, boxId); | ||
return ssbKeys.box(boxMasterPrivateKey, [ | ||
@@ -67,0 +91,0 @@ { |
@@ -0,1 +1,3 @@ | ||
export const REMOTE_ACTION = "__REMOTE_ACTION"; | ||
export const LOCATION_FIELD = "field"; | ||
@@ -2,0 +4,0 @@ export const LOCATION_DECK = "deck"; |
@@ -8,2 +8,5 @@ import { hash } from "@streamplace/ssb-keys"; | ||
} | ||
if (game.game) { | ||
game = game.game; | ||
} | ||
return ( | ||
@@ -10,0 +13,0 @@ hash(stringify(game)) |
@@ -0,6 +1,11 @@ | ||
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 "./next-actions"; | ||
export * from "./random-util"; | ||
export * from "./target-helper"; | ||
export * from "./random-util"; | ||
export * from "./util"; | ||
export * from "./constants"; | ||
export { default as Box } from "./box"; | ||
export { default as hashState } from "./hash-state"; |
@@ -6,2 +6,4 @@ // very simple PRNG from here https://gist.github.com/blixt/f17b47c62508be59987b | ||
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()); | ||
@@ -21,2 +23,3 @@ return Object.keys(arr) | ||
setSeed(seed) { | ||
// todo: broken and insecure | ||
if (typeof seed === "string") { | ||
@@ -23,0 +26,0 @@ let seedInt = 0; |
@@ -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,18 @@ } | ||
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) { | ||
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 +114,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 +119,0 @@ }) |
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
59970
31
989
3
+ Addedisomorphic-fetch@^2.2.1
+ Addedencoding@0.1.13(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedisomorphic-fetch@2.2.1(transitive)
+ Addednode-fetch@1.7.3(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedwhatwg-fetch@3.6.20(transitive)
Updated@streamplace/ssb-keys@7.0.21