Skip to content

Commit 8abb764

Browse files
committed
update data
1 parent e87ce63 commit 8abb764

File tree

3 files changed

+56
-19
lines changed

3 files changed

+56
-19
lines changed

第一季 从零开始写游戏服务器 第二期/最新课节--课程代码/class3/src/classcode/NetSnakeClt.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,48 @@ func (this *NetDataConn) EntryGameSnake(ProtocolData map[string]interface{}) {
3737
panic("玩家进入游戏数据错误, 字段为空!")
3838
return
3939
}
40-
StrLogin_Name := ProtocolData["Code"].(string)
40+
//StrLogin_Name := ProtocolData["Code"].(string)
4141
Icode := ProtocolData["Icode"].(float64)
4242
// 进入游戏 进行匹配
4343
fmt.Println("玩家进行匹配!!!")
4444
// 1 匹配算法 --》匹配在线玩家的
4545
// 2 保存数据 --> chnnel 环境操作
46-
Chan_match <- Icode
46+
Chan_match <- int(Icode)
4747
// 3 返回数据
48-
48+
data1 := Match_Player()
4949
// 4 发送数据 --> 返回数据操作
5050
data := &Proto2.S2S_PlayerEntryGame{
5151
Protocol: Proto.G_Snake_Proto,
5252
Protocol2: Proto2.S2S_PlayerEntryGameProto2,
5353
RoomID: 1,
54+
Data: data1,
5455
MapPlayer: nil, // 玩家的名字
5556
}
5657
// 发送数据给客户端了
5758
this.PlayerSendMessage(data)
5859
return
5960
}
6061

62+
// 匹配玩家
63+
func Match_Player() []int {
64+
65+
for {
66+
islice := make([]int, 2, 2)
67+
if len(Chan_match) < 2 {
68+
return nil
69+
}
70+
// 枚举数据
71+
for data := range Chan_match {
72+
islice = append(islice, data)
73+
if len(islice) >= 2 {
74+
return islice
75+
}
76+
}
77+
}
78+
79+
return nil
80+
}
81+
6182
// 登录游戏协议
6283
func (this *NetDataConn) LoginGameSnake(ProtocolData map[string]interface{}) {
6384
// 数据链接信息需要保存

第一季 从零开始写游戏服务器 第二期/最新课节--课程代码/class3/src/snake/NetRec.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,20 @@ func SyncMeassgeFun(content string) {
6666
// 字符串 解析成 json
6767

6868
func HandleCltProtocol(protocol interface{}, protocol2 interface{}, ProtocolData map[string]interface{}) {
69-
defer func() { // 必须要先声明defer,否则不能捕获到panic异常
70-
if err := recover(); err != nil {
71-
strerr := fmt.Sprintf("%s", err)
72-
//发消息给客户端
73-
ErrorST := Proto2.G_Error_All{
74-
Protocol: Proto.G_Error_Proto, // 主协议
75-
Protocol2: Proto2.G_Error_All_Proto, // 子协议
76-
ErrCode: "80006",
77-
ErrMsg: "亲,您发的数据的格式不对!" + strerr,
78-
}
79-
// 发送给玩家数据
80-
fmt.Println("贪吃蛇的主协议!!!", ErrorST)
81-
}
82-
}()
69+
// defer func() { // 必须要先声明defer,否则不能捕获到panic异常
70+
// if err := recover(); err != nil {
71+
// strerr := fmt.Sprintf("%s", err)
72+
// //发消息给客户端
73+
// ErrorST := Proto2.G_Error_All{
74+
// Protocol: Proto.G_Error_Proto, // 主协议
75+
// Protocol2: Proto2.G_Error_All_Proto, // 子协议
76+
// ErrCode: "80006",
77+
// ErrMsg: "亲,您发的数据的格式不对!" + strerr,
78+
// }
79+
// // 发送给玩家数据
80+
// fmt.Println("贪吃蛇的主协议!!!", ErrorST)
81+
// }
82+
// }()
8383

8484
// 协议处理
8585
switch protocol {
@@ -109,7 +109,7 @@ func HandleCltProtocol2Snake(protocol2 interface{}, ProtocolData map[string]inte
109109
{
110110
fmt.Println("贪吃蛇:玩家匹配成功协议!!!")
111111
// 玩家进入游戏的协议
112-
// EntryGameSnake(ProtocolData)
112+
EntryGameSnakeRec(ProtocolData)
113113
}
114114

115115
default:
@@ -124,3 +124,17 @@ func EntryGameSnake(ProtocolData map[string]interface{}) {
124124
fmt.Println("贪吃蛇:玩家进入游戏的协议!!!", StrToken)
125125
return
126126
}
127+
128+
func EntryGameSnakeRec(ProtocolData map[string]interface{}) {
129+
130+
if ProtocolData["Data"] == nil {
131+
fmt.Println("贪吃蛇:玩家进入游戏,无玩家匹配。")
132+
return
133+
}
134+
135+
fmt.Println("贪吃蛇:玩家进入游戏,玩家匹配。")
136+
return
137+
StrToken := ProtocolData["Data"].([]int)
138+
fmt.Println("贪吃蛇:玩家进入游戏的协议!!!", StrToken)
139+
return
140+
}

第一季 从零开始写游戏服务器 第二期/最新课节--课程代码/vender/src/Proto/Proto2/Proto2Snake.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ type C2S_PlayerEntryGame struct {
4545
Protocol int
4646
Protocol2 int
4747
Code string //临时码
48+
Icode int
4849
}
4950

5051
// 返回数据操作
5152
type S2S_PlayerEntryGame struct {
5253
Protocol int
5354
Protocol2 int
54-
RoomID int //房间ID
55+
RoomID int //房间ID
56+
Data []int
5557
MapPlayer map[int]*player.PlayerSt // 玩家的结构信息
5658
}
5759

0 commit comments

Comments
 (0)