提交信息

This commit is contained in:
1
2025-08-27 20:52:15 +00:00
parent fc2f88f14a
commit b36ff6d0f1
10 changed files with 283 additions and 18 deletions

View File

@@ -1 +1,44 @@
package model
package model
import "blazing/cool"
const TableNameTask = "task"
// Task mapped from table <task>
type Task struct {
*cool.Model
PlayerID uint64 `gorm:"not null;index:idx_task_by_player_id;comment:'所属玩家ID'" json:"player_id"`
Data string `gorm:"type:text;not null;comment:'全部数据'" json:"data"`
}
// TaskInfo 单个任务的详细信息,包含任务步骤状态和整体状态
type TaskInfo struct {
// TaskInfo 任务步骤信息对应Java的@ArraySerialize(FIXED_LENGTH=20)注解
// struc:"[20]byte" 确保二进制序列化时固定20字节长度json标签指定JSON字段名
TaskInfo []uint32 `struc:"[20]byte" json:"task_info"`
// Status 任务整体状态0-未接受1-已接受2-已完成未领取3-已完成已领取
// json标签指定JSON字段名与业务状态说明保持一致
Status byte `json:"status"`
}
// TableName PlayerInfo's table name
func (*Task) TableName() string {
return TableNamePlayerInfo
}
// GroupName PlayerInfo's table group
func (*Task) GroupName() string {
return "default"
}
// NewPlayerInfo create a new PlayerInfo
func NewTask() *Task {
return &Task{
Model: cool.NewModel(),
}
}
// init 创建表
func init() {
cool.CreateTable(&Task{})
}

View File

@@ -0,0 +1,104 @@
package service
import (
"blazing/cool"
"blazing/modules/blazing/model"
"encoding/json"
)
type TaskService struct {
*UserService
}
func NewTaskService(id uint32) *TaskService {
return &TaskService{
&UserService{
userid: id,
Service: &cool.Service{
Model: model.NewTask(),
},
},
}
}
func (s *TaskService) Exec(t func(map[uint32]model.TaskInfo) bool) (ret bool) {
var tt model.Task
s.GetModel().Scan(&tt)
var ttt map[uint32]model.TaskInfo
json.Unmarshal([]byte(tt.Data), &ttt)
ret = t(ttt)
t1, _ := json.Marshal(&ttt)
tt.Data = string(t1)
s.GetModel().Save(&tt) //退出时保存
return
}
func (s *TaskService) AcceptTask(task uint32) (ret bool) {
s.Exec(func(ttt map[uint32]model.TaskInfo) bool {
ft, ok := ttt[task]
if ok { //如果找到任务
if ft.Status == 0 { //可以接受
ft.Status = 1
return true
} else {
return false
}
} else {
ttt[task] = model.TaskInfo{
Status: 1,
}
}
return false
})
return ret
}
/**
* 更新任务步骤
*/
func (s *TaskService) AddTaskBuf(task uint32, info model.TaskInfo) bool {
return s.Exec(func(ttt map[uint32]model.TaskInfo) bool {
if conditions, ok := ttt[task]; ok {
conditions.TaskInfo = info.TaskInfo
ttt[task] = conditions
return true
}
return false
})
}
/**
* 完成任务
*/
func (s *TaskService) CompleteTask(task uint32) bool {
return s.Exec(func(ttt map[uint32]model.TaskInfo) bool {
if conditions, ok := ttt[task]; ok {
conditions.Status = 3
ttt[task] = conditions
}
return false
})
}
/**
* 校验任务是否已经完成
*/
func (s *TaskService) CheckTaskCompleted(task uint32) bool {
return s.Exec(func(ttt map[uint32]model.TaskInfo) bool {
if conditions, ok := ttt[task]; ok {
return conditions.Status == 3
}
return false
})
}

View File

@@ -0,0 +1,31 @@
package service
import (
"blazing/cool"
"blazing/modules/blazing/model"
"encoding/json"
"github.com/gogf/gf/v2/database/gdb"
)
type UserService struct {
userid uint32
*cool.Service
}
func (s *UserService) GetModel() *gdb.Model {
return cool.DBM(s.Model).Where("player_id", s.userid)
}
func (s *UserService) Exec(t func(map[uint32]model.TaskInfo) bool) {
var tt model.Task
s.GetModel().Scan(&tt)
var ttt map[uint32]model.TaskInfo
json.Unmarshal([]byte(tt.Data), &ttt)
t(ttt)
t1, _ := json.Marshal(&ttt)
tt.Data = string(t1)
s.GetModel().Save(&tt) //退出时保存
}