thrift backend server

This commit is contained in:
hahwu 2026-03-05 09:58:56 +08:00
parent a1d97a1ab6
commit 33dc62db36
12 changed files with 2859 additions and 2 deletions

52
src/server/backend.thrift Normal file
View File

@ -0,0 +1,52 @@
namespace go backend
struct ResAdminInfo{
1: string Name,
2: i64 Uid,
3: i32 AreaId,
4: i32 Face;
5: double Charge;
6: double MaxCharge;
7: i32 Level;
8: i32 Diamond;
9: i32 Star;
10: i32 Energy;
11: string UserName;
12: i64 LoginTime;
13: i32 Cumulative;
14: i32 RegisterTime;
15: i8 Ban;
16: i32 TodayCumulative;
17: i32 Bouns;
18: string Code;
19: map<string, i32> ChessMap;
20: list<Actlog> ActLog;
21: list<FriendInfo> FriendList;
22: map<i32,OrderInfo> OrderList;
}
struct Actlog{
1: i8 Type;
2: i64 Time;
3: string Param;
}
struct OrderInfo{
1: i32 Id;
2: i32 Type;
3: i64 Time;
4: string ChessId;
5: i8 Diff;
}
struct FriendInfo{
1: i64 Uid;
2: string NickName;
3: i32 Level;
4: i64 LogoutTime;
5: i64 LoginTime;
6: i32 Face;
}
service GameAdminService{
ResAdminInfo GetAdminInfo(1:i32 Uid);
}

View File

@ -0,0 +1,125 @@
package backend
import (
"context"
"fmt"
"net"
"server/db"
"server/game"
GoUtil "server/game_util"
"server/gen-go/backend"
"github.com/apache/thrift/lib/go/thrift"
)
type GameServiceHandler struct{}
func NewGameServiceHandler() *GameServiceHandler {
return &GameServiceHandler{}
}
func (h *GameServiceHandler) GetAdminInfo(ctx context.Context, id int32) (*backend.ResAdminInfo, error) {
player := game.G_GameLogicPtr.GetPlayer(int64(id))
online := true
if player == nil {
player = new(game.Player)
player.M_DwUin = int64(id)
player.InitPlayerOnly()
player.ZeroUpdate(nil)
online = false
}
res := &backend.ResAdminInfo{}
res.Name = player.GetBaseMod().NickName
res.UID = player.M_DwUin
res.AreaId = int32(player.GetDecorateMod().GetAreaId())
res.Face = int32(player.GetFaceMod().SetId)
res.Charge = player.GetChargeMod().Charge
res.MaxCharge = player.GetChargeMod().MaxCharge
res.Level = int32(player.GetPlayerBaseMod().GetLevel())
res.Diamond = int32(player.GetPlayerBaseMod().GetDiamond())
res.Star = int32(player.GetPlayerBaseMod().GetStar())
res.Energy = int32(player.GetPlayerBaseMod().GetEnergy())
res.UserName = player.GetPlayerBaseMod().GetName()
res.LoginTime = player.GetBaseMod().LoginTime
res.Cumulative = int32(player.GetBaseMod().Cumulative)
res.RegisterTime = int32(player.GetPlayerBaseMod().GetRegisterTime())
res.TodayCumulative = int32(player.GetBaseMod().TodayCumulative)
res.Ban = int8(db.GetPlayerBan(player.GetBaseMod().Account))
if online {
res.Cumulative = int32(int64(player.GetBaseMod().Cumulative) + GoUtil.Now() - int64(player.GetBaseMod().LoginTime))
res.TodayCumulative = int32(int64(player.GetBaseMod().TodayCumulative) + GoUtil.Now() - int64(player.GetBaseMod().LoginTime))
}
res.Code = player.GetBaseMod().AddCode
res.ChessMap = player.GetChessMod().ChessMap
resActLog := make([]*backend.Actlog, 0, len(player.GetFriendMod().ActivityLog))
for _, log := range player.GetFriendMod().ActivityLog {
resActLog = append(resActLog, &backend.Actlog{
Type: int8(log.Type),
Time: log.Time,
Param: log.Param,
})
}
res.ActLog = resActLog
friendList := player.GetFriendMod().NewFriendList
type friendInfo struct {
Uid int64
NickName string
Avatar int
Level int
LogoutTime int64
LoginTime int64
}
resFriendList := make([]*backend.FriendInfo, 0, len(friendList))
for v := range friendList {
ps := game.G_GameLogicPtr.GetSimplePlayerByUid(v)
if ps == nil {
continue
}
resFriendList = append(resFriendList, &backend.FriendInfo{
UID: int64(v),
NickName: ps.Name,
Face: int32(ps.Face),
Level: int32(ps.Level),
LogoutTime: ps.Loginout,
LoginTime: ps.Login,
})
}
res.FriendList = resFriendList
OrderMap := make(map[int32]*backend.OrderInfo)
Index := 0
for k, v := range player.GetOrderMod().OrderList {
OrderMap[int32(Index)] = &backend.OrderInfo{
ID: int32(k),
Type: int32(v.Type),
Time: v.Timestamp,
ChessId: GoUtil.IntSliceToString(v.MergeId),
Diff: int8(v.Diff),
}
Index++
}
res.OrderList = OrderMap
return res, nil
}
func Start() {
addr := net.JoinHostPort("localhost", "9090")
handler := NewGameServiceHandler()
//创建处理器
processor := backend.NewGameAdminServiceProcessor(handler)
transportFactory := thrift.NewTBufferedTransportFactory(8192)
protocolFactory := thrift.NewTBinaryProtocolFactoryConf(&thrift.TConfiguration{})
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
fmt.Printf("Failed to resolve address %s: %v\n", addr, err)
return
}
serverTransport := thrift.NewTServerSocketFromAddrTimeout(tcpAddr, 0)
server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
fmt.Printf("Starting the server on %s...\n", addr)
if err := server.Serve(); err != nil {
fmt.Printf("Error starting the server: %v\n", err)
}
}

