• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[LIB] Achievements Lib - 075-achievement.lua

EDIT:

I'm using dev 0.4, rev3884 and if somebody login, this message spam in console.
imagemgu.jpg
 
any1 can stop spam this
Code:
 function doPlayerAddAchievement(cid, achie) -- 'achie' may be an achievement ID or achievement NAME 
    if type(achie) == "string" then -- name
        --if not getPlayerAchievementByName(cid, achie) then
           -- doCreatureSetStorage(cid, getAchievementStorageIdByName(achie), getAchievementStorageValueByName(achie))
            doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, getAchievementMessageByName(achie))
        --end
    elseif type(achie) == "number" then-- id
        --if not getPlayerAchievement(cid, achie) then
          --  doCreatureSetStorage(cid, getAchievementStorageId(achie), getAchievementStorageValue(achie))
            doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, getAchievementMessage(achie))
        --end
    end
    return true
end
IF IS W/O -- not work
 
I have fixed bugs and ready to release a new version, with SQL but I'm not sure about it. Can someone say if it will work or not?

Execute SQL:
SQL:
CREATE TABLE `player_achievements`
(
	`player_id` INT NOT NULL DEFAULT 0,
	`id` INT NOT NULL DEFAULT 0,
	`grade` INT NOT NULL DEFAULT 0,
	`points` INT NOT NULL DEFAULT 0,
	`secret` BOOLEAN NOT NULL DEFAULT FALSE,
	KEY (`player_id`), UNIQUE (`player_id`, `id`),
	FOREIGN KEY (`player_id`) REFERENCES `players`(`id`) ON DELETE CASCADE
) ENGINE = InnoDB;

And my question is here: Will this work? (Adding an achievement to player)
info is a table with achievement data and id is achievement id
Lua:
db.executeQuery("INSERT INTO `player_achievements` WHERE `player_id` = cid VALUES (" .. id .. ", " .. info.grade .. ", " .. info.points .. ", " .. info.secret .. ");")

If it won't work, please fix it and I will release new version.
 
It won't work, change query to
Code:
db.executeQuery("INSERT INTO `player_achievements` VALUES (" .. getPlayerGUID(cid) .. ", " .. id .. ", " .. info.grade .. ", " .. info.points .. ", " .. info.secret .. ");"
 
awww... I'll tomorrow check it, now if you want, try change:
Lua:
return getCreatureStorage(cid, getAchievementStorageId(id)) >= getAchievementStorageValue(id);
for
Lua:
return tonumber(getCreatureStorage(cid, getAchievementStorageId(id))) >= tonumber(getAchievementStorageValue(id));

And I mentioned in first post: CHANGE ALL "value = x" FOR YOUR VALUES.
 
Last edited:
Instead of the onThink script, it might be a good idea to add this to the script:
Code:
old_doCreatureSetStorage = doCreatureSetStorage
function doCreatureSetStorage(cid, sid, value)
	if(isPlayer(cid)) then
		if sid >= ACHIEVEMENT_FIRST and sid <= ACHIEVEMENT_LAST then
			local id = sid - ACHIEVEMENT_BASE
			if getAchievementStorageValue(id) >= value and getCreatureStorage(cid, sid) >= getAchievementStorageValue(id) then
				doPlayerAddAchievement(cid, id, true);
			end
		end
	end
	return old_doCreatureSetStorage(cid, sid, value)
end

This way you will avoid looping through all the achievements every 1 second, for 200~ players if server is popular. That is 180 achievements in your script, and 200 players = 36000 loops just to check for achievements. I'm not one to care about performance very much, but this is a very big waste that can be avoided with a few lines of code.

Also, it might be a good idea to make ACHIEVEMENT_FIRST = 1, and ACHIEVEMENT_LAST = #ACHIEVEMENTS instead of + ACHIEVEMENT_BASE. Because you always call "local id = i - ACHIVEMENT_BASE" after it anyways.

My last suggestion would to add a function like:
Code:
function doPlayerGiveAchievement(cid, achievementid) 
	doCreatureSetStorage(cid, getAchievementStorageId(achievementid), getCreatureStorage(cid, getAchievementStorageId(achievementid)) + 1)
end

To avoid this ugly line:
Code:
doCreatureSetStorage(cid, getAchievementStorageId(30), getCreatureStorage(cid, getAchievementStorageId(30)) + 1)

These are the improvements I think you can make for now, if I find anything else I'll tell you about it, if you want.

Thanks for releasing this script, I can see you put alot of work in it.
 
Last edited:
Back
Top