# Fight for Gold Bricks

local Api                   = {}                         
local CrazyArcadeEvent      = {}
local Config                = {}
local PropLogic             = {}
local CrazyArcadeMgr        = {}
local LogicEvent            = {}
local CrazyArcadeData       = {}
local GameLogic             = {}
local TimerManager          = {}

function listenevents_crazyarcade()

    ScriptSupportEvent:registerEvent([=[Game.Start]=],      CrazyArcadeEvent.GameStart)
    ScriptSupportEvent:registerEvent([=[Game.Run]=],        CrazyArcadeEvent.GameRun)
    ScriptSupportEvent:registerEvent([=[Player.Die]=],      CrazyArcadeEvent.PlayerDie)
    ScriptSupportEvent:registerEvent([=[Player.Revive]=],   CrazyArcadeEvent.PlayerRevived)
    ScriptSupportEvent:registerEvent([=[Game.AnyPlayer.EnterGame]=],   CrazyArcadeEvent.PlayerEnterGame)

    ScriptSupportEvent:registerEvent([=[Block.Remove]=],   CrazyArcadeEvent.BlockRemove)
    ScriptSupportEvent:registerEvent([=[Player.PickUpItem]=],   CrazyArcadeEvent.PickUpItem)
    ScriptSupportEvent:registerEvent([=[Player.AreaIn]=],   CrazyArcadeEvent.AreaIn)
    ScriptSupportEvent:registerEvent([=[Player.ConsumeItem]=],   CrazyArcadeEvent.ConsumeItem)
    ScriptSupportEvent:registerEvent([=[Block.Add]=],   CrazyArcadeEvent.BlockAdd)
    ScriptSupportEvent:registerEvent([=[Player.SelectShortcut]=],   CrazyArcadeEvent.SelectShortcut)

end


Api = {
    Chat        = class.Chat.new(),
    World       = class.World.new(),
    Backpack    = class.Backpack.new(),
    Player      = class.Player.new(),
    Area        = class.Area.new(),
    Actor       = class.Actor.new(),
    Team        = class.Team.new(),
    Block       = class.Block.new(),
    GameRule    = class.GameRule.new()
}


LogicEvent = {
    BlockRemoveEvent = 1,
    PlayerPickUpItem = 2,
    ChangeCurCanPlaceBumpNunEvent = 3,
    ChangeCurBumpIdEvent = 4,
    InitGameEvent = 5,
    EnterAreaEvent = 6,
    CarryCoreBlockStateInEvent = 7,
    CarryCoreBlockStateOutEvent = 8,
    PlayerConsumeItemEvent = 9,
    BlockAddEvent = 10,
    ChangeBumpIDEvent = 11,
    ChangeBumpNumEvent = 12,
    PlayerRevivedEvent = 13,
    PlayerDieEvent = 14,
    TeamFailEvent = 15,
    CoreBlockNumChaneEvent = 16,


}


CrazyArcadeEvent = {

    GameStart = function()
        Api.Chat:sendChat("Game start ")
        CrazyArcadeMgr.HandleEvent(LogicEvent.InitGameEvent,nil)
    end,

    GameRun = function()
        TimerManager.Update()
    end,

    PlayerDie = function(param)

        print(param)
        CrazyArcadeMgr.HandleEvent(LogicEvent.PlayerDieEvent,param)
    end,

    PlayerRevived = function(param)

        print(param)
        CrazyArcadeMgr.HandleEvent(LogicEvent.PlayerRevivedEvent,param)
    end,

    PlayerEnterGame = function(param)

    end,

    BlockRemove = function(param)
        CrazyArcadeMgr.HandleEvent(LogicEvent.BlockRemoveEvent,param)

    end,

    PickUpItem =function(param)
        CrazyArcadeMgr.HandleEvent(LogicEvent.PlayerPickUpItem,param)

    end,

    AreaIn = function(param)

        CrazyArcadeMgr.HandleEvent(LogicEvent.EnterAreaEvent,param)
    end,

    ConsumeItem = function(param)

        CrazyArcadeMgr.HandleEvent(LogicEvent.PlayerConsumeItemEvent,param)
    end,

    BlockAdd = function(param)

        CrazyArcadeMgr.HandleEvent(LogicEvent.BlockAddEvent,param)
    end,

    SelectShortcut = function(param)

    end,

}


