<commit_before>--------------------------------------------------------
-- This file is part of the JX3 Mingyi Plugin.
-- @link     : https://jx3.derzh.com/
-- @desc     : еͼ ¼NPCDoodadλ ṩ
-- @author   :  @˫ @׷Ӱ
-- @modifier : <NAME> (<EMAIL>)
-- @copyright: Copyright (c) 2013 EMZ Kingsoft Co., Ltd.
--------------------------------------------------------
MY.CreateDataRoot(MY_DATA_PATH.GLOBAL)
local _L = MY.LoadLangPack(MY.GetAddonInfo().szRoot .. 'MY_MiddleMapMark/lang/')
local l_szKeyword, l_dwMapID, l_nMapIndex = ''
local DB = MY.ConnectDatabase(_L['MY_MiddleMapMark'], {'cache/npc_doodad_rec.v2.db', MY_DATA_PATH.GLOBAL})
if not DB then
	return MY.Sysmsg({_L['Cannot connect to database!!!'], r = 255, g = 0, b = 0}, _L['MY_MiddleMapMark'])
end
DB:Execute('CREATE TABLE IF NOT EXISTS NpcInfo (templateid INTEGER, poskey INTEGER, mapid INTEGER, x INTEGER, y INTEGER, name VARCHAR(20) NOT NULL, title VARCHAR(20) NOT NULL, level INTEGER, PRIMARY KEY(templateid, poskey))')
DB:Execute('CREATE INDEX IF NOT EXISTS mmm_name_idx ON NpcInfo(name, mapid)')
DB:Execute('CREATE INDEX IF NOT EXISTS mmm_title_idx ON NpcInfo(title, mapid)')
DB:Execute('CREATE INDEX IF NOT EXISTS mmm_template_idx ON NpcInfo(templateid, mapid)')
local DBN_W  = DB:Prepare('REPLACE INTO NpcInfo (templateid, poskey, mapid, x, y, name, title, level) VALUES (?, ?, ?, ?, ?, ?, ?, ?)')
local DBN_DM  = DB:Prepare('DELETE FROM NpcInfo WHERE mapid = ?')
local DBN_RI = DB:Prepare('SELECT templateid, poskey, mapid, x, y, name, title, level FROM NpcInfo WHERE templateid = ?')
local DBN_RN = DB:Prepare('SELECT templateid, poskey, mapid, x, y, name, title, level FROM NpcInfo WHERE name LIKE ? OR title LIKE ?')
local DBN_RNM = DB:Prepare('SELECT templateid, poskey, mapid, x, y, name, title, level FROM NpcInfo WHERE (name LIKE ? AND mapid = ?) OR (title LIKE ? AND mapid = ?)')
DB:Execute('CREATE TABLE IF NOT EXISTS DoodadInfo (templateid INTEGER, poskey INTEGER, mapid INTEGER, x INTEGER, y INTEGER, name VARCHAR(20) NOT NULL, PRIMARY KEY (templateid, poskey))')
DB:Execute('CREATE INDEX IF NOT EXISTS mmm_name_idx ON DoodadInfo(name, mapid)')
local DBD_W  = DB:Prepare('REPLACE INTO DoodadInfo (templateid, poskey, mapid, x, y, name) VALUES (?, ?, ?, ?, ?, ?)')
local DBD_DM  = DB:Prepare('DELETE FROM DoodadInfo WHERE mapid = ?')
local DBD_RI = DB:Prepare('SELECT templateid, poskey, mapid, x, y, name FROM DoodadInfo WHERE templateid = ?')
local DBD_RN = DB:Prepare('SELECT templateid, poskey, mapid, x, y, name FROM DoodadInfo WHERE name LIKE ?')
local DBD_RNM = DB:Prepare('SELECT templateid, poskey, mapid, x, y, name FROM DoodadInfo WHERE name LIKE ? AND mapid = ?')

