code format, add login, add protoc
This commit is contained in:
parent
e07a170549
commit
7fccb49a1f
@ -1 +1,2 @@
|
|||||||
node ../../../tools/robot/app.js -hws://192.168.100.45:7801 -utest134345 -d../../../server/imserver/
|
node ../../../tools/robot/app.js -hws://192.168.100.45:7801 -utest134345 -d../../../server/imserver/
|
||||||
|
pause
|
@ -1,10 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"q5"
|
|
||||||
"f5"
|
|
||||||
"cs"
|
"cs"
|
||||||
|
"f5"
|
||||||
"mt"
|
"mt"
|
||||||
|
"q5"
|
||||||
)
|
)
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
|
@ -5,3 +5,5 @@ var wspListener = new (WSPListener)
|
|||||||
var playerMgr = new(PlayerMgr)
|
var playerMgr = new(PlayerMgr)
|
||||||
var handlerMgr = new(HandlerMgr)
|
var handlerMgr = new(HandlerMgr)
|
||||||
var httpListener = new(HttpListener)
|
var httpListener = new(HttpListener)
|
||||||
|
|
||||||
|
//var friendMgr = new(FriendsMgr)
|
||||||
|
120
server/imserver/friendsmgr.go
Normal file
120
server/imserver/friendsmgr.go
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cs"
|
||||||
|
"errors"
|
||||||
|
"mt"
|
||||||
|
"q5"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
ID int
|
||||||
|
Username string
|
||||||
|
}
|
||||||
|
|
||||||
|
// user1, user2 构成一个好友关系
|
||||||
|
type Friendship struct {
|
||||||
|
User1 *User
|
||||||
|
User2 *User
|
||||||
|
}
|
||||||
|
|
||||||
|
type FriendsMgr struct {
|
||||||
|
cs.MsgHandlerImpl
|
||||||
|
users map[int]*User
|
||||||
|
friendships map[int][]*Friendship
|
||||||
|
pendingReqs map[int]map[int]bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fm *FriendsMgr) init() {
|
||||||
|
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()
|
||||||
|
|
||||||
|
// make members
|
||||||
|
fm.users = make(map[int]*User)
|
||||||
|
fm.friendships = make(map[int][]*Friendship)
|
||||||
|
fm.pendingReqs = make(map[int]map[int]bool)
|
||||||
|
|
||||||
|
// 1. Load DB,
|
||||||
|
// 2. DB Data Map to Struct
|
||||||
|
// 3. RegisterUser
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fm *FriendsMgr) unInit() {
|
||||||
|
// 1. Loop struct data
|
||||||
|
// 2. Struct Data Persist to DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fm *FriendsMgr) RegisterUser(id int, username string) {
|
||||||
|
fm.users[id] = &User{ID: id, Username: username}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fm *FriendsMgr) SearchFriends(query string) []*User {
|
||||||
|
var results []*User
|
||||||
|
lowercaseQuery := strings.ToLower(query)
|
||||||
|
for _, user := range fm.users {
|
||||||
|
if strings.Contains(strings.ToLower(user.Username), lowercaseQuery) {
|
||||||
|
results = append(results, user)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddFriendRequest 发送好友请求
|
||||||
|
func (fm *FriendsMgr) AddFriendRequest(user1ID, user2ID int) error {
|
||||||
|
//// Check if users exist
|
||||||
|
//user1, exists1 := fm.users[user1ID]
|
||||||
|
//user2, exists2 := fm.users[user2ID]
|
||||||
|
//if !exists1 || !exists2 {
|
||||||
|
// return errors.New("users not exist")
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//if fm.pendingReqs[user2ID] == nil {
|
||||||
|
// fm.pendingReqs[user2ID] = make(map[int]bool)
|
||||||
|
//}
|
||||||
|
//fm.pendingReqs[user2ID][user1ID] = true
|
||||||
|
//
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AcceptFriendRequest 接受好友请求
|
||||||
|
func (fm *FriendsMgr) AcceptFriendRequest(user1ID, user2ID int) error {
|
||||||
|
if !fm.pendingReqs[user1ID][user2ID] {
|
||||||
|
return errors.New("no pending friend request from user1 to user2")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new friendship
|
||||||
|
friendship := &Friendship{
|
||||||
|
User1: fm.users[user1ID],
|
||||||
|
User2: fm.users[user2ID],
|
||||||
|
}
|
||||||
|
fm.friendships[user1ID] = append(fm.friendships[user1ID], friendship)
|
||||||
|
fm.friendships[user2ID] = append(fm.friendships[user2ID], friendship)
|
||||||
|
|
||||||
|
delete(fm.pendingReqs[user1ID], user2ID)
|
||||||
|
if len(fm.pendingReqs[user1ID]) == 0 {
|
||||||
|
delete(fm.pendingReqs, user1ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetFriends 我的好友列表
|
||||||
|
func (fm *FriendsMgr) GetFriends(userID int) []*User {
|
||||||
|
var friends []*User
|
||||||
|
for _, friendship := range fm.friendships[userID] {
|
||||||
|
if friendship.User1.ID != userID {
|
||||||
|
friends = append(friends, friendship.User1)
|
||||||
|
} else {
|
||||||
|
friends = append(friends, friendship.User2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return friends
|
||||||
|
}
|
@ -11,9 +11,7 @@ type GameDb struct {
|
|||||||
|
|
||||||
type GameDbTable struct {
|
type GameDbTable struct {
|
||||||
f5.IdMetaTable[GameDb]
|
f5.IdMetaTable[GameDb]
|
||||||
selfConf *GameDb
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *GameDb) Init1() {
|
func (this *GameDb) Init1() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ type IMClusterTable struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *IMCluster) Init1() {
|
func (this *IMCluster) Init1() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IMClusterTable) GetListenPort() int32 {
|
func (this *IMClusterTable) GetListenPort() int32 {
|
||||||
|
@ -2,10 +2,11 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"cs"
|
"cs"
|
||||||
"q5"
|
"encoding/json"
|
||||||
"f5"
|
"f5"
|
||||||
"mt"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"mt"
|
||||||
|
"q5"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PlayerMgr struct {
|
type PlayerMgr struct {
|
||||||
@ -55,9 +56,56 @@ func (this *PlayerMgr) unInit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *PlayerMgr) CMLogin(hdr *f5.MsgHdr, msg *cs.CMLogin) {
|
func (this *PlayerMgr) CMLogin(hdr *f5.MsgHdr, msg *cs.CMLogin) {
|
||||||
serverInfo := "192.168.100.39:1000"
|
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 := &cs.SMLogin{}
|
||||||
rspMsg.ServerInfo = &serverInfo
|
rspMsg.ServerInfo = &serverInfo
|
||||||
|
rspMsg.AccountId = &resObj.Info.AccountID
|
||||||
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
|
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,7 +113,7 @@ func (this *PlayerMgr) reportServerState(masterIp string, masterPort int32) {
|
|||||||
params := map[string]string{
|
params := map[string]string{
|
||||||
"node_id": q5.ToString(f5.GetApp().GetNodeId()),
|
"node_id": q5.ToString(f5.GetApp().GetNodeId()),
|
||||||
"instance_id": q5.ToString(f5.GetApp().GetInstanceId()),
|
"instance_id": q5.ToString(f5.GetApp().GetInstanceId()),
|
||||||
"ip": "",
|
"ip": "192.168.100.164",
|
||||||
"port": q5.ToString(mt.Table.IMCluster.GetListenPort()),
|
"port": q5.ToString(mt.Table.IMCluster.GetListenPort()),
|
||||||
"online_num": q5.ToString(0),
|
"online_num": q5.ToString(0),
|
||||||
"room_num": q5.ToString(0),
|
"room_num": q5.ToString(0),
|
||||||
@ -80,3 +128,11 @@ func (this *PlayerMgr) reportServerState(masterIp string, masterPort int32) {
|
|||||||
f5.GetSysLog().Info(rsp.GetRawData())
|
f5.GetSysLog().Info(rsp.GetRawData())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *PlayerMgr) addPlayer(p *Player) {
|
||||||
|
this.accountIdHash[p.accountId] = p
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *PlayerMgr) addSocketHash(wsp f5.WspCliConn, p *Player) {
|
||||||
|
this.socketHash[wsp] = p
|
||||||
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
syntax = "proto2";
|
||||||
package cs;
|
package cs;
|
||||||
|
option go_package = ".;cs";
|
||||||
|
|
||||||
//消息id定义
|
//消息id定义
|
||||||
enum CMMessageId_e
|
enum CMMessageId_e
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
syntax = "proto2";
|
||||||
package cs;
|
package cs;
|
||||||
|
option go_package = ".;cs";
|
||||||
|
|
||||||
/*
|
/*
|
||||||
约定:
|
约定:
|
||||||
@ -95,6 +97,7 @@ message CMLogin
|
|||||||
message SMLogin
|
message SMLogin
|
||||||
{
|
{
|
||||||
optional string server_info = 1; //服务器信息(重连时使用)
|
optional string server_info = 1; //服务器信息(重连时使用)
|
||||||
|
optional string account_id = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
//断线重连
|
//断线重连
|
||||||
|
@ -1,3 +1,2 @@
|
|||||||
protoc --go_out=..\cs .\cs_*.proto
|
protoc --go_out=..\cs .\cs_*.proto
|
||||||
protoc --go_out=..\ss .\ss_*.proto
|
protoc --go_out=..\ss .\ss_*.proto
|
||||||
protoc --go_out=..\mtb .\mt*.proto
|
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
|
syntax = "proto2";
|
||||||
package ss;
|
package ss;
|
||||||
|
option go_package = ".;ss";
|
||||||
|
|
||||||
|
|
||||||
//消息id定义
|
//消息id定义
|
||||||
enum SSMessageId_e
|
enum SSMessageId_e
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
syntax = "proto2";
|
||||||
package ss;
|
package ss;
|
||||||
|
option go_package = ".;ss";
|
||||||
|
|
||||||
message SS_Ping
|
message SS_Ping
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user