local function split(inputstr, sep)
    if sep == nil then
        sep = "%s"
    end
    local t={} ; local i=1
    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
        t[i] = str
        i = i + 1
    end
    return t
end

local result        = {}
local matchId       = ARGV[1]
local playerId      = ARGV[2]
local newPosX       = ARGV[3]
local newPosY       = ARGV[4]
local removeCoin    = ARGV[5]
local score         = ARGV[6]
local isDead        = tonumber(ARGV[7])
local curTime       = ARGV[8]
local actionId      = ARGV[9]
local curPositionX  = redis.call("HGET", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "playerX")
local curPositionY  = redis.call("HGET", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "playerY")

redis.call("HSET", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "ACTIONID", actionId)
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))

if(tonumber(removeCoin) == 1) then
    redis.call("HDEL", "BATTLES:MATCH:{" .. matchId .. "}:coins", newPosY .. ":" .. newPosX)
    redis.call("HINCRBY", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "coinsCollected", 1)
    local score = redis.call("HINCRBY", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "score", score)
    table.insert(result, score)
else 
    local score = redis.call("HGET", "BATTLES:MATCH:{" .. matchId .. "}:" .. playerId, "score")
    table.insert(result, score)
end

if(tonumber(isDead) == 1) then
    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)
end
    table.insert(result, actionId)
return result


