Jump to content
Sign in to follow this  
V3ct0r

Система награды игроков по расписанию

Recommended Posts

Система награды игроков по расписанию

 

С помощью данной системы можно выдавать всем игрокам на сервере в определенное время награду, указанную в расписании: все игроки, которые в данное время будут онлайн, получат предмет.

 

Как установить

 

1) Создайте файл с названием "pkodev.gift.lua" в директории "GameServer\resource\script\calculate\mods";

2) Поместите в файл следующий код:

--------------------------------------------------------------
-- Gift system script
--
-- Author: V3ct0r from PKODev.NET
-- Version: 1.0 (11/08/2021)
--
-- How to install the system:
-- 1) Put this file to '\GameServer\resource\script\calculate\mods' folder
-- 2) Put the following line at the beginning of 'mlist.lua' file:
--     dofile(GetResPath("script\\calculate\\mods\\pkodev.gift.lua"))
-- 3) Done!
--
-- Interface:
-- 1) Add a gift to queue:
--     local gift_id = giftmod:addGift(item_id, number, hour, minute, second, month, day, year)
-- 2) Remove a gift from queue
--     local success = giftmod:removeGift(gift_id) -- Where 'success' can be 'true' or 'false'
-- 3) Get gift number in queue
--     local number = giftmod:getGiftNumber()
-- 4) Print a list of queued gifts in GameServer window:
--     giftmod:printGifts()
-- 
-- These commands also can be used with &lua_all GM-command, for example:
--     &lua_all giftmod:addGift(item_id, number, hour, minute, second, month, day, year)
-- Note: After GameServer.exe restart gifts. which were added using GM-command, will be removed
--------------------------------------------------------------

-- Update guard
if (PKODEV_GIFT_GUARD ~= nil) then
	
	-- Exit the script
	return
	
end

-- Define update guard
PKODEV_GIFT_GUARD = true

-- Print a log
print("Loading pkodev.gift.lua")




-- Class Gift
Gift = {}

-- Make a Gift
function Gift:new(id, item_id, count, gift_time)

	-- Private fields
	local private = {
		id = id or -1,
		item_id = item_id or -1,
		count = count or 0,
		gift_time = gift_time or 0,
		is_given = false
	}
	
	-- Public fields
	local public = { }
	
	
	-- Get an gift ID
	function public:getId()        return private.id end
	
	-- Get an item id
	function public:getItemId()    return private.item_id end
	
	-- Get an item count
	function public:getItemCount() return private.count end
	
	-- Get a time at which to give the gift
	function public:getTime()      return private.gift_time end
	
	
	setmetatable(public, self)
	self.__index = self; 
	
	return public
	
end


-- Class GiftSystem
GiftSystem = {}