MY_MiddleMapMark = {}
do
---------------------------------------------------------------
-- ݴ洢
---------------------------------------------------------------
local L16 = 0x10000
local L32 = 0x100000000
-- NPC 8ߣڸþʵΪͬһ
-- DOODAD 2ߣڸþʵΪͬһ
-- ģҪݿ½key
local NPC_MAX_DISTINCT_DISTANCE = 8 * 64
local DOODAD_MAX_DISTINCT_DISTANCE = 2 * 64
-- 47 - 32 λ mapid
-- 31 - 16 λ x
-- 15 -  0 λ y
local function GeneNpcInfoPosKey(mapid, x, y)
	return mapid * L32 + math.floor(x / NPC_MAX_DISTINCT_DISTANCE) * L16 + math.floor(y / NPC_MAX_DISTINCT_DISTANCE)
end
local function GeneDoodadInfoPosKey(mapid, x, y)
	return mapid * L32 + math.floor(x / DOODAD_MAX_DISTINCT_DISTANCE) * L16 + math.floor(y / DOODAD_MAX_DISTINCT_DISTANCE)
end

local SZ_CACHE_PATH = 'cache/NPC_DOODAD_REC/'
if IsLocalFileExist(MY.FormatPath(SZ_CACHE_PATH)) then
	DB:Execute('BEGIN TRANSACTION')
	for _, dwMapID in ipairs(GetMapList()) do
		local data = MY.LoadLUAData(SZ_CACHE_PATH .. dwMapID .. '.$lang.jx3dat')
		if type(data) == 'string' then
			data = MY.JsonDecode(data)
		end
		if data then
			for _, p in ipairs(data.Npc) do
				DBN_W:ClearBindings()
				DBN_W:BindAll(p.dwTemplateID, GeneNpcInfoPosKey(dwMapID, p.nX, p.nY), dwMapID, p.nX, p.nY, AnsiToUTF8(p.szName), AnsiToUTF8(p.szTitle), p.nLevel)
				DBN_W:Execute()
			end
			for _, p in ipairs(data.Doodad) do
				DBD_W:ClearBindings()
				DBD_W:BindAll(p.dwTemplateID, GeneDoodadInfoPosKey(dwMapID, p.nX, p.nY), dwMapID, p.nX, p.nY, AnsiToUTF8(p.szName))
				DBD_W:Execute()
			end
			MY.Debug({'MiddleMapMark cache trans from file to sqlite finished!'}, 'MY_MiddleMapMark', MY_DEBUG.LOG)
		end
	end
	DB:Execute('END TRANSACTION')
	CPath.DelDir(MY.FormatPath(SZ_CACHE_PATH))
end

---------------------------------------------------------------
-- ݲɼ
---------------------------------------------------------------
local l_npc = {}
local l_doodad = {}
local l_tempMap = false
local l_renderTime = 0
local MAX_RENDER_INTERVAL = GLOBAL.GAME_FPS * 5
local function PushDB()
	if empty(l_npc) and empty(l_doodad) then
		return
	end
	DB:Execute('BEGIN TRANSACTION')

	for i, p in pairs(l_npc) do
		if not p.temp then
			DBN_W:ClearBindings()
			DBN_W:BindAll(p.templateid, p.poskey, p.mapid, p.x, p.y, AnsiToUTF8(p.name), AnsiToUTF8(p.title), p.level)
			DBN_W:Execute()
		end
	end
	l_npc = {}

	for i, p in pairs(l_doodad) do
		if not p.temp then
			DBD_W:ClearBindings()
			DBD_W:BindAll(p.templateid, p.poskey, p.mapid, p.x, p.y, AnsiToUTF8(p.name))
			DBD_W:Execute()
		end
	end
	l_doodad = {}

	DB:Execute('END TRANSACTION')<commit_msg>修复一处本地变量作用域问题<commit_after>--------------------------------------------------------
