@cardcore/util
Advanced tools
Comparing version 0.0.1-2bfe84b6 to 0.0.1-494be1c5
@@ -13,9 +13,12 @@ "use strict"; | ||
var boxCache = {}; | ||
var Box = { | ||
open: function open(boxId, box, me) { | ||
open: function open(state, boxId) { | ||
var box = state.game.boxes[boxId]; | ||
var privateKey = void 0; | ||
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); | ||
} | ||
@@ -31,10 +34,26 @@ if (!privateKey) { | ||
}, | ||
getPrivate: function getPrivate(box, me) { | ||
getPrivate: function getPrivate(state, boxId) { | ||
var box = state.game.boxes[boxId]; | ||
var me = state.client.keys; | ||
return _ssbKeys2.default.unbox(box.keys[me.id], me); | ||
}, | ||
traverse: function traverse(boxId, boxes, me) { | ||
if (!boxes[boxId]) { | ||
traverse: function traverse(state, boxId) { | ||
if (typeof state === "string") { | ||
throw new Error("deprecated call to Box.traverse"); | ||
} | ||
if (boxCache[boxId]) { | ||
return boxCache[boxId]; | ||
} | ||
var result = this._traverse(state, boxId); | ||
if (result) { | ||
boxCache[boxId] = result; | ||
} | ||
return result; | ||
}, | ||
_traverse: function _traverse(state, boxId) { | ||
if (!state.game.boxes[boxId]) { | ||
return boxId; // idk maybe a unitId or something | ||
} | ||
var boxContents = Box.open(boxId, boxes[boxId], me); | ||
var boxContents = Box.open(state, boxId); | ||
if (!boxContents) { | ||
@@ -44,5 +63,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); | ||
} | ||
@@ -97,4 +116,4 @@ // got something that wasn't a box — we're done! | ||
}, | ||
addKey: function addKey(box, me, ownerId) { | ||
var boxMasterPrivateKey = _ssbKeys2.default.unbox(box.keys[me.id], me); | ||
addKey: function addKey(state, boxId, ownerId) { | ||
var boxMasterPrivateKey = this.getPrivate(state, boxId); | ||
return _ssbKeys2.default.box(boxMasterPrivateKey, [{ | ||
@@ -101,0 +120,0 @@ id: ownerId, |
@@ -16,2 +16,8 @@ "use strict"; | ||
var _box = require("./box"); | ||
var _box2 = _interopRequireDefault(_box); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var noop = function noop(x) { | ||
@@ -25,3 +31,3 @@ return x; | ||
* @export | ||
* @param {object} state | ||
* @param {object} game | ||
* @param {object} target | ||
@@ -37,8 +43,10 @@ * @property {string} target.player | ||
var hasFullState = !!state.game; | ||
var game = hasFullState ? state.game : state; | ||
var units = {}; | ||
var 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(function (playerId) { | ||
return game.players[playerId].field.includes(target.unitId); | ||
}); | ||
@@ -48,8 +56,8 @@ } else { | ||
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(function (p) { | ||
return p !== game.turn; | ||
}); | ||
@@ -69,3 +77,3 @@ } else { | ||
try { | ||
for (var _iterator = Object.entries(state.players)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
for (var _iterator = Object.entries(game.players)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _ref = _step.value; | ||
@@ -82,3 +90,3 @@ | ||
if (types.includes(_constants.TYPE_FACE)) { | ||
units[player.unitId] = state.units[player.unitId]; | ||
units[player.unitId] = game.units[player.unitId]; | ||
_owners[player.unitId] = playerId; | ||
@@ -102,5 +110,17 @@ } | ||
for (var _iterator3 = player[location][Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var unitId = _step3.value; | ||
var boxId = _step3.value; | ||
units[unitId] = state.units[unitId]; | ||
if (!hasFullState) { | ||
throw new Error("Deprecated call to target(), need full state"); | ||
} | ||
var unitId = _box2.default.traverse(state, boxId); | ||
if (!unitId) { | ||
console.warn("Tried to target something I can't decrypt, " + JSON.stringify({ | ||
location: location, | ||
boxId: boxId, | ||
playerId: playerId | ||
})); | ||
continue; | ||
} | ||
units[unitId] = game.units[unitId]; | ||
_owners[unitId] = playerId; | ||
@@ -192,4 +212,4 @@ } | ||
function targetArray(state, action) { | ||
return Object.values(target(state, action, function (unit, details) { | ||
function targetArray(game, action) { | ||
return Object.values(target(game, action, function (unit, details) { | ||
return Object.assign({}, details, { unit: unit }); | ||
@@ -196,0 +216,0 @@ })); |
{ | ||
"name": "@cardcore/util", | ||
"version": "0.0.1-2bfe84b6", | ||
"version": "0.0.1-494be1c5", | ||
"description": "various streamcards utilities", | ||
@@ -14,6 +14,7 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@streamplace/ssb-keys": "7.0.18", | ||
"@streamplace/ssb-keys": "7.0.21", | ||
"isomorphic-fetch": "^2.2.1", | ||
"json-stable-stringify": "^1.0.1" | ||
}, | ||
"gitHead": "2bfe84b63d6352d5a91c2ba8a0e0afc41e6798e5" | ||
"gitHead": "494be1c5f4d77a18b99e08e6b1f0d840a3d1a7c0" | ||
} |
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 @@ { |
@@ -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,8 +29,10 @@ * @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.includes(target.unitId); | ||
}); | ||
@@ -39,7 +42,7 @@ } else { | ||
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 { | ||
@@ -53,3 +56,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)) { | ||
@@ -59,3 +62,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; | ||
@@ -67,4 +70,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; | ||
@@ -93,5 +110,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 }; | ||
@@ -98,0 +115,0 @@ }) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49488
855
3
+ Addedisomorphic-fetch@^2.2.1
+ Added@streamplace/chloride@2.2.12(transitive)
+ Added@streamplace/private-box@0.2.1(transitive)
+ Added@streamplace/ssb-keys@7.0.21(transitive)
+ 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)
- Removed@streamplace/chloride@2.2.11(transitive)
- Removed@streamplace/ssb-keys@7.0.18(transitive)
- Removedchloride@2.4.1(transitive)
- Removedini@1.3.8(transitive)
- Removednan@2.22.0(transitive)
- Removednode-gyp-build@4.8.4(transitive)
- Removedprivate-box@0.2.1(transitive)
- Removedsodium-native@2.4.93.4.1(transitive)
Updated@streamplace/ssb-keys@7.0.21