root/trunk/lua/lib.shardlist.lua @ 2963

Revision 2963, 6.5 kB (checked in by ghoulsblade, 18 months ago)
mainmenu:accountlist : shows time since last login and top7 char skills
Line 
1
2-- paths
3
4function GetConfigDirPath               () return gMainWorkingDir.."config/" end
5function GetShardListDirPath            () return GetConfigDirPath().."shards/" end
6function GetShardMemoryFilePath         () return GetConfigDirPath().."shardmemory.xml" end
7function GetCharFilePath                (loginname,charid) return GetConfigDirPath().."chars/"..table.concat({gLoginServerIP,gLoginServerPort,ShardListFileNamePartEncode(loginname or gLoginname),(giGameServerID or 0),(charid or gCharID or 0)},".")..".xml" end
8function GetShardConfigFilePath         (shardname) return GetShardListDirPath()..ShardListFileNamePartEncode(shardname)..".xml" end
9
10function ShardListFileNamePartEncode    (x) return string.gsub(x,"[^0-9a-zA-Z_%-%(%) ]",".") end
11
12-- notes
13
14function InitShardList () 
15    LoadStoredPasswords()
16    LoadShardMemory()
17    LoadShardList()
18    --~ for shardname,shard in pairs(gShardList) do
19        --~ local filepath = GetShardConfigFilePath(shardname)
20        --~ print("InitShardList",shardname,shard,filepath)
21        --~ SimpleXMLSave(filepath,shard)
22    --~ end
23    --~ SimpleXML_Test()
24    --~ os.exit(0)
25end
26
27-- ***** ***** ***** ***** ***** chars
28
29RegisterListener("Hook_Packet_Character_List",function (charlist) 
30    local plaincharlist = {serverid=giGameServerID}
31    for k,v in pairs(charlist.chars) do plaincharlist[k] = v.name end
32    plaincharlist.gLoginServerIP = gLoginServerIP
33    plaincharlist.gLoginServerPort = gLoginServerPort
34    plaincharlist.gLoginname = gLoginname
35    plaincharlist.giGameServerID = giGameServerID
36    ShardMemorySet("charlist",table.concat({gLoginServerIP,gLoginServerPort,ShardListFileNamePartEncode(gLoginname),giGameServerID},":"),plaincharlist)
37end)
38
39RegisterListener("Hook_Player_Full_equip",function (mobiledata,equipmentdata) 
40    if (gShardListPlayerEquipAlreadySaved) then return end
41    gShardListPlayerEquipAlreadySaved = true
42    ShardListSavePlayerMobile()
43    RegisterIntervalStepper(30*1000,ShardListSavePlayerMobile) 
44end)
45
46RegisterListener("Hook_Player_Skills",function (skills) 
47    if (gShardListPlayerSkillAlreadySaved) then return end
48    gShardListPlayerSkillAlreadySaved = true
49    ShardListSavePlayerMobile()
50end)
51
52-- stores body,equip,mount       todo : skills, but change too often for xml
53function ShardListSavePlayerMobile ()
54    local mobile = GetPlayerMobile()
55    print("###ShardListSavePlayerMobile",mobile,gPlayerSkills)
56    if (not mobile) then return end
57    if (not gPlayerSkills) then return end
58    local charname = gCharName
59   
60    --[[
61    -- calc and check has, no need to save if nothing changed
62    local hash = {mobile.artid,mobile.hue}
63    for k,layer in pairs(gLayerType) do
64        local item = mobile:GetEquipmentAtLayer(layer)
65        if (item) then table.insert(hash,item.serial) end
66    end
67    for skillid,skill in pairs(gPlayerSkills) do table.insert(hash,skill.base_value) end
68    hash = table.concat(hash,"#")
69    if (hash == gShardListSavePlayerMobile_LastHash) then return end
70    gShardListSavePlayerMobile_LastHash = hash
71    print("saving data...",hash)
72    ]]--
73   
74    -- save the data
75    local data = {  charname=charname, artid=mobile.artid, hue=mobile.hue, xloc=mobile.xloc, yloc=mobile.yloc, zloc=mobile.zloc, map=gMapIndex, equip={},skills={} }
76    for k,layer in pairs(gLayerType) do 
77        local item = mobile:GetEquipmentAtLayer(layer) 
78        if (item) then data.equip[layer] = {artid=item.artid,hue=item.hue} end
79    end
80    for skillid,skill in pairs(gPlayerSkills) do 
81        data.skills[skillid] = {value=skill.value,base_value=skill.base_value,lockstate=skill.lockstate,name=glSkillNames[skillid]} 
82    end
83    data.time = os.time()
84    data.timetext = os.date("%d %b %Y %H:%M",data.time) -- 10 Feb 2009 16:14       %H:%M:%S
85    SimpleXMLSave(GetCharFilePath(),data)
86end
87
88-- ***** ***** ***** ***** ***** shard list
89
90function LoadShardList () 
91    local path_folder = GetShardListDirPath()
92    local arr_files = dirlist(path_folder,false,true)
93    for k,filename in pairs(arr_files) do
94        local ext = string.lower(string.sub(filename,-4))
95        local shardname = string.sub(filename,1,-5)
96        local filepath = path_folder .. filename
97        print("LoadShardList",shardname,filepath,ext) 
98        if (ext == ".xml") then 
99            gShardList[shardname] = SimpleXMLLoad(filepath)
100        end
101    end
102end
103 
104-- ***** ***** ***** ***** ***** shard memory
105
106function LoadShardMemory    () gShardMemory =   SimpleXMLLoad(GetShardMemoryFilePath()) or {} end
107function SaveShardMemory    ()                  SimpleXMLSave(GetShardMemoryFilePath(),gShardMemory) end
108
109function ShardMemoryGetList (listname) return gShardMemory[listname] end
110function ShardMemoryGet     (listname,key) 
111    local list = gShardMemory[listname]
112    return list and list[key]
113end
114function ShardMemorySet     (listname,key,value)
115    local list = gShardMemory[listname]
116    if (not list) then list = {} gShardMemory[listname] = list end
117    list[key] = value
118    SaveShardMemory()
119end
120
121-- ***** ***** ***** ***** ***** stored passwords
122
123-- store password when login succeeds
124RegisterListener("Hook_Packet_Server_List",function ()
125    if (not gRememberPassword) then return end
126    SetStoredPassword(gLoginServerIP,gLoginServerPort,gLoginname,gPassword)
127end)
128gStoredPasswords = {}
129function GetStoredPasswordsFilePath () return GetConfigDirPath().."passwords.xml" end
130function SaveStoredPasswords ()                     SimpleXMLSave(GetStoredPasswordsFilePath(),gStoredPasswords) end
131function LoadStoredPasswords () gStoredPasswords =  SimpleXMLLoad(GetStoredPasswordsFilePath()) or {} end
132
133function GetStoredPassword          (host,port,username) return gStoredPasswords[host..":"..port..":"..username] end
134function SetStoredPassword          (host,port,username,password) gStoredPasswords[host..":"..port..":"..username] = password SaveStoredPasswords() end
135function ClearStoredPassword        (host,port,username) gStoredPasswords[host..":"..port..":"..username] = nil SaveStoredPasswords() end
136function ClearAllStoredPasswords    () gStoredPasswords = {} SaveStoredPasswords() end
137
138function TestStoredPasswords ()
139        local host,port = "example.org",2593
140        local user1 = "weirdguy"
141        local user2 = "somedude"
142        LoadStoredPasswords()
143        --~ SetStoredPassword(host,port,user1,"secret1234")
144        --~ SetStoredPassword(host,port,user2,"secret5555")
145        print(user1,GetStoredPassword(host,port,user1)) --~ weirdguy        secret1234
146        print(user2,GetStoredPassword(host,port,user2)) --~ somedude        secret5555
147        os.exit(0)
148end
Note: See TracBrowser for help on using the browser.