-- This file is part of the JX3 Mingyi Plugin.
-- @link     : https://jx3.derzh.com/
-- @desc     : еͼ ¼NPCDoodadλ ṩ
-- @author   :  @˫ @׷Ӱ
-- @modifier : <NAME> (<EMAIL>)
-- @copyright: Copyright (c) 2013 EMZ Kingsoft Co., Ltd.
--------------------------------------------------------
MY.CreateDataRoot(MY_DATA_PATH.GLOBAL)
local _L = MY.LoadLangPack(MY.GetAddonInfo().szRoot .. 'MY_MiddleMapMark/lang/')
local l_szKeyword, l_dwMapID, l_nMapIndex, l_renderTime = '', nil, nil, 0
local DB = MY.ConnectDatabase(_L['MY_MiddleMapMark'], {'cache/npc_doodad_rec.v2.db', MY_DATA_PATH.GLOBAL})
if not DB then
	return MY.Sysmsg({_L['Cannot connect to database!!!'], r = 255, g = 0, b = 0}, _L['MY_MiddleMapMark'])
end
DB:Execute('CREATE TABLE IF NOT EXISTS NpcInfo (templateid INTEGER, poskey INTEGER, mapid INTEGER, x INTEGER, y INTEGER, name VARCHAR(20) NOT NULL, title VARCHAR(20) NOT NULL, level INTEGER, PRIMARY KEY(templateid, poskey))')
DB:Execute('CREATE INDEX IF NOT EXISTS mmm_name_idx ON NpcInfo(name, mapid)')
DB:Execute('CREATE INDEX IF NOT EXISTS mmm_title_idx ON NpcInfo(title, mapid)')
DB:Execute('CREATE INDEX IF NOT EXISTS mmm_template_idx ON NpcInfo(templateid, mapid)')
local DBN_W  = DB:Prepare('REPLACE INTO NpcInfo (templateid, poskey, mapid, x, y, name, title, level) VALUES (?, ?, ?, ?, ?, ?, ?, ?)')
local DBN_DM  = DB:Prepare('DELETE FROM NpcInfo WHERE mapid = ?')
local DBN_RI = DB:Prepare('SELECT templateid, poskey, mapid, x, y, name, title, level FROM NpcInfo WHERE templateid = ?')
local DBN_RN = DB:Prepare('SELECT templateid, poskey, mapid, x, y, name, title, level FROM NpcInfo WHERE name LIKE ? OR title LIKE ?')
local DBN_RNM = DB:Prepare('SELECT templateid, poskey, mapid, x, y, name, title, level FROM NpcInfo WHERE (name LIKE ? AND mapid = ?) OR (title LIKE ? AND mapid = ?)')
DB:Execute('CREATE TABLE IF NOT EXISTS DoodadInfo (templateid INTEGER, poskey INTEGER, mapid INTEGER, x INTEGER, y INTEGER, name VARCHAR(20) NOT NULL, PRIMARY KEY (templateid, poskey))')
DB:Execute('CREATE INDEX IF NOT EXISTS mmm_name_idx ON DoodadInfo(name, mapid)')
local DBD_W  = DB:Prepare('REPLACE INTO DoodadInfo (templateid, poskey, mapid, x, y, name) VALUES (?, ?, ?, ?, ?, ?)')
local DBD_DM  = DB:Prepare('DELETE FROM DoodadInfo WHERE mapid = ?')
local DBD_RI = DB:Prepare('SELECT templateid, poskey, mapid, x, y, name FROM DoodadInfo WHERE templateid = ?')
local DBD_RN = DB:Prepare('SELECT templateid, poskey, mapid, x, y, name FROM DoodadInfo WHERE name LIKE ?')
local DBD_RNM = DB:Prepare('SELECT templateid, poskey, mapid, x, y, name FROM DoodadInfo WHERE name LIKE ? AND mapid = ?')

