functions.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. function lobby.load()
  2. local file = io.open(minetest.get_worldpath() .. '/levels', 'r')
  3. if file then
  4. lobby.savedata = minetest.deserialize(file:read())
  5. file:close()
  6. else
  7. lobby.savedata = {}
  8. lobby.savedata.IDs = {}
  9. lobby.savedata.data = {}
  10. end
  11. end
  12. function lobby.save()
  13. local file = io.open(minetest.get_worldpath() .. '/levels', 'w')
  14. file:write(minetest.serialize(lobby.savedata))
  15. file:close()
  16. end
  17. function lobby.corpse_entry(pos, map_id)
  18. local corpse_pos = lobby.corpses[map_id]
  19. table.insert(corpse_pos, pos)
  20. lobby.corpses[map_id] = corpse_pos
  21. end
  22. function lobby.corpse_removal(map_id)
  23. local corpse_pos = lobby.corpses[map_id]
  24. for key, value in pairs(corpse_pos) do
  25. minetest.set_node(value, {name='air'})
  26. end
  27. end
  28. function lobby.players_on_level(map_id)
  29. local survivors = ''
  30. for _, player in pairs(minetest.get_connected_players()) do
  31. local rname = player:get_player_name()
  32. if lobby.game[rname] == map_id then
  33. survivors = survivors..rname..', '
  34. end
  35. end
  36. return survivors
  37. end
  38. function lobby.update_maps(map_name)
  39. local count = lobby.map[map_name] - 1
  40. if count <= 0 then
  41. lobby.map[map_name] = 0
  42. else
  43. lobby.map[map_name] = count
  44. end
  45. end
  46. function lobby.vote(map_id, force)
  47. local needed_votes = lobby.map[map_id] - 1
  48. local actual_votes = lobby.votes[map_id]
  49. if actual_votes >= needed_votes or force then
  50. lobby.vote_timer[map_id] = 0
  51. --Reset vote status and kick player with the highest score.
  52. local high_score = 0
  53. local kick = ''
  54. local traitor = lobby.traitors[map_id]
  55. for _, player in pairs(minetest.get_connected_players()) do
  56. local rname = player:get_player_name()
  57. if lobby.game[rname] == map_id then
  58. local score = lobby.suspect[rname]
  59. if score > high_score then
  60. high_score = score
  61. kick = rname
  62. end
  63. lobby.suspect[rname] = 0
  64. player:set_physics_override({speed=1})
  65. if kick == traitor then
  66. lobby.team_win(map_id)
  67. elseif needed_votes == 2 and kick ~= traitor then
  68. lobby.traitor_win(traitor, map_id)
  69. end
  70. end
  71. end
  72. if kick ~= '' then
  73. lobby.message_to_level(map_id, 'The votes are in, '..kick..' will be kicked.')
  74. player = minetest.get_player_by_name(kick)
  75. player:setpos({x=0, y=0, z=0})
  76. local player_inv = player:get_inventory()
  77. player_inv:set_list('main', {})
  78. lobby.game[kick] = 'lobby'
  79. lobby.update_maps(map_id)
  80. else
  81. lobby.message_to_level(map_id, 'Nobody was kicked this round.')
  82. end
  83. end
  84. end
  85. function lobby.team_win(map_id)
  86. lobby.corpse_removal(map_id)
  87. for _, player in pairs(minetest.get_connected_players()) do
  88. local rname = player:get_player_name()
  89. if lobby.game[rname] == map_id then
  90. minetest.chat_send_player(rname, 'The traitor was defeated.')
  91. player:setpos({x=0, y=0, z=0})
  92. player:set_physics_override({speed=1})
  93. local player_inv = player:get_inventory()
  94. player_inv:set_list('main', {})
  95. lobby.game[rname] = 'lobby'
  96. end
  97. end
  98. lobby.map[map_id] = 0
  99. minetest.chat_send_all('The traitor was defeated on this round in the '..map_id)
  100. end
  101. function lobby.traitor_win(traitor, map_id)
  102. local player = minetest.get_player_by_name(traitor)
  103. lobby.corpse_removal(map_id)
  104. minetest.chat_send_all('The traitor won this round on '..map_id..'.')
  105. minetest.chat_send_player(traitor, 'CONGRATULATIONS!!! You killed all the crew!')
  106. lobby.game[traitor] = 'lobby'
  107. player:setpos({x=0, y=0, z=0})
  108. player:set_physics_override({speed=1})
  109. local player_inv = player:get_inventory()
  110. player_inv:set_list('main', {})
  111. lobby.map[map_id] = 0
  112. end