Config = {

    CoreBlockId         = 9945,
    ProduceBlockId      = 2003,
    FailJudgeTime       = 5,


    TeamIdEnum = {
        TeamOne     = 1,
        TeamTwo     = 2,
        TeamThree   = 3,
        TeamFour    = 4,
        TeamFive    = 5,
    },


    MapIdEnum = {
        MapOne      = 1,
        MapTwo      = 2,
        MapThree    = 3,
    },


    PowerProp = {
        { minNum = 0, maxNum = 2,   bumpId = 12824,    bumpBlockId =1015 },
        { minNum = 3, maxNum = 5,  bumpId = 12823,    bumpBlockId =1014 },
    },


    NumberProp = {
        { minNum = 0,   maxNum = 0,       placeBumpNum = 2 },
        { minNum = 1,   maxNum = 3,       placeBumpNum = 4 },
        { minNum = 4,   maxNum = 7,       placeBumpNum = 6 },
        { minNum = 8,   maxNum = 15,      placeBumpNum = 8 },
    },


    PropConfig = {

        [1] = { min = 1,    max = 5, desc = "Trick item ",   id = 12515,     pos = 1 },
        [2] = { min = 6,    max = 10, desc = "Invincible item ",    id = 12507,     pos = 1 },
        [3] = { min = 11,   max = 40, desc = "Quantity item ",    id = 12510,     pos = 2 },
        [4] = { min = 41,   max = 60, desc = "Power item ",    id = 12505,     pos = 2 },
    },


    MischiefPropBuff = {
        { buffId =1006 , level = 1},
        { buffId =1007 , level = 1},
        { buffId =1008 , level = 1},
    },


    PropEnum = {
        MischiefProp            = 1,
        InvincibleProp          = 2,
        NumberProp              = 3,
        PowerProp               = 4,
    },


    CampBlockPos ={
        [1] = {
            {teamid = 1,   x=-108,  y=7,  z = 139,},
            {teamid = 2,   x=-105,  y=7,  z = 131,},
        },

        [2] = {
            {teamid = 1,   x=-111,  y=7,  z = 115,},
            {teamid = 2,   x=-103,  y=7,  z = 115,},
            {teamid = 3,   x=-103,  y=7,  z = 105,},
            {teamid = 4,   x=-111,  y=7,  z = 105,},
        },

        [3] = {
            {teamid = 1,   x=-112,  y=7,  z = 77,},
            {teamid = 2,   x=-100,   y=7,  z = 70,},
            {teamid = 3,   x=-112,  y=7,  z = 63,},

        },

        [4] = {
            {teamid = 1,   x=-111,  y=7,  z = 20,},
            {teamid = 2,   x=-111,  y=7,  z = 33,},
            {teamid = 3,   x=-98,   y=7,  z = 33,},
            {teamid = 4,   x=-98,   y=7,  z = 20,},
            {teamid = 4,   x=-105,  y=7,  z = 26,},
        },

    },


    BirthPointBlockPos ={
        [1] = {
            {teamid = 1, blockid = 991,   x=-109,  y=7,  z = 140,},
            {teamid = 2, blockid = 992,   x=-104,  y=7,  z = 130,},
        },

        [2] = {
            {teamid = 1, blockid = 991,   x=-112,  y=7,  z = 116,},
            {teamid = 2, blockid = 992,   x=-102,  y=7,  z = 116,},
            {teamid = 3, blockid = 993,   x=-102,  y=7,  z = 104,},
            {teamid = 4, blockid = 994,   x=-112,  y=7,  z = 104,},
        },

        [3] = {
            {teamid = 1, blockid = 991,   x=-113,  y=7,  z = 78,},
            {teamid = 2, blockid = 992,   x=-99,   y=7,  z = 70,},
            {teamid = 3, blockid = 993,   x=-113,  y=7,  z = 62,},

        },

        [4] = {
            {teamid = 1, blockid = 991,   x=-110,  y=7,  z = 21,},
            {teamid = 2, blockid = 992,   x=-110,  y=7,  z = 32,},
            {teamid = 3, blockid = 993,   x=-99,   y=7,  z = 32,},
            {teamid = 4, blockid = 994,   x=-99,   y=7,  z = 21,},
            {teamid = 4, blockid = 995,   x=-104,  y=7,  z = 27,},
        },

    },


    CoreBlockEnum = {
        One = 2001,
        Two = 2002,
    },




    AreaIDEnum = {
      TeamOneCoreBlock          = 1,
      TeamTwoCoreBlock          = 2,
      TeamThreeCoreBlock        = 3,
      TeamFourCoreBlock         = 4,
      TeamFiveCoreBlock         = 5,
    },

}