View File

@ -172,7 +172,6 @@ func AdminPlayerInfo(args []interface{}) error {
res["Diamond"] = player.GetPlayerBaseMod().GetDiamond()
res["Star"] = player.GetPlayerBaseMod().GetStar()
res["Energy"] = player.GetPlayerBaseMod().GetEnergy()
res["Diamond"] = player.GetPlayerBaseMod().GetDiamond()
res["Mac"] = player.GetPlayerBaseMod().GetName()
res["Login"] = player.PlayMod.getBaseMod().LoginTime
res["Cumulative"] = player.PlayMod.getBaseMod().Cumulative

View File

@ -7,6 +7,7 @@ import (
"server/game/mod/chess"
"server/game/mod/decorate"
"server/game/mod/endless"
"server/game/mod/face"
"server/game/mod/friend"
"server/game/mod/fur"
limitedTimeEvent "server/game/mod/limited_time_event"
@ -67,3 +68,11 @@ func (p *Player) GetFurMod() *fur.FurMod {
func (p *Player) GetFriendMod() *friend.FriendMod {
return p.PlayMod.getFriendMod()
}
func (p *Player) GetFaceMod() *face.FaceMod {
return p.PlayMod.getFaceMod()
}
func (p *Player) GetLimitedTimeEventMod() *limitedTimeEvent.LimitedTimeEventMod {
return p.PlayMod.getLimitedTimeEventMod()
}

View File

@ -0,0 +1,6 @@
// Code generated by Thrift Compiler (0.22.0). DO NOT EDIT.
package backend
var GoUnusedProtection__ int;

View File

@ -0,0 +1,34 @@
// Code generated by Thrift Compiler (0.22.0). DO NOT EDIT.
package backend
import (
"bytes"
"context"
"errors"
"fmt"
"iter"
"log/slog"
"time"
thrift "github.com/apache/thrift/lib/go/thrift"
"strings"
"regexp"
)
// (needed to ensure safety because of naive import list construction.)
var _ = bytes.Equal
var _ = context.Background
var _ = errors.New
var _ = fmt.Printf
var _ = iter.Pull[int]
var _ = slog.Log
var _ = time.Now
var _ = thrift.ZERO
// (needed by validator.)
var _ = strings.Contains
var _ = regexp.MatchString
func init() {
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,164 @@
// Code generated by Thrift Compiler (0.22.0). DO NOT EDIT.
package main
import (
"context"
"flag"
"fmt"
"math"
"net"
"net/url"
"os"
"strconv"
"strings"
thrift "github.com/apache/thrift/lib/go/thrift"
"server/gen-go/backend"
)
var _ = backend.GoUnusedProtection__
func Usage() {
fmt.Fprintln(os.Stderr, "Usage of ", os.Args[0], " [-h host:port] [-u url] [-f[ramed]] function [arg1 [arg2...]]:")
flag.PrintDefaults()
fmt.Fprintln(os.Stderr, "\nFunctions:")
fmt.Fprintln(os.Stderr, " ResAdminInfo GetAdminInfo(i32 Uid)")
fmt.Fprintln(os.Stderr)
os.Exit(0)
}
type httpHeaders map[string]string
func (h httpHeaders) String() string {
var m map[string]string = h
return fmt.Sprintf("%s", m)
}
func (h httpHeaders) Set(value string) error {
parts := strings.Split(value, ": ")
if len(parts) != 2 {
return fmt.Errorf("header should be of format 'Key: Value'")
}
h[parts[0]] = parts[1]
return nil
}
func main() {
flag.Usage = Usage
var host string
var port int
var protocol string
var urlString string
var framed bool
var useHttp bool
headers := make(httpHeaders)
var parsedUrl *url.URL
var trans thrift.TTransport
_ = strconv.Atoi
_ = math.Abs
flag.Usage = Usage
flag.StringVar(&host, "h", "localhost", "Specify host and port")
flag.IntVar(&port, "p", 9090, "Specify port")
flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson, json)")
flag.StringVar(&urlString, "u", "", "Specify the url")
flag.BoolVar(&framed, "framed", false, "Use framed transport")
flag.BoolVar(&useHttp, "http", false, "Use http")
flag.Var(headers, "H", "Headers to set on the http(s) request (e.g. -H \"Key: Value\")")
flag.Parse()
if len(urlString) > 0 {
var err error
parsedUrl, err = url.Parse(urlString)
if err != nil {
fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
flag.Usage()
}
host = parsedUrl.Host
useHttp = len(parsedUrl.Scheme) <= 0 || parsedUrl.Scheme == "http" || parsedUrl.Scheme == "https"
} else if useHttp {
_, err := url.Parse(fmt.Sprint("http://", host, ":", port))
if err != nil {
fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
flag.Usage()
}
}
cmd := flag.Arg(0)
var err error
var cfg *thrift.TConfiguration = nil
if useHttp {
trans, err = thrift.NewTHttpClient(parsedUrl.String())
if len(headers) > 0 {
httptrans := trans.(*thrift.THttpClient)
for key, value := range headers {
httptrans.SetHeader(key, value)
}
}
} else {
portStr := fmt.Sprint(port)
if strings.Contains(host, ":") {
host, portStr, err = net.SplitHostPort(host)
if err != nil {
fmt.Fprintln(os.Stderr, "error with host:", err)
os.Exit(1)
}
}
trans = thrift.NewTSocketConf(net.JoinHostPort(host, portStr), cfg)
if err != nil {
fmt.Fprintln(os.Stderr, "error resolving address:", err)
os.Exit(1)
}
if framed {
trans = thrift.NewTFramedTransportConf(trans, cfg)
}
}
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating transport", err)
os.Exit(1)
}
defer trans.Close()
var protocolFactory thrift.TProtocolFactory
switch protocol {
case "compact":
protocolFactory = thrift.NewTCompactProtocolFactoryConf(cfg)
case "simplejson":
protocolFactory = thrift.NewTSimpleJSONProtocolFactoryConf(cfg)
case "json":
protocolFactory = thrift.NewTJSONProtocolFactory()
case "binary", "":
protocolFactory = thrift.NewTBinaryProtocolFactoryConf(cfg)
default:
fmt.Fprintln(os.Stderr, "Invalid protocol specified: ", protocol)
Usage()
os.Exit(1)
}
iprot := protocolFactory.GetProtocol(trans)
oprot := protocolFactory.GetProtocol(trans)
client := backend.NewGameAdminServiceClient(thrift.NewTStandardClient(iprot, oprot))
if err := trans.Open(); err != nil {
fmt.Fprintln(os.Stderr, "Error opening socket to ", host, ":", port, " ", err)
os.Exit(1)
}
switch cmd {
case "GetAdminInfo":
if flag.NArg() - 1 != 1 {
fmt.Fprintln(os.Stderr, "GetAdminInfo requires 1 args")
flag.Usage()
}
tmp0, err18 := (strconv.Atoi(flag.Arg(1)))
if err18 != nil {
Usage()
return
}
argvalue0 := int32(tmp0)
value0 := argvalue0
fmt.Print(client.GetAdminInfo(context.Background(), value0))
fmt.Print("\n")
break
case "":
Usage()
default:
fmt.Fprintln(os.Stderr, "Invalid function ", cmd)
}
}

View File

@ -46,7 +46,10 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
)
require golang.org/x/sys v0.29.0 // indirect
require (
github.com/apache/thrift v0.22.0 // indirect
golang.org/x/sys v0.29.0 // indirect
)
require (
filippo.io/edwards25519 v1.1.0 // indirect

View File

@ -73,6 +73,8 @@ github.com/aliyun/credentials-go v1.3.6/go.mod h1:1LxUuX7L5YrZUWzBrRyk0SwSdH4OmP
github.com/aliyun/credentials-go v1.4.5/go.mod h1:Jm6d+xIgwJVLVWT561vy67ZRP4lPTQxMbEYRuT2Ti1U=
github.com/aliyun/credentials-go v1.4.6 h1:CG8rc/nxCNKfXbZWpWDzI9GjF4Tuu3Es14qT8Y0ClOk=
github.com/aliyun/credentials-go v1.4.6/go.mod h1:Jm6d+xIgwJVLVWT561vy67ZRP4lPTQxMbEYRuT2Ti1U=
github.com/apache/thrift v0.22.0 h1:r7mTJdj51TMDe6RtcmNdQxgn9XcyfGDOzegMDRg47uc=
github.com/apache/thrift v0.22.0/go.mod h1:1e7J/O1Ae6ZQMTYdy9xa3w9k+XHWPfRvdPyJeynQ+/g=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=

View File

@ -2,6 +2,7 @@ package test
import (
"fmt"
"server/backend"
decorateCfg "server/conf/decorate"
languageCfg "server/conf/language"
notification_cfg "server/conf/notification"
@ -14,6 +15,10 @@ import (
"testing"
)
func TestThriftServer(t *testing.T) {
backend.Start()
}
func TestFixDecorate(t *testing.T) {
// Initialize player
p := new(game.Player)

View File

@ -0,0 +1,2 @@
2026/03/05 00:00:00 [debug ] CreateDailyLogFile
2026/03/05 00:00:00 [debug ] Server ZeroFlush