Skip to content

Instantly share code, notes, and snippets.

@mkarneim
Created August 27, 2018 12:28
Show Gist options
  • Select an option

  • Save mkarneim/f274c10e0d9addbd53cbf397b12581ad to your computer and use it in GitHub Desktop.

Select an option

Save mkarneim/f274c10e0d9addbd53cbf397b12581ad to your computer and use it in GitHub Desktop.
-- last-man-standing.lua
-- /lua require('last-man-standing').start("gold",{"mickkay","piggy"})
local pkg = {}
local module = ...
local log
local singleton
local contains
local remove
local leave
local joinPlayers
local findPlayers
local terminate
local copy
local tell
local arena
local spellPos
local playerList
local joinedPlayers
local winner = nil
function pkg.start(aArenaName, aPlayerNames)
if not aArenaName then
error("Missing mandatory parameter aArenaName")
end
arenaName = aArenaName
if not aPlayerNames then
error("Missing mandatory parameter aPlayerNames")
end
if #aPlayerNames == 0 then
error("At least one player is required")
end
spellPos = spell.pos:floor()
singleton(module.."."..arenaName)
joinedPlayers = joinPlayers(aPlayerNames)
playerList = copy(joinedPlayers)
local queue = Events.collect("LivingDeathEvent")
while true do
local event = queue:next(20)
if event then
if event.name == "LivingDeathEvent" then --and type(event.entity)=="Player" then
if contains(playerList, event.entity) then
remove(playerList, event.entity)
leave(event.entity)
end
end
end
if not event then
if #playerList == 1 then
winner = playerList[1].name
terminate()
end
if #playerList == 0 then
terminate()
end
end
end
end
-- Logs the given message into the chat
function log( message, ...)
local n = select('#', ...)
if n>0 then
message = string.format(message, ...)
end
spell:execute([[
/say %s
]], message)
end
function singleton(name)
spell:execute([[/wol spell break byName "%s"]], name)
spell.name = name
end
function contains(list, element)
for _,v in pairs(list) do
if v==element then
return true
end
end
return false
end
function remove(list, element)
for i,v in pairs(list) do
if v==element then
table.remove(list, i)
return
end
end
error("element not in list")
end
function leave(player)
log("Player %s left the game in arena '%s'!", player.name, arenaName)
end
function joinPlayers(playerNames)
local result = findPlayers(playerNames)
for _,p in pairs(result) do
tell(result, "Player %s joined the game in arena '%s'!", p.name, arenaName)
end
return result
end
function findPlayers(playerNames)
local result = {}
for _,name in pairs(playerNames) do
--local p = Entities.find(string.format("@a[name=%s]",name))[1]
local p = Entities.find(string.format("@e[name=%s]",name))[1]
if p then
table.insert(result,p)
end
end
return result
end
function terminate()
if winner then
tell(joinedPlayers, "The winner of arena '%s' is %s!", arenaName, winner)
end
tell(joinedPlayers, "Game in arena '%s' has been finished.", arenaName)
spell:execute([[/wol spell break byName "%s"]], spell.name)
end
function copy(list)
local result = {}
for _,v in pairs(list) do
table.insert(result, v)
end
return result
end
function tell(players, message, ...)
local n = select('#', ...)
if n>0 then
message = string.format(message, ...)
end
for _,player in pairs(players) do
spell:execute([[
/tellraw %s ["",{"text":"%s","color":"gold","bold":true}]
]], player.name, message)
end
end
return pkg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment