feat: add total player count and total playtime logic
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 1m27s
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 1m27s
- Implemented TotalPlayerCountLogic to retrieve the total number of unique players within a specified time range. - Implemented TotalPlayTimeLogic to calculate the total playtime of players within a specified time range. - Created ServiceContext to manage database connections and initialize necessary tables. - Added types for total player count and total playtime requests and responses. - Set up the main server file to start the application with the necessary configurations. - Updated go.mod and go.sum for new dependencies.
This commit is contained in:
64
app/internal/logic/serverlistlogic.go
Normal file
64
app/internal/logic/serverlistlogic.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.cialloo.com/CiallooWeb/ServerStatistics/app/internal/svc"
|
||||
"git.cialloo.com/CiallooWeb/ServerStatistics/app/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ServerListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// Get the list of monitored game servers
|
||||
func NewServerListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ServerListLogic {
|
||||
return &ServerListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ServerListLogic) ServerList(req *types.ServerListReq) (resp []types.ServerListResp, err error) {
|
||||
// Query to select all servers from the database
|
||||
query := `SELECT name, ip, port, category, bot_count, human_count, mapname FROM server ORDER BY id`
|
||||
|
||||
rows, err := l.svcCtx.DB.Query(query)
|
||||
if err != nil {
|
||||
l.Logger.Errorf("Failed to query servers: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
// Iterate through the results and build the response
|
||||
for rows.Next() {
|
||||
var server types.ServerListResp
|
||||
err = rows.Scan(
|
||||
&server.ServerName,
|
||||
&server.ServerIP,
|
||||
&server.ServerPort,
|
||||
&server.Category,
|
||||
&server.BotCount,
|
||||
&server.HumanCount,
|
||||
&server.MapName,
|
||||
)
|
||||
if err != nil {
|
||||
l.Logger.Errorf("Failed to scan server row: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
resp = append(resp, server)
|
||||
}
|
||||
|
||||
// Check for any error that occurred during iteration
|
||||
if err = rows.Err(); err != nil {
|
||||
l.Logger.Errorf("Error iterating through server rows: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user