• Stars
    star
    611
  • Rank 73,048 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 7 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

💰 A Javascript framework for creating idle games

💰 Merchant.js 💰

Merchant is a system for creating programs that manage changing numbers over time. It's especially useful for creating idle games (sometimes called incremental games), but could potentially be used in other games or programs.

If you're not sure what an idle game is, click here to find out. Note: If you're trying to be productive right now, that link may not be for you.

Merchant works well with Redux, but doesn't require it. Functions return copies and don't modify state. Since Merchant is built on immutable, most of it's key concepts are stored as immutable objects.

To be frank, Merchant is really just a collection of patterns with some helpful functions. It's fairly minimalistic but also gives you a good amount of power without sacrificing efficiency.

Docs here.

Live example here. 🐶

Installation

With yarn:

yarn add merchant.js

With npm:

npm install --save merchant.js

Concepts

Currencies

A currency is a string label for some number in the system. You can define these yourself like you normally would:

const GOLD = "GOLD";
const SILVER = "SILVER";
const MAGIC_POWER = "MAGIC_POWER";

Ledgers

A Ledger in Merchant is an immutable Map object where the keys are currencies and the values are numbers.

const { Map } = require("immutable");
const ledger = Map({ GOLD: 5, SILVER: 10 });

Ledger values can be either positive or negative.

const ledger = Map({ GOLD: -5, SILVER: 10 });

Wallet

A Wallet is a special ledger that keeps your main state. It's generally computed by adding several ledgers together.

You can generally think of all other ledgers as "updates" to your wallet.

Items

An item is something that can be bought and can effect the main ledger. They're generally JSON defined in a pouch file:

// pouch.js
import { GOLD, POWER } from "./currencies";

export const MagicSword = {
  type: "MagicSword",
  cost: () => {
    return Map({ [GOLD]: -5 });
  }
  effect: (state) => {
    return Map({ [POWER]: state.currentPowerLevel });
  }
};

An item must have a type attribute. It's useful to give it the same name as the item itself, but not required.

An item can have a cost function that returns a ledger. This is used by the buy function to determine the cost. Note that value should be negative if you want to subtract from your wallet.

An item can have a effect function that's used by the effects function to generate an effects ledger.

Both of these functions can take in the "state" if you would like. If you're using redux, you can treat these like mini reducers. This is pretty useful if you want to make the cost variable over time or a function of how many items you own.

To pass the state in, you can just throw it in the merchant functions:

const newWallet = buy(MagicSword, wallet, state);
const newLedger = effects(pouch, wallet, state);

Also note that there's no "amount" or "count" attribute in here, nor is this a class with a constructor. Items should not be instantiated. They should not store or contain state. They're blueprints, not objects.

The "amount" of the item can be stored in the wallet. So I can have a wallet that looks like this:

const wallet = Map({
  GOLD: 5,
  MagicSword: 2
});

We do this so that calculating a per-tick case without having to run through 10k "cost" functions.

Helpful Merchant Functions

Summing Ledgers Together

With Merchant, you can sum an arbitrary number of ledgers together.

import { sum } from "merchant.js";

// ...

const ledger = sum(ledgerOne, ledgerTwo, ledgerThree);

Scaling Ledgers

If you would like to multiply all currencies by a number, you can use the scale function:

import { scale } from "merchant.js";

const ledger = Map({ GOLD: 2, SILVER: -10 });
const scaledLedger = scale(ledger, 2);

scaledLedger.get(GOLD); // 4
scaledLedger.get(SILVER); // -20

Checking all values are positive

You can check if all currencies in a ledger are positive with the inTheBlack function:

import { inTheBlack } from "merchant.js";

inTheBlack(Map({ GOLD: 2, SILVER: -10 })); // False
inTheBlack(Map({ GOLD: 2, SILVER: 10 })); // True

Getting Unique Currencies

If you would like the currencies defined in an arbitrary collection of ledgers, you can use the currencies function:

import { currencies } from "merchant.js";

currencies(ledgerOne, ledgerTwo, ledgerThree); // ["GOLD", "SILVER", "MAGIC_POWER"]

