Browse Source

misc: land and out api(#master)

master
OhYee 2 years ago
parent
commit
f7d03f425f
Signed by: OhYee
GPG Key ID: 5A9E1F63ED274FBB
  1. 163
      extensions/queue/main.go
  2. 8
      extensions/queue/register.go
  3. 125
      extensions/queue/type.go

163
extensions/queue/main.go

@ -1,11 +1,13 @@ @@ -1,11 +1,13 @@
package queue
import (
"fmt"
"time"
"github.com/OhYee/blotter/api"
"github.com/OhYee/blotter/api/pkg/user"
"github.com/OhYee/blotter/mongo"
"github.com/OhYee/blotter/output"
"github.com/OhYee/blotter/register"
"github.com/OhYee/rainbow/errors"
"go.mongodb.org/mongo-driver/bson"
@ -13,12 +15,13 @@ import ( @@ -13,12 +15,13 @@ import (
)
func userValid(u *user.TypeDB) error {
if u.NintendoSwitch == "" &&
u.NintendoSwitchName == "" &&
u.AnimalCrossingName == "" &&
if u.QQUnionID == "" ||
u.QQ == "" ||
u.NintendoSwitch == "" ||
u.NintendoSwitchName == "" ||
u.AnimalCrossingName == "" ||
u.AnimalCrossingIsland == "" {
return errors.New("请在个人设置界面填写 Nintendo Switch、动森信息")
return errors.New("请在个人设置界面填写 QQ 号、Nintendo Switch、动森信息并绑定 QQ 互联")
}
if u.Black > time.Now().Unix() {
return errors.New("您由于多次违规,已被拉黑")
@ -250,13 +253,13 @@ func insert(userID primitive.ObjectID, ID string) (err error) { @@ -250,13 +253,13 @@ func insert(userID primitive.ObjectID, ID string) (err error) {
return
}
ids, err := mongo.Add("blotter", "queue_members", nil, bson.M{
"user": userID,
"in_time": time.Now().Unix(),
"out_time": 0,
"status": 0,
"queue": queueID,
})
ids, err := mongo.Add("blotter", "queue_members", nil, NewMemberDB(
userID,
queueID,
time.Now().Unix(),
0,
0,
))
if err != nil {
return
}
@ -285,7 +288,7 @@ func Get(context register.HandleContext) (err error) { @@ -285,7 +288,7 @@ func Get(context register.HandleContext) (err error) {
res := new(GetResponse)
context.RequestArgs(args)
if res.Queue, err = getQueue(args.ID); err != nil {
if res.Queue, err = getQueue(context.GetUser(), args.ID); err != nil {
return
}
@ -298,7 +301,7 @@ func Get(context register.HandleContext) (err error) { @@ -298,7 +301,7 @@ func Get(context register.HandleContext) (err error) {
return
}
func getQueue(id string) (res *Queue, err error) {
func getQueue(u *user.TypeDB, id string) (res *Queue, err error) {
queueID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return
@ -325,6 +328,14 @@ func getQueue(id string) (res *Queue, err error) { @@ -325,6 +328,14 @@ func getQueue(id string) (res *Queue, err error) {
if cnt > 0 {
q := &qs[0]
waitingMembers := q.GetWaitingMembers()
output.Debug("%+v", waitingMembers)
if !(u != nil &&
(u.ID == q.Leader ||
(len(waitingMembers) > 0 && u.ID == waitingMembers[0].User))) {
q.Password = ""
}
res = q.ToQueue()
}
@ -386,7 +397,129 @@ func getAllQueue(all bool, offset int64, number int64) (cnt int64, res []*Queue, @@ -386,7 +397,129 @@ func getAllQueue(all bool, offset int64, number int64) (cnt int64, res []*Queue,
if cnt, err = mongo.Aggregate("blotter", "queue", pipeline, nil, &qs); err != nil {
return
}
res = QueueDBsToQueues(qs)
res = queueDBsToQueues(qs, true)
return
}
type LandRequest struct {
QueueID string `json:"queue_id"`
MemberID string `json:"member_id"`
}
type LandResponse api.SimpleResponse
func Land(context register.HandleContext) (err error) {
u := context.GetUser()
if u == nil {
context.Forbidden()
return
}
args := new(LandRequest)
res := new(LandResponse)
context.RequestArgs(args)
if err = userValid(u); err != nil {
res.Success = false
res.Title = "着陆失败"
res.Content = err.Error()
err = context.ReturnJSON(res)
return
}
if err = landAndOut(u.ID, args.QueueID, args.MemberID, "land"); err != nil {
res.Success = false
res.Title = "着陆失败"
res.Content = err.Error()
} else {
res.Success = true
res.Title = "着陆成功"
}
err = context.ReturnJSON(res)
return
}
func landAndOut(userObjID primitive.ObjectID, queueID string, memberID string, op string) (err error) {
defer errors.Wrapper(&err)
if op != "land" && op != "out" {
err = errors.New("op must be \"land\" or \"out\"")
return
}
fieldName := fmt.Sprintf("%s_time", op)
queueObjID, err := primitive.ObjectIDFromHex(queueID)
if err != nil {
return
}
memberObjID, err := primitive.ObjectIDFromHex(memberID)
if err != nil {
return
}
cnt, err := mongo.Find("blotter", "queue", bson.M{
"_id": queueObjID,
"finish_time": 0,
}, nil, nil)
if cnt == 0 {
err = errors.New("队伍不存在或已结束")
return
}
res, err := mongo.Update("blotter", "queue_members", bson.M{
"_id": memberObjID,
"queue": queueObjID,
"user": userObjID,
fieldName: 0,
}, bson.M{
"$set": bson.M{
fieldName: time.Now().Unix(),
},
}, nil)
if err == nil && res.ModifiedCount == 0 {
err = errors.New("未找到符合的记录")
}
return
}
type OutRequest struct {
QueueID string `json:"queue_id"`
MemberID string `json:"member_id"`
}
type OutResponse api.SimpleResponse
func Out(context register.HandleContext) (err error) {
u := context.GetUser()
if u == nil {
context.Forbidden()
return
}
args := new(OutRequest)
res := new(OutResponse)
context.RequestArgs(args)
if err = userValid(u); err != nil {
res.Success = false
res.Title = "出队失败"
res.Content = err.Error()
err = context.ReturnJSON(res)
return
}
if err = landAndOut(u.ID, args.QueueID, args.MemberID, "out"); err != nil {
res.Success = false
res.Title = "出队失败"
res.Content = err.Error()
} else {
res.Success = true
res.Title = "出队成功"
}
err = context.ReturnJSON(res)
return
}

8
extensions/queue/register.go

@ -21,6 +21,14 @@ func Register() *register.Extension { @@ -21,6 +21,14 @@ func Register() *register.Extension {
"insert",
Insert,
)
ext.PreRegister(
"land",
Land,
)
ext.PreRegister(
"out",
Out,
)
ext.PreRegister(
"get",
Get,

125
extensions/queue/type.go

@ -7,26 +7,33 @@ import ( @@ -7,26 +7,33 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
)
type QueueBase struct {
Password string `json:"password" bson:"password"`
Description string `json:"description" bson:"description"`
Max int64 `json:"max" bson:"max"`
CreateTime int64 `json:"create_time" bson:"create_time"`
FinishTime int64 `json:"finish_time" bson:"finish_time"`
}
// Queue for animial crossing
type Queue struct {
QueueDB `bson:",inline"`
Leader *user.Type `json:"leader" bson:"leader"`
Queue []*Member `json:"queue" bson:"queue"`
QueueBase `bson:",inline"`
ID string `json:"id" bson:"_id"`
Leader *user.Type `json:"leader" bson:"leader"`
Queue []*Member `json:"queue" bson:"queue"`
}
type QueueDB struct {
ID primitive.ObjectID `json:"id" bson:"_id"`
Leader primitive.ObjectID `json:"leader" bson:"leader"`
Password string `json:"password" bson:"paswsword"`
Description string `json:"description" bson:"description"`
Max int64 `json:"max" bson:"max"`
CreateTime int64 `json:"create_time" bson:"create_time"`
FinishTime int64 `json:"finish_time" bson:"finish_time"`
Queue []*MemberDB `json:"queue" bson:"queue"`
QueueBase `bson:",inline"`
ID primitive.ObjectID `json:"id" bson:"_id"`
Leader primitive.ObjectID `json:"leader" bson:"leader"`
Queue []*MemberDB `json:"queue" bson:"queue"`
}
// QueueDBsToQueues transfer []*QueueDB to []*Queue
func QueueDBsToQueues(queues []*QueueDB) (res []*Queue) {
// queueDBsToQueues transfer []*QueueDB to []*Queue
func queueDBsToQueues(queues []*QueueDB, removePassword bool) (res []*Queue) {
ids := make([]primitive.ObjectID, 0)
for _, q := range queues {
@ -66,42 +73,102 @@ func QueueDBsToQueues(queues []*QueueDB) (res []*Queue) { @@ -66,42 +73,102 @@ func QueueDBsToQueues(queues []*QueueDB) (res []*Queue) {
ms = append(ms, m.ToMember(u))
}
}
res = append(res, &Queue{
QueueDB: *q,
Leader: userMap[q.Leader].Desensitization(false),
Queue: ms,
})
queue := &Queue{
QueueBase: q.QueueBase,
ID: q.ID.Hex(),
Leader: userMap[q.Leader].Desensitization(false),
Queue: ms,
}
if removePassword {
queue.Password = ""
}
res = append(res, queue)
}
return
}
func (q *QueueDB) ToQueue() *Queue {
queues := QueueDBsToQueues([]*QueueDB{q})
queues := queueDBsToQueues([]*QueueDB{q}, false)
if len(queues) > 0 {
return queues[0]
}
return nil
}
// GetWaitingMembers get the members which are waiting for password
func (q *QueueDB) GetWaitingMembers() (res []*MemberDB) {
max := q.Max
res = make([]*MemberDB, 0)
for _, member := range q.Queue {
if member.OutTime == 0 && member.LandTime != 0 {
// on island
max--
}
}
if max <= 0 {
return
}
for _, member := range q.Queue {
if member.OutTime == 0 && member.LandTime == 0 {
// waiting
res = append(res, member)
max--
if max <= 0 {
return
}
}
}
return
}
type MemberBase struct {
InTime int64 `json:"in_time" bson:"in_time"`
LandTime int64 `json:"land_time" bson:"land_time"`
OutTime int64 `json:"out_time" bson:"out_time"`
// Status int8 `json:"status" bson:"status"` // 0 in queue; 1 landed; 2 backed; 3 canceled
}
type Member struct {
MemberDB `bson:",inline"`
User *user.Type `json:"user" bson:"user"`
MemberBase `bson:",inline"`
ID string `json:"id" bson:"_id"`
User *user.Type `json:"user" bson:"user"`
}
type MemberDB struct {
ID primitive.ObjectID `json:"id" bson:"_id"`
User primitive.ObjectID `json:"user" bson:"user"`
Queue primitive.ObjectID `json:"queue" bson:"queue"`
InTime int64 `json:"in_time" bson:"in_time"`
OutTime int64 `json:"out_time" bson:"out_time"`
Status int8 `json:"status" bson:"status"` // 0 in queue; 1 landed; 2 backed; 3 canceled
MemberBase `bson:",inline"`
ID primitive.ObjectID `json:"id" bson:"_id"`
User primitive.ObjectID `json:"user" bson:"user"`
Queue primitive.ObjectID `json:"queue" bson:"queue"`
}
// ToMember transfer MemberDB to Member
func (m *MemberDB) ToMember(u *user.TypeDB) *Member {
return &Member{
MemberDB: *m,
User: u.Desensitization(false),
MemberBase: m.MemberBase,
ID: m.ID.Hex(),
User: u.Desensitization(false),
}
}
// NewMemberDB initial a MemberDB
func NewMemberDB(
userID, queueID primitive.ObjectID,
inTime, landTime, outTime int64,
) *MemberDB {
return &MemberDB{
ID: primitive.NewObjectID(),
User: userID,
Queue: queueID,
MemberBase: MemberBase{
InTime: inTime,
LandTime: landTime,
OutTime: outTime,
},
}
}

Loading…
Cancel
Save