CrazyArcadeMgr = {

    HandleEvent = function(event,param)
        if event == LogicEvent.BlockRemoveEvent then
            PropLogic.ProduceBlock(param.blockid,param.x,param.y,param.z)

        elseif event == LogicEvent.PlayerPickUpItem then
            PropLogic.PickUpItem(param)

        elseif event == LogicEvent.ChangeCurCanPlaceBumpNunEvent then
            PropLogic.ChangeCurCanPlaceBumpNum(param.teamid)

        elseif event == LogicEvent.ChangeCurBumpIdEvent then
            PropLogic.ChangeCurBumpId(param.teamid)

        elseif event == LogicEvent.InitGameEvent then
            GameLogic.SetCurMapId()
            GameLogic.GameInit()

        elseif event == LogicEvent.EnterAreaEvent then
            GameLogic.EnterArea(param)

        elseif event == LogicEvent.CarryCoreBlockStateInEvent then
            GameLogic.CarryCoreBlock(param.teamid,param.uin,param.otherTeamId)

        elseif event == LogicEvent.CarryCoreBlockStateOutEvent then
            GameLogic.CarryOutCoreBlock(param.teamid,param.uin)

        elseif event == LogicEvent.PlayerConsumeItemEvent then
            GameLogic.PlayerConsumeItem(param)

        elseif event == LogicEvent.BlockAddEvent then
            GameLogic.BlockAdd(param)

        elseif event == LogicEvent.ChangeBumpEvent then
            PropLogic.ChangeBump(param.teamid)

        elseif event == LogicEvent.ChangeBumpNumEvent then
            PropLogic.ChangeBumpNum(param.teamid)

        elseif event == LogicEvent.PlayerRevivedEvent then
            GameLogic.PlayerRevived(param)

        elseif event == LogicEvent.PlayerDieEvent then
            GameLogic.PlayerDie(param)

        elseif event == LogicEvent.TeamFailEvent then
            GameLogic.SetTeamFail(param.teamid)

        elseif event == LogicEvent.CoreBlockNumChaneEvent then
            GameLogic.CoreBlockChange(param.teamid)

        end
    end,

}


