This commit is contained in:
@@ -9,7 +9,7 @@ const TableNameTask = "player_task"
|
||||
|
||||
// Task mapped from table <task>
|
||||
type Task struct {
|
||||
Base
|
||||
*cool.Model
|
||||
PlayerID uint64 `gorm:"not null;index:idx_task_by_player_id;comment:'所属玩家ID'" json:"player_id"`
|
||||
TaskID uint32 `gorm:"not null;comment:'任务ID'" json:"task_id"`
|
||||
Data []uint32 `struc:"[20]byte" gorm:"type:jsonb;not null;default:'[]';comment:'全部数据'" json:"data"`
|
||||
@@ -30,7 +30,7 @@ func (*Task) GroupName() string {
|
||||
// NewPlayerInfo create a new PlayerInfo
|
||||
func NewTask() *Task {
|
||||
return &Task{
|
||||
Base: *NewBase(),
|
||||
Model: cool.NewModel(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,8 @@ package service
|
||||
import (
|
||||
"blazing/cool"
|
||||
"blazing/modules/player/model"
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/pointernil/bitset32"
|
||||
)
|
||||
@@ -30,32 +30,51 @@ func (s *TaskService) ShopRequirementError() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取任务信息
|
||||
func (s *TaskService) Exec(id uint32, t func(*model.Task) bool) {
|
||||
var gg model.Task
|
||||
|
||||
m1 := s.dbm(s.Model).Where("task_id", id)
|
||||
|
||||
m1.Scan(&gg)
|
||||
if gg.Data == nil {
|
||||
gg.Data = make([]uint32, 0)
|
||||
func (s *TaskService) GetTask(id uint32) (*model.Task, error) {
|
||||
var task *model.Task
|
||||
if err := s.dbm(s.Model).Where("task_id", id).Scan(&task); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tre := t(&gg)
|
||||
|
||||
if !tre { //不需要更新
|
||||
return
|
||||
if task == nil {
|
||||
task = model.NewTask()
|
||||
task.PlayerID = uint64(s.userid)
|
||||
task.TaskID = id
|
||||
}
|
||||
if cool.Config.ServerInfo.IsVip != 0 {
|
||||
|
||||
return
|
||||
}
|
||||
gg.PlayerID = uint64(s.userid)
|
||||
gg.TaskID = id
|
||||
_, err := m1.Save(gg)
|
||||
if err != nil {
|
||||
cool.Logger.Error(context.TODO(), "task save failed", s.userid, id, err)
|
||||
if task.Data == nil {
|
||||
task.Data = make([]uint32, 0)
|
||||
}
|
||||
|
||||
return task, nil
|
||||
}
|
||||
|
||||
func (s *TaskService) SetTask(task *model.Task) error {
|
||||
if task == nil {
|
||||
return errors.New("task is nil")
|
||||
}
|
||||
|
||||
task.PlayerID = uint64(s.userid)
|
||||
if task.Data == nil {
|
||||
task.Data = make([]uint32, 0)
|
||||
}
|
||||
|
||||
if task.ID == 0 {
|
||||
_, err := s.dbm_fix(s.Model).
|
||||
Data("player_id", task.PlayerID, "task_id", task.TaskID, "data", task.Data).
|
||||
Insert()
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if !strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
_, err := s.dbm_fix(s.Model).
|
||||
Where("player_id", task.PlayerID).
|
||||
Where("task_id", task.TaskID).
|
||||
Data("data", task.Data).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
type TaskService struct {
|
||||
|
||||
Reference in New Issue
Block a user