Code Monkey home page Code Monkey logo

Comments (4)

kiltedken avatar kiltedken commented on September 20, 2024

Warning: I just started programming with tweego and sugarcube, so please double check all the things I wrote here.

I think the code needed <<capture>> around the <<link "Sell">> . I'm not sure it needed all these variables_price, _item, _amount, but I'm tired and will experiment later with that.

Also, the initializing <<pickup>>'s had some commas in them that I removed to get the list to work:

Original:

<<pickup $shop "coal" 10 "gold nugget" 2, "iron ingot" 5>>
<<pickup $storage "iron ingot" 1 "glass shard" 20>>
<<pickup $player "glass shard" 10 "pretty stone" 20, "gemstone" 5>>

Here's the code that worked for me. I'd appreciate any feedback you might have (like what variables <<capture>> needed). Thank you for all your work!


:: StoryTitle
Simple Inventory 3 - Shop

:: StoryData
{
    "format": "SugarCube",
    "format-version": "2.36.1",
    "ifid": "862E1FA6-35C6-4E10-BB86-D720812D1528"
}

:: Story JavaScript [script]
Config.ui.stowBarInitially = false;

:: Story Stylesheet [stylesheet]
.shopping-interface p {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
}

:: StoryInit
<<newinv $player>>
<<newinv $shop>>
<<newinv $storage>>

<<set $gold to 100>>

<<set setup.prices = new Map([
    ["gemstone", 35],
    ["coal", 2],
    ["iron ingot", 12],
    ["glass shard", 5],
    ["gold nugget", 50],
    ["pretty stone", 3]
])>>

<<pickup $shop "coal" 10 "gold nugget" 2 "iron ingot" 5>>
<<pickup $storage "iron ingot" 1 "glass shard" 20>>
<<pickup $player "glass shard" 10 "pretty stone" 20 "gemstone" 5>>

:: StoryCaption
Gold: @@#gold;<<= $gold>>@@

:: widgets [widget nobr]
<<widget "gold">>
    <<set $gold to Math.clamp($gold + _args[0], 0, Infinity)>>
    <<replace "#gold">><<= $gold>><</replace>>
<</widget>>

<<widget "buylink">>
    <<set _item to _args[0]>>
    <<set _amount to _args[1]>>
    <<set _price to setup.prices.get(_item) || 1>>
    <<set _price to -1 * _price>>
    <p @data-item="_item" class="buy-listing">
        <span>_item</span>
        <span class="count">_amount</span>
        <span class="buy-link">
            <<capture _price, _item, _amount>>
                <<link "Buy">>
                    <<if $gold < _price>>
                        <<run UI.alert("You don't have enough gold!")>>
                    <<else>>
                        <<gold _price>>
                        <<transfer $shop $player _item 1>>
                        <<set _amount -->>
                        <<if _amount > 0>>
                            <<replace `"p.buy-listing[data-item=\"" + _item + "\"] > .count"`>>_amount<</replace>>
                        <<else>>
                            <<remove `"p.buy-listing[data-item=\"" + _item + "\"]"`>>
                        <</if>>
                    <</if>>
                <</link>>
            <</capture>>
        </span>
    </p>
<</widget>>

<<widget "selllink">>
    <<set _item to _args[0]>>
    <<set _amount to _args[1]>>
    <<set _price to setup.prices.get(_item) || 1>>
    <p @data-item="_item" class="sell-listing">
        <span>_item</span>
        <span class="count">_amount</span>
        <span class="sell-link">
            <<capture _price, _item, _amount>>
                <<link "Sell">>
                    <<if $gold < _price>>
                        <<run UI.alert("You don't have enough gold!")>>
                    <<else>>
                        <<gold _price>>
                        <<transfer $player $shop _item 1>>
                        <<set _amount -->>
                        <<if _amount > 0>>
                            <<replace `"p.sell-listing[data-item=\"" + _item + "\"] > .count"`>>_amount<</replace>>
                        <<else>>
                            <<remove `"p.sell-listing[data-item=\"" + _item + "\"]"`>>
                        <</if>>
                    <</if>>
                <</link>>
            <</capture>>
        </span>
    </p>
<</widget>>

:: StoryMenu
<<link "Inventory">>
    <<run Dialog.setup("Inventory", "inventory"); Dialog.wiki(Story.get("Inventory").text); Dialog.open()>>
<</link>>

:: Inventory
<<inv $player>>

:: Start
You are at your house.

Next to you is your storage locker!

[[Open storage locker|Storage]]
[[Go to the market|Market]]

:: Storage
<h2>On hand:</h2>
<<give $player $storage stack all>>
<h2>In storage:</h2>
<<take $storage $player stack all>>

[[Go back|Start]]

:: Market
You arrive at the market!

[[Buy Items|Buy]]
[[Sell Items|Sell]]
[[Go home|Start]]

:: Buy [nobr]
<h2>Items for sale:</h2>
<div class="shopping-interface">
<<for _item, _amount range $shop.table>>
    <<capture _item _amount>>
        <<buylink _item _amount>>
    <</capture>>
<</for>>
</div>
<br><br>
[[Go back|Market]]

:: Sell [nobr]
<h2>Items on hand:</h2>
<div class="shopping-interface">
<<for _item, _amount range $player.table>>
    <<capture _item _amount>>
        <<selllink _item _amount>>
    <</capture>>
<</for>>
</div>
<br><br>
[[Go back|Market]]

from simple-inventory.

ChapelR avatar ChapelR commented on September 20, 2024

There's already a capture for these links in the for loop, I'm not sure why it's not working. I'll look into this when I have time.

from simple-inventory.

clvanwinkle83 avatar clvanwinkle83 commented on September 20, 2024

Can I check if this issue has been looked at? I an not a coder really but from what I can tell there seems to be an issue with the the widgets buylink's if else logic. I was also curious if there is an easy way to display the gold value of the item in the buy and sell windows interface. I did figure out how to have the number display by changing the storystyle to have another columns and added another in both the buy and sell widgets. I am also trying to figure out how to make a header for the columns in the buy sell interfaces.

If needed I can provide my code later.

from simple-inventory.

Nerehim avatar Nerehim commented on September 20, 2024

Heya! @clvanwinkle83
I'm no dev but I found a fix for this.

I started by displaying the _price value both from the widget and from the store Passage. Turns out, the price in the Passage was the right one, but not in the widget. It wasn't being captured properly, as in the original code only the _item and _amount were included in the <>.

<h2>Items for sale:</h2>
<div class="shopping-interface">
<<for _item, _amount range $shop.table>>
    <<capture _item _amount _price>>
        <<buylink _item _amount>>
    <</capture>>
<</for>>
</div>

I hope it helps!

from simple-inventory.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.