CrazyArcadeData = {

    [Config.TeamIdEnum.TeamOne] = {
        IsFail                  = false,
        CoreBlockNum            = 2,
        NumberPropNum           = 0,
        MischiefPropNum         = 0,
        InvinciblePropNum       = 0,
        PowerPropNum            = 0,
        CurCanPlaceBumpNum      = 2,
        CurBumpId               = 12824,
        IsCarryCoreBlock        = false,
        IsDied                  = false,
    },
    [Config.TeamIdEnum.TeamTwo] = {
        IsFail                  = false,
        CoreBlockNum            = 2,
        NumberPropNum           = 0,
        MischiefPropNum         = 0,
        InvinciblePropNum       = 0,
        PowerPropNum            = 0,
        CurCanPlaceBumpNum      = 2,
        CurBumpId               = 12824,
        IsCarryCoreBlock        = false,
        IsDied                  = false,
    },
    [Config.TeamIdEnum.TeamThree] = {
        IsFail                  = false,
        CoreBlockNum            = 2,
        NumberPropNum           = 0,
        MischiefPropNum         = 0,
        InvinciblePropNum       = 0,
        PowerPropNum            = 0,
        CurCanPlaceBumpNum      = 2,
        CurBumpId               = 12824,
        IsCarryCoreBlock        = false,
        IsDied                  = false,
    },
    [Config.TeamIdEnum.TeamFour] = {
        IsFail                  = false,
        CoreBlockNum            = 2,
        NumberPropNum           = 0,
        MischiefPropNum         = 0,
        InvinciblePropNum       = 0,
        PowerPropNum            = 0,
        CurCanPlaceBumpNum      = 2,
        CurBumpId               = 12824,
        IsCarryCoreBlock        = false,
        IsDied                  = false,
    },
    [Config.TeamIdEnum.TeamFive] = {
        IsFail                  = false,
        CoreBlockNum            = 2,
        NumberPropNum           = 0,
        MischiefPropNum         = 0,
        InvinciblePropNum       = 0,
        PowerPropNum            = 0,
        CurCanPlaceBumpNum      = 2,
        CurBumpId               = 12824,
        IsCarryCoreBlock        = false,
        IsDied                  = false,
    },

    CurMapId = 1,
    AreaIDMap = { },


    SetCurMapId = function(curMapId)
        if curMapId <= 0 then return end
        local this = CrazyArcadeData

        this.CurMapId = curMapId
    end,


    SetNumberPropNum = function(teamid,num)
        if num == nil then return end
        local this = CrazyArcadeData

        this[teamid].NumberPropNum = num;

        CrazyArcadeMgr.HandleEvent(LogicEvent.ChangeCurCanPlaceBumpNunEvent,{teamid = teamid})
    end,


    SetMischiefPropNum = function(teamid,num)
        if num == nil then return end
        local this = CrazyArcadeData


        this[teamid].MischiefPropNum = num;
    end,


    SetInvinciblePropNum = function(teamid,num)
        if num == nil then return end
        local this = CrazyArcadeData


        this[teamid].InvinciblePropNum = num;
    end,


    SetPowerPropNum = function(teamid,num)
        if num == nil then return end
        local this = CrazyArcadeData


        this[teamid].PowerPropNum = num;
        CrazyArcadeMgr.HandleEvent(LogicEvent.ChangeCurBumpIdEvent,{teamid = teamid})
    end,


    SetCurCanPlaceBumpNum = function(teamid,num)
        if num == nil then return end
        local this = CrazyArcadeData

        this[teamid].CurCanPlaceBumpNum = num;
        CrazyArcadeMgr.HandleEvent(LogicEvent.ChangeBumpNumEvent,{teamid = teamid})
    end,


    SetCurBumpId = function(teamid,id)
        if teamid == nil or id == nil then return end
        local this = CrazyArcadeData


        this[teamid].CurBumpId = id
        CrazyArcadeMgr.HandleEvent(LogicEvent.ChangeBumpEvent,{teamid = teamid})
    end,


    SetArea = function(areaid,type,x,y,z)
        if areaid == nil then return end

        CrazyArcadeData.AreaIDMap[areaid] = type

    end,


    SetCarryState = function(teamid,state,uin,otherTeamId)
        if teamid == nil or state == nil then return end
        local this = CrazyArcadeData;



        this[teamid].IsCarryCoreBlock = state
        if state == true  then
            CrazyArcadeMgr.HandleEvent(LogicEvent.CarryCoreBlockStateInEvent,
                    { teamid = teamid,uin = uin,otherTeamId =otherTeamId  })
        elseif state == false and this[teamid].IsDied == false then
            CrazyArcadeMgr.HandleEvent(LogicEvent.CarryCoreBlockStateOutEvent,{ teamid = teamid,uin =uin })
        end

    end,

    SetCoreBlockNum = function(teamid,num)
        if teamid == nil or num == nil then return end
        local this = CrazyArcadeData;


        this[teamid].CoreBlockNum = num
        CrazyArcadeMgr.HandleEvent(LogicEvent.CoreBlockNumChaneEvent,{teamid=teamid})
    end,

    SetPlayerDieState = function(teamid,state)
        if teamid == nil or state == nil then return end
        local this = CrazyArcadeData;

        this[teamid].IsDied = state

    end,

    SetTeamFailState = function(teamid,isFail)
        if teamid == nil or isFail == nil then return end
        local this = CrazyArcadeData;

        this[teamid].IsFail = isFail
        if isFail == true then
            CrazyArcadeMgr.HandleEvent(LogicEvent.TeamFailEvent,{teamid=teamid})
        end
    end,


    ClearTeamItemData = function(teamid)
        if teamid == nil then return end

        CrazyArcadeData[teamid].NumberPropNum           = 0
        CrazyArcadeData[teamid].InvinciblePropNum         = 0
        CrazyArcadeData[teamid].MischiefPropNum   = 0
        CrazyArcadeData[teamid].PowerPropNum            = 0
        CrazyArcadeData[teamid].CurCanPlaceBumpNum      = 2
        CrazyArcadeData[teamid].CurBumpId               = 12824

    end,

}


