GithubHelp home page GithubHelp logo

clojure-data-definitions's Introduction

Clojure Data Definitions

Object schemas can bridge the gap between programming and and custom configuration. Functional languages like Scheme or Clojure can be used to generate C header files ro byte code, respectively. When combine these two ideas, you unlock a great deal of cross-team flexibility. Artists and animators now have the confidence to change code and programmers have better tooling.

This small toy projects writes the meta of Magic the Gathering cards in Clojure and then converts these objects to java classes to work with.

In theory you would be able to hot reload these classes and swap the byte code in and out at run time, but I haven't figured that out yet.

Below is the process I described below :

Clojure Definitions

(ns bazaar.core)

(def cards {"Goblin King"       {:power     2
                                 :toughness 7
                                 :type      :creature
                                 :subtype   :goblin
                                 :ability   "Other goblin creatures get +1/+1 and have mountainwalk"
                                 :cost      {:white     0
                                             :blue      0
                                             :black     7
                                             :red       2
                                             :green     0
                                             :colorless 1}}
            "Karona, False God" {:power     5
                                 :toughness 5
                                 :type      :legendary-creature
                                 :keywords  [:haste]
                                 :ability   "At the beginning of each player's upkeep, that player untaps Karona, False God and gains control of it.\nWhenever Karona attacks, creatures of the type of your choice get +3/+3 until end of turn"
                                 :cost      {:white     1
                                             :blue      1
                                             :black     1
                                             :red       1
                                             :green     1
                                             :colorless 1}
                                 :set       :scourge}})

(defn card-names [] (keys cards))

Clojure -> Java

// imports ...

public class Main {
    public static void main(String[] args) throws JsonProcessingException {
        // setup
        var mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        mapper.registerModule(new Jdk8Module());
        IFn require = Clojure.var("clojure.core", "require");
        require.invoke(Clojure.read("bazaar.core"));

        // grab functions
        IFn fCards = Clojure.var("bazaar.core", "cards");
        IFn fCardNames = Clojure.var("bazaar.core", "card-names");

        // call functions + process data
        Object cardNames = fCardNames.invoke();
        if (cardNames instanceof Iterable) {
            var cardNameIterator = (Iterable) cardNames;

            for (var key : cardNameIterator) {
                if (key instanceof String) {
                    var name = (String) key;
                    var cardWithName = fCards.invoke(name);

                    if (cardWithName == null) throw new RuntimeException("No card with name: " + name);
                    if (!(cardWithName instanceof Map)) throw new RuntimeException("Card: " + name + " must be a map");

                    var card = (Map) cardWithName;

                    var power = Optional.of((Long) (card.get(Keyword.intern("power"))));
                    var toughness = Optional.of((Long) (card.get(Keyword.intern("toughness"))));
                    var cardType = Optional.ofNullable(CardType.from(card.get(Keyword.intern("type"))));
                    var subtype = Optional.ofNullable(CardSubtype.from(card.get(Keyword.intern("subtype"))));
                    var cost = Optional.ofNullable(CardCost.from(card.get(Keyword.intern("cost"))));
                    var ability = Optional.ofNullable(CardAbility.from(card.get(Keyword.intern("ability"))));
                    var keywords = Optional.ofNullable(CardKeyword.from(card.get(Keyword.intern("keywords"))));
                    var set = Optional.ofNullable(CardSet.from(card.get(Keyword.intern("set"))));

                    var constructedCard = new CardModel(
                            name,
                            power,
                            toughness,
                            cardType,
                            subtype,
                            cost,
                            ability,
                            keywords,
                            set
                    );

                    System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(constructedCard));
                }
            }
        }
    }
}

Printing Java Objects

{
  "name" : "Goblin King",
  "power" : 2,
  "toughness" : 7,
  "cardType" : "CREATURE",
  "cardSubType" : "GOBLIN",
  "cost" : {
    "white" : 0,
    "blue" : 0,
    "black" : 7,
    "red" : 2,
    "green" : 0,
    "colorless" : 1
  },
  "ability" : {
    "abilityText" : "Other goblin creatures get +1/+1 and have mountainwalk"
  },
  "keywords" : null,
  "set" : null
}
{
  "name" : "Karona, False God",
  "power" : 5,
  "toughness" : 5,
  "cardType" : "LEGENDARY_CREATURE",
  "cardSubType" : null,
  "cost" : {
    "white" : 1,
    "blue" : 1,
    "black" : 1,
    "red" : 1,
    "green" : 1,
    "colorless" : 1
  },
  "ability" : {
    "abilityText" : "At the beginning of each player's upkeep, that player untaps Karona, False God and gains control of it.\nWhenever Karona attacks, creatures of the type of your choice get +3/+3 until end of turn"
  },
  "keywords" : [ "HASTE" ],
  "set" : "SCOURGE"
}

clojure-data-definitions's People

Contributors

gearhartlove avatar

Watchers

 avatar

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.