game2006go/server/imserver/playermgr.go
2023-08-14 17:47:23 +08:00

150 lines
3.7 KiB
Go

package main
import (
"cs"
"encoding/json"
"f5"
"fmt"
"mt"
"q5"
)
type PlayerMgr struct {
cs.MsgHandlerImpl
accountIdHash map[string]*Player
socketHash map[f5.WspCliConn]*Player
}
func (this *PlayerMgr) init() {
this.accountIdHash = make(map[string]*Player)
this.socketHash = make(map[f5.WspCliConn]*Player)
f5.GetTimer().SetInterval(
1000,
func(e int32, args *q5.Args) {
if e == q5.TIMER_EXEC_EVENT {
mt.Table.MasterCluster.Traverse(
func(meta *mt.MasterCluster) bool {
this.reportServerState(meta.GetIp(), meta.GetListenPort())
return true
})
}
})
conn := q5.NewMysql(
mt.Table.GameDb.GetById(0).GetHost(),
mt.Table.GameDb.GetById(0).GetPort(),
mt.Table.GameDb.GetById(0).GetUser(),
mt.Table.GameDb.GetById(0).GetPasswd(),
mt.Table.GameDb.GetById(0).GetDatabase(),
)
conn.Open()
rows, err := conn.Query("SELECT * FROM t_user LIMIT 1")
if err == nil {
dataSet := f5.NewDataSet(rows)
if dataSet.Next() {
columns := dataSet.GetColumns()
for i := 0; i < len(columns); i++ {
f5.GetSysLog().Info(
"%s:%s",
columns[i],
*dataSet.GetByName(columns[i]))
}
}
} else {
f5.GetSysLog().Info("mysql error", err)
}
}
func (this *PlayerMgr) unInit() {
}
func (this *PlayerMgr) CMLogin(hdr *f5.MsgHdr, msg *cs.CMLogin) {
params := map[string]string{
"c": "User",
"a": "info",
"account_id": msg.GetAccountId(),
"session_id": msg.GetSessionId(),
"target_id": msg.GetAccountId(),
}
url := fmt.Sprintf("%s/webapp/index.php", mt.Table.Config.GetById(0).GetGameapiUrl())
f5.GetHttpCliMgr().SendJsStyleRequest(
url,
params,
func(rsp f5.HttpCliResponse) {
this.CMLoginResult(hdr, msg, rsp)
})
}
func (this *PlayerMgr) CMLoginResult(hdr *f5.MsgHdr, msg *cs.CMLogin, rsp f5.HttpCliResponse) {
resObj := struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
Info struct {
Activated string `json:"activated"`
RenameCount string `json:"rename_count"`
AccountID string `json:"account_id"`
Name string `json:"name"`
} `json:"info"`
}{}
err := json.Unmarshal([]byte(rsp.GetRawData()), &resObj)
if err != nil {
f5.GetSysLog().Info("Api服务器JSON 解析错误:", err)
return
}
if resObj.Errcode != 0 {
f5.GetSysLog().Error("Api服务器errcode:%d", resObj.Errcode)
return
}
player := Player{
socket: hdr.GetSocket(),
accountId: msg.GetAccountId(),
sessionId: msg.GetAccountId(),
}
// Add to online user
this.addPlayer(&player)
this.addSocketHash(hdr.GetSocket(), &player)
serverInfo := "192.168.100.39:2000"
rspMsg := &cs.SMLogin{}
rspMsg.ServerInfo = &serverInfo
rspMsg.AccountId = &resObj.Info.AccountID
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
func (this *PlayerMgr) reportServerState(masterIp string, masterPort int32) {
params := map[string]string{
"node_id": q5.ToString(f5.GetApp().GetNodeId()),
"instance_id": q5.ToString(f5.GetApp().GetInstanceId()),
"ip": "192.168.100.164",
"port": q5.ToString(mt.Table.IMCluster.GetListenPort()),
"online_num": q5.ToString(0),
"room_num": q5.ToString(0),
"channel": q5.ToString(0),
"alive_count": q5.ToString(0),
"servicing": q5.ToString(1),
}
f5.GetHttpCliMgr().SendQuickChannelJsStyleRequest(
fmt.Sprintf("http://%s:%d/webapp/index.php?c=GS&a=report&", masterIp, masterPort),
params,
func(rsp f5.HttpCliResponse) {
f5.GetSysLog().Info(rsp.GetRawData())
})
}
func (this *PlayerMgr) addPlayer(p *Player) {
this.accountIdHash[p.accountId] = p
}
func (this *PlayerMgr) getPlayer(accountId string) *Player {
player, ok := this.accountIdHash[accountId]
if ok {
return player
}
return nil
}
func (this *PlayerMgr) addSocketHash(wsp f5.WspCliConn, p *Player) {
this.socketHash[wsp] = p
}