PropLogic = {

    RandomState = 1,


    ProduceBlock= function(blockid,x,y,z)
        if blockid == nil or blockid ~= Config.ProduceBlockId then return end

        if PropLogic.RandomState == 1 then
            math.randomseed(tostring(os.time()):reverse():sub(1, 7))
            PropLogic.RandomState = -1
        else
            math.randomseed(os.time())
            PropLogic.RandomState =1
        end

        local randomRate = math.random(1,1000);


        local tempProp = nil;

        for k,v in pairs(Config.PropConfig) do
            if v.min<=randomRate/10 and v.max >= randomRate/10 then
                tempProp = v;
            end
        end


        if tempProp == nil then return end
        Api.World:spawnItem(x, y, z, tempProp.id, 1)
        --Api.Chat:sendChat(tempProp.desc)

    end,


    PickUpItem = function(param)

        local ret,teamid = Api.Player:getTeam(param.eventobjid)
        local propIndex = -1
        local isCoreBlock = false

        for k,v in pairs(Config.PropConfig) do
            if v.id == param.itemid then
                propIndex = k
                break
            end
        end

        if param.itemid == Config.CoreBlockId then
            isCoreBlock = true
        end

        if propIndex == Config.PropEnum.MischiefProp then
            PropLogic.PickMischiefProp(teamid,param.eventobjid,propIndex)
            --CrazyArcadeData.SetMischiefPropNum(teamid,param.itemnum+CrazyArcadeData[teamid].MischiefPropNum)
        elseif propIndex == Config.PropEnum.InvincibleProp then
            PropLogic.PickInvincibleProp(teamid,param.eventobjid,propIndex)
            --CrazyArcadeData.SetInvinciblePropNum(teamid,param.itemnum+CrazyArcadeData[teamid].InvinciblePropNum)
        elseif propIndex == Config.PropEnum.NumberProp then
            Api.Backpack:removeGridItemByItemID(param.eventobjid, param.itemid, param.itemnum);
            Api.Player:gainItems(param.eventobjid,param.itemid,param.itemnum,2);
            CrazyArcadeData.SetNumberPropNum(teamid,param.itemnum+CrazyArcadeData[teamid].NumberPropNum)
        elseif propIndex == Config.PropEnum.PowerProp then
            Api.Backpack:removeGridItemByItemID(param.eventobjid, param.itemid, param.itemnum);
            Api.Player:gainItems(param.eventobjid,param.itemid,param.itemnum,2);
            CrazyArcadeData.SetPowerPropNum(teamid,param.itemnum+CrazyArcadeData[teamid].PowerPropNum)
        end


        if isCoreBlock then
            CrazyArcadeData.SetCarryState(teamid,true,param.eventobjid,-1)
        elseif propIndex == -1 then
            Api.Backpack:removeGridItemByItemID(param.eventobjid, param.itemid, param.itemnum);
        end


        if CrazyArcadeData[teamid].IsCarryCoreBlock and propIndex ~= -1 then
            Api.Backpack:removeGridItemByItemID(param.eventobjid, param.itemid, param.itemnum);
        end


    end,


    ChangeCurCanPlaceBumpNum = function(teamid)
        if teamid == nil then return end
        local numberPropNum = CrazyArcadeData[teamid].NumberPropNum;

        local canPlaceBumpNum = -1
        for k,v in pairs(Config.NumberProp) do
            if numberPropNum >= v.minNum  and numberPropNum <= v.maxNum then
                canPlaceBumpNum = v.placeBumpNum
                break
            end
        end

        if canPlaceBumpNum == -1 then
            local maxNum = 0
            local bumpNum = 0
            for k,v in pairs(Config.NumberProp) do
                if maxNum <v.maxNum then
                    maxNum = v.maxNum
                    bumpNum = v.placeBumpNum
                end
            end

            if  numberPropNum >maxNum then
                canPlaceBumpNum = bumpNum
            end

        end

        if canPlaceBumpNum ~= -1 and CrazyArcadeData[teamid].CurCanPlaceBumpNum ~= canPlaceBumpNum then
            CrazyArcadeData.SetCurCanPlaceBumpNum(teamid,canPlaceBumpNum)
        end
    end,


    ChangeCurBumpId = function(teamid)
        if teamid == nil then return end
        local powerPropNum = CrazyArcadeData[teamid].PowerPropNum

        local curBumpId = -1
        for k,v in pairs(Config.PowerProp) do
            if powerPropNum >= v.minNum and powerPropNum <= v.maxNum then
                curBumpId = v.bumpId
                break
            end
        end

        if curBumpId == -1 then
            local maxNum = 0
            local bumpId = 0
            for k,v in pairs(Config.PowerProp) do
                if maxNum <v.maxNum then
                    maxNum = v.maxNum
                    bumpId = v.bumpId
                end
            end

            if  powerPropNum >maxNum then
                curBumpId = bumpId
            end

        end

        if curBumpId ~= -1 and CrazyArcadeData[teamid].CurBumpId ~= curBumpId then
            CrazyArcadeData.SetCurBumpId(teamid,curBumpId)
        end

    end,


    ChangeBump = function(teamid)
        if teamid == nil then return end
        local ret,arr,players = Api.Team:getTeamPlayers(teamid)
        if arr >0 then
            for k1,v1 in pairs(players) do
                Api.Player:removeBackpackItem(v1, Config.PowerProp[1].bumpId, CrazyArcadeData[teamid].CurCanPlaceBumpNum)
                if CrazyArcadeData[teamid].IsCarryCoreBlock == false then
                    Api.Player:gainItems(v1,Config.PowerProp[2].bumpId,CrazyArcadeData[teamid].CurCanPlaceBumpNum,1);
                end
            end
        end

    end,


    ChangeBumpNum = function(teamid)
        if teamid == nil then return end

        local ret,arr,players = Api.Team:getTeamPlayers(teamid)
        if arr >0 then
            for k1,v1 in pairs(players) do
                Api.Player:removeBackpackItem(v1, CrazyArcadeData[teamid].CurBumpId, CrazyArcadeData[teamid].CurCanPlaceBumpNum)
                if CrazyArcadeData[teamid].IsCarryCoreBlock == false then
                    Api.Player:gainItems(v1,CrazyArcadeData[teamid].CurBumpId,CrazyArcadeData[teamid].CurCanPlaceBumpNum,1);
                end
            end
        end
    end,


    PickInvincibleProp = function(teamid,uin,propType)
        if teamid == nil or uin ==nil then return end

        Api.Actor:addBuff(uin,999,2,100)
        local prop = Config.PropConfig[propType]
        if prop ~= nil then
            Api.Player:removeBackpackItem(uin, prop.id, 1)
        end
    end,


    PickMischiefProp = function(teamid,uin,propType)
        if teamid == nil or uin ==nil then return end

        local random = math.random(1,3)

        local buff = Config.MischiefPropBuff[random]
        if buff ~= nil then
            Api.Actor:addBuff(uin,buff.buffId,buff.level,100)
        end
        local prop = Config.PropConfig[propType]
        if prop ~= nil then
            Api.Player:removeBackpackItem(uin, prop.id, 1)
        end

    end,

}


