Page 1 of 1

Problem when make a database for equipment

Posted: Wed Mar 27, 2024 4:40 pm
by doubleruri
After 10 years, I come back to MUD~~ :D

Now I don't have much time for find what equipment to wear for different situation.I want make a database to quickly find what I need.(Old information of EQs is post on BBS)
After serveral days scripting for catch information , I meet problem when adding data to SQLite DB...

The identify example is like below: (In Chinese Traditional, I translate and mark with different color in English at right)
物品名稱: 火靈之吻(kiss of salamander) Item: 火靈之吻(kiss of salamander)
物品狀態: 發光 .Flag: Glowing
裝備位置: 盾牌Wear: shield
影響 智力 2 點. Int 2
The problem is
  1. Can I name Table with non-English but other language encoding in UTF-8?
    I create a database

    Code: Select all

    db:create("eqbank", {shield = "名稱","狀態","智力"})
    work and show correct in DB browser. But when I add data in Mudlet, I cannot did it.

    Code: Select all

    local mydb =  db:get_database("eqbank")
    db:add(mydb.Shield, {名稱 = "火靈之吻" ,狀態 = "發光", 智力 = 2})
    db:close(mydb)
    Mudlet says syntax error near symble = and give me a little ladybug XD
    I tried use [[名稱]] and "名稱" '名稱', all are wrong, but I change table name in English, It work!
    I am not a English player, reading 90+ information in mother langue will be more happy.
    Database.jpg
  • 2. How to script correctly and lightly when adding data to database?
    I made a alias for test

    Code: Select all

    pattern: ^dddd$
    code: local mydb = db:get_database("eqbank")
    db:add(mydb.Shield,{Int=15})
    
    Nothing add, Error report is :
    LUA ERROR: when running script writedata (Alias86),
    reason: ...ppData\Local\Mudlet\app-4.17.2\mudlet-lua\lua\DB.lua:780: attempt to index local 'conn'
    (a nil value)

    And put db:create in that will work

    Code: Select all

    pattern: ^dddd$
    code: 
    db:create("eqbank", {shield = "Name","Flag","Int"})
    local mydb = db:get_database("eqbank")
    db:add(mydb.Shield,{Int=15})
    
    my database will be 10 sheets (hands/boots/etc..) , every sheet has 90 column
    If put every db:create code before I add data, script will be more complex.
    How to resolve it??
  • Question3 is : Can I add data in Mudlet table like Cmud?
    I have a data in table:
    i.e.

    Code: Select all

    ItemData = {Name = "shield", Flag = "glow" , Int = 2}
    In Cmud I can use (#new ItemData) to put every data into db (but Cmud is crash too often :? )
    In Mudlet, Is there simple script instead of put every key in table one by one?
    P.S. My colum is same as variants i.e Name = ItemData .Name AC=ItemData .AC

Re: Problem when make a database for equipment

Posted: Mon May 20, 2024 3:58 pm
by doubleruri
Answer myself.
It is welcome that anyone rewrite it for more good English reading. :D

1. Can I name Table with non-English but other language encoding in UTF-8?
Today mudlet cannot support it though you can use db broswer application or sqlite to create it.
maybe work on db:create() function can improve it?

2. How to script correctly and lightly when adding data to database?
Cause by not "conn database" yet.
run db:create(database_name,table) can conn yourdatabase.

A small tip is:
If you want to arrange your field by special order,and read in other database browser application with same order.
Add field ONE BY ONE can do that!
So below example code can help you. Write it in button :D
Code: [show] | [select all] lua
local AllName = {"CName","Name","LV","Type","Magic"} 
--field order as you want
local Data1 = {{ }}
for i = 1 ,#(AllName) do
   table.insert(Data1[1], AllName[i])
 --If you need name the sheet.--
   db:create("database_name", {sheet_name = unpack(Data1)})
-- If you don't need name the sheet.--  
-- db:create("database_name",unpack(Data1)) 
   display(Data1[1][i]) --[Debug]Check  which field was create. You can delete it.
end
display("end") --[Debug]Let us know it is done,you can replease it by any you like.
3.Tip about add/update data into database:
A table like :

Code: Select all

Item = {{ Name = "Candy", Type = "Food", LV = "1"}}
Important: There is 2 {{ & }}.

Code: Select all

db:add(sheet,unpack(Item))
If it is a simple table {} ,

Code: Select all

db:add(sheet,Item)
db:add(sheet,{Item})
db:add(sheet,unpack(Item))
ALL Not work. I don't know why XD

Note: db:update() only work on a table has _row_id field.(db:fetch() got it)
db:add() set _row_id nil

This can be used to debug your script.

Re: Problem when make a database for equipment

Posted: Mon May 20, 2024 5:39 pm
by Jor'Mox
So while I agree that it would be ideal to make databases work properly when using non-Latin characters for, if I'm understanding this correctly, the name of the database and its various field names, it should be possible to work around the language barrier for day to day use by creating a table with the original name and its translation, and using that to automatically swap things out. So, for example, if you see a shield that you want to add to your database, it should be possible to capture the fact that it is a shield using triggers as normal, run the text for the item type through a script using the table to translate, and have that script return "shield" for your code to use moving forward. And in the same way, when you query to find a shield to use, you should be able to type in shield in Chinese, again have that translated via script, use that to get the data from the database, and once you have the data, again use the script to translate any English terms into their Chinese equivalents, so that all you see at the end is text in Chinese.

That said, I haven't had great experiences with databases in Mudlet. They caused so many issues for me that even though I did eventually get them working for what I wanted to use them for, I ended up switching over to just using really big, complicated tables instead, because tables were much more capable of handling changes to the data structure itself. And since the amount of data in question is relatively small, searching through tables is quick enough for my purposes, and likely would be in yours as well. Here is a function from the larger script I wrote that shows both methods for, in this case, retrieving information from the database or table about people I encountered in the game.
Code: [show] | [select all] lua
function dslpnp.people.showInfo(show,name)
    if dslpnp.people.Active then
        if not name or name == "" then
            error("showInfo: invalid name to be searched for.",2)
        end
        if not table.contains({"info","kinfo","cinfo","craft"},show) then
            error("showInfo: invalid show string, must be kinfo, cinfo, info, or craft.",2)
        end
        local configs = dslpnp.people.configs
        local text, index = "", 0
        local info = {}
        local org_type
        name = string.lower(name)
        if show == "kinfo" then
            org_type = "kingdom"
            for k,v in pairs(dslpnp.people.king_list) do
                if string.starts(string.lower(k), name) then
                    name = v
                    break
                end
            end
        elseif show == "cinfo" then
            org_type = "clan"
            if string.starts("conclave",string.lower(name)) then
                dslpnp.people.showInfo(show,"White Robes")
                dslpnp.people.showInfo(show,"Red Robes")
                dslpnp.people.showInfo(show,"Black Robes")
                return
            end
        end

        if configs.use_database then name = name .. "%" end
        if show == "info" then
            if configs.use_database then
                info = db:fetch(dsl_db.people,db:like(dsl_db.people.name,name),{dsl_db.people.level},true)
            else
                for k,v in pairs(dslpnp.data.people) do
                    if string.starts(string.lower(v.name),name) then
                        table.insert(info,v)
                    end
                end
            end
        elseif show ~= "craft" then
            if configs.use_database then
                info = db:fetch(dsl_db.people,{db:like(dsl_db.people.org,name),db:eq(dsl_db.people.org_type,org_type)},{dsl_db.people.level},true)
            else
                name = string.lower(name)
                for k,v in pairs(dslpnp.data.people) do
                    if v.org_type == org_type and string.starts(string.lower(v.org),name) then
                        table.insert(info,v)
                    end
                end
            end
        else
            if configs.use_database then
                info = db:fetch(dsl_db.people,db:like(dsl_db.people.craft,name),{dsl_db.people.level},true)
            else
                name = string.lower(name)
                for k,v in pairs(dslpnp.data.people) do
                    if v.craft and string.starts(string.lower(v.craft),name) then
                        table.insert(info,v)
                    end
                end
            end

            local function sort_craft(a,b)
                if b and craft_ranks[a.craft_rank] > craft_ranks[b.craft_rank] then
                    return true
                end
                return false
            end

            table.sort(info,sort_craft)
        end
        index = 0
        for k,v in pairs(info) do
            if configs.show_names == "all" or v.last_seen._timestamp > os.time() - configs.show_names * 86400 then
                if show == "craft" then
                    text = format_person_info(v,true)
                else
                    text = format_person_info(v)
                end
                index = index + 1
                cecho(text)
            end
        end
        echo("\n\nPlayers found: " .. index .. "\n")
    end
end