GmAction.go 2.7 KB
// GM系统API
package gmaction

import (
	"pro2d/common"
	"pro2d/common/logger"
	"pro2d/models"
	"pro2d/pb"
	"strconv"
)

type GmAction struct{}

// GMParams GM系统API请求参数的基础类型
type GMParams map[string]string

/*
AddExp 增加经验
 exp 增加经验数量
*/
func (gm *GmAction) AddExp(role *models.RoleModel, params GMParams) {
	expIncrease, _ := strconv.Atoi(params["exp"])
	exp := role.IncrProperty("exp", int64(expIncrease))
	role.UpdateProperty("exp", exp, true)
}

/*
AddEquip 添加装备
 id: 装备id
*/
func (gm *GmAction) AddEquip(role *models.RoleModel, params GMParams) {
	//TODO 验证装备是否存在
	id := params["id"]
	tbId, _ := strconv.Atoi(id)

	equip := &pb.Equipment{
		Id:      role.IncreEquipByKey(1),
		RoleId:  role.Data.Id,
		TbId:    int32(tbId),
		Quality: 1,
	}
	role.AddEquip(equip)
}

/*
AddItem 添加物品
 id: 物品id
 count: 物品数量
*/
func (gm *GmAction) AddItem(role *models.RoleModel, params GMParams) {
	id := params["id"]
	count, _ := strconv.Atoi(params["count"])
	role.AddItem(id, int32(count))
}

/*
UpdatePackLimit 更新背包限制
	clotheslimit: 服饰限制数
	weaponslimit: 武器限制数
	jewelrylimit: 首饰限制数
	materiallimit: 材料限制数
	otherlimit: 其他限制数
*/
func (gm *GmAction) UpdatePackLimit(role *models.RoleModel, params GMParams) {
	update := make(map[string]interface{}, 5)
	c, ok := params["clotheslimit"]
	if ok {
		l, _ := strconv.Atoi(c)
		update["clotheslimit"] = uint32(l)
	}
	w, ok := params["weaponslimit"]
	if ok {
		l, _ := strconv.Atoi(w)
		update["weaponslimit"] = uint32(l)
	}
	o, ok := params["otherlimit"]
	if ok {
		l, _ := strconv.Atoi(o)
		update["otherlimit"] = uint32(l)
	}

	o, ok = params["jewelrylimit"]
	if ok {
		l, _ := strconv.Atoi(o)
		update["jewelrylimit"] = uint32(l)
	}

	o, ok = params["materiallimit"]
	if ok {
		l, _ := strconv.Atoi(o)
		update["materiallimit"] = uint32(l)
	}

	role.UpdateProperties(update, true)
}

/*GMEmail 发送邮件
参数 &title= &stitle= &content= &attachments=
*/
func (gm *GmAction) GMEmail(role *models.RoleModel, params GMParams) {
	title, ok := params["title"]
	if !ok {
		title = ""
	}
	stitle, ok := params["stitle"]
	if !ok {
		stitle = ""
	}
	content, ok := params["content"]
	if !ok {
		content = ""
	}
	attachments, ok := params["attachments"]
	if !ok {
		attachments = ""
	}
	id, err := common.GetNextEmail()
	if err != nil {
		logger.Error(err)
		return
	}
	email := &pb.Email{
		Id:          id,
		RoleId:      role.Data.Id,
		Title:       title,
		Stitle:      stitle,
		Content:     content,
		Attachments: attachments,
		Status:      0,
		CreateTime:  common.Timex(),
	}
	m := models.NewEmailModelPB(email)
	err = m.Create()
	if err != nil {
		logger.Error(err)
	}
}