GameLogic = {


    GameInit = function()

        local campBlockData = Config.CampBlockPos[CrazyArcadeData.CurMapId]

        Api.GameRule.GravityFactor = 10
        Api.GameRule.BlockDestroy = 0
        Api.GameRule.PlayerDieDrops = 1


        for k,v in pairs(campBlockData) do
            local ret,areaid = Api.Area:createAreaRect({x=v.x,y=v.y,z=v.z}, {x=1,y=1,z=1})
            if ret == ErrorCode.OK then
                CrazyArcadeData.SetArea(areaid,k)
            end
        end


        --Initializing Team Score
        TimerManager.CreateTimer(1,function ()
            local teamNum = #campBlockData or 0;
            if teamNum > 0 then
                for k,v in pairs(campBlockData) do
                    Api.Team:setTeamScore(v.teamid,2)
                    local ret,arr,players = Api.Team:getTeamPlayers(v.teamid)
                    if arr >0 then
                        for k1,v1 in pairs(players) do
                            Api.Player:gainItems(v1,CrazyArcadeData[v.teamid].CurBumpId,CrazyArcadeData[v.teamid].CurCanPlaceBumpNum,1);
                        end
                    end
                end
            end
        end)


        local birthPoint = Config.BirthPointBlockPos[CrazyArcadeData.CurMapId]


        local ret,playerNum,array = Api.World:getAllPlayers(-1)

        if ret == ErrorCode.OK then
            for k,v in pairs(array) do

                local ret,teamId = Api.Player:getTeam(v)
                local pos = birthPoint[teamId]
                --Api.Player:setPosition(v, pos.x, pos.y, pos.z)
                Api.Player:setRevivePoint(v, pos.x, pos.y, pos.z)
            end
        end


    end,


    SetCurMapId = function()
        local ret,playerNum = Api.World:getAllPlayers(-1)

        if playerNum == nil then return end
        local curMapId = 1

        if playerNum <= 2 then
            curMapId = 1
        elseif playerNum == 3 or playerNum == 6 then
            curMapId = 3
        elseif playerNum == 4 then
            curMapId = 2
        elseif playerNum == 5 then
            curMapId = 4
        end

        CrazyArcadeData.SetCurMapId(curMapId);

    end,


    EnterArea = function(param)
        if param == nil then return end
        local areaData = CrazyArcadeData.AreaIDMap
        if areaData == nil then return end

        local areaType = areaData[param.areaid]

        local num = 0;
        local ret,teamid = Api.Player:getTeam(param.eventobjid)
        if areaType == teamid then
            if CrazyArcadeData[teamid].IsCarryCoreBlock == true then
                num = CrazyArcadeData[teamid].CoreBlockNum +1
                CrazyArcadeData.SetCoreBlockNum(teamid,num)
                CrazyArcadeData.SetCarryState(teamid,false,param.eventobjid)
            end
        else
            if CrazyArcadeData[teamid].IsCarryCoreBlock == false then
                num = CrazyArcadeData[areaType].CoreBlockNum -1

                if num < 0 then return end
                CrazyArcadeData.SetCoreBlockNum(areaType,num)
                CrazyArcadeData.SetCarryState(teamid,true,param.eventobjid,areaType)
            end
        end

    end,


    CarryCoreBlock = function(teamid,uin,otherTeamId)
        Api.Backpack:clearAllPack(uin)

        Api.Actor:addBuff(uin,8,2,5000000)
        if otherTeamId ~= -1 then
            local ret, curNum = Api.Team:getTeamScore(otherTeamId);
            if curNum > 0 then
                Api.Team:setTeamScore(otherTeamId,curNum-1)
                GameLogic.SetCoreDisplay(otherTeamId)
            end
        end

        Api.Player:gainItems(uin,Config.CoreBlockId,1,1);



    end,


    CarryOutCoreBlock = function(teamid,uin)
        Api.Actor:removeBuff(uin,8)
        Api.Backpack:clearAllPack(uin)
        local ret, curNum = Api.Team:getTeamScore(teamid);
        if curNum >= 0 then
            Api.Team:setTeamScore(teamid,curNum + 1)
            GameLogic.SetCoreDisplay(teamid)
        end


        GameLogic.ResetItemToPlayer(teamid,uin)
    end,


    CoreBlockChange = function(teamId)
        if teamId == nil then return end

        if CrazyArcadeData[teamId].CoreBlockNum > 0 then return end

        TimerManager.CreateTimer(Config.FailJudgeTime,function()
            if CrazyArcadeData[teamId].CoreBlockNum <= 0 then
                CrazyArcadeData.SetTeamFailState(teamId,true)
            end
        end)
    end,


    ResetItemToPlayer = function(teamid,uin)
        if teamid == nil or uin == nil then return end

        local data = CrazyArcadeData[teamid]

        Api.Player:gainItems(uin,data.CurBumpId,data.CurCanPlaceBumpNum,1);
        for k,v in pairs(Config.PropConfig) do
            if k == Config.PropEnum.MischiefProp then
                Api.Player:gainItems(uin,v.id,data.SaveSelfPropNum,v.pos);
            elseif k == Config.PropEnum.InvincibleProp then
                Api.Player:gainItems(uin,v.id,data.SpeedReductionPropNum,v.pos);
            elseif k == Config.PropEnum.NumberProp then
                Api.Player:gainItems(uin,v.id,data.NumberPropNum,v.pos);
            elseif k == Config.PropEnum.PowerProp then
                Api.Player:gainItems(uin,v.id,data.PowerPropNum,v.pos);
            end
        end
    end,


    PlayerConsumeItem = function(param)
        if param == nil then return end

        local ret,teamid = Api.Player:getTeam(param.eventobjid)
        if param.itemid == CrazyArcadeData[teamid].CurBumpId then
            TimerManager.CreateTimer(3,function()

                if CrazyArcadeData[teamid].IsDied then return end
                if CrazyArcadeData[teamid].CurBumpId ~= param.itemid then return end
                local ret,itemNum = Api.Backpack:getItemNumByBackpackBar(param.eventobjid, 1, param.itemid)
                if CrazyArcadeData[teamid].CurCanPlaceBumpNum <=itemNum then return end
                if CrazyArcadeData[teamid].IsCarryCoreBlock then return end
                Api.Player:gainItems(param.eventobjid,param.itemid,param.itemnum,1);
            end)
        end
    end,


    BlockAdd = function(param)
        if param == nil then return end

    end,


    PlayerRevived = function(param)
        if param == nil or param.eventobjid == nil then return end

        Api.Backpack:clearAllPack(param.eventobjid)
        local ret,teamid = Api.Player:getTeam(param.eventobjid)
        CrazyArcadeData.SetPlayerDieState(teamid,false)

        CrazyArcadeData.ClearTeamItemData(teamid)
        Api.Player:gainItems(param.eventobjid,CrazyArcadeData[teamid].CurBumpId,CrazyArcadeData[teamid].CurCanPlaceBumpNum,1);
    end,


    PlayerDie = function(param)
        if param == nil or param.eventobjid == nil then return end

        local ret,teamid = Api.Player:getTeam(param.eventobjid)
        CrazyArcadeData.SetPlayerDieState(teamid,true)
        GameLogic.SpawnPlayerAllItem(teamid,param.eventobjid)

    end,


    SpawnPlayerAllItem = function(teamid,uin)
        if uin == nil or teamid == nil then return end
        local numberPropId = Config.PropConfig[Config.PropEnum.NumberProp].id
        local powerPropId =  Config.PropConfig[Config.PropEnum.PowerProp].id

        local ret,x,y,z = Api.Actor:getPosition(uin)
        if ret ~= ErrorCode.OK then return end
        if CrazyArcadeData[teamid].NumberPropNum >0 then
            Api.World:spawnItem(x, y, z, numberPropId, CrazyArcadeData[teamid].NumberPropNum)
        end

        if CrazyArcadeData[teamid].PowerPropNum >0 then
            Api.World:spawnItem(x, y, z, powerPropId, CrazyArcadeData[teamid].PowerPropNum)
        end

        if CrazyArcadeData[teamid].IsCarryCoreBlock then
            CrazyArcadeData.SetCarryState(teamid,false,uin)
            Api.World:spawnItem(x, y, z, Config.CoreBlockId, 1)
        end

        Api.Backpack:clearAllPack(uin)
    end,


    SetTeamFail = function(teamid)
        if teamid == nil then return end

    end,


    SetCoreDisplay = function(teamId)
        if teamId == nil then return end

        local curNum = CrazyArcadeData[teamId].CoreBlockNum
        local posTable = Config.CampBlockPos[CrazyArcadeData.CurMapId]
        local pos = posTable[teamId]

        local index = math.floor(curNum/2)
        local remainder = curNum%2

        for i = 1,5 do
            local newPos = {x=pos.x,y=pos.y+i-1,z=pos.z}
            Api.Block:destroyBlock(newPos.x,newPos.y,newPos.z,false)
        end

        if index >0 then
            for i = 1,index do
                local newPos = {x=pos.x,y=pos.y+i-1,z=pos.z}
                Api.Block:placeBlock(Config.CoreBlockEnum.Two,newPos.x,newPos.y,newPos.z,3)
            end
        end

        if index >= 0 and remainder == 1 then
            local newPos = {x=pos.x,y=pos.y+index,z=pos.z}
            Api.Block:placeBlock(Config.CoreBlockEnum.One,newPos.x,newPos.y,newPos.z,3)
        end

    end,

}


TimerManager = {
    TaskList = {},
    Tick = 0,

    CreateTimer = function(timeInterval,func)
        if timeInterval == nil then return end
        local this = TimerManager

        local timeList = {
            beginTime = os.time(),
            timeInterval = timeInterval,
            func = func
        }
        table.insert(this.TaskList,timeList)
    end,


    Update = function()
        local this = TimerManager
        this.Tick = this.Tick + 1

        if this.Tick >=10 then
            this.Tick = 0
            for k,v in pairs(this.TaskList) do
                if v == nil then break end
                if os.time() >=v.beginTime+v.timeInterval and v.func ~= nil then
                    v.func()
                    table.remove(this.TaskList,k)
                end
            end
        end
    end,

}


listenevents_crazyarcade()

Last Update: 9/9/2019, 5:57:21 PM