1function onModalWindow(player, modalWindowId, buttonId, choiceId)
2
3 player:unregisterEvent("DungeonSystem_Modal")
4
5 local dg = DUNGEON_SYSTEM.Dungeons
6 local msg = DUNGEON_SYSTEM.Messages
7 local modal = DUNGEON_SYSTEM.Modal
8 local sto = DUNGEON_SYSTEM.Storages
9
10 if (modalWindowId == DUNGEON_SYSTEM.Modal.ID) then
11
12 if buttonId == 200 then
13
14 local players = {}
15 for x = dg[choiceId].FromPos.x, dg[choiceId].ToPos.x do
16 for y = dg[choiceId].FromPos.y, dg[choiceId].ToPos.y do
17 for z = dg[choiceId].FromPos.z, dg[choiceId].ToPos.z do
18 local tile = Tile(x, y, z)
19 local creature = tile:getTopCreature()
20 if creature and creature:isPlayer() then
21 players[#players+1] = creature
22 end
23 end
24 end
25 end
26
27 if #players > 0 then
28 player:getPosition():sendMagicEffect(CONST_ME_POFF)
29 return player:sendTextMessage(MESSAGE_STATUS_SMALL, msg.PlayerInside)
30 end
31
32 if (dg[choiceId].NeedLevel) then
33 if (player:getLevel() < dg[choiceId].Level) then
34 player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format(msg.MsgNeedLevel, dg[choiceId].Level))
35 return false
36 end
37 end
38
39 local pt = player:getParty()
40 local partyMembers
41
42 local party = {}
43 party = partyMembers
44
45 local fromPosPlayer = player:getPosition()
46
47 if (dg[choiceId].NeedParty) then
48
49 if not pt then
50
51 player:sendTextMessage(MESSAGE_EVENT_ADVANCE, msg.MsgUniqueNeedParty)
52 return false
53
54 else
55
56 partyMembers = pt:getMembers()
57 local party = player:getParty():getMemberCount()
58
59 if not (pt:getLeader():getName() == player:getName()) then
60
61 player:sendTextMessage(MESSAGE_EVENT_ADVANCE, msg.MsgLeaderParty)
62 return false
63
64 else
65
66 local partyMaisUm = party+1
67
68 if not (partyMaisUm == dg[choiceId].AmountParty) then
69 player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format(msg.MsgNeedParty, dg[choiceId].AmountParty))
70 return false
71 end
72
73 local names = {}
74 local posOfPlayer = {}
75
76 for i = 1, party do
77
78 local ppt = partyMembers[i]
79 local fromPosPpt = ppt:getPosition()
80
81 if DUNGEON_SYSTEM.PzToEntry then
82 if not getTileInfo(fromPosPpt).protection then
83 player:getPosition():sendMagicEffect(CONST_ME_POFF)
84 return player:sendTextMessage(MESSAGE_EVENT_ADVANCE, msg.NeedPzMsg)
85 elseif not getTileInfo(fromPosPlayer).protection then
86 player:getPosition():sendMagicEffect(CONST_ME_POFF)
87 return player:sendTextMessage(MESSAGE_EVENT_ADVANCE, msg.NeedPzSoloMsg)
88 end
89 end
90
91 -- SE TEM COOLDOWN
92 if player:getStorageValue(sto.TimerCooldown) - os.time() > 0 or ppt:getStorageValue(sto.TimerCooldown) - os.time() > 0 then
93 if player:getStorageValue(sto.TimerCooldown) - os.time() > 0 then
94 player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format(msg.ToCooldown, string.format(player:getStorageValue(sto.TimerCooldown)-os.time())))
95 player:getPosition():sendMagicEffect(CONST_ME_POFF)
96 ppt:getPosition():sendMagicEffect(CONST_ME_POFF)
97 else
98 player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format(msg.WaitFriendsCooldown, string.format(ppt:getStorageValue(sto.TimerCooldown)-os.time())))
99 player:getPosition():sendMagicEffect(CONST_ME_POFF)
100 ppt:getPosition():sendMagicEffect(CONST_ME_POFF)
101 end
102 return false
103 end
104
105 names[i] = ppt:getName()
106
107 end
108
109 local positionPlayer = player:getPosition()
110 local distance = DUNGEON_SYSTEM.SQMsDistanceOfLeader
111
112 for x = positionPlayer.x - distance, positionPlayer.x + distance do
113 for y = positionPlayer.y - distance, positionPlayer.y + distance do
114 for z = positionPlayer.z, positionPlayer.z do
115 local tileP = Tile(x, y, z)
116 local creatureP = tileP:getTopCreature()
117 if creatureP and creatureP:isPlayer() then
118 for xx = 1, #names do
119 if creatureP:getName() == names[xx] then
120 posOfPlayer[#posOfPlayer+1] = creatureP
121 end
122 end
123 end
124 end
125 end
126 end
127
128 if #posOfPlayer+1 ~= dg[choiceId].AmountParty then
129 return player:sendTextMessage(MESSAGE_EVENT_ADVANCE, msg.MsgDistanceLeader)
130 end
131
132 if dg[choiceId].SpawnMonsters then
133 for m, l in pairs(dg[choiceId].Monsters) do
134 Game.createMonster(m, l)
135 end
136 end
137
138 player:teleportTo(dg[choiceId].DungeonPos)
139 player:getPosition():sendMagicEffect(40)
140 player:say(msg.ToEntry, TALKTYPE_MONSTER_SAY)
141
142 addEvent(function()
143 player:teleportTo(fromPosPlayer)
144 player:setStorageValue(sto.TimerCooldown, os.time() + DUNGEON_SYSTEM.CooldownTime)
145 player:getPosition():sendMagicEffect(40)
146 player:popupFYI(msg.ToFail)
147
148 -- Remove All monsters
149 local monsterPos = {}
150 for x = dg[choiceId].FromPos.x, dg[choiceId].ToPos.x do
151 for y = dg[choiceId].FromPos.y, dg[choiceId].ToPos.y do
152 for z = dg[choiceId].FromPos.z, dg[choiceId].ToPos.z do
153 local tile = Tile(x, y, z)
154 local monster = tile:getTopCreature()
155 if monster and monster:isMonster() then
156 monster:remove()
157 end
158 end
159 end
160 end
161
162 end, dg[choiceId].DungeonTime*1000)
163
164 for mx = 1, party do
165
166 local ppt = partyMembers[mx]
167 local fromPosPpt = ppt:getPosition()
168
169 ppt:teleportTo(dg[choiceId].DungeonPos)
170 ppt:getPosition():sendMagicEffect(40)
171
172 addEvent(function()
173 ppt:teleportTo(fromPosPpt)
174 ppt:setStorageValue(sto.TimerCooldown, os.time() + DUNGEON_SYSTEM.CooldownTime)
175 ppt:getPosition():sendMagicEffect(40)
176 ppt:popupFYI(msg.ToFail)
177 end, dg[choiceId].DungeonTime*1000)
178
179 end
180
181 end
182
183 end
184
185 else
186
187 if DUNGEON_SYSTEM.PzToEntry then
188 if not getTileInfo(fromPosPlayer).protection then
189 player:getPosition():sendMagicEffect(CONST_ME_POFF)
190 return player:sendTextMessage(MESSAGE_EVENT_ADVANCE, msg.NeedPzSoloMsg)
191 end
192 end
193
194 if player:getStorageValue(sto.TimerCooldown) - os.time() > 0 then
195 player:getPosition():sendMagicEffect(CONST_ME_POFF)
196 player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format(msg.ToCooldown, string.format(player:getStorageValue(sto.TimerCooldown)-os.time())))
197 return false
198 end
199
200 if dg[choiceId].SpawnMonsters then
201 for m, l in pairs(dg[choiceId].Monsters) do
202 Game.createMonster(m, l)
203 end
204 end
205
206 -- To Dungeon
207 player:teleportTo(dg[choiceId].DungeonPos)
208 player:getPosition():sendMagicEffect(40)
209 player:say(msg.ToEntry, TALKTYPE_MONSTER_SAY)
210 player:setStorageValue(sto.TimerDungeon, os.time() + dg[choiceId].DungeonTime)
211
212 -- Back Dungeon
213 addEvent(function()
214 player:teleportTo(fromPosPlayer)
215 player:setStorageValue(sto.TimerCooldown, os.time() + DUNGEON_SYSTEM.CooldownTime)
216 player:getPosition():sendMagicEffect(40)
217 player:popupFYI(msg.ToFail)
218
219 -- Remove All monsters
220 local monsterPos = {}
221 for x = dg[choiceId].FromPos.x, dg[choiceId].ToPos.x do
222 for y = dg[choiceId].FromPos.y, dg[choiceId].ToPos.y do
223 for z = dg[choiceId].FromPos.z, dg[choiceId].ToPos.z do
224 local tile = Tile(x, y, z)
225 local monster = tile:getTopCreature()
226 if monster and monster:isMonster() then
227 monster:remove()
228 end
229 end
230 end
231 end
232
233 end, dg[choiceId].DungeonTime*1000)
234
235 end
236
237
238 elseif buttonId == 201 then
239
240 player:registerEvent("DungeonSystemDetails_Modal")
241
242 local needPartyMsg, needLevelMsg = "", ""
243
244 if (dg[choiceId].NeedParty) then
245 needPartyMsg = string.format("Party: %s players", dg[choiceId].AmountParty)
246 else
247 needPartyMsg = "Party: 1 players"
248 end
249
250 if (dg[choiceId].NeedLevel) then
251 needLevelMsg = string.format("Level: %s+", dg[choiceId].Level)
252 else
253 needLevelMsg = "Don't need Level"
254 end
255
256 local currentTime = os.time()
257 local endTime = dg[choiceId].DungeonTime + currentTime -- Calculate the end time
258
259 -- Check if the dungeon time has already passed
260 if endTime < currentTime then
261 endTime = currentTime -- Set it to the current time to avoid negative values
262 end
263
264 local timeDiff = endTime - currentTime
265
266 -- Calculate hours, minutes, and seconds as before
267 local hours = math.floor(timeDiff / 3600)
268 local minutes = math.floor((timeDiff % 3600) / 60)
269 local seconds = timeDiff % 60
270
271 -- Format the time difference as a string
272 local timeDiffStr = string.format("%02d:%02d:%02d", hours, minutes, seconds)
273
274
275 local detailsModal = ModalWindow(modal.IDDetails, modal.TitleDetails, string.format(" Dungeon %s \n\n%s\n%s\nTime to make Dungeon: %s", dg[choiceId].Name, needPartyMsg, needLevelMsg, timeDiffStr))
276
277
278 detailsModal:addButton(100, "Back")
279
280 detailsModal:setDefaultEnterButton(100)
281 detailsModal:setDefaultEscapeButton(100)
282
283 detailsModal:sendToPlayer(player)
284
285 -- player:popupFYI(string.format(">> Dungeon %s <<\n\n" .. needPartyMsg .. "\n" .. needLevelMsg .."\n\nTime to Dungeon: %s", dg[choiceId].Name, string.format(dg[choiceId].DungeonTime)))
286
287 end
288
289 end
290
291end