role.go 3.39 KB
package models

import (
	"pro2d/common"
	"pro2d/common/components"
	"pro2d/common/db/mongoproxy"
	"pro2d/common/logger"
	"pro2d/pb"
	"sync/atomic"
)

type RoleModel struct {
	components.ISchema
	Role  *pb.Role
	Heros SchemaMap
	Teams SchemaMap
	Prop  *PropModel

	lastSaveTs int64
}

func RoleExistByUid(uid string) *RoleModel {
	data := &pb.Role{Uid: uid}

	if err := mongoproxy.FindOne(mongoproxy.GetBsonM("uid", uid), data); err != nil {
		logger.Error("Role exist err: %v", err)
		return nil
	}

	r := &RoleModel{
		ISchema: NewSchema(data.Id, data),
		Role:    data,
		Heros:   make(SchemaMap),
		Teams:   make(SchemaMap),
		Prop:    new(PropModel),
	}
	r.LoadAll()
	return r
}

func NewRole(id string) *RoleModel {
	data := &pb.Role{Id: id}
	m := &RoleModel{
		ISchema: NewSchema(id, data),
		Role:    data,
		Heros:   make(SchemaMap),
		Teams:   make(SchemaMap),
	}
	return m
}

func (m *RoleModel) InitRole() {
	//init hero
	h1 := pb.Hero{
		Id:         common.SnowFlack.NextValStr(),
		RoleId:     m.Role.Id,
		Type:       1,
		Level:      1,
		ReinCount:  0,
		ReinPoint:  0,
		Equipments: "",
	}
	m.AddHero(&h1)

	h2 := h1
	h2.Id = common.SnowFlack.NextValStr()
	h2.Type = 2
	m.AddHero(&h2)

	h3 := h1
	h3.Id = common.SnowFlack.NextValStr()
	h3.Type = 3
	m.AddHero(&h3)

	h4 := h1
	h4.Id = common.SnowFlack.NextValStr()
	h4.Type = 4
	m.AddHero(&h4)

	//init team
	t1 := pb.Team{
		Id:      common.SnowFlack.NextValStr(),
		RoleId:  m.Role.Id,
		HeroId1: h1.Id,
		HeroId2: h2.Id,
		HeroId3: h3.Id,
	}
	m.AddTeam(&t1)

	t2 := t1
	t2.Id = common.SnowFlack.NextValStr()
	m.AddTeam(&t2)

	t3 := t1
	t3.Id = common.SnowFlack.NextValStr()
	m.AddTeam(&t3)

	t4 := t1
	t4.Id = common.SnowFlack.NextValStr()
	m.AddTeam(&t4)
}

func (m *RoleModel) LoadHero() {
	heros := make([]*pb.Hero, 10)
	err := mongoproxy.FindMany("hero", "role_id", m.Role.Id, &heros)
	if err != nil {
		logger.Error(err)
		return
	}
	for _, hero := range heros {
		m.Heros[hero.Id] = NewHero(hero)
	}
}

func (m *RoleModel) LoadTeams() {
	teams := make([]*pb.Team, 4)
	err := mongoproxy.FindMany("hero", "role_id", m.Role.Id, &teams)
	if err != nil {
		logger.Error(err)
		return
	}
	for _, team := range teams {
		m.Teams[team.Id] = NewTeam(team)
	}
}

func (m *RoleModel) LoadAll() {
	m.LoadHero()
	m.LoadTeams()
}

func (m *RoleModel) updateProperty(property map[string]interface{}) {
}

func (m *RoleModel) GetAllHero() []*pb.Hero {
	var h []*pb.Hero
	for _, hero := range m.Heros {
		h = append(h, hero.(*HeroModel).Hero)
	}
	return h
}

func (m *RoleModel) GetAllTeam() []*pb.Team {
	var t []*pb.Team
	for _, team := range m.Teams {
		t = append(t, team.(*TeamModel).Team)
	}
	return t
}

func (m *RoleModel) AddHero(hero *pb.Hero) {
	h := NewHero(hero)
	h.Create()
	m.Heros[hero.Id] = h
}

func (m *RoleModel) AddTeam(team *pb.Team) {
	t := NewTeam(team)
	t.Create()
	m.Teams[team.Id] = t
}

func (m *RoleModel) OnRecoverTimer(now int64) {
	m.saveRoleData(now)
}

func (m *RoleModel) OnOfflineEvent() {
	// 设置最新的登录时间
	m.saveRoleData(common.Timex())
}

func (m *RoleModel) saveRoleData(now int64) {
	if now-m.lastSaveTs < common.SaveDataInterval {
		return
	}
	atomic.StoreInt64(&m.lastSaveTs, now)
	m.Update()

	tbObjs := []components.ISchema{}
	for _, tbObj := range tbObjs {
		if tbObj != nil {
			tbObj.Update()
		}
	}

	mpObjs := []SchemaMap{m.Heros, m.Teams}
	for _, mpObj := range mpObjs {
		for _, v := range mpObj {
			if v != nil {
				v.Update()
			}
		}
	}
}