-- Make a Gift system
function GiftSystem:new()
	
	-- Private fields
	local private = {
		
		-- List of gifts to give
		gifts = { },
		
		-- List of active players
		players = { },
		
		-- Timer function
		timer_func = nil,
		
		-- List of hooks
		hooks = { }
	
	}
	
	-- Public fields
	local public = { }
	
	
	-- Player entered a map event
	function private:on_player_entered_map(role, mapdesc, map)
		
		-- Check that map exists in the list
		if ( private.hooks[map] ~= nil ) then
			
			-- Check that enter function is hooked
			if ( private.hooks[map].enter ~= nil ) then
			
				-- Call original function
				private.hooks[map].enter(role, mapdesc)
			
			end
			
		end
		
		-- Add player to the list
		private.players[ GetRoleID(role) ] = role
		
	end
	
	-- Player leaved a map event
	function private:on_player_leaved_map(role, map)
		
		-- Check that map exists in the list
		if ( private.hooks[map] ~= nil ) then
			
			-- Check that leave function is hooked
			if ( private.hooks[map].leave ~= nil ) then
			
				-- Call original function
				private.hooks[map].leave(role)
			
			end
			
		end
		
		-- Remove player from the list
		private.players[ GetRoleID(role) ] = nil
		
	end
	
	-- Timer event
	function private:on_timer_event(map)
		
		-- Check that timer function is hooked
		if ( private.timer_func ~= nil ) then
			
			-- Call original function
			private.timer_func(map)
			
		end
		
		-- Get current system time
		local t = os.time()
		
		-- Update gifts
		for i = table.getn(private.gifts), 1, -1 do
			
			-- Get a gift
			local gift = private.gifts[i]
			
			-- Check that it's time to give out the gift
			if ( t >= gift:getTime() ) then
				
				-- Give the gift to players
				for cha_id, role in pairs(private.players) do
				
					GiveItem(role, 0, gift:getItemId(), gift:getItemCount(), 4) 
					
				end
				
				-- Remove the gift from the list
				table.remove(private.gifts, i)
				
			end
			
		end
		
	end
	
	-- Set hooks
	function private:setHook()
		
		-- Search for 'after_enter_', 'before_leave_' and timer functions
		for key, value in pairs(_G) do
			
			-- Get a global item name in lower case
			local name = string.lower(key)
			
			-- Player entered a map
			if ( string.find(name, "after_enter_") == 1 ) then
				
				-- Get map name
				local map = string.sub(name, string.len("after_enter_") + 1)
				
				-- Add map to the list
				private.hooks[map] = private.hooks[map] or {}
				
				-- Associate original enter function address with map name
				private.hooks[map].enter = value
				
				-- Set the hook
				_G[key] = function(role, mapdesc) private:on_player_entered_map(role, mapdesc, map) end
				
			end
			
			-- Player leaved a map
			if ( string.find(name, "before_leave_") == 1 ) then
				
				-- Get map name
				local map = string.sub(name, string.len("before_leave_") + 1)
				
				-- Add map to the list
				private.hooks[map] = private.hooks[map] or {}
							
				-- Associate original leave function address with map name
				private.hooks[map].leave = value
				
				-- Set the hook
				_G[key] = function(role) private:on_player_leaved_map(role, map) end
				
			end
			
			-- Timer
			if ( private.timer_func == nil ) then
			
				-- Search for timer function
				if ( string.find(name, "map_copy_run_") == 1 ) then
					
					-- Number of underscore characters
					local n = 0
					
					-- Count the number of underscore characters
					string.gsub(name, ".", function(c) if ( c == '_' ) then n = n + 1 end end)		
					
					-- Number of underscore characters should be equal to 3
					if (n == 3) then
					
						-- Set timer function
						private.timer_func = value
						
						-- Set the hook
						_G[key] = function(map) private:on_timer_event(map) end
						
					end
					
				end
			
			end
			
		end
		
		-- Check that timer hook is enabled
		if ( private.timer_func == nil ) then
			
			print("pkodev.gift: Warning, suitable timer function is not found!")
			
		end

	end

	-- Add a gift to the list
	function public:addGift(item_id, count, h, m, s, mon, day, year)
	
		-- Check item ID
		if ( string.lower(GetItemName(item_id)) == "unknown" ) then
			
			-- Do not add a gift
			return -1
			
		end
		
		-- Check item count
		if (count <= 0) then
			
			-- Do not add a gift
			return -1
			
		end
	
		-- Get item timestamp
		local gift_timestamp = os.time{
			month = mon,
			day = day,
			year = year, 
			hour = h, 
			min = m,
			sec = s
		}
		
		-- Check that item time is not expired
		if ( gift_timestamp <= os.time() ) then
			
			-- Do not add a gift
			return -1
		
		end
		
		-- Get an ID for new gift
		local gift_id = public:getGiftNumber()
		
		-- Create a gift
		local gift = Gift:new(gift_id, item_id, count, gift_timestamp)
		
		-- Add a gift to the list
		table.insert(private.gifts, gift)

		-- Return gift ID
		return gift_id
	
	end
	
	-- Remove a gift by ID
	function public:removeGift(gift_id)
		
		-- Find a gift in the list
		for index, gift in pairs(private.gifts) do
			
			-- Check gift ID
			if ( gift_id == gift:getId() ) then
				
				-- Remove the gift from the list
				table.remove(private.gifts, index)

				-- Gift removed
				return true
			
			end
			
		end
		
		-- Gift not found
		return false
		
	end
	
	-- Get gift number
	function public:getGiftNumber()
		
		return table.getn(private.gifts)
		
	end
	
	-- Print a list of gifts
	function public:printGifts()
		
		-- Get gift number
		local n = public:getGiftNumber()
		
		-- Check that there are gifts in the list
		if (n > 0) then
		
			-- Print all gifts
			for index, gift in pairs(private.gifts) do
				
				-- Get time data
				local temp = os.date("*t", gift:getTime())
				
				-- Print a gift
				print(
					string.format(
						"pkodev.gift: %d) [Gift ID: %d] - %s x %d at %02d:%02d:%02d %02d/%02d/%02d",
						index,
						gift:getId(),
						GetItemName(gift:getItemId()),
						gift:getItemCount(),
						temp.hour,
						temp.min,
						temp.sec,
						temp.month,
						temp.day,
						temp.year
					)
				)
				
			end
		
		else
			
			-- No gifts
			print("There are no active gifts!")
			
		end
	
	end

	
	-- Enable the necessary hooks for the gift system to work
	private:setHook()

	setmetatable(public, self)
	self.__index = self;

	return public
	
