
// 军团活动蛮夷入侵城门血量等数据存储

import { Member, GateMembersRec } from "../../domain/battleField/guildActivity";

export class GateActivityObject {
    public startTime: number;
    private members: Map<string, Array<Member>> = new Map(); // 每个军团参与的成员 guildCode => [{roleId, job}]
    private membersRecord: Map<string, GateMembersRec> = new Map(); // 每个成员的回合数和敌军数，防刷 recCode => GateMembersRec
    private guilds: Map<number, string[]> = new Map(); // 参加的所有军团 server => [guildCode]
    
    public getObj(guildCode: string, serverId: number) {
        // console.log(guildCode, serverId)
        return {
            members: this.members.get(guildCode)||[],
            memberesRecord: [...this.membersRecord],
            guilds: this.guilds.get(serverId)||[]
        }
    }

    public startActivity() {
        this.startTime = Date.now();
    }
    
    public getTimeGap() {
        return Math.floor((Date.now() - this.startTime)/1000);
    }

    public getGuilds() {
        return this.guilds;
    }

    public pushMembers(guildCode: string, serverId: number, roleId: string, job: number, code: string) {
        let member: Member;
        if(this.members.has(guildCode)) {
            let members = this.members.get(guildCode);
            if(members.findIndex(cur => cur.roleId == roleId) == -1) {
                member = { roleId, job, code }
                members.push(member);
            }
        } else {
            member = { roleId, job, code }
            this.members.set(guildCode, [member]);
        }
        if(this.guilds.has(serverId)) {
            let arr = this.guilds.get(serverId);
            if(!arr.includes(guildCode)) arr.push(guildCode);
            this.guilds.set(serverId, arr);
        } else {
            this.guilds.set(serverId, [guildCode]);
        }
        return member;
    }

    public getMemberRecord(recCode: string, roleId: string) {
        return this.membersRecord.get(recCode)||new GateMembersRec(roleId)
    }

    public setMemberRecord(recCode: string, newMemberRecord: GateMembersRec) {
        this.membersRecord.set(recCode, newMemberRecord);
    }

    public delMemberRecord(recCode: string) {
        this.membersRecord.delete(recCode);
    }

    public delGuildRecord(guildCode: string, serverId: number) {
        let guildList = this.guilds.get(serverId);
        if(guildList) {
            let index = guildList.indexOf(guildCode);
            guildList.splice(index, 1);
        }
        this.members.delete(guildCode);
    }
}