void
MCMercenaryManager::saveData()
{
    CCUserDefault *userDefault = CCUserDefault::sharedUserDefault();
    JsonBox::Array mercenaries;
    CCObject *obj;
    MCMercenary *mercenary;
    char o_id_buffer[5] = {0};
    char *c_str_o_id = o_id_buffer;
    
    CCArray *hired = hired_->getRoles();
    hired->removeObject(MCHero::sharedHero());
    CCARRAY_FOREACH(hired, obj) {
        mercenary = (MCMercenary *) obj;
        mc_object_id_t o_id = mercenary->getID();
        o_id_buffer[0] = o_id.class_;
        o_id_buffer[1] = o_id.sub_class_;
        o_id_buffer[2] = o_id.index_;
        o_id_buffer[3] = o_id.sub_index_;
        JsonBox::Object mercenaryObject;
        
        mercenaryObject["id"] = JsonBox::Value(c_str_o_id);
        mercenaryObject["name"] = JsonBox::Value(mercenary->getName()->getCString());
        mercenaries.push_back(mercenaryObject);
    }
JsonBox::Array Inventory::jsonArray()
{
	JsonBox::Array a;
	for(auto item : this->items)
	{
		// Skip if the id does not match to the type T
		if(item.first->id.substr(0, entityToString<T>().size()) != entityToString<T>())
			continue;
		// Otherwise add the item to the array
		JsonBox::Array pair;
		pair.push_back(JsonBox::Value(item.first->id));
		pair.push_back(JsonBox::Value(item.second));
		a.push_back(JsonBox::Value(pair));
	}

	return a;
}
Esempio n. 3
0
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    JsonBox::Object menuItem;
    
    JsonBox::Array menuItemArray;
    
    JsonBox::Object obj1;
    obj1["value1"] = JsonBox::Value("a");
    obj1["value2"] = JsonBox::Value("b");
    JsonBox::Object obj2;
    obj2["value1"] = JsonBox::Value(1);
    obj2["value2"] = JsonBox::Value(2);
    JsonBox::Object obj3;
    obj3["value1"] = JsonBox::Value(true);
    obj3["value2"] = JsonBox::Value(12.12);
    
    menuItemArray.push_back(obj1);
    menuItemArray.push_back(obj2);
    menuItemArray.push_back(obj3);
    
    menuItem["menuItem"] = menuItemArray;
    std::cout << menuItem << std::endl;
    JsonBox::Value v(menuItem);
    v.writeToFile((CCFileUtils::sharedFileUtils()->getWritablePath()+"JsonTest").c_str());
    

    JsonBox::Value v2;
    v2.loadFromFile((CCFileUtils::sharedFileUtils()->getWritablePath()+"JsonTest").c_str());
    std::cout << v2 << std::endl;
    v2.writeToStream(std::cout, true, true);
    v2.writeToFile((CCFileUtils::sharedFileUtils()->getWritablePath()+"JsonTest").c_str(), false, false);
    std::cout<<"--------"<<v2["menuItem"]<<std::endl;
    std::cout<<"--------"<<v2["menuItem"][size_t(0)]<<std::endl;
    return true;
}
void
MCBackpack::saveEffectiveItems()
{
    CCUserDefault *userDefault = CCUserDefault::sharedUserDefault();
    JsonBox::Array effectiveItems;
    
    /* 道具 */
    effectiveItems.push_back(JsonBox::Value((int) healthPotion_->count));
    effectiveItems.push_back(JsonBox::Value((int) physicalPotion_->count));
    effectiveItems.push_back(JsonBox::Value((int) fireballTrapWide_->count));
    effectiveItems.push_back(JsonBox::Value((int) fireballTrapDamage_->count));
    effectiveItems.push_back(JsonBox::Value((int) curseTrapWide_->count));
    effectiveItems.push_back(JsonBox::Value((int) curseTrapDamage_->count));
    effectiveItems.push_back(JsonBox::Value((int) paralysisTrapWide_->count));
    effectiveItems.push_back(JsonBox::Value((int) paralysisTrapDamage_->count));
    effectiveItems.push_back(JsonBox::Value((int) fogTrapWide_->count));
    effectiveItems.push_back(JsonBox::Value((int) fogTrapDamage_->count));
    effectiveItems.push_back(JsonBox::Value((int) flashTrapWide_->count));
    effectiveItems.push_back(JsonBox::Value((int) flashTrapDamage_->count));
    JsonBox::Value effectiveItemsValue(effectiveItems);
    ostringstream outputStream;
    effectiveItemsValue.writeToStream(outputStream);
    string data = outputStream.str();
#if MC_DEBUG_SAVEDATA == 1
    const char *output = data.c_str();
#else
    const char *input = data.c_str();
    char  *output;
    mc_size_t len = strlen(input);
    MCBase64Encode((mc_byte_t *) input, len, (mc_byte_t **) &output);
#endif
    userDefault->setStringForKey(kMCEffectiveItemsKey, output);
#if MC_DEBUG_SAVEDATA != 1
    delete []output;
#endif
}