Skip to content

Commit

Permalink
refactor(1kce): to use remote grainMaps and distribute powers
Browse files Browse the repository at this point in the history
  • Loading branch information
kumavis committed Nov 22, 2023
1 parent bb3dd7e commit 2ccdd4c
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 144 deletions.
15 changes: 5 additions & 10 deletions packages/cli/demo/1kce/game.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { E, Far } from '@endo/far';
import { makeIteratorRef } from '@endo/daemon/reader-ref.js';
import { makeSyncArrayGrain, makeSyncGrain, makeSyncGrainArrayMap, makeSyncGrainMap, makeDerivedSyncGrain, composeGrainsAsync, composeGrains } from '@endo/grain';
import { makeRemoteGrain } from '@endo/grain/captp.js';
import { makeRemoteGrain, makeRemoteGrainMap } from '@endo/grain/captp.js';

const playerRemoteToLocal = new Map()
class Player {
Expand All @@ -13,9 +13,6 @@ class Player {
async getName () {
return localPlayer.name
},
async followHand (canceled) {
return makeIteratorRef(localPlayer.hand.follow(canceled))
},
async getHandGrain () {
return makeRemoteGrain(localPlayer.hand)
},
Expand Down Expand Up @@ -254,11 +251,13 @@ export function makeGame () {
// game state, aggregated for remote subscribers
const state = makeSyncGrainMap({
log: logGrain,
currentPlayer: currentPlayerIndex,
currentPlayerIndex,
currentPlayer: currentRemotePlayer,
currentTurnPhase: currentTurnPhaseName,
locations,
scores: scoresGrain,
deck: deckGrain,
players: remotePlayers,
})
const followState = (canceled) => {
return state.follow(canceled)
Expand Down Expand Up @@ -305,7 +304,7 @@ export const make = (powers) => {
return makeIteratorRef(game.followState(canceled))
},
async getStateGrain () {
return makeRemoteGrain(game.state)
return makeRemoteGrainMap(game.state)
},

async followCurrentPlayer (canceled) {
Expand All @@ -319,10 +318,6 @@ export const make = (powers) => {
return makeIteratorRef(game.getCardsAtLocation(location).follow(canceled))
},

async followCardsAtPlayerLocation (remotePlayer, canceled) {
const { name } = playerRemoteToLocal.get(remotePlayer)
return makeIteratorRef(game.getCardsAtLocation(name).follow(canceled))
},
async getCardsAtPlayerLocationGrain (remotePlayer) {
const { name } = playerRemoteToLocal.get(remotePlayer)
return makeRemoteGrain(game.getCardsAtLocation(name))
Expand Down
102 changes: 17 additions & 85 deletions packages/cli/demo/1kce/ui/app.js
Original file line number Diff line number Diff line change
@@ -1,110 +1,42 @@
import React from 'react';
import { E } from '@endo/far';
import { makeRefIterator } from '@endo/daemon/ref-reader.js';
import { makeReadonlyArrayGrainFromRemote } from '@endo/grain/captp.js';
import { makeReadonlyGrainMapFromRemote } from '@endo/grain/captp.js';
import { h } from './util.js';
import { DeckManagerComponent, PlayGameComponent } from './game.js';

// no way of resolving relative paths from the weblet
const projectRootPath = './demo/1kce';

const makeThing = async (powers, importFullPath, resultName) => {
const workerName = 'MAIN';
const powersName = 'NONE';
const deck = await E(powers).importUnsafeAndEndow(
workerName,
importFullPath,
powersName,
resultName,
);
return deck
}

const makeNewDeck = async (powers) => {
const importFullPath = `${projectRootPath}/deck.js`;
const resultName = 'deck';
return await makeThing(powers, importFullPath, resultName)
}

const makeGame = async (powers) => {
const importFullPath = `${projectRootPath}/game.js`;
const resultName = 'game';
return await makeThing(powers, importFullPath, resultName)
}

export const App = ({ powers }) => {
export const App = ({ inventory }) => {
const [deck, setDeck] = React.useState(undefined);
const [game, setGame] = React.useState(undefined);
const [{ game, stateGrain }, setGame] = React.useState({});

const actions = {
const deckMgmt = {
// deck mgmt
async fetchDeck () {
// workaround for https://github.com/endojs/endo/issues/1843
if (await E(powers).has('deck')) {
const deck = await E(powers).lookup('deck')
// has-check is workaround for https://github.com/endojs/endo/issues/1843
if (await inventory.has('deck')) {
const deck = await inventory.lookup('deck')
setDeck(deck)
}
},
async makeNewDeck () {
const deck = await makeNewDeck(powers)
const deck = await inventory.makeNewDeck()
setDeck(deck)
},
async addCardToDeck (card) {
await E(deck).add(card);
},
async addCardToDeckByName (cardName) {
const card = await E(powers).lookup(cardName)
const card = await inventory.lookup(cardName)
await E(deck).add(card);
},
async reverseLookupCard (card) {
return await E(powers).reverseLookup(card)
},
async getCardDetails (card) {
return await E(card).getDetails()
},
async getCardRenderer (card) {
let renderer
try {
const code = await E(card).getRendererCode()
const compartment = new Compartment({ Math, console })
const makeRenderer = compartment.evaluate(`(${code})`)
renderer = makeRenderer()
} catch (err) {
console.error(err)
// ignore missing or failed renderer
renderer = () => {}
}
return renderer
},

followCardsAtPlayerLocation (player, canceled) {
return makeRefIterator(E(game).followCardsAtPlayerLocation(player, canceled))
},
getCardsAtPlayerLocationGrain (player) {
const remoteGrain = E(game).getCardsAtPlayerLocationGrain(player)
return makeReadonlyArrayGrainFromRemote(remoteGrain)
},
followPlayerHand (player, canceled) {
return makeRefIterator(E(player).followHand(canceled))
},
getPlayerHandGrain (player) {
const remoteGrain = E(player).getHandGrain()
return makeReadonlyArrayGrainFromRemote(remoteGrain)
},

// inventory
subscribeToNames () {
return makeRefIterator(E(powers).followNames())
},
async removeName (name) {
await E(powers).remove(name)
},
}

// game
const gameMgmt = {
async start () {
// make game
const game = await makeGame(powers)
setGame(game)
const game = await inventory.makeGame()
const stateGrain = makeReadonlyGrainMapFromRemote(E(game).getStateGrain())
setGame({ game, stateGrain })
await E(game).start(deck)
},
async playCardFromHand (player, card, destinationPlayer) {
Expand All @@ -114,7 +46,7 @@ export const App = ({ powers }) => {

// on first render
React.useEffect(() => {
actions.fetchDeck()
deckMgmt.fetchDeck()
}, []);

return (
Expand All @@ -130,8 +62,8 @@ export const App = ({ powers }) => {
fontSize: '42px',
},
}, ['🃏1kce🃏']),
!game && h(DeckManagerComponent, { key: 'deck-manager', actions, deck }),
deck && h(PlayGameComponent, { key: 'play-game-component', actions, game }),
!game && h(DeckManagerComponent, { key: 'deck-manager', deck, deckMgmt, inventory }),
deck && h(PlayGameComponent, { key: 'play-game-component', game, stateGrain, gameMgmt }),
])
)
};
10 changes: 5 additions & 5 deletions packages/cli/demo/1kce/ui/endo.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ const useBrokenSubscriptionForArray = (getSubFn, deps) => {
return state;
}

export const ObjectsListObjectComponent = ({ actions, name }) => {
export const ObjectsListObjectComponent = ({ addAction, name }) => {
return (
h('div', {}, [
h('span', { key: 'name'}, [name]),
h('button', {
key: 'add',
onClick: async () => {
await actions.addCardToDeckByName(name)
await addAction(name)
},
style: {
margin: '6px',
Expand All @@ -63,9 +63,9 @@ export const ObjectsListObjectComponent = ({ actions, name }) => {
)
}

export const ObjectsListComponent = ({ actions }) => {
export const ObjectsListComponent = ({ inventory, addAction }) => {
const names = useBrokenSubscriptionForArray(
() => actions.subscribeToNames(),
() => inventory.subscribeToNames(),
[],
)

Expand All @@ -79,7 +79,7 @@ export const ObjectsListComponent = ({ actions }) => {
key: name,
}, [
h('span', null, [
h(ObjectsListObjectComponent, { actions, name }),
h(ObjectsListObjectComponent, { addAction, name }),
]),
])
})
Expand Down
Loading

0 comments on commit 2ccdd4c

Please sign in to comment.