end

-- Create an instance of the gift system
giftmod = GiftSystem:new()

-- Add gifts to the queue
giftmod:addGift(863,   1, 16, 6, 30, 11,  9, 2022) -- 'Gem of Rage'          x 1 at  16:06:30 11/09/2022
giftmod:addGift(684,   1, 16, 7, 25, 11, 15, 2022) -- 'New Sheepskin Scroll' x 1 at  16:07:25 15/09/2022
giftmod:addGift(1849, 99, 12, 7, 0,  11, 18, 2022) -- 'Cake'                 x 99 at 12:07:00 18/09/2022

-- Print queued gifts
giftmod:printGifts()

3) Откройте файл "GameServer\resource\script\monster\mlist.lua" и в самом начале запишите строку:

dofile(GetResPath("script\\calculate\\mods\\pkodev.gift.lua"))

4) Запустите GameServer.exe. В окне консоли Вы должны увидеть следующий вывод:

pkodev.gift: 1) [Gift ID: 0] - Gem of Rage x 1 at 16:06:30 11/09/2022
pkodev.gift: 2) [Gift ID: 1] - New Sheepskin Scroll x 1 at 16:07:25 11/15/2022
pkodev.gift: 3) [Gift ID: 2] - Cake x 99 at 12:07:00 11/18/2022

5) Настройте систему по Вашему усмотрению (см. раздел "Как использовать" далее);

6) Процесс установки системы завершен.

 

 

Как использовать

 

1) Вы можете определить расписание выдачи наград в конце файла "pkodev.gift.lua" используя команду giftmod:addGift():

local gift_id = giftmod:addGift(ID_предмета, количество, час, минута, секунда, месяц, день, год)

Также Вы можете добавить награду в очередь используя GM-команду*

&lua_all giftmod:addGift(ID_предмета, количество, час, минута, секунда, месяц, день, год)

*Примечание: После перезапуска GameServer.exe, все награды, которые были добавлены с помощью GM-команды, удалятся.

 

2) Вы можете удалить награду из расписания с помощью команды "giftmod:removeGift()": 

local success = giftmod:removeGift(gift_id) -- Где 'success' может быть 'true' или 'false'

Где переменная gift_id - это ID награды, которая ранее была добавлена в очередь командой giftmod:addGift().

 

3) Вы можете получить число наград в очереди с помощью команды giftmod:getGiftNumber():

local number = giftmod:getGiftNumber()

4) Наконец, Вы можете отобразить очередь наград в окне консоли GameServer.exe:

giftmod:printGifts()

 

Это все! :smile:

 

Скачать скрипт системы (3 Кб)

  • Like 3
  • Thanks 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...