You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
package model
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type GameServer struct {
|
|
ID int64 `json:"id" bson:"_id"`
|
|
ServerName string `json:"server_name" bson:"server_name"`
|
|
Status int64 `json:"status" bson:"status"`
|
|
}
|
|
|
|
type RespGameServer struct {
|
|
ID int64 `json:"id"`
|
|
ServerName string `json:"server_name"`
|
|
Status int64 `json:"status"`
|
|
UserCount int64 `json:"user_count"`
|
|
OnlineCount int64 `json:"online_count"`
|
|
}
|
|
|
|
func GetGameServerList(collection *mongo.Collection, offset int64, limit int64) ([]RespGameServer, int64, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
opts := options.Find().SetLimit(limit).SetSkip(offset).SetSort(bson.D{{Key: "_id", Value: 1}})
|
|
cur, err := collection.Find(ctx, bson.D{}, opts)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defer cur.Close(ctx)
|
|
list := []GameServer{}
|
|
err = cur.All(context.Background(), &list)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
result := []RespGameServer{}
|
|
|
|
for _, v := range list {
|
|
res := RespGameServer{
|
|
ID: v.ID,
|
|
ServerName: v.ServerName,
|
|
Status: v.Status,
|
|
UserCount: 0,
|
|
OnlineCount: 0,
|
|
}
|
|
result = append(result, res)
|
|
}
|
|
|
|
count, err := collection.CountDocuments(ctx, bson.D{})
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return result, count, nil
|
|
}
|
|
|
|
func AddGameServer(collection *mongo.Collection, doc GameServer) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
_, err := collection.InsertOne(ctx, doc)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func UpdateGameServer(collection *mongo.Collection, doc GameCode) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
filter := bson.D{{Key: "code", Value: doc.Code}}
|
|
update := bson.D{{Key: "$set", Value: bson.D{{Key: "expire_type", Value: doc.ExpireType}, {Key: "start_expire", Value: doc.StartExpire}, {Key: "end_expire", Value: doc.EndExpire}, {Key: "items", Value: doc.Items}}}}
|
|
_, err := collection.UpdateOne(ctx, filter, update)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|