bool Database::updateCharacter(int characterID, const FieldsType& fields)
{
	map<string, string> sqlFields;

	for (auto it = fields.begin(); it != fields.end(); ++it)
	{
		const string& name = it->first;
		const Sqf::Value& val = it->second;

		//arrays
		if (name == "worldspace" || name == "inventory" || name == "backpack" || name == "medical" || name == "state" || name == "dog")
			sqlFields[name] = "'" + lexical_cast<string>(val)+"'";
		//booleans
		else if (name == "just_ate" || name == "just_drank")
		{
			if (boost::get<bool>(val))
			{
				string newName = "last_ate";
				if (name == "just_drank")
					newName = "last_drank";

				sqlFields[newName] = "CURRENT_TIMESTAMP";
			}
		}
		//other stats
		else if (name == "zombie_kills" || name == "headshots" || name == "survival_time" || name == "survivor_kills" || name == "lastserver" || name == "class" || name == "ranger" || name == "outlaw" || name == "hunter" || name == "nomad" || name == "survivalist")
		{
			sqlFields[name] = "'" + lexical_cast<string>(val)+"'";
		}
		//strings
		else if (name == "model")
		{
			sqlFields[name] = "'" + boost::get<string>(val) +"'";
		}
	}

	if (sqlFields.size() > 0)
	{
		string setClause = "";
		bool joinProfile = false;
		for (auto it = sqlFields.begin(); it != sqlFields.end();)
		{
			string fieldName = it->first;

			if (fieldName == "ranger" || fieldName == "outlaw" || fieldName == "hunter" || fieldName == "nomad" || fieldName == "survivalist") {
				joinProfile = true;
				setClause += "p.`" + fieldName + "` = " + it->second;
			}
			else {
				setClause += "s.`" + fieldName + "` = " + it->second;
			}
			++it;
			if (it != sqlFields.end())
				setClause += " , ";
		}

		string query = "update `survivor` s ";
		if (joinProfile)
			query += "join `profile` p on s.`unique_id` = p.`unique_id` ";
		query += "set " + setClause + " where s.`id` = " + lexical_cast<string>(characterID);


		//bool exRes = getDB()->execute(query.c_str());
		//poco_assert(exRes == true);

		Poco::Data::Statement stmt((*activeSession));
		stmt << query, now;
	}

	return true;
};
Example #2
0
bool SqlCharDataSource::updateCharacter( int characterId, int serverId, const FieldsType& fields )
{
	map<string,string> sqlFields;

	for (auto it=fields.begin();it!=fields.end();++it)
	{
		const string& name = it->first;
		const Sqf::Value& val = it->second;

		//arrays
		if (name == "Worldspace" || name == "Inventory" || name == "Backpack" || name == "Medical" || name == "CurrentState")
			sqlFields[name] = "'"+getDB()->escape(lexical_cast<string>(val))+"'";
		//booleans
		else if (name == "JustAte" || name == "JustDrank")
		{
			if (boost::get<bool>(val))
			{
				string newName = "LastAte";
				if (name == "JustDrank")
					newName = "LastDrank";

				sqlFields[newName] = "CURRENT_TIMESTAMP";
			}
		}
		//addition integeroids
		else if (name == "KillsZ" || name == "HeadshotsZ" || name == "DistanceFoot" || name == "Duration" ||
			name == "KillsH" || name == "KillsB" || name == "Humanity")
		{
			int integeroid = static_cast<int>(Sqf::GetDouble(val));
			char intSign = '+';
			if (integeroid < 0)
			{
				intSign = '-';
				integeroid = abs(integeroid);
			}

			if (integeroid > 0) 
				sqlFields[name] = "(`"+name+"` "+intSign+" "+lexical_cast<string>(integeroid)+")";
		}
		//strings
		else if (name == "Model")
			sqlFields[name] = "'"+getDB()->escape(boost::get<string>(val))+"'";
	}

	if (sqlFields.size() > 0)
	{
		string query = "UPDATE `Character_DATA` SET ";
		for (auto it=sqlFields.begin();it!=sqlFields.end();)
		{
			string fieldName = it->first;
			if (fieldName == "Worldspace")
				fieldName = _wsFieldName;

			query += "`" + fieldName + "` = " + it->second;
			++it;
			if (it != sqlFields.end())
				query += " , ";
		}
		query += ", `InstanceID` = " + lexical_cast<string>(serverId) + "  WHERE `CharacterID` = " + lexical_cast<string>(characterId);
		bool exRes = getDB()->execute(query.c_str());
		poco_assert(exRes == true);

		return exRes;
	}

	return true;
}