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

Fix bug where same card is shown every time you forage #46

Merged
merged 2 commits into from
Jan 16, 2024
Merged
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
2 changes: 1 addition & 1 deletion client/ren_client/interfaces/spellcrafter_game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface ISpellcrafterGame {
// the game stats for this current game
stats: GameStats,
// The card indices that the player is holding
cards: Array<number>,
cards: Array<[number, number]>,
// card index of familiar (if present)
familiar: Familiar | null,

Expand Down
4 changes: 2 additions & 2 deletions client/ren_client/plugins/dojo_spellcrafter_game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { call, subscribe } from "esdeka";
export class DojoSpellcrafterGame implements ISpellcrafterGame {
_time: number;
_stats: GameStats;
_cards: number[];
_cards: Array<[number, number]>;
_familiar: Familiar | null;
host: Window;

Expand All @@ -21,7 +21,7 @@ export class DojoSpellcrafterGame implements ISpellcrafterGame {
return this._stats;
}

get cards(): number[] {
get cards(): Array<[number, number]> {
return this._cards;
}

Expand Down
6 changes: 3 additions & 3 deletions client/ren_client/plugins/local_spellcrafter_game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const TICKS_FOR_FAMILIAR_FORAGE = 2;
export class LocalSpellcrafterGame implements ISpellcrafterGame {
_time: number;
_stats: GameStats;
_cards: number[];
_cards: Array<[number, number]>;
_familiar: Familiar | null;

constructor() {
Expand All @@ -28,7 +28,7 @@ export class LocalSpellcrafterGame implements ISpellcrafterGame {
lightDark: 0,
barriers: 3,
};
this._cards = [1,2,3,4,5,6,7];
this._cards = [];
this._familiar = null;
}

Expand All @@ -40,7 +40,7 @@ export class LocalSpellcrafterGame implements ISpellcrafterGame {
return this._stats;
}

get cards(): number[] {
get cards(): Array<[number, number]> {
return this._cards;
}

Expand Down
65 changes: 42 additions & 23 deletions client/ren_client/plugins/spellcrafter_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { ISpellcrafterGame } from "../interfaces/spellcrafter_game";
import { DojoSpellcrafterGame } from "./dojo_spellcrafter_game";

export class SpellcrafterPlugin extends RenJS.Plugin {
// class SpellcrafterPlugin extends Plugin {
// class SpellcrafterPlugin extends Plugin {

spellcrafterGame: ISpellcrafterGame;
cardDisplayGroup;
setCard;
barrierImages: any = [];
host: Window;

onInit(): void {
const unsubscribe = subscribe("spellcrafter", event => {
console.log("Ren received initial state:", event.data.action.payload);
Expand Down Expand Up @@ -49,8 +49,8 @@ export class SpellcrafterPlugin extends RenJS.Plugin {
this.barrierImages.push(this.game.add.image(900, 50, "barrier"));
this.barrierImages.forEach(img => { this.game.gui.hud.add(img) });

this.syncState()
}
this._syncState()
}

onAction(action): void {
}
Expand All @@ -60,12 +60,12 @@ export class SpellcrafterPlugin extends RenJS.Plugin {
// call SpellCrafter: forage forest
// call SpellCrafter: interact 3
// call spellCrafter : showItemsChoice
onCall({body}): void {
onCall({ body }): void {
console.log("spellcrafter called with: ", body);
const [method, ...args] = body.split(" ");

const decodeCall = (method: string, ...args: any[]): Promise<void> => {
switch(method) {
switch (method) {
case "printDebug":
console.log(this.spellcrafterGame);
return Promise.resolve();
Expand All @@ -92,24 +92,24 @@ export class SpellcrafterPlugin extends RenJS.Plugin {
return Promise.resolve();
case "hideBarriers":
// this.barrierImages.forEach(img => { img.visible = false });
return Promise.resolve();
return Promise.resolve();
default:
throw new Error("invalid method: " + method);
}
}

decodeCall(method, ...args).then(() => {
this.syncState();
this._syncState();
}).catch(async (err) => {
console.error(err.message);
this.syncState();
this._syncState();
await this.game.managers.text.display(err.message, "default");
this.game.managers.story.startScene("manageActions");
// hang forever
}).finally(async () => {
this.game.resolveAction(); // must call this to return control to the story
});
}
}

/// Display the currently owned items and allow the player to select one
/// After this resolves the `chosenItem` variable will hold the index of the chosen item
Expand Down Expand Up @@ -155,16 +155,16 @@ export class SpellcrafterPlugin extends RenJS.Plugin {
selectedCardIndex = (selectedCardIndex - 1 + this.spellcrafterGame.cards.length) % this.spellcrafterGame.cards.length;
updateCardDisplay()
}, this, 0);
const rightButton = this.game.add.button(875+150, 845+150, "back-button", () => {
const rightButton = this.game.add.button(875 + 150, 845 + 150, "back-button", () => {
selectedCardIndex = (selectedCardIndex + 1) % this.spellcrafterGame.cards.length;
updateCardDisplay()
}, this, 0);
rightButton.rotation = Math.PI;

const addToSpellButton = this.game.add.button(40, 1025, "button", async () => {
hideCardChoser();
let pre_stats = {...this.spellcrafterGame.stats};
this.game.managers.logic.vars["lastAddedItemName"] = cards[this.spellcrafterGame.cards[selectedCardIndex][0]].name;
let pre_stats = { ...this.spellcrafterGame.stats };
this.game.managers.logic.vars["lastAddedItemName"] = cards[this.spellcrafterGame.cards[selectedCardIndex][0]].name;
await this.spellcrafterGame.interact(this.spellcrafterGame.cards[selectedCardIndex][0]);
this.game.managers.logic.vars["chaosDelta"] = this.spellcrafterGame.stats.chaos - pre_stats.chaos;
this.game.managers.logic.vars["powerDelta"] = this.spellcrafterGame.stats.power - pre_stats.power;
Expand All @@ -181,9 +181,11 @@ export class SpellcrafterPlugin extends RenJS.Plugin {
async forage(region: string): Promise<void> {
let pre_chaos = this.spellcrafterGame.stats.chaos;
let pre_barriers = this.spellcrafterGame.stats.barriers;
let pre_cards = this.spellcrafterGame.cards;

await this.spellcrafterGame.forage(region);

this._setLastForagedItem(findNewCard(pre_cards, this.spellcrafterGame.cards));
this.game.managers.logic.vars["chaosDelta"] = this.spellcrafterGame.stats.chaos - pre_chaos;
this.game.managers.logic.vars["barriersDelta"] = this.spellcrafterGame.stats.barriers - pre_barriers;
}
Expand All @@ -208,7 +210,9 @@ export class SpellcrafterPlugin extends RenJS.Plugin {

async claimFamiliarItem(): Promise<void> {
try {
let pre_cards = this.spellcrafterGame.cards;
await this.spellcrafterGame.claimFamiliarItem();
this._setLastForagedItem(findNewCard(pre_cards, this.spellcrafterGame.cards));
this.game.managers.logic.vars["familiarReturnedItem"] = true;
} catch (err) { // just print errors here since we know this can fail
console.log("familiar item check failed: ", err.message);
Expand All @@ -234,9 +238,16 @@ export class SpellcrafterPlugin extends RenJS.Plugin {
this.cardDisplayGroup.visible = false;
}

_setLastForagedItem(lastForagedItem: number | null): void {
this.game.managers.logic.vars["lastForagedItem"] = lastForagedItem ? cards[lastForagedItem].card_id : null
this.game.managers.logic.vars["lastForagedItemName"] = lastForagedItem ? cards[lastForagedItem].name : null
this.game.managers.logic.vars["lastForagedItemDescription"] = lastForagedItem ? cards[lastForagedItem].description : null
this.game.managers.logic.vars["lastForagedItemFlavour"] = lastForagedItem ? cards[lastForagedItem].flavour : null
}

/// copies variables from the game state object into the renjs context
/// so they can be displayed in-game and used to alter the story flow
syncState(): void {
_syncState(): void {
const stats = this.spellcrafterGame.stats;

this.game.managers.logic.vars["chaos"] = stats.chaos;
Expand All @@ -248,23 +259,31 @@ export class SpellcrafterPlugin extends RenJS.Plugin {
this.game.managers.logic.vars["time"] = this.spellcrafterGame.time;
this.game.managers.logic.vars["itemCount"] = this.spellcrafterGame.cards.length;

const lastForagedItem: number | null = this.spellcrafterGame.cards.length > 0 ? this.spellcrafterGame.cards[this.spellcrafterGame.cards.length - 1][0] : null;
this.game.managers.logic.vars["lastForagedItem"] = lastForagedItem ? cards[lastForagedItem].card_id : null
this.game.managers.logic.vars["lastForagedItemName"] = lastForagedItem ? cards[lastForagedItem].name : null
this.game.managers.logic.vars["lastForagedItemDescription"] = lastForagedItem ? cards[lastForagedItem].description : null
this.game.managers.logic.vars["lastForagedItemFlavour"] = lastForagedItem ? cards[lastForagedItem].flavour : null
if (this.spellcrafterGame.familiar) {
this.game.managers.logic.vars["familiar"] = this.spellcrafterGame.familiar.id;
this.game.managers.logic.vars["familiarName"] = this.spellcrafterGame.familiar.familiarType;
this.game.managers.logic.vars["familiarIdle"] = this.spellcrafterGame.familiar.busyUntil <= this.spellcrafterGame.time;
} else {
this.game.managers.logic.vars["familiar"] = null;
}
for(let i = 0; i < 3; i++) {
if (stats.barriers >= (i+1))
this.barrierImages[2-i].loadTexture("barrier");
for (let i = 0; i < 3; i++) {
if (stats.barriers >= (i + 1))
this.barrierImages[2 - i].loadTexture("barrier");
else
this.barrierImages[2-i].loadTexture("barrier-broken");
this.barrierImages[2 - i].loadTexture("barrier-broken");
}
}
}

function findNewCard(preCards: Array<[number, number]>, postCards: Array<[number, number]>): number | null {
const before = new Map();
preCards.forEach(([cardId, count]) => {
before.set(cardId, count);
})
for (let i = 0; i < postCards.length; i++) {
if(before.get(postCards[i][0]) == undefined || before.get(postCards[i][0]) < postCards[i][1]) {
return postCards[i][0];
}
}
return null;
}