Buying items

You can charge the wallet with the cost of an item with the buy function:

import { buy } from "merchant.js";

const newWallet = buy(MagicSword, wallet);

Note that you can also pass in your total state too.

import { buy } from "merchant.js";

const newWallet = buy(MagicSword, wallet, state);

Made with ❤️ by @flaqueeau.

More Repositories

1

quirk

✨🐙 A GPL Licensed Cognitive Behavioral Therapy app for iOS and Android. Currently a teaching tool
TypeScript
2,194
star
2

filet

🍖 A small temporary file utility for Go testing.
Go
74
star
3

penguin

🐧 A tool for recoloring and exporting SVG icons.
JavaScript
55
star
4

thaum

Thaum is a simple boilerplate and scaffolding command line utility.
Go
23
star
5

data-structures

JavaScript
16
star
6

freak

A panic / mood tracker 😱
Go
12
star
7

mark-dom

A query API for markdown
JavaScript
11
star
8

vx-tut

JavaScript
7
star
9

react-mutate

Add extensions to a React application!
JavaScript
7
star
10

boop

A scripting language written in golang
Go
6
star
11

rain

Implementation of Ben Porter's Rain
Go
6
star
12

notenv

Friends don't let friends use .env
TypeScript
5
star
13

metapod

An experimental ActivityPub based podcasting platform
Go
4
star
14

use-timeout

A react-hook for accessing setTimeout without constantly resetting.
TypeScript
4
star
15

use-slate-lifecycle

Add "onUserStartsTyping" and "onUserStopsTyping" events to your slate-js editor
TypeScript
4
star
16

github-paths

Generate Github urls for creating issues and editing/viewing source files
JavaScript
3
star
17

Sposure

A portable exposure therapy game.
Swift
3
star
18

script-kitty

JavaScript
3
star
19

phone-your-rep

CSS
2
star
20

copen

A fuzzy opener for visual studio code built on autojump
Shell
2
star
21

bluesquares

Dart
2
star
22

tiny-next-project

JavaScript
2
star
23

api.lintrule.com

TypeScript
2
star
24

sea-floor

JavaScript
1
star
25

aurora-demo-site

HTML
1
star
26

wipe

A small bash script that safely resets everything on your branch to what's on remote
Shell
1
star
27

koby-express

JavaScript
1
star
28

gu-port

CSS
1
star
29

blog

JavaScript
1
star
30

send-me-that

JavaScript
1
star
31

wail

A go tool for streaming stdin to a websocket 😱
Go
1
star
32

wikiracer

Go
1
star
33

pre

A cli that adds text to the beginning of files
Go
1
star
34

xss-example

JavaScript
1
star
35

legislation.fyi

🗳A state-based legislative tracker
JavaScript
1
star
36

one-click-hugo-cms

CSS
1
star
37

jtwittercrawler

Automatically exported from code.google.com/p/jtwittercrawler
Java
1
star
38

change-me

JavaScript
1
star
39

islands

Go
1
star
40

aurora-research

Python
1
star
41

handshakers

JavaScript
1
star
42

useslib

A Cyrstal macro for testing if a user has a C dep installed
Crystal
1
star
43

use-toggle

Like useState, but for toggling something on/off.
TypeScript
1
star
44

slate-with-placeholders

Add a slatejs placeholder to any block
TypeScript
1
star
45

keystore

A dead-simple key-value store gated by an EIP-191 signed message
1
star
46

gtn-express

JavaScript
1
star
47

block

A command line tool for blocking websites
Crystal
1
star
48

studyforcitizenship.com

TypeScript
1
star
49

stop-if

An assert statement that turns off in production
JavaScript
1
star
50

hobkin

🧚 An experimental static blog generator for MDX
JavaScript
1
star
51

hellogonzaga

a demo
HTML
1
star
52

powerx-demo

JavaScript
1
star
53

kobylearnstabs

JavaScript
1
star
54

gtn-example

JavaScript
1
star
55

lilia-calc

TypeScript
1
star
56

keyboardsmash

JavaScript
1
star
57

blocker

Swift
1
star
58

Flaque

1
star