Untitled

mail@pastecode.io avatar
unknown
plain_text
21 days ago
4.4 kB
1
Indexable
Never
#include <iostream>
#include <bits/stdc++.h>
#include <jsoncpp/json/json.h>

using namespace std;


/*
{
  "collectionName": "Cuddly Kitties",
  "collectionSize": 5,
  "traits": [{
    "name": "ears",
    "values": [
      "tiny",
      "big",
      "sad",
      "pointy",
      "SomethingRare"
      {tiny, 1}//rare
      {big, 10} // common
      {sad, 5}
      {pointy, 4}
    ]
  },{
    "name": "eyes",
    "values": [
      "sad",
      "bright",
      "happy",
      "angry"
    ]
  },{
    "name": "mouth",
    "values": [
      "angry",
      "sad",
      "open",
      "smiling"
    ]
  }]
}


[{
  "name": "Cuddly Kitties #0",
  "attributes": [{
    "trait_type": "ears",
    "value": "pointy"
  },{
    "trait_type": "eyes",
    "value": "happy"
  },{
    "trait_type": "mouth",
    "value": "open"
  }]
},{
  "name": "Cuddly Kitties #1",
  "attributes": [{
    "trait_type": "ears",
    "value": "big"
  },{
    "trait_type": "eyes",
    "value": "angry"
  },{
    "trait_type": "mouth",
    "value": "smiling"
  }]
}, {
  "name": "Cuddly Kitties #2",
  ...
}, {
  "name": "Cuddly Kitties #3",
  ...
}, {
  "name": "Cuddly Kitties #4",
  ...
}]
*/

struct TraitValue {
  string name;
  int rarity;
  TraitValue(const string& name, int rarity): name(name), rarity(rarity) {};
};

struct Trait {
  string name;
  vector<TraitValue*> values;
  vector<int> probabilisticModel;
  Trait(const string& name, vector<TraitValue*>& values) {
    this->name = name;
    this->values = values;
    for (TraitValue* value : values) {
      probabilisticModel.push_back(value->rarity + (probabilisticModel.empty() ? 0 : probabilisticModel.back()));
    }
  };
};

struct Output {
  string name;
  // trait_type, value;
  vector<pair<string, string>> attributes;
};

class TraitsGenerator {
  string collectionName;
  int collectionSize; 
  vector<Trait*> traits;
  
  Output* generate(int value) {
    Output* op = new Output;
    op->name = collectionName + "#" + to_string(value);
    for (auto trait : traits) {
      int randomValueForIndex = rand() % trait->probabilisticModel.back();
      int index = lower_bound(trait->probabilisticModel.begin(), trait->probabilisticModel.end(), randomValueForIndex) - trait->probabilisticModel.begin();
      op->attributes.push_back({ trait->name,  trait->values[index]->name});
    }
    
    return op;
  }
  
  string getHash(Output* output) {
    string hash = "";
    for (auto [type, value] : output->attributes) {
      hash += type + "#" + value + "#";
    }
    return hash;
  }

public:
  TraitsGenerator(const string& collectionName, int collectionSize, vector<Trait*>& traits): collectionName(collectionName), collectionSize(collectionSize), traits(traits) {};
  
  vector<Output*> generateUniques() {
    vector<Output*> ans;
    unordered_set<string> seen;
    int collectionsGenerated = 0;
    while(collectionsGenerated < collectionSize) {
      Output* output = generate(collectionsGenerated);
      string hash = getHash(output);
      if (!seen.contains(hash)) {
        ++collectionsGenerated;
        ans.push_back(output);
        seen.insert(hash);
      }
    }
    
    return ans;
  }
  
};



int main() {
  int numberOfOutputs = 5;
  TraitValue* earsTV1 = new TraitValue("tiny", 1);
  TraitValue* earsTV2 = new TraitValue("big", 2);
  TraitValue* earsTV3 = new TraitValue("pointy", 3);
  
  TraitValue* eyesTV1 = new TraitValue("sad", 1);
  TraitValue* eyesTV2 = new TraitValue("bright", 10);
  TraitValue* eyesTV3 = new TraitValue("yellow", 20);
  
  TraitValue* mouthTV1 = new TraitValue("angry", 5);
  TraitValue* mouthTV2 = new TraitValue("sad", 5);
  TraitValue* mouthTV3 = new TraitValue("happy", 5);
  
  
  
  vector<TraitValue*> earsValues({earsTV1, earsTV2, earsTV3});
  vector<TraitValue*> eyesValues({eyesTV1, eyesTV2, eyesTV3});
  vector<TraitValue*> mouthValues({mouthTV1, mouthTV2, mouthTV3});

  Trait* ears = new Trait("ears", earsValues);
  Trait* eyes = new Trait("eyes", eyesValues);
  Trait* mouths = new Trait("mouths", mouthValues);

  vector<Trait*> traits({ ears, eyes, mouths });
  TraitsGenerator traitsGenerator("Kitty", 5, traits);
  vector<Output*> outputs = traitsGenerator.generateUniques();
  
  cout<<outputs.size();
  
  for (auto& output : outputs) {
    cout<<"name: "<<output->name<<endl;
    for (const auto [type, value] : output->attributes) {
      cout<<"type: "<<type<<" value: "<<value<<endl;
    }
  }
  
  
}
Leave a Comment