MY_MiddleMapMark = {}
do
---------------------------------------------------------------
-- ݴ洢
---------------------------------------------------------------
local L16 = 0x10000
local L32 = 0x100000000
-- NPC 8ߣڸþʵΪͬһ
-- DOODAD 2ߣڸþʵΪͬһ
-- ģҪݿ½key
local NPC_MAX_DISTINCT_DISTANCE = 8 * 64
local DOODAD_MAX_DISTINCT_DISTANCE = 2 * 64
-- 47 - 32 λ mapid
-- 31 - 16 λ x
-- 15 -  0 λ y
local function GeneNpcInfoPosKey(mapid, x, y)
	return mapid * L32 + math.floor(x / NPC_MAX_DISTINCT_DISTANCE) * L16 + math.floor(y / NPC_MAX_DISTINCT_DISTANCE)
end
local function GeneDoodadInfoPosKey(mapid, x, y)
	return mapid * L32 + math.floor(x / DOODAD_MAX_DISTINCT_DISTANCE) * L16 + math.floor(y / DOODAD_MAX_DISTINCT_DISTANCE)
end

local SZ_CACHE_PATH = 'cache/NPC_DOODAD_REC/'
if IsLocalFileExist(MY.FormatPath(SZ_CACHE_PATH)) then
	DB:Execute('BEGIN TRANSACTION')
	for _, dwMapID in ipairs(GetMapList()) do
		local data = MY.LoadLUAData(SZ_CACHE_PATH .. dwMapID .. '.$lang.jx3dat')
		if type(data) == 'string' then
			data = MY.JsonDecode(data)
		end
		if data then
			for _, p in ipairs(data.Npc) do
				DBN_W:ClearBindings()
				DBN_W:BindAll(p.dwTemplateID, GeneNpcInfoPosKey(dwMapID, p.nX, p.nY), dwMapID, p.nX, p.nY, AnsiToUTF8(p.szName), AnsiToUTF8(p.szTitle), p.nLevel)
				DBN_W:Execute()
			end
			for _, p in ipairs(data.Doodad) do
				DBD_W:ClearBindings()
				DBD_W:BindAll(p.dwTemplateID, GeneDoodadInfoPosKey(dwMapID, p.nX, p.nY), dwMapID, p.nX, p.nY, AnsiToUTF8(p.szName))
				DBD_W:Execute()
			end
			MY.Debug({'MiddleMapMark cache trans from file to sqlite finished!'}, 'MY_MiddleMapMark', MY_DEBUG.LOG)
		end
	end
	DB:Execute('END TRANSACTION')
	CPath.DelDir(MY.FormatPath(SZ_CACHE_PATH))
end

---------------------------------------------------------------
-- ݲɼ
---------------------------------------------------------------
local l_npc = {}
local l_doodad = {}
local l_tempMap = false
local MAX_RENDER_INTERVAL = GLOBAL.GAME_FPS * 5
local function PushDB()
	if empty(l_npc) and empty(l_doodad) then
		return
	end
	DB:Execute('BEGIN TRANSACTION')

	for i, p in pairs(l_npc) do
		if not p.temp then
			DBN_W:ClearBindings()
			DBN_W:BindAll(p.templateid, p.poskey, p.mapid, p.x, p.y, AnsiToUTF8(p.name), AnsiToUTF8(p.title), p.level)
			DBN_W:Execute()
		end
	end
	l_npc = {}

	for i, p in pairs(l_doodad) do
		if not p.temp then
			DBD_W:ClearBindings()
			DBD_W:BindAll(p.templateid, p.poskey, p.mapid, p.x, p.y, AnsiToUTF8(p.name))
			DBD_W:Execute()
		end
	end
	l_doodad = {}

	DB:Execute('END TRANSACTION')
{'fraction_non_alphanumeric': 0.19192403606368694, 'fraction_numerical': 0.014195281028198734, 'mean_word_length': 3.2891814068284657, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2637209', 'n_tokens_mistral': 4297, 'n_tokens_neox': 3927, 'n_words': 1006}