Advertisement
Karluwu

MF HANGMAN REAL

Mar 2nd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.14 KB | None | 0 0
  1. tfm.exec.disableAfkDeath(true)
  2. tfm.exec.disableAutoShaman(true)
  3. tfm.exec.disableAutoNewGame(true)
  4. tfm.exec.disableAutoScore(true)
  5. tfm.exec.disableAutoTimeLeft(true)
  6.  
  7. chars = {"&","é","~","\"","{","|","è","`","_","à","@","]","+","=","}","¨","ë","ä","ü","ö","£","<",">","0","1","2","3","4","5","6","7","8","9"}
  8.  
  9. lang = {}
  10.  
  11. lang.en = {
  12. ask_word = "Define the word to find",
  13. choose_word = "Choose a word : (between 2 and 12 characters)",
  14. more_players = "You have to be at least <font color='#FF0000'>2</font> players to play Hangman.",
  15. next_turn_1 = "Next turn in ",
  16. next_turn_2 = " second(s).",
  17. turn_of_1 = "It's ",
  18. turn_of_2 = "'s turn ! ",
  19. patientez = "Wait while he's choosing a word...",
  20. word_found = "The word was found !",
  21. word_not_found = "No luck! The word was ",
  22. time_out = "Time out !",
  23. pass_turn = "The game master has passed his turn !",
  24. quit = "The game master has quit."
  25. }
  26.  
  27. text = lang.en
  28.  
  29. players = {}
  30. master = ""
  31.  
  32. letters = {}
  33. invertLetters = {}
  34.  
  35. word = ""
  36. hasDefinedWord = false
  37.  
  38. timer = 0
  39. bestPlayer = ""
  40. pendu_level = 0
  41.  
  42. beginReset = false
  43. hasToReset = false
  44. resetTimer = 0
  45.  
  46. isTimeOut = false
  47. hasWon = false
  48. hasLost = false
  49. hasSkipped = false
  50. hasQuit = false
  51.  
  52. lettersEntered = {}
  53.  
  54. id = {}
  55.  
  56. id["ask_word_main"] = 1
  57. id["ask_word_button"] = 2
  58. id["ask_word_popup"] = 3
  59. id["pendu"] = 4
  60. id["reset_timer"] = 5
  61. id["reset_timer_label"] = 6
  62. id["turn"] = 7
  63. id["turn_label"] = 8
  64. id["one_player"] = 9
  65. id["one_player_label"] = 10
  66.  
  67. function eventNewGame()
  68. updatePlayersList()
  69.  
  70. ui.removeTextArea(id["one_player"])
  71. ui.removeTextArea(id["one_player_label"])
  72.  
  73. letters = {}
  74. invertLetters = {}
  75. word = ""
  76. hasDefinedWord = false
  77. timer = 0
  78.  
  79. if getNbPlayers() > 1 then
  80. master = randomPlayer()
  81. tfm.exec.movePlayer(master, 400, 90, false, 0, 0, false)
  82.  
  83. askWord()
  84. drawPendu()
  85. else
  86. removeAll()
  87.  
  88. ui.addTextArea(id["one_player"], "", nil, 5, 110, 790, 25, 0xC0C0C0, 0x595959, 1f)
  89. ui.addTextArea(id["one_player_label"], "<p align='center'><BL><font color='#000000'>"..text.more_players.."</font></p>", nil, 25, 115, 750, 30, 0xC0C0C0, 0xC0C0C0, 0f)
  90.  
  91. drawWord()
  92. drawPendu()
  93. end
  94. end
  95.  
  96. function eventPlayerDied(playerName)
  97. tfm.exec.respawnPlayer(playerName)
  98. end
  99.  
  100. function eventNewPlayer(playerName)
  101. table.insert(players, playerName)
  102.  
  103. if getNbPlayers() == 2 then
  104. tfm.exec.newGame("@4488917")
  105. else
  106. tfm.exec.respawnPlayer(playerName)
  107. drawWord()
  108. drawPendu()
  109. end
  110. end
  111.  
  112. function eventPlayerLeft(playerName)
  113. local toRemove = 0
  114.  
  115. for i,p in pairs(players) do
  116. if p==playerName then
  117. toRemove = i
  118. end
  119. end
  120.  
  121. table.remove(players, toRemove)
  122.  
  123. if getNbPlayers() == 1 then
  124. tfm.exec.newGame("@4488917")
  125. else
  126. if playerName==master then
  127. hasQuit = true
  128. reset()
  129. end
  130. end
  131. end
  132.  
  133. function eventLoop(currentTime, timeRemaining)
  134. timer = timer + 0.5
  135. if beginReset then
  136. ui.removeTextArea(id["ask_word_main"])
  137. ui.removeTextArea(id["ask_word_button"])
  138.  
  139. resetTimer = resetTimer + 0.5
  140.  
  141. ui.addTextArea(id["reset_timer"], "", nil, 5, 110, 790, 25, 0xC0C0C0, 0x595959, 1f)
  142. if isTimeOut then ui.addTextArea(id["reset_timer_label"], "<p align='center'><BL>"..text.time_out.." <font color='#000000'>"..text.next_turn_1.."<font color='#FF0000'>"..math.floor(10 - resetTimer).."</font>"..text.next_turn_2.."</font></p>", nil, 25, 115, 750, 30, 0xC0C0C0, 0xC0C0C0, 0f) end
  143. if hasWon then ui.addTextArea(id["reset_timer_label"], "<p align='center'><BL>"..text.word_found.." <font color='#000000'>"..text.next_turn_1.."<font color='#FF0000'>"..math.floor(10 - resetTimer).."</font>"..text.next_turn_2.."</font></p>", nil, 25, 115, 750, 30, 0xC0C0C0, 0xC0C0C0, 0f) end
  144. if hasLost then ui.addTextArea(id["reset_timer_label"], "<p align='center'><font color='#000000'>"..text.word_not_found.."</font><BL>"..word:gsub("^%l", string.upper).."<font color='#000000'> ! "..text.next_turn_1.."<font color='#FF0000'>"..math.floor(10 - resetTimer).."</font>"..text.next_turn_2.."</font></p>", nil, 25, 115, 750, 30, 0xC0C0C0, 0xC0C0C0, 0f) end
  145. if hasSkiped then ui.addTextArea(id["reset_timer_label"], "<p align='center'><BL>"..text.pass_turn.."<font color='#000000'> "..text.next_turn_1.."<font color='#FF0000'>"..math.floor(10 - resetTimer).."</font>"..text.next_turn_2.."</font></p>", nil, 25, 115, 750, 30, 0xC0C0C0, 0xC0C0C0, 0f) end
  146. if hasQuit then ui.addTextArea(id["reset_timer_label"], "<p align='center'><BL>"..text.quit.."<font color='#000000'> "..text.next_turn_1.."<font color='#FF0000'>"..math.floor(10 - resetTimer).."</font>"..text.next_turn_2.."</font></p>", nil, 25, 115, 750, 30, 0xC0C0C0, 0xC0C0C0, 0f) end
  147. end
  148.  
  149. checkBestPlayer()
  150.  
  151. if timer==25 and not hasDefinedWord and getNbPlayers() > 1 then
  152. isTimeOut = true
  153. reset()
  154. end
  155.  
  156. if resetTimer==10 then
  157. isTimeOut = false
  158. hasWon = false
  159. hasLost = false
  160. hasSkiped = false
  161. hasQuit = false
  162.  
  163. hasToReset = true
  164.  
  165. reset()
  166. end
  167. end
  168.  
  169. function eventChatCommand(playerName, message)
  170. local args = {}
  171.  
  172. for arg in message:gmatch("[^%s]+") do
  173. table.insert(args, arg:lower())
  174. end
  175.  
  176. if not hasLost and not hasSkiped and not hasQuit and args[1] ~= nil then
  177. if args[1]==word and playerName ~= master and not hasWon then
  178. local score = 0
  179.  
  180. for _,letter in pairs(letters) do
  181. if letter=="_" then score = score + 1 end
  182. end
  183.  
  184. tfm.exec.setPlayerScore(playerName, score, true)
  185.  
  186. local i = 1
  187.  
  188. while i <= word:len() do
  189. if letters[i]~="_" then
  190. invertLetters[i] = letters[i]
  191. letters[i] = "_"
  192. end
  193.  
  194. i = i + 1
  195. end
  196.  
  197. drawWord()
  198. hasWon = true
  199. reset()
  200. end
  201.  
  202. if args[1]=="skip" and playerName==master and not hasWon and not hasLost and not isTimeOut then
  203. hasSkiped = true
  204. reset()
  205. end
  206.  
  207. if args[1]:len()==1 and hasDefinedWord and args[1]~= "_" and args[1]~="-" and args[1]~="'" and playerName ~= master then
  208. local isEntered = false
  209.  
  210. for _,letter in pairs(lettersEntered) do
  211. if letter==args[1] then
  212. isEntered = true
  213. end
  214. end
  215.  
  216. if not isEntered then
  217. local score = 0
  218. local idsToRemove = {}
  219. local isFalse = true
  220.  
  221. table.insert(lettersEntered, args[1])
  222.  
  223. for id,letter in pairs(letters) do
  224. if letter==args[1] then
  225. table.insert(idsToRemove, id)
  226. isFalse = false
  227. end
  228. end
  229.  
  230. for _,idToRemove in pairs(idsToRemove) do
  231. invertLetters[idToRemove] = letters[idToRemove]
  232. letters[idToRemove] = "_"
  233. end
  234.  
  235. score = #idsToRemove
  236.  
  237. if isFalse then
  238. if tfm.get.room.playerList[playerName].score > 0 then score = -1 end
  239. pendu_level = pendu_level + 1
  240. end
  241.  
  242. tfm.exec.setPlayerScore(playerName, score, true)
  243.  
  244. drawWord()
  245. drawPendu()
  246. end
  247. end
  248. end
  249. end
  250.  
  251. function eventTextAreaCallback(textAreaId, playerName, callback)
  252. if callback=="callbackAskWord" then
  253. ui.addPopup(id["ask_word_popup"], 2, text.choose_word, master, 300, 175, 200)
  254. end
  255. end
  256.  
  257. function eventPopupAnswer(popupId, playerName, answer)
  258. if popupId==id["ask_word_popup"] and not isTimeOut and master==playerName then
  259. local choosedWord = tostring(answer)
  260.  
  261. if checkWord(choosedWord) then
  262. defineWord(choosedWord)
  263. hasDefinedWord = true
  264.  
  265. askWord()
  266.  
  267. ui.removeTextArea(id["turn"])
  268. ui.removeTextArea(id["turn_label"])
  269. end
  270. end
  271. end
  272.  
  273. function askWord()
  274. ui.removeTextArea(id["reset_timer"])
  275. ui.removeTextArea(id["reset_timer_label"])
  276. ui.removeTextArea(id["ask_word_main"])
  277. ui.removeTextArea(id["ask_word_button"])
  278.  
  279. if not hasDefinedWord then
  280. ui.addTextArea(id["ask_word_main"], "", master, 5, 110, 790, 35, 0xC0C0C0, 0x595959, 1f)
  281. ui.addTextArea(id["ask_word_button"], "<p align='center'><a href='event:callbackAskWord'>"..text.ask_word.."</a></p>", master, 300, 120, 190, 16, 0x595959, 0x595959, 1f)
  282.  
  283. for p,_ in pairs(tfm.get.room.playerList) do
  284. if p~=master then
  285. ui.addTextArea(id["turn"], "", p, 5, 110, 790, 25, 0xC0C0C0, 0x595959, 1f)
  286. ui.addTextArea(id["turn_label"], "<p align='center'><font color='#000000'>"..text.turn_of_1.."</font><BL>"..master.."<font color='#000000'>"..text.turn_of_2..text.patientez.."</font></p>", p, 25, 115, 750, 30, 0xC0C0C0, 0xC0C0C0, 0f)
  287. end
  288. end
  289. end
  290. end
  291.  
  292. function defineWord(new_word)
  293. word = string.lower(string.gsub(new_word, " ", "-"))
  294.  
  295. letters = {}
  296.  
  297. local i = 36
  298.  
  299. while i < 50 do
  300. ui.removeTextArea(i)
  301. i = i + 1
  302. end
  303.  
  304. for letter in new_word:gmatch"." do
  305. if letter==" " or letter=="-" then
  306. table.insert(invertLetters, "-")
  307. table.insert(letters, "_")
  308. elseif letter=="'" then
  309. table.insert(invertLetters, "'")
  310. table.insert(letters, "_")
  311. else
  312. table.insert(letters, letter:lower())
  313. table.insert(invertLetters, "_")
  314. end
  315. end
  316.  
  317. drawWord()
  318. drawPendu()
  319. end
  320.  
  321. function drawWord()
  322. local textId = 36
  323. local i = 1
  324. local ancreX = 40
  325.  
  326. if #word==0 then
  327. local i = 36
  328.  
  329. while i < 50 do
  330. ui.removeTextArea(i)
  331. i = i + 1
  332. end
  333. else
  334. while i <= word:len() do
  335. ui.addTextArea(textId, "<p align='center'><font size='40' color='#000000'>"..invertLetters[i]:upper().."</font></p>", nil, ancreX, 150, 40, 60, 0xC0C0C0, 0xC0C0C0, 1f)
  336. ancreX = ancreX + 60
  337. textId = textId + 1
  338. i = i + 1
  339. end
  340.  
  341. local finished = true
  342. local j = 1
  343.  
  344. while j <= word:len() do
  345. if invertLetters[j]=="_" then finished = false end
  346. j = j + 1
  347. end
  348.  
  349. if finished then
  350. hasWon = true
  351. reset()
  352. end
  353. end
  354. end
  355.  
  356. function drawPendu()
  357. local pendu = ""
  358.  
  359. if pendu_level==1 then
  360. pendu = pendu.."<br /><br /><br /><br /><br /><br /><br /><br /><br /> _________"
  361.  
  362. elseif pendu_level==2 then
  363. pendu = pendu.."<br />"
  364. pendu = pendu.." |<br />"
  365. pendu = pendu.." |<br />"
  366. pendu = pendu.." |<br />"
  367. pendu = pendu.." |<br />"
  368. pendu = pendu.." |<br />"
  369. pendu = pendu.." |<br />"
  370. pendu = pendu.." |<br />"
  371. pendu = pendu.." |<br />"
  372. pendu = pendu.." ____|____"
  373.  
  374. elseif pendu_level==3 then
  375. pendu = pendu.." __________.__<br />"
  376. pendu = pendu.." |<br />"
  377. pendu = pendu.." |<br />"
  378. pendu = pendu.." |<br />"
  379. pendu = pendu.." |<br />"
  380. pendu = pendu.." |<br />"
  381. pendu = pendu.." |<br />"
  382. pendu = pendu.." |<br />"
  383. pendu = pendu.." |<br />"
  384. pendu = pendu.." ____|____"
  385.  
  386. elseif pendu_level==4 then
  387. pendu = pendu.." __________.__<br />"
  388. pendu = pendu.." | /<br />"
  389. pendu = pendu.." |/<br />"
  390. pendu = pendu.." |<br />"
  391. pendu = pendu.." |<br />"
  392. pendu = pendu.." |<br />"
  393. pendu = pendu.." |<br />"
  394. pendu = pendu.." |<br />"
  395. pendu = pendu.." |<br />"
  396. pendu = pendu.." ____|____"
  397.  
  398. elseif pendu_level==5 then
  399. pendu = pendu.." __________.__<br />"
  400. pendu = pendu.." | / |<br />"
  401. pendu = pendu.." |/<br />"
  402. pendu = pendu.." |<br />"
  403. pendu = pendu.." |<br />"
  404. pendu = pendu.." |<br />"
  405. pendu = pendu.." |<br />"
  406. pendu = pendu.." |<br />"
  407. pendu = pendu.." |<br />"
  408. pendu = pendu.." ____|____"
  409.  
  410. elseif pendu_level==6 then
  411. pendu = pendu.." __________.__<br />"
  412. pendu = pendu.." | / |<br />"
  413. pendu = pendu.." |/ O<br />"
  414. pendu = pendu.." |<br />"
  415. pendu = pendu.." |<br />"
  416. pendu = pendu.." |<br />"
  417. pendu = pendu.." |<br />"
  418. pendu = pendu.." |<br />"
  419. pendu = pendu.." |<br />"
  420. pendu = pendu.." ____|____"
  421.  
  422. elseif pendu_level==7 then
  423. pendu = pendu.." __________.__<br />"
  424. pendu = pendu.." | / |<br />"
  425. pendu = pendu.." |/ O<br />"
  426. pendu = pendu.." | /|<br />"
  427. pendu = pendu.." |<br />"
  428. pendu = pendu.." |<br />"
  429. pendu = pendu.." |<br />"
  430. pendu = pendu.." |<br />"
  431. pendu = pendu.." |<br />"
  432. pendu = pendu.." ____|____"
  433.  
  434. elseif pendu_level==8 then
  435. pendu = pendu.." __________.__<br />"
  436. pendu = pendu.." | / |<br />"
  437. pendu = pendu.." |/ O<br />"
  438. pendu = pendu.." | /|\<br />"
  439. pendu = pendu.." |<br />"
  440. pendu = pendu.." |<br />"
  441. pendu = pendu.." |<br />"
  442. pendu = pendu.." |<br />"
  443. pendu = pendu.." |<br />"
  444. pendu = pendu.." ____|____"
  445.  
  446. elseif pendu_level==9 then
  447. pendu = pendu.." __________.__<br />"
  448. pendu = pendu.." | / |<br />"
  449. pendu = pendu.." |/ O<br />"
  450. pendu = pendu.." | /|\\<br />"
  451. pendu = pendu.." | |<br />"
  452. pendu = pendu.." |<br />"
  453. pendu = pendu.." |<br />"
  454. pendu = pendu.." |<br />"
  455. pendu = pendu.." |<br />"
  456. pendu = pendu.." ____|____"
  457.  
  458. elseif pendu_level==10 then
  459. pendu = pendu.." __________.__<br />"
  460. pendu = pendu.." | / |<br />"
  461. pendu = pendu.." |/ O<br />"
  462. pendu = pendu.." | /|\\<br />"
  463. pendu = pendu.." | |<br />"
  464. pendu = pendu.." | /<br />"
  465. pendu = pendu.." |<br />"
  466. pendu = pendu.." |<br />"
  467. pendu = pendu.." |<br />"
  468. pendu = pendu.." ____|____"
  469.  
  470. elseif pendu_level==11 then
  471. pendu = pendu.." __________.__<br />"
  472. pendu = pendu.." | / | <br />"
  473. pendu = pendu.." |/ O <br />"
  474. pendu = pendu.." | /|\\ <br />"
  475. pendu = pendu.." | | <br />"
  476. pendu = pendu.." | / \\ <br />"
  477. pendu = pendu.." | <br />"
  478. pendu = pendu.." | <br />"
  479. pendu = pendu.." | <br />"
  480. pendu = pendu.." ____|____"
  481.  
  482. hasLost = true
  483. reset()
  484. end
  485.  
  486. ui.addTextArea(id["pendu"], pendu, nil, 323, 235, 135, 138, 0x010101, 0xFFFFFF, 0.5f)
  487. end
  488.  
  489. function reset()
  490. beginReset = true
  491.  
  492. if hasToReset then
  493. if getNbPlayers() < 2 then
  494. tfm.exec.newGame("@4488917")
  495. else
  496. letters = {}
  497. invertLetters = {}
  498. word = ""
  499. hasDefinedWord = false
  500. pendu_level = 0
  501. beginReset = false
  502. hasToReset = false
  503. resetTimer = 0
  504. lettersEntered = {}
  505.  
  506. drawWord()
  507. drawPendu()
  508.  
  509. local randX = math.random(799)
  510. tfm.exec.movePlayer(master, randX, 385, false, 0, 0, false)
  511.  
  512. local oldMaster = master
  513.  
  514. if getNbPlayers()~=1 then
  515. if bestPlayer==oldMaster then
  516. while master==oldMaster do
  517. master = randomPlayer()
  518. end
  519. else
  520. master = bestPlayer
  521. end
  522. else
  523. master = bestPlayer
  524. end
  525.  
  526. randX = math.random(799)
  527.  
  528. tfm.exec.movePlayer(master, randX, 90, false, 0, 0, false)
  529. tfm.exec.setPlayerScore(master, 0, false)
  530.  
  531. timer = 0
  532.  
  533. askWord()
  534. end
  535. end
  536. end
  537.  
  538. function removeAll()
  539. ui.removeTextArea(id["ask_word_main"])
  540. ui.removeTextArea(id["ask_word_button"])
  541. ui.removeTextArea(id["ask_word_popup"])
  542. ui.removeTextArea(id["pendu"])
  543. ui.removeTextArea(id["reset_timer"])
  544. ui.removeTextArea(id["reset_timer_label"])
  545. ui.removeTextArea(id["turn"])
  546. ui.removeTextArea(id["turn_label"])
  547. ui.removeTextArea(id["one_player"])
  548. ui.removeTextArea(id["one_player_label"])
  549. end
  550.  
  551. function checkWord(word_arg)
  552. if word_arg:len() >= 2 and word_arg:len() <= 12 then
  553. for _,c in pairs(chars) do
  554. if string.match(word_arg, c) then
  555. return false
  556. end
  557. end
  558.  
  559. return true
  560. else
  561. return false
  562. end
  563. end
  564.  
  565. function checkBestPlayer()
  566. topScore = 0
  567. bestPlayer = randomPlayer()
  568.  
  569. for name,player in pairs(tfm.get.room.playerList) do
  570. if player.score >= topScore then
  571. topScore = player.score
  572. bestPlayer = name
  573. end
  574. end
  575. end
  576.  
  577. function getNbPlayers()
  578. return #players
  579. end
  580.  
  581. function updatePlayersList()
  582. players = {}
  583.  
  584. for p,_ in pairs(tfm.get.room.playerList) do
  585. table.insert(players, p)
  586. end
  587. end
  588.  
  589. function randomPlayer()
  590. return players[math.random(1,#players)]
  591. end
  592.  
  593. for name,player in pairs(tfm.get.room.playerList) do
  594. tfm.exec.setPlayerScore(name, 0, false)
  595. end
  596.  
  597. updatePlayersList()
  598. bestPlayer = randomPlayer()
  599. tfm.exec.newGame("@444666")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement