local result        = {}
local matchId       = ARGV[1]
local playerId      = ARGV[2]
local isDead        = tonumber(ARGV[3])
local curTime       = tonumber(ARGV[4])
local newPosX       = ARGV[5]
local newPosY       = ARGV[6]

local curPositionX  = redis.call("HGET", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "playerX")
local curPositionY  = redis.call("HGET", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "playerY")

if(tonumber(isDead) == 0) then
    redis.call("HSET", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "STATE", 0)
    local score = redis.call("HGET", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "score")
    table.insert(result, score)
else
    redis.call("HSET", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "STATE", isDead)
    redis.call("HSET", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "DEADTIME", curTime)
    redis.call("HINCRBY", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "deathCount", 1)

    redis.call("HSET", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "playerX", newPosX)
    redis.call("HSET", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "playerY", newPosY)
    redis.call("HDEL", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "map:" .. curPositionX .. ":" .. curPositionY)
    redis.call("HSET", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "map:" .. newPosX .. ":" .. newPosY, tonumber(playerId))
    local score = redis.call("HGET", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "score")
    table.insert(result, score)
end

return result
