17 changed files with 442 additions and 84 deletions
@ -0,0 +1,27 @@
|
||||
// =================================================================================
|
||||
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
|
||||
// =================================================================================
|
||||
|
||||
package dao |
||||
|
||||
import ( |
||||
"tyj_admin/internal/dao/internal" |
||||
) |
||||
|
||||
// internalGameOnlineCountDao is internal type for wrapping internal DAO implements.
|
||||
type internalGameOnlineCountDao = *internal.GameOnlineCountDao |
||||
|
||||
// gameOnlineCountDao is the data access object for table game_online_count.
|
||||
// You can define custom methods on it to extend its functionality as you wish.
|
||||
type gameOnlineCountDao struct { |
||||
internalGameOnlineCountDao |
||||
} |
||||
|
||||
var ( |
||||
// GameOnlineCount is globally public accessible object for table game_online_count operations.
|
||||
GameOnlineCount = gameOnlineCountDao{ |
||||
internal.NewGameOnlineCountDao(), |
||||
} |
||||
) |
||||
|
||||
// Fill with you ideas below.
|
||||
@ -0,0 +1,77 @@
|
||||
// ==========================================================================
|
||||
// Code generated by GoFrame CLI tool. DO NOT EDIT.
|
||||
// ==========================================================================
|
||||
|
||||
package internal |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/gogf/gf/v2/database/gdb" |
||||
"github.com/gogf/gf/v2/frame/g" |
||||
) |
||||
|
||||
// GameOnlineCountDao is the data access object for table game_online_count.
|
||||
type GameOnlineCountDao struct { |
||||
table string // table is the underlying table name of the DAO.
|
||||
group string // group is the database configuration group name of current DAO.
|
||||
columns GameOnlineCountColumns // columns contains all the column names of Table for convenient usage.
|
||||
} |
||||
|
||||
// GameOnlineCountColumns defines and stores column names for table game_online_count.
|
||||
type GameOnlineCountColumns struct { |
||||
Id string //
|
||||
Online string //
|
||||
DailyTop string //
|
||||
} |
||||
|
||||
// gameOnlineCountColumns holds the columns for table game_online_count.
|
||||
var gameOnlineCountColumns = GameOnlineCountColumns{ |
||||
Id: "id", |
||||
Online: "online", |
||||
DailyTop: "daily_top", |
||||
} |
||||
|
||||
// NewGameOnlineCountDao creates and returns a new DAO object for table data access.
|
||||
func NewGameOnlineCountDao() *GameOnlineCountDao { |
||||
return &GameOnlineCountDao{ |
||||
group: "default", |
||||
table: "game_online_count", |
||||
columns: gameOnlineCountColumns, |
||||
} |
||||
} |
||||
|
||||
// DB retrieves and returns the underlying raw database management object of current DAO.
|
||||
func (dao *GameOnlineCountDao) DB() gdb.DB { |
||||
return g.DB(dao.group) |
||||
} |
||||
|
||||
// Table returns the table name of current dao.
|
||||
func (dao *GameOnlineCountDao) Table() string { |
||||
return dao.table |
||||
} |
||||
|
||||
// Columns returns all column names of current dao.
|
||||
func (dao *GameOnlineCountDao) Columns() GameOnlineCountColumns { |
||||
return dao.columns |
||||
} |
||||
|
||||
// Group returns the configuration group name of database of current dao.
|
||||
func (dao *GameOnlineCountDao) Group() string { |
||||
return dao.group |
||||
} |
||||
|
||||
// Ctx creates and returns the Model for current DAO, It automatically sets the context for current operation.
|
||||
func (dao *GameOnlineCountDao) Ctx(ctx context.Context) *gdb.Model { |
||||
return dao.DB().Model(dao.table).Safe().Ctx(ctx) |
||||
} |
||||
|
||||
// Transaction wraps the transaction logic using function f.
|
||||
// It rollbacks the transaction and returns the error from function f if it returns non-nil error.
|
||||
// It commits the transaction and returns nil if function f returns nil.
|
||||
//
|
||||
// Note that, you should not Commit or Rollback the transaction in function f
|
||||
// as it is automatically handled by this function.
|
||||
func (dao *GameOnlineCountDao) Transaction(ctx context.Context, f func(ctx context.Context, tx *gdb.TX) error) (err error) { |
||||
return dao.Ctx(ctx).Transaction(ctx, f) |
||||
} |
||||
@ -0,0 +1,17 @@
|
||||
// =================================================================================
|
||||
// Code generated by GoFrame CLI tool. DO NOT EDIT.
|
||||
// =================================================================================
|
||||
|
||||
package do |
||||
|
||||
import ( |
||||
"github.com/gogf/gf/v2/frame/g" |
||||
) |
||||
|
||||
// GameOnlineCount is the golang structure of table game_online_count for DAO operations like Where/Data.
|
||||
type GameOnlineCount struct { |
||||
g.Meta `orm:"table:game_online_count, do:true"` |
||||
Id interface{} //
|
||||
Online interface{} //
|
||||
DailyTop interface{} //
|
||||
} |
||||
@ -0,0 +1,12 @@
|
||||
// =================================================================================
|
||||
// Code generated by GoFrame CLI tool. DO NOT EDIT.
|
||||
// =================================================================================
|
||||
|
||||
package entity |
||||
|
||||
// GameOnlineCount is the golang structure for table game_online_count.
|
||||
type GameOnlineCount struct { |
||||
Id int `json:"id" description:""` |
||||
Online int `json:"online" description:""` |
||||
DailyTop int `json:"dailyTop" description:""` |
||||
} |
||||
@ -1,62 +1,79 @@
|
||||
package internal |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"fmt" |
||||
mqtt "github.com/eclipse/paho.mqtt.golang" |
||||
"strings" |
||||
"context" |
||||
"github.com/gogf/gf/v2/frame/g" |
||||
"log" |
||||
"tyj_admin/api/v1/game" |
||||
"tyj_admin/internal/dao" |
||||
"tyj_admin/internal/model/do" |
||||
"tyj_admin/internal/model/entity" |
||||
) |
||||
|
||||
type MqttOnlineReq struct { |
||||
ReqId int64 `json:"reqId"` |
||||
ModuleId string `json:"moduleId"` |
||||
func IncrOnlineCount(ctx context.Context, server int) { |
||||
model := dao.GameOnlineCount.Ctx(ctx) |
||||
model = model.Where("id=?", server) |
||||
_, err := model.Update("online=online+1") |
||||
if err != nil { |
||||
log.Print(err) |
||||
return |
||||
} |
||||
var online []*entity.GameOnlineCount |
||||
_ = model.Scan(&online) |
||||
if len(online) > 0 && online[0].Online > online[0].DailyTop { |
||||
_, _ = model.Update(g.Map{dao.GameOnlineCount.Columns().DailyTop: online[0].Online}) |
||||
} |
||||
} |
||||
|
||||
type MqttOnlineRes struct { |
||||
RespId int `json:"respId"` |
||||
Error interface{} `json:"error"` |
||||
Body map[string]OnlineBody `json:"body"` |
||||
func DecrOnlineCount(ctx context.Context, server int) { |
||||
model := dao.GameOnlineCount.Ctx(ctx) |
||||
model = model.Where("id=?", server) |
||||
_, err := model.Update("online=online-1") |
||||
if err != nil { |
||||
log.Print(err) |
||||
return |
||||
} |
||||
} |
||||
|
||||
type OnlineBody struct { |
||||
ServerId string `json:"serverId"` |
||||
LoginedList []OnlineLoginInfo `json:"loginedList"` |
||||
} |
||||
func SetOnlineCount(ctx context.Context, server int, count int) { |
||||
model := dao.GameOnlineCount.Ctx(ctx) |
||||
model = model.Where("id=?", server) |
||||
var onlineIndex []*entity.GameOnlineCount |
||||
_ = model.Scan(&onlineIndex) |
||||
if len(onlineIndex) == 0 { |
||||
_, _ = dao.GameOnlineCount.Ctx(ctx).Insert(do.GameOnlineCount{Id: server, Online: count, DailyTop: count}) |
||||
} |
||||
|
||||
type OnlineLoginInfo struct { |
||||
LoginTime int64 `json:"loginTime"` |
||||
Uid int32 `json:"uid"` |
||||
Address string `json:"address"` |
||||
} |
||||
_, err := model.Update(g.Map{dao.GameOnlineCount.Columns().Online: count}) |
||||
if err != nil { |
||||
log.Print(err) |
||||
return |
||||
} |
||||
|
||||
func SendMqttOnline(msg interface{}, c chan *MqttOnlineRes, server string) { |
||||
var online []*entity.GameOnlineCount |
||||
_ = model.Scan(&online) |
||||
if len(online) > 0 && online[0].Online > online[0].DailyTop { |
||||
_, _ = model.Update(g.Map{dao.GameOnlineCount.Columns().DailyTop: online[0].Online}) |
||||
} |
||||
} |
||||
|
||||
registerFunc := func(client mqtt.Client, qtmsg mqtt.Message) { |
||||
ss := string(msg.([]byte)) |
||||
fmt.Println(ss) |
||||
err := ClientSend("client", 0, false, ss) |
||||
if err != nil { |
||||
c <- &MqttOnlineRes{Error: err} |
||||
func ResetOnlineTop(ctx context.Context) { |
||||
model := dao.GameOnlineCount.Ctx(ctx) |
||||
var online []*entity.GameOnlineCount |
||||
_ = model.Scan(&online) |
||||
for _, v := range online { |
||||
if v != nil { |
||||
_, _ = model.Where("id=?", v.Id).Update(g.Map{dao.GameOnlineCount.Columns().DailyTop: v.Online}) |
||||
} |
||||
//ClientSend("client", 0, false, `{"reqId":3,"moduleId":"webadmin","body":{"uids":"lq0001","gm":1,"moduleType":"changeGM"}}`)
|
||||
} |
||||
|
||||
callback := func(client mqtt.Client, qtmsg mqtt.Message) { |
||||
res := &MqttOnlineRes{} |
||||
client.Disconnect(1) |
||||
ss := string(qtmsg.Payload()) |
||||
ss = ss[1 : len(ss)-1] |
||||
ss = strings.Replace(ss, "\\", "", -1) |
||||
err := json.Unmarshal([]byte(ss), res) |
||||
if err == nil && res.RespId == 1 { |
||||
c <- res |
||||
return |
||||
} |
||||
c <- &MqttOnlineRes{Error: err} |
||||
|
||||
} |
||||
if err := NewMqttClient(registerFunc, callback, server); err != nil { |
||||
c <- &MqttOnlineRes{Error: err} |
||||
} |
||||
} |
||||
|
||||
func GetOnlineTopList(ctx context.Context, req *game.RoleOnlineTopReq) (res *game.RoleOnlineTopRes, err error) { |
||||
model := dao.GameOnlineCount.Ctx(ctx) |
||||
if req.ServerId != 0 { |
||||
model = model.Where("id=?", req.ServerId) |
||||
} |
||||
res = new(game.RoleOnlineTopRes) |
||||
_ = model.Scan(&res.Onlines) |
||||
return |
||||
} |
||||
|
||||
Loading…
Reference in new issue