Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1kce demo - daemon persistence #1867

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/cli/demo/1kce/cards/deja-vu.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { E, Far } from '@endo/far'
export const make = () => {
return Far('deja vu', {
async play (gameController) {
const cards = await E(gameController).getDeckCards()
await E(gameController).addCardsToDeck(cards)
// duplicate the cards in the draw stack, in the same order.
const cards = await E(gameController).getDrawStackCards()
const cardIds = cards.map(({ id }) => id)
await E(gameController).addCardsByIdToDrawStack(cardIds)
kumavis marked this conversation as resolved.
Show resolved Hide resolved
},
getDetails () {
return {
name: 'deja vu',
description: 'duplicate the cards in the deck, in the same order.\n\n-25 points',
description: 'duplicate the cards in the draw stack, in the same order.\n\n-25 points',
pointValue: -25,
}
},
Expand Down
21 changes: 11 additions & 10 deletions packages/cli/demo/1kce/cards/library-of-alexandria.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { E, Far } from '@endo/far'

export const make = () => {

return Far('library of alexandria', {
async play (controller) {
await E(controller).setScoreFn(Far('scoreFn container', {
scoreFn: async ({ cards }) => {
let score = 0
for (const card of cards) {
const { name } = await E(card).getDetails()
score += name.length * 10
}
return score
},
}))
// tell the game to call "scoreFunction" on this card when it wants to calculate scores
await E(controller).setScoreFn('scoreFunction')
kumavis marked this conversation as resolved.
Show resolved Hide resolved
},
async scoreFunction ({ cardsData }) {
let score = 0
for (const cardData of cardsData) {
const { name } = await E(cardData.remote).getDetails()
score += name.length * 10
}
return score
},
getDetails () {
return {
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/demo/1kce/cards/pokemon-outrage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { E, Far } from '@endo/far'
export const make = () => {
return Far('pokemon-outrage', {
async play (gameController) {
const cards = await E(gameController).getDeckCards()
await E(gameController).addCardsToDeck(cards)

},
getDetails () {
return {
Expand Down
33 changes: 31 additions & 2 deletions packages/cli/demo/1kce/deck.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
import { Far } from '@endo/far';
import { E, Far } from '@endo/far';
import { makeIteratorRef } from '@endo/daemon/reader-ref.js';
import { makeSyncArrayGrain } from '@endo/grain';
import { makeRemoteGrain } from '@endo/grain/captp.js';
import { makeRefIterator } from '@endo/daemon/ref-reader.js';

export const make = () => {
const cardPrefix = 'card-';

export const make = async (powers) => {
const cards = makeSyncArrayGrain();

// cards already saved in petstore
const loadExistingNames = async () => {
for await (const name of await E(powers).list()) {
if (!name.startsWith(cardPrefix)) continue
const indexString = name.slice(cardPrefix.length)
kumavis marked this conversation as resolved.
Show resolved Hide resolved
const card = await E(powers).lookup(name);
cards.setAtIndex(indexString, card);
}
}
// incomming cards
const listenForIncommingCards = async () => {
for await (const message of makeRefIterator(E(powers).followMessages())) {
if (message.type !== 'package') continue
const petName = `${cardPrefix}${cards.getLength()}`
await E(powers).adopt(message.number, 'card', petName);
const card = await E(powers).lookup(petName);
cards.push(card);
}
}

// ensure all cards are loaded before continuing
await loadExistingNames()
// listen for new cards, but dont await as it will never resolve
listenForIncommingCards()

return Far('Deck', {
add (card) {
cards.push(card);
Expand Down
Loading
Loading