Kod:
--[[ Returns: The time format in years, months, days, hours, minutes and seconds of a 'sec' amount of time (in seconds) to format. Example: get_time_format(52165786) => returns "1 year, 7 months, 25 days, 11 hours, 17 minutes and 34 seconds". ]] get_time_format = function(sec) local sec = tonumber(sec); local final_str = ""; local string_type = ""; local epoch = {["hour"] = 1, ["day"] = 1, ["month"] = 1, ["year"] = 1970}; local time_formats = { {["date"] = tonumber(os.date('%S', sec)), ["plural"] = "seconds", ["singular"] = "second"}, {["date"] = tonumber(os.date('%M', sec)), ["plural"] = "minutes", ["singular"] = "minute"}, {["date"] = tonumber(os.date('%H', sec))-epoch["hour"], ["plural"] = "hours", ["singular"] = "hour"}, {["date"] = tonumber(os.date('%d', sec))-epoch["day"], ["plural"] = "days", ["singular"] = "day"}, {["date"] = tonumber(os.date('%m', sec))-epoch["month"], ["plural"] = "months", ["singular"] = "month"}, {["date"] = tonumber(os.date('%Y', sec))-epoch["year"], ["plural"] = "years", ["singular"] = "year"} }; for strings, data in time_formats do if (data["date"] > 0) then if (strings > 1) then final_str = string.format(" %s", final_str); end -- if if (data["date"] > 1) then string_type = data["plural"]; else string_type = data["singular"]; end -- if/else final_str = string.format("%d %s%s", data["date"], string_type, final_str); end -- if end -- for return final_str; end -- function
Metin2 Lua İçin Yeni Zaman Fonksiyonu
Metin2 özel sunucularında geliştirme yaparken zamanla ilgili işlemler oldukça önemli bir rol oynar. Özellikle PvP sistemlerinde zaman bazlı etkinlikler, turnuvalar, goblinlerin belirli aralıklarla ortaya çıkması gibi durumlar için doğru zaman fonksiyonları kullanılır. Bu yazıda, Metin2Lobby olarak, Lua script tarafında kullanabileceğiniz yeni bir zaman fonksiyonu sunuyoruz.
Zaman Fonksiyonunun Amacı
Bu fonksiyon sayesinde sunucu tarafında gerçek zamanlı olarak sistem saati okunabilir, zaman farkları hesaplanabilir ve oyuncular için özel zamanlanmış görevler planlanabilir. Özellikle turnuva veya etkinlik gibi zaman duyarlı sistemlerde büyük kolaylık sağlar.
Fonksiyon Tanımı ve Kullanımı
Kod:
[BR][/BR]function GetCurrentTime()[BR][/BR] local currentTime = os.time(os.date('*t'))[BR][/BR] return currentTime[BR][/BR]end[BR][/BR][BR][/BR]function GetFormattedTime(format)[BR][/BR] local currentTime = os.date(format or '%Y-%m-%d %H:%M:%S')[BR][/BR] return currentTime[BR][/BR]end[BR][/BR][BR][/BR]function TimeDifference(start, finish)[BR][/BR] return math.abs(finish - start)[BR][/BR]end[BR][/BR]
Örnek Kullanım Senaryosu
Bir PvP turnuvası başlatıldığında, turnuvanın başlangıç zamanı kaydedilir. Turnuva süresi 30 dakika sürdüğüne göre, kalan süreyi hesaplamak için bu fonksiyonlar kullanılabilir:
Kod:
[BR][/BR]local startTime = GetCurrentTime()[BR][/BR]local endTime = startTime + (30 * 60) -- 30 dakika[BR][/BR][BR][/BR]local remainingTime = TimeDifference(GetCurrentTime(), endTime)[BR][/BR]chat('Turnuvaya kalan sure: ' .. remainingTime .. ' saniye')[BR][/BR]
C++ ile Entegrasyon
Bu Lua fonksiyonları, C++ tabanlı Metin2 sunucu kaynak kodları ile entegre çalışarak zaman tabanlı sistemleri daha esnek hale getirir. Özellikle DB Core tarafında zaman damgaları ile eşleştirildiğinde, loglama, raporlama ve otomatik etkinlik başlatma gibi işlemler daha verimli hale gelir.
Avantajları
- Gerçek zamanlı saat okuma
- Formatlı tarih/saat çıktısı
- Kolay zaman farkı hesaplama
- PvP sistemlerinde zamansal kontrol sağlama
Sonuç
Metin2 özel sunucu geliştiricileri için bu tür zaman fonksiyonları, sistemin daha kararlı ve kullanıcı dostu olmasına yardımcı olur. Geliştiriciler, bu yapıyı kullanarak daha sofistike zaman kontrollü sistemler inşa edebilir.
New Time Function for Metin2 Lua
In Metin2 private server development, time-related operations play a crucial role. Especially in PvP systems, events based on time such as tournaments, timed monster spawns like goblins, require accurate time functions. In this article, we present a new time function usable on the Lua script side.
Purpose of the Time Function
With this function, the system clock can be read in real-time on the server side, time differences can be calculated, and special time-based tasks for players can be scheduled. It provides great convenience in time-sensitive systems such as tournaments or events.
Function Definition and Usage
Kod:
[BR][/BR]function GetCurrentTime()[BR][/BR] local currentTime = os.time(os.date('*t'))[BR][/BR] return currentTime[BR][/BR]end[BR][/BR][BR][/BR]function GetFormattedTime(format)[BR][/BR] local currentTime = os.date(format or '%Y-%m-%d %H:%M:%S')[BR][/BR] return currentTime[BR][/BR]end[BR][/BR][BR][/BR]function TimeDifference(start, finish)[BR][/BR] return math.abs(finish - start)[BR][/BR]end[BR][/BR]
Example Use Case
When a PvP tournament starts, the start time is recorded. If the tournament lasts 30 minutes, these functions can be used to calculate the remaining time:
Kod:
[BR][/BR]local startTime = GetCurrentTime()[BR][/BR]local endTime = startTime + (30 * 60) -- 30 minutes[BR][/BR][BR][/BR]local remainingTime = TimeDifference(GetCurrentTime(), endTime)[BR][/BR]chat('Remaining time until end: ' .. remainingTime .. ' seconds')[BR][/BR]
Integration with C++
These Lua functions work integrated with Metin2 server source codes based on C++, making time-based systems more flexible. When paired with timestamp entries in the DB Core, operations such as logging, reporting, and automatic event triggering become more efficient.
Advantages
- Real-time clock reading
- Formatted date/time output
- Easy time difference calculation
- Time-based control in PvP systems
Conclusion
For Metin2 private server developers, such time functions help make the system more stable and user-friendly. Developers can build more sophisticated time-controlled systems using this structure.
