Player.go 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  1. package Module
  2. import (
  3. "Server-Core/Server/GameServer/Server/PlaneGame/Utils"
  4. message "Server-Core/Server/Message"
  5. "encoding/json"
  6. "strconv"
  7. "sync"
  8. "time"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. "github.com/golang/protobuf/proto"
  12. "github.com/gorilla/websocket"
  13. log "github.com/sirupsen/logrus"
  14. rtctokenbuilder "github.com/AgoraIO/Tools/DynamicKey/AgoraDynamicKey/go/src/rtctokenbuilder2"
  15. )
  16. type GamePlayer struct {
  17. Ticker *time.Ticker
  18. Id string
  19. Name string
  20. HeadImg string
  21. Uid string
  22. GamePlane []*GamePlane
  23. GamePlaneIndex []int
  24. GameRoom *GameRoom
  25. GameRoomId int //上一把的房间id
  26. Status uint32 // 玩家的当前状态, 0、大厅,1 表示匹配,2: 进入房间已落座, 3: ready, 4: play, 5: 胜利后观战, 6: 胜利后离开,7: 组队逃跑,8: 逃跑,9托管状态,10观战,11进房未落座,12已建房,未拉取房间信息,13结算待退出,14已开游戏未进房
  27. Num int //代表当前第几轮
  28. SeatId uint32
  29. NumberOfRounds []*GameNumberOfRounds
  30. Money uint32 //金额
  31. Kick uint32 //撞棋数
  32. BeKick uint32 //被撞棋数
  33. FinishNum uint32 //到达终点数
  34. LastHeartbeat time.Time //最后一次心跳时间
  35. PlayerNum uint32 //玩家人数
  36. //Status2 uint32 // 玩家的当前状态, 1代表没有开启任何房间,2在匹配中,3是在房间内
  37. MaxNum int //最大房间人数
  38. Mode int //游戏模式
  39. AdmissionFee uint32 //入场费
  40. RoomType int
  41. }
  42. // 根据uid查询websocket连接信息
  43. var UserClientIds map[string]*websocket.Conn = make(map[string]*websocket.Conn)
  44. // 根据websocket连接信息查询uid
  45. var ClientUserIds map[*websocket.Conn]string = make(map[*websocket.Conn]string)
  46. var GamePlayers = make(map[string]*GamePlayer)
  47. // 飞机
  48. type GamePlane struct {
  49. Id string
  50. CurPosIndex uint32
  51. IsFly int //0代表未起飞,1已经起飞,2到达终点
  52. Uid string
  53. }
  54. type GameNumberOfRounds struct {
  55. Num int
  56. NumberOfRounds []*uint32
  57. }
  58. type RequestUserData struct {
  59. Data []GamePlayer `json:"data"`
  60. }
  61. // 根据seatId获取房间内对应玩家
  62. func getPlayerBySeatId(seatId uint32, room *GameRoom) *GamePlayer {
  63. // uid := clientUserIds[ws]
  64. for _, value := range room.Players {
  65. log.Info("每一个人的seatId", value.SeatId)
  66. if seatId == value.SeatId {
  67. return value
  68. }
  69. }
  70. return nil
  71. //return room.Players[int(seatId)]
  72. }
  73. // 根据uid获取房间内对应玩家
  74. func getPlayer(uid string) *GamePlayer {
  75. return GamePlayers[uid]
  76. }
  77. func getNextSeatId(seatId uint32, room *GameRoom) uint32 {
  78. // if seatId < uint32(room.StartPlayerNum-1) {
  79. // seatId = seatId + 1
  80. // } else {
  81. // seatId = 0
  82. // }
  83. // for _, value := range room.Players {
  84. // if value.SeatId == seatId {
  85. // if value.Status == uint32(message.GameStatus_Player_Status_5) || value.Status == uint32(message.GameStatus_Player_Status_6) || value.Status == uint32(message.GameStatus_Player_Status_8) || value.Status == 0 {
  86. // seatId = getNextSeatId(seatId, room)
  87. // break
  88. // } else {
  89. // {
  90. // return seatId
  91. // }
  92. // }
  93. // }
  94. // }
  95. if seatId == 0 {
  96. for _, v := range room.Players {
  97. if v.Status == 3 || v.Status == 9 {
  98. if v.SeatId == 1 {
  99. return 1
  100. }
  101. }
  102. }
  103. for _, v := range room.Players {
  104. if v.Status == 3 || v.Status == 9 {
  105. if v.SeatId == 2 {
  106. return 2
  107. }
  108. }
  109. }
  110. for _, v := range room.Players {
  111. if v.Status == 3 || v.Status == 9 {
  112. if v.SeatId == 3 {
  113. return 3
  114. }
  115. }
  116. }
  117. } else if seatId == 1 {
  118. for _, v := range room.Players {
  119. if v.Status == 3 || v.Status == 9 {
  120. if v.SeatId == 2 {
  121. return 2
  122. }
  123. }
  124. }
  125. for _, v := range room.Players {
  126. if v.Status == 3 || v.Status == 9 {
  127. if v.SeatId == 3 {
  128. return 3
  129. }
  130. }
  131. }
  132. for _, v := range room.Players {
  133. if v.Status == 3 || v.Status == 9 {
  134. if v.SeatId == 0 {
  135. return 0
  136. }
  137. }
  138. }
  139. } else if seatId == 2 {
  140. for _, v := range room.Players {
  141. if v.Status == 3 || v.Status == 9 {
  142. if v.SeatId == 3 {
  143. return 3
  144. }
  145. }
  146. }
  147. for _, v := range room.Players {
  148. if v.Status == 3 || v.Status == 9 {
  149. if v.SeatId == 0 {
  150. return 0
  151. }
  152. }
  153. }
  154. for _, v := range room.Players {
  155. if v.Status == 3 || v.Status == 9 {
  156. if v.SeatId == 1 {
  157. return 1
  158. }
  159. }
  160. }
  161. } else if seatId == 3 {
  162. for _, v := range room.Players {
  163. if v.Status == 3 || v.Status == 9 {
  164. if v.SeatId == 0 {
  165. return 0
  166. }
  167. }
  168. }
  169. for _, v := range room.Players {
  170. if v.Status == 3 || v.Status == 9 {
  171. if v.SeatId == 1 {
  172. return 1
  173. }
  174. }
  175. }
  176. for _, v := range room.Players {
  177. if v.Status == 3 || v.Status == 9 {
  178. if v.SeatId == 2 {
  179. return 2
  180. }
  181. }
  182. }
  183. }
  184. return seatId
  185. }
  186. // 返回玩家数据,等待玩家准备完毕
  187. func GetUserInfo(ws *websocket.Conn, data []byte) {
  188. var uid = ClientUserIds[ws]
  189. req := &message.GetUserInfoReq{}
  190. proto.Unmarshal(data, req)
  191. rsp := &message.GetUserInfoRsp{}
  192. errcode := uint32(message.GameStatus_GAME_STATUS_OK)
  193. rsp.Result = &errcode
  194. log.Info("获取用户数据", uid)
  195. //测试期间:没有账号则创建账号和密码,有账号则验证密码
  196. //先验证账号不能为空
  197. lock4.Lock()
  198. //gameplayer := getPlayer(req.Uid)
  199. // gameplayer := GamePlayers[req.Uid]
  200. if len(uid) == 0 {
  201. room := gameRooms[int(req.RoomId)]
  202. //加入观战玩家
  203. UserClientIds[uid] = ws
  204. ClientUserIds[ws] = uid
  205. var gameplayer = &GamePlayer{}
  206. gameplayer.Uid = uid
  207. //room := gameRooms[int(gameplayer.GameRoom.Id)]
  208. if room.GameEnd == 1 {
  209. //userRoom[req.Uid] = room
  210. //查看房间人数
  211. var names = []string{}
  212. var headImg = []string{}
  213. var uids = []string{}
  214. var seatId = []uint32{}
  215. var playerStatus = []uint32{}
  216. players := room.Players
  217. for i := 0; i < len(players); i++ {
  218. if players[i] != nil {
  219. if players[i].Status == 3 || players[i].Status == 9 {
  220. names = append(names, players[i].Name)
  221. headImg = append(headImg, players[i].HeadImg)
  222. uids = append(uids, players[i].Uid)
  223. seatId = append(seatId, players[i].SeatId)
  224. playerStatus = append(playerStatus, players[i].Status)
  225. }
  226. }
  227. }
  228. rsp.Nick = names
  229. rsp.HeadUrl = headImg
  230. rsp.PlayerID = uids
  231. rsp.SeatID = seatId
  232. rsp.PlayerStatus = playerStatus
  233. rsp.PlayerNum = uint32(len(players))
  234. room.GzPlayers = append(room.GzPlayers, gameplayer)
  235. var i = uint32(len(room.GzPlayers))
  236. rsp.WatchingPlayers = &i
  237. rsp.GameType = uint32(room.Mode)
  238. rData, _ := proto.Marshal(rsp) //回包
  239. log.Info("返回用户信息", rsp)
  240. sendData(ws, int16(message.MSGID_Get_User_Info_Rsp), rData)
  241. gameplayer.Status = uint32(message.GameStatus_Player_Status_10)
  242. var playnum uint32 = uint32(len(players))
  243. rsp2 := &message.JoinRoomInfoRsp{Nick: gameplayer.Name, HeadUrl: gameplayer.HeadImg, PlayerID: gameplayer.Uid, SeatID: &gameplayer.SeatId, PlayerStatus: gameplayer.Status, PlayerNum: &playnum}
  244. log.Info("通知到玩家加入房间", rsp2)
  245. rData2, _ := proto.Marshal(rsp2) //回包
  246. for _, value := range players {
  247. if value.Uid != gameplayer.Uid {
  248. if value.Status == uint32(message.GameStatus_Player_Status_3)||value.Status == uint32(message.GameStatus_Player_Status_9) {
  249. sendData(UserClientIds[value.Uid], int16(message.MSGID_Join_Room_Info_Rsp), rData2)
  250. }
  251. }
  252. }
  253. for j := 0; j < len(room.GzPlayers); j++ {
  254. if room.GzPlayers[j].Uid != gameplayer.Uid {
  255. //
  256. //if room.GzPlayers[j].Status == uint32(message.GameStatus_Player_Status_3) || room.GzPlayers[j].Status == uint32(message.GameStatus_Player_Status_10) {
  257. sendData(UserClientIds[room.GzPlayers[j].Uid], int16(message.MSGID_Join_Room_Info_Rsp), rData2)
  258. // }
  259. }
  260. }
  261. } else {
  262. //返回倒计时
  263. room.GzPlayers = append(room.GzPlayers, gameplayer)
  264. //查看房间人数
  265. var names = []string{}
  266. var headImg = []string{}
  267. var uids = []string{}
  268. var seatId = []uint32{}
  269. var playerStatus = []uint32{}
  270. players := room.Players
  271. for i := 0; i < len(players); i++ {
  272. if players[i] != nil {
  273. if players[i].Status == 3 {
  274. names = append(names, players[i].Name)
  275. headImg = append(headImg, players[i].HeadImg)
  276. uids = append(uids, players[i].Uid)
  277. seatId = append(seatId, players[i].SeatId)
  278. playerStatus = append(playerStatus, players[i].Status)
  279. }
  280. }
  281. }
  282. rsp.Nick = names
  283. rsp.HeadUrl = headImg
  284. rsp.PlayerID = uids
  285. rsp.SeatID = seatId
  286. rsp.PlayerStatus = playerStatus
  287. rsp.PlayerNum = uint32(len(players))
  288. var time uint32 = 10
  289. rsp.Countdown = &time
  290. rsp.GameType = uint32(room.Mode)
  291. var i = uint32(len(room.GzPlayers))
  292. rsp.WatchingPlayers = &i
  293. rData, _ := proto.Marshal(rsp) //回包
  294. log.Info("返回用户信息", rsp)
  295. sendData(ws, int16(message.MSGID_Get_User_Info_Rsp), rData)
  296. }
  297. } else {
  298. gameplayer := GamePlayers[uid]
  299. //查看房间人数
  300. //room := userRoom[gameplayer.Uid]
  301. room := gameplayer.GameRoom
  302. //room := gameRooms[int(req.RoomId)]
  303. var names = []string{}
  304. var headImg = []string{}
  305. var uids = []string{}
  306. var seatId = []uint32{}
  307. var playerStatus = []uint32{}
  308. players := room.Players
  309. for _, value := range room.Players {
  310. log.Info("uid", uid)
  311. log.Info("players[i].Uid", value.Uid)
  312. if value.Uid == uid {
  313. //如果是没有准备第一次进入房间,则会更新玩家座位号,如果是掉线重连
  314. //if value.Status == uint32(message.GameStatus_Player_Status_2) || value.Status == uint32(message.GameStatus_Player_Status_1) {
  315. if value.Status ==14 {
  316. gameplayer.Status = uint32(message.GameStatus_Player_Status_3)
  317. log.Print("获取room.PlayerNum", room.PlayerNum)
  318. //seatId 0 1 2
  319. gameplayer.SeatId = uint32(room.PlayerNum)
  320. room.PlayerNum = room.PlayerNum + 1
  321. //lock.Unlock()
  322. if len(players) == 2 {
  323. if gameplayer.SeatId == 0 {
  324. gameplayer.GamePlaneIndex = Maps[0]
  325. } else if gameplayer.SeatId == 1 {
  326. gameplayer.GamePlaneIndex = Maps[2]
  327. }
  328. } else if len(players) == 3 {
  329. if gameplayer.SeatId == 0 {
  330. gameplayer.GamePlaneIndex = Maps[0]
  331. } else if gameplayer.SeatId == 1 {
  332. gameplayer.GamePlaneIndex = Maps[1]
  333. } else if gameplayer.SeatId == 2 {
  334. gameplayer.GamePlaneIndex = Maps[2]
  335. }
  336. } else if len(players) == 4 {
  337. if gameplayer.SeatId == 0 {
  338. gameplayer.GamePlaneIndex = Maps[0]
  339. } else if gameplayer.SeatId == 1 {
  340. gameplayer.GamePlaneIndex = Maps[1]
  341. } else if gameplayer.SeatId == 2 {
  342. gameplayer.GamePlaneIndex = Maps[2]
  343. } else if gameplayer.SeatId == 3 {
  344. gameplayer.GamePlaneIndex = Maps[3]
  345. }
  346. }
  347. }
  348. }
  349. if value.Status == uint32(message.GameStatus_Player_Status_3)||value.Status == uint32(message.GameStatus_Player_Status_9) {
  350. names = append(names, value.Name)
  351. headImg = append(headImg, value.HeadImg)
  352. uids = append(uids, value.Uid)
  353. seatId = append(seatId, value.SeatId)
  354. playerStatus = append(playerStatus, value.Status)
  355. }
  356. }
  357. rsp.Nick = names
  358. rsp.HeadUrl = headImg
  359. rsp.PlayerID = uids
  360. rsp.SeatID = seatId
  361. if gameplayer.Status == 3 {
  362. rsp.SelfSeatId = &gameplayer.SeatId
  363. }
  364. //添加观战玩家
  365. if gameplayer.Status == 10 {
  366. room.GzPlayers = append(room.GzPlayers, gameplayer)
  367. }
  368. rsp.PlayerStatus = playerStatus
  369. rsp.PlayerNum = uint32(len(players))
  370. var i = uint32(len(room.GzPlayers))
  371. rsp.WatchingPlayers = &i
  372. rsp.GameType = uint32(room.Mode)
  373. rData, _ := proto.Marshal(rsp) //回包
  374. log.Info("返回用户信息", rsp)
  375. sendData(ws, int16(message.MSGID_Get_User_Info_Rsp), rData)
  376. //广播除本人外的所有在线用户加入房间
  377. var playnum uint32 = uint32(len(players))
  378. rsp2 := &message.JoinRoomInfoRsp{Nick: gameplayer.Name, HeadUrl: gameplayer.HeadImg, PlayerID: gameplayer.Uid, SeatID: &gameplayer.SeatId, PlayerStatus: gameplayer.Status, PlayerNum: &playnum}
  379. log.Info("通知到玩家加入房间", rsp2)
  380. rData2, _ := proto.Marshal(rsp2) //回包
  381. for _, value := range players {
  382. if value.Uid != gameplayer.Uid {
  383. if value.Status == uint32(message.GameStatus_Player_Status_3)||value.Status == uint32(message.GameStatus_Player_Status_9) {
  384. sendData(UserClientIds[value.Uid], int16(message.MSGID_Join_Room_Info_Rsp), rData2)
  385. }
  386. }
  387. }
  388. for j := 0; j < len(room.GzPlayers); j++ {
  389. if room.GzPlayers[j].Uid != gameplayer.Uid {
  390. //if room.GzPlayers[j].Status == uint32(message.GameStatus_Player_Status_3) || room.GzPlayers[j].Status == uint32(message.GameStatus_Player_Status_10) {
  391. sendData(UserClientIds[room.GzPlayers[j].Uid], int16(message.MSGID_Join_Room_Info_Rsp), rData2)
  392. // }
  393. }
  394. }
  395. log.Print("加入房间的人数", len(room.Players))
  396. log.Info("加入房间的观战人数", len(room.GzPlayers))
  397. //log.Info("房间内的人数", len(room.Players))
  398. if room.PlayerNum == room.StartPlayerNum {
  399. //没有开始游戏才会开始游戏
  400. if room.GameEnd == GameNotStarted {
  401. for _, value := range room.Players {
  402. //初始化飞机
  403. var plane = []*GamePlane{}
  404. for j := 0; j < 4; j++ {
  405. gamePlane := createPlane(j, value.Uid)
  406. plane = append(plane, &gamePlane)
  407. }
  408. value.GamePlane = plane
  409. }
  410. log.Info("广播开始游戏")
  411. room.GameEnd = GamePlaying
  412. //玩家都准备完毕了就可以游戏
  413. if room.ticker != nil {
  414. room.ticker.Stop()
  415. }
  416. nextplayer := getPlayerBySeatId(0, room)
  417. broadInfoSz(nextplayer)
  418. }
  419. }
  420. }
  421. lock4.Unlock()
  422. }
  423. // var lock sync.Mutex
  424. var lock4 sync.Mutex
  425. // 返回玩家数据,等待玩家准备完毕
  426. func createPlayer(i int, plane []*GamePlane, data []GamePlayer, room *GameRoom) GamePlayer {
  427. var gamePlayer = GamePlayer{}
  428. gamePlayer.GamePlane = plane
  429. gamePlayer.GameRoom = room
  430. //gamePlayer.Id = strconv.Itoa(i)
  431. gamePlayer.Uid = data[i].Uid
  432. gamePlayer.Name = data[i].Name
  433. gamePlayer.HeadImg = data[i].HeadImg
  434. gamePlayer.Status = uint32(0)
  435. //gamePlayer.SeatId = uint32(i)
  436. gamePlayer.Money = data[i].Money
  437. gamePlayer.FinishNum = 0
  438. gamePlayer.Num = 0
  439. var rounds = []*GameNumberOfRounds{}
  440. var num *GameNumberOfRounds = new(GameNumberOfRounds)
  441. var nums = []*uint32{}
  442. (*num).Num = 0
  443. (*num).NumberOfRounds = nums
  444. rounds = append(rounds, num)
  445. gamePlayer.NumberOfRounds = rounds
  446. return gamePlayer
  447. }
  448. func createPlayer2(uid string, ws *websocket.Conn,url string,name string, money uint32) *GamePlayer {
  449. var gamePlayer = GamePlayer{}
  450. //gamePlayer.GameRoom = room
  451. //gamePlayer.Id = strconv.Itoa(i)
  452. gamePlayer.Uid = uid
  453. uid2, _ := strconv.Atoi(uid)
  454. //userrsp := Utils.GetUserInfoData(uid2)
  455. //现在是facebook的登录,如果需要语聊房就切换GetUserInfoData
  456. userrsp := Utils.GetFbData(uid2,url,name,money)
  457. log.Printf("查看返回值", userrsp)
  458. if userrsp.Code != 200 {
  459. BroadErrorMsg("没有该玩家信息", ws)
  460. return nil
  461. } else {
  462. gamePlayer.Name = userrsp.Data[0].NickName
  463. gamePlayer.HeadImg = userrsp.Data[0].Avatar
  464. gamePlayer.Money = uint32(userrsp.Data[0].GoldCoins)
  465. }
  466. //log.Printf("查询到的玩家信息", userrsp.Data[0])
  467. gamePlayer.Status = uint32(0)
  468. //gamePlayer.SeatId = uint32(i)
  469. //gamePlayer.Status2 = uint32(PlayerIdle)
  470. gamePlayer.FinishNum = 0
  471. gamePlayer.Num = 0
  472. var rounds = []*GameNumberOfRounds{}
  473. var num *GameNumberOfRounds = new(GameNumberOfRounds)
  474. var nums = []*uint32{}
  475. (*num).Num = 0
  476. (*num).NumberOfRounds = nums
  477. rounds = append(rounds, num)
  478. gamePlayer.NumberOfRounds = rounds
  479. GamePlayers[gamePlayer.Uid] = &gamePlayer
  480. return &gamePlayer
  481. }
  482. func resetPlayer(gamePlayer *GamePlayer) int {
  483. //gamePlayer.GameRoom = room
  484. //gamePlayer.Id = strconv.Itoa(i)
  485. uid2, _ := strconv.Atoi(gamePlayer.Uid)
  486. userrsp := Utils.GetUserInfoData(uid2)
  487. if userrsp.Code != 200 {
  488. log.Printf("没有玩家信息")
  489. //return nil
  490. //gamePlayers[gamePlayer.Uid]=nil
  491. delete(GamePlayers, gamePlayer.Uid)
  492. return 666
  493. }
  494. log.Printf("查询到的玩家信息", userrsp.Data[0])
  495. gamePlayer.Name = userrsp.Data[0].NickName
  496. gamePlayer.HeadImg = userrsp.Data[0].Avatar
  497. gamePlayer.Money = uint32(userrsp.Data[0].GoldCoins)
  498. gamePlayer.Status = uint32(message.GameStatus_Player_Status_1)
  499. //gamePlayer.SeatId = uint32(i)
  500. //gamePlayer.Status2 = uint32(PlayerIdle)
  501. gamePlayer.FinishNum = 0
  502. gamePlayer.Num = 0
  503. var rounds = []*GameNumberOfRounds{}
  504. var num *GameNumberOfRounds = new(GameNumberOfRounds)
  505. var nums = []*uint32{}
  506. (*num).Num = 0
  507. (*num).NumberOfRounds = nums
  508. rounds = append(rounds, num)
  509. gamePlayer.NumberOfRounds = rounds
  510. return 200
  511. //gamePlayers[gamePlayer.Uid] = &gamePlayer
  512. //return &gamePlayer
  513. }
  514. // 用户登录 建立socket连接
  515. func Login(ws *websocket.Conn, data []byte) {
  516. /*req := &message.UserInfoReq{}
  517. proto.Unmarshal(data, req) //解包
  518. userClientIds[req.Uid] = ws
  519. clientUserIds[ws] = req.Uid
  520. var gamePlayer = GamePlayer{Uid: req.Uid, Name: req.Name, HeadImg: req.HeadImg, Money: req.Money}
  521. gamePlayers[req.Uid] = &gamePlayer*/
  522. //需要携带房间号,如果有的话说明在一个房间,
  523. //玩家
  524. }
  525. // 确认一下对接方式
  526. // 语聊房客户端直接传输roomId给游戏服务器,然后就
  527. // 根据窗口id 找到对应的窗口
  528. // 玩家根据游戏窗口加入窗口进行广播
  529. // 携带窗口id和uid
  530. func JoinRoomReq(ws *websocket.Conn, data []byte) {
  531. //语聊房携带id加入游戏房间
  532. //uid := clientUserIds[ws]
  533. req := &message.JoinRoomReq{}
  534. proto.Unmarshal(data, req) //解包
  535. log.Println("获取请求的参数", req)
  536. JoinRoom(req.Uid, int(*req.RoomId), ws)
  537. }
  538. func JoinRoom(uid string, roomId int, ws *websocket.Conn) {
  539. var player = GamePlayers[uid]
  540. //log.Info("uid", req.Uid)
  541. //gamePlayer :
  542. room := gameRooms[roomId]
  543. if room == nil {
  544. BroadErrorMsg("房间不存在,无法加入", ws)
  545. return
  546. } else {
  547. if room.Status == RoomIdle {
  548. BroadErrorMsg("房间还没开,不能使用", ws)
  549. return
  550. }
  551. }
  552. if player == nil {
  553. player = createPlayer2(uid, ws,"","",0)
  554. if nil == player {
  555. BroadErrorMsg("没有该玩家信息", ws)
  556. return
  557. }
  558. } else {
  559. if player.Status != 10 {
  560. BroadErrorMsg("玩家未拉取房间信息", ws)
  561. return
  562. // for _, player2 := range room.Players {
  563. // if player2.Uid == player.Uid {
  564. // BroadErrorMsg("已加入游戏不能创建游戏房间", ws)
  565. // return
  566. // }
  567. // }
  568. }
  569. }
  570. lock6.Lock()
  571. room.RoomPlayers[player.Uid] = player
  572. // ClientUserIds[ws] = req.Uid
  573. // UserClientIds[req.Uid] = ws
  574. //gamePlayer.Uid = req.Uid
  575. rsp4 := &message.JoinRoomInfoRsp{}
  576. if len(room.Players) < room.MaxNum {
  577. var canjoin = true
  578. for _, value := range room.Players {
  579. log.Printf("查看房间内剩余玩家", value.Uid)
  580. if value.Uid == uid {
  581. //已加入不能重复加入
  582. canjoin = false
  583. }
  584. }
  585. if canjoin {
  586. log.Print("加入玩家", player.Uid)
  587. if player.Money<room.AdmissionFee{
  588. BroadErrorMsg("金币不足",ws)
  589. return
  590. }
  591. //delete(room.GzPlayers,player)
  592. player.GameRoom = room
  593. player.AdmissionFee = room.AdmissionFee
  594. player.MaxNum = room.MaxNum
  595. player.Mode = room.Mode
  596. player.Status = uint32(message.GameStatus_Player_Status_2)
  597. player.FinishNum = 0
  598. player.Num = 0
  599. var rounds = []*GameNumberOfRounds{}
  600. var num *GameNumberOfRounds = new(GameNumberOfRounds)
  601. var nums = []*uint32{}
  602. (*num).Num = 0
  603. (*num).NumberOfRounds = nums
  604. rounds = append(rounds, num)
  605. player.NumberOfRounds = rounds
  606. // player.GameRoom = room
  607. Rcfcz(player.Uid, room.AdmissionFee, 2)
  608. //broadRoomInfo(room, ws)
  609. room.RoomPlayers[player.Uid] = player
  610. if room.Players[0] == nil {
  611. room.Players[0] = player
  612. var seat uint32 = 0
  613. rsp4.SeatID = &seat
  614. } else if room.Players[1] == nil {
  615. room.Players[1] = player
  616. var seat uint32 = 1
  617. rsp4.SeatID = &seat
  618. } else if room.Players[2] == nil {
  619. room.Players[2] = player
  620. var seat uint32 = 2
  621. rsp4.SeatID = &seat
  622. } else if room.Players[3] == nil {
  623. room.Players[3] = player
  624. var seat uint32 = 3
  625. rsp4.SeatID = &seat
  626. }
  627. } else {
  628. BroadErrorMsg("该玩家已加入", ws)
  629. lock6.Unlock()
  630. return
  631. }
  632. }
  633. // else {
  634. // //broadRoomInfo(room, ws)
  635. // log.Info("加入观战玩家")
  636. // room.RoomPlayers[player.Uid] = player
  637. // player.GameRoom = room
  638. // player.Status = uint32(message.GameStatus_Player_Status_10)
  639. // room.GzPlayers = append(room.GzPlayers, player)
  640. // //BroadErrorMsg("人数已满,不能加入", ws)
  641. // lock6.Unlock()
  642. // return
  643. // }
  644. //broadInfo(player.GameRoom, int16(message.MSGID_Room_User_Info_Rsp), rData)
  645. //广播新玩家加入
  646. ClientUserIds[ws] = uid
  647. UserClientIds[uid] = ws
  648. rsp4.PlayerID = player.Uid
  649. rsp4.Nick = player.Name
  650. rsp4.HeadUrl = player.HeadImg
  651. rData4, _ := proto.Marshal(rsp4) //回包
  652. log.Info("广播玩家加入", rsp4)
  653. BroadInfoRoom(player.GameRoom, int16(message.MSGID_Room_Join_User_Rsp), rData4)
  654. //如果房间是匹配模式满人则直接开始游戏
  655. if player.GameRoom.Mode == ModeMatch {
  656. if len(room.Players) == room.MaxNum {
  657. uid := strconv.Itoa(room.Homeowner)
  658. for i := 0; i < len(MatchGameplayers); i++ {
  659. if uid == MatchGameplayers[i].Uid {
  660. MatchGameplayers[i].Status = uint32(message.GameStatus_Player_Status_2)
  661. MatchGameplayers = append(MatchGameplayers[:i], MatchGameplayers[i+1:]...)
  662. rsp3 := &message.RoomStartGameRsp{}
  663. rData3, _ := proto.Marshal(rsp3) //回包
  664. log.Info("广播开始玩游戏")
  665. BroadInfoRoom(room, int16(message.MSGID_Room_Start_Game_Rsp), rData3)
  666. //Utils.ChatRoomBroadcast(room.Id, 2, 1, room.Id)
  667. go countdonw2(player, 1)
  668. break
  669. }
  670. }
  671. }
  672. }
  673. lock6.Unlock()
  674. }
  675. // 游戏restart 按钮
  676. func RoomRestartReq(ws *websocket.Conn, data []byte) {
  677. //req := &message.RoomInfoReq{}
  678. player := GamePlayers[ClientUserIds[ws]]
  679. // for key, player2 := range player.GameRoom.Players {
  680. // if player2.Uid == player.Uid {
  681. // delete(player.GameRoom.Players, key)
  682. // }
  683. // }
  684. //delete(player.GameRoom.RoomPlayers, player.Uid)
  685. //player.GameRoom = nil
  686. //log.Info("room====", room.Mode)
  687. if nil != player {
  688. if player.Status !=0 {
  689. BroadErrorMsg("该玩家已经在匹配或者已开房", ws)
  690. return
  691. }
  692. } else {
  693. BroadErrorMsg("没有该玩家信息", ws)
  694. return
  695. }
  696. if player.Money < player.AdmissionFee {
  697. BroadErrorMsg("该玩家入场费不足", ws)
  698. return
  699. }
  700. //Rcfcz(player.Uid, uint32(player.AdmissionFee), 2)
  701. lock3.Lock()
  702. player.LastHeartbeat=time.Now()
  703. //如果是房间内房主开启就拉人
  704. if player.Mode == ModeClass {
  705. //player.Status2 = uint32(PlayerRooming)
  706. // player.Status = uint32(message.GameStatus_Player_Status_2)
  707. room := gameRooms[player.GameRoomId]
  708. // if room.RoomType == RoomIn {
  709. // uid, _ := strconv.Atoi(player.Uid)
  710. // if room.Homeowner == uid {
  711. // //如果是房主点的restart 会重新拉人 ,重新加入游戏房间
  712. // result := Utils.ChatRoomBroadcast(room.ChatRoomId, BroadcastInvisit, room.Id)
  713. // if result.Code != 200 {
  714. // log.Printf("发送邀请失败,通知客户端再发起邀请")
  715. // }
  716. // }
  717. // }
  718. log.Printf("进入了经典模式")
  719. //通知到返回房间信息
  720. GamePlayers[ClientUserIds[ws]].Status = 10
  721. broadRoomInfo(room, ws)
  722. var uid = strconv.Itoa(room.Homeowner)
  723. if uid != player.Uid {
  724. JoinRoom(player.Uid, player.GameRoomId, ws)
  725. } else {
  726. Rcfcz(player.Uid, uint32(player.AdmissionFee), 2)
  727. GamePlayers[ClientUserIds[ws]].Status = 2
  728. GamePlayers[ClientUserIds[ws]].GameRoom = room
  729. GamePlayers[ClientUserIds[ws]].AdmissionFee = room.AdmissionFee
  730. GamePlayers[ClientUserIds[ws]].MaxNum = room.MaxNum
  731. GamePlayers[ClientUserIds[ws]].Mode = room.Mode
  732. //GamePlayers[ClientUserIds[ws]].Status = uint32(message.GameStatus_Player_Status_2)
  733. GamePlayers[ClientUserIds[ws]].FinishNum = 0
  734. GamePlayers[ClientUserIds[ws]].Num = 0
  735. var rounds = []*GameNumberOfRounds{}
  736. var num *GameNumberOfRounds = new(GameNumberOfRounds)
  737. var nums = []*uint32{}
  738. (*num).Num = 0
  739. (*num).NumberOfRounds = nums
  740. rounds = append(rounds, num)
  741. GamePlayers[ClientUserIds[ws]].NumberOfRounds = rounds
  742. }
  743. //room := player.GameRoom
  744. } else if player.Mode == ModeMatch {
  745. log.Printf("进入了匹配模式")
  746. match(player, ws, int(player.AdmissionFee), player.MaxNum)
  747. }
  748. lock3.Unlock()
  749. }
  750. func match(player *GamePlayer, ws *websocket.Conn, adminfee int, playernum int) {
  751. // 重新匹配
  752. log.Info("进入匹配模式")
  753. // 判断是否没有玩家已经开房,判断开了房是否能开始游戏
  754. lock2.Lock()
  755. var roomCreated = false
  756. //Rcfcz(player.Uid, uint32(adminfee), 2)
  757. for i := 0; i < len(MatchGameplayers); i++ {
  758. if MatchGameplayers[i].AdmissionFee == uint32(adminfee) && playernum == int(MatchGameplayers[i].MaxNum) {
  759. //如果房间满了就开始游戏
  760. //player.Status = uint32(message.GameStatus_Player_Status_2)
  761. player.Status = uint32(message.GameStatus_Player_Status_1)
  762. MatchGameplayers = append(MatchGameplayers, player)
  763. //如果匹配金额相同 并且玩家人数相同就进入游戏对战
  764. roomCreated = true
  765. //移出次玩家
  766. room := MatchGameplayers[i].GameRoom
  767. player.GameRoom = room
  768. room.RoomPlayers[player.Uid] = player
  769. // userRoom[req.Uid] = room
  770. //匹配成功会加入房间
  771. if room.Players[0] == nil {
  772. player.SeatId = 0
  773. room.Players[0] = player
  774. } else if room.Players[1] == nil {
  775. player.SeatId = 1
  776. room.Players[1] = player
  777. } else if room.Players[2] == nil {
  778. player.SeatId = 2
  779. room.Players[2] = player
  780. } else if room.Players[3] == nil {
  781. player.SeatId = 3
  782. room.Players[3] = player
  783. }
  784. broadRoomInfo(room, ws)
  785. //广播新玩家加入
  786. rsp4 := &message.RoomJoinUserRsp{}
  787. rsp4.PlayerID = player.Uid
  788. rsp4.Nick = player.Name
  789. rsp4.HeadUrl = player.HeadImg
  790. rsp4.SeatID = player.SeatId
  791. log.Print("回传数据====", rsp4)
  792. rData4, _ := proto.Marshal(rsp4) //回包
  793. broadInfoExceptUid(room, int16(message.MSGID_Room_Join_User_Rsp), rData4, player.Uid)
  794. //BroadInfoRoom(room, int16(message.MSGID_Room_Join_User_Rsp), rData4)
  795. log.Info("room.MaxNum", room.MaxNum)
  796. log.Info("len====", len(room.Players))
  797. if len(room.Players) == room.MaxNum {
  798. log.Info("开始清除匹配信息")
  799. player.GameRoom.ticker.Stop()
  800. for _, player := range room.Players {
  801. for j := 0; j < len(MatchGameplayers); j++ {
  802. if player.Uid == MatchGameplayers[j].Uid {
  803. //MatchGameplayers[i].Status = uint32(message.GameStatus_Player_Status_2)
  804. MatchGameplayers = append(MatchGameplayers[:j], MatchGameplayers[j+1:]...)
  805. }
  806. }
  807. }
  808. time.Sleep(2 * time.Second)
  809. for _, v := range player.GameRoom.Players {
  810. v.Status=14
  811. // for i := 0; i < len(player.GameRoom.GzPlayers); i++ {
  812. // if player.GameRoom.GzPlayers[i].Uid == v.Uid {
  813. // player.GameRoom.GzPlayers = append(player.GameRoom.GzPlayers[:i], player.GameRoom.GzPlayers[i+1:]...)
  814. // //MatchGameplayers = append(MatchGameplayers[:j], MatchGameplayers[j+1:]...)
  815. // }
  816. // }
  817. }
  818. rsp3 := &message.RoomStartGameRsp{}
  819. rData3, _ := proto.Marshal(rsp3) //回包
  820. log.Info("广播开始玩游戏")
  821. room.StartPlayerNum = len(room.Players)
  822. go countdonw2(player, 2)
  823. BroadInfo(room, int16(message.MSGID_Room_Start_Game_Rsp), rData3)
  824. result := Utils.ChatRoomBroadcast(player.GameRoom.ChatRoomId, BroadcastInvisit, player.GameRoom.Id)
  825. if result.Code != 200 {
  826. log.Printf("通知语聊房开始游戏失败")
  827. }
  828. }
  829. break
  830. }
  831. }
  832. log.Info("roomCreated====", roomCreated)
  833. if !roomCreated {
  834. //如果没有匹配到房间就自己开房
  835. // result := rcfcz(player.Uid, uint32(adminfee), Reduce)
  836. // if result != 200 {
  837. // BroadErrorMsg("入场费不足,不能进入房间", ws)
  838. // lock2.Unlock()
  839. // return
  840. // }
  841. //userRoom[req.Uid] = idleRoom
  842. player.Status = uint32(message.GameStatus_Player_Status_1)
  843. MatchGameplayers = append(MatchGameplayers, player)
  844. //player.GameRoom.Players = append(player.GameRoom.Players, player)
  845. room := GetIdleRoom(uint32(adminfee), playernum, ModeMatch)
  846. player.SeatId = 0
  847. room.RoomType = RoomOut
  848. player.GameRoom = room
  849. // uuid, _ := strconv.Atoi(player.Uid)
  850. //room.RoomPlayers=append(room.RoomPlayers,player)
  851. room.RoomPlayers[player.Uid] = player
  852. //room.Homeowner = uuid
  853. //idleRoom.Players = append(idleRoom.Players, player)
  854. if room.Players[0] == nil {
  855. room.Players[0] = player
  856. } else if room.Players[1] == nil {
  857. room.Players[1] = player
  858. } else if room.Players[2] == nil {
  859. room.Players[2] = player
  860. } else if room.Players[3] == nil {
  861. room.Players[3] = player
  862. }
  863. // rsp3 := &message.MatchRsp{}
  864. // var success uint32 = 0
  865. // rsp3.Result = &success
  866. // rData3, _ := proto.Marshal(rsp3) //回包
  867. // log.Info("广播开始匹配")
  868. // //BroadInfo(room, int16(message.MSGID_Match_Rsp), rData3)
  869. // sendData(ws, int16(message.MSGID_Match_Rsp), rData3)
  870. // broadRoomInfo(room, ws)
  871. // rsp3 := &message.CreateGameRsp{}
  872. // var rid uint32 = uint32(room.Id)
  873. // rsp3.RoomId = &rid
  874. // rData3, _ := proto.Marshal(rsp3) //回包
  875. // sendData(ws, int16(message.MSGID_Create_Game_Rsp), rData3)
  876. broadRoomInfo(room, ws)
  877. go countdonw3(player.Uid, uint32(room.Id))
  878. }
  879. // 15046 149946
  880. lock2.Unlock()
  881. }
  882. // 查看房间信息
  883. func RoomInfoReq(ws *websocket.Conn, data []byte) {
  884. req := &message.RoomInfoReq{}
  885. proto.Unmarshal(data, req) //解包
  886. log.Printf("roomId====", req)
  887. var room = gameRooms[int(*req.RoomId)]
  888. if room == nil {
  889. BroadErrorMsg("没有该房间信息", ws)
  890. return
  891. }
  892. if room.Status == 1 {
  893. BroadErrorMsg("该房间还不能使用", ws)
  894. return
  895. }
  896. // //回传玩家信息
  897. var player = GamePlayers[req.Uid]
  898. if player == nil {
  899. log.Printf("建立了新玩家")
  900. player = createPlayer2(req.Uid, ws,"","",0)
  901. if nil == player {
  902. //BroadErrorMsg("没有该玩家信息", ws)
  903. return
  904. }
  905. }
  906. log.Print("status====", player.Status)
  907. if player.Status != 0 && player.Status != 12 {
  908. log.Print("status====", player.Status)
  909. BroadErrorMsg("该玩家已经加入房间", ws)
  910. return
  911. }
  912. player.GameRoom = room
  913. player.Status = 10
  914. player.LastHeartbeat=time.Now()
  915. log.Print("status2====", player.Status)
  916. UserClientIds[player.Uid] = ws
  917. ClientUserIds[ws] = player.Uid
  918. room.RoomPlayers[player.Uid] = player
  919. broadRoomInfo(room, ws)
  920. }
  921. func broadRoomInfo(room *GameRoom, ws *websocket.Conn) {
  922. //回传玩家信息
  923. var uids = []string{}
  924. var names = []string{}
  925. var headImg = []string{}
  926. var moneys = []uint32{}
  927. var seatId = []uint32{}
  928. for i := 0; i < 4; i++ {
  929. if room.Players[i] != nil {
  930. if room.Players[i].Ticker != nil {
  931. //room.Players[i].Ticker.Stop()
  932. }
  933. uids = append(uids, room.Players[i].Uid)
  934. names = append(names, room.Players[i].Name)
  935. headImg = append(headImg, room.Players[i].HeadImg)
  936. moneys = append(moneys, room.Players[i].Money)
  937. seatId = append(seatId, uint32(i))
  938. }
  939. }
  940. // for _, value := range room.Players {
  941. // }
  942. rsp := &message.RoomInfoRsp{}
  943. rsp.Money = moneys
  944. rsp.PlayerID = uids
  945. rsp.Nick = names
  946. rsp.HeadUrl = headImg
  947. rsp.SeatId = seatId
  948. rsp.AdmissionFee = room.AdmissionFee
  949. rsp.PlayerNum = uint32(room.MaxNum)
  950. rsp.Mode = uint32(room.Mode)
  951. rData, _ := proto.Marshal(rsp) //回包
  952. log.Info("回传房间信息", rsp)
  953. //MSGID_Room_Info_Rsp
  954. sendData(ws, int16(message.MSGID_Room_Info_Rsp), rData)
  955. }
  956. // 开始游戏通知客户端
  957. func RoomStartGameReq(ws *websocket.Conn, data []byte) {
  958. uid := ClientUserIds[ws]
  959. player := GamePlayers[uid]
  960. if player.GameRoom==nil{
  961. BroadErrorMsg("游戏房间没有", ws)
  962. return
  963. }
  964. if len(player.GameRoom.Players) < player.GameRoom.MinNum {
  965. BroadErrorMsg("一个人不能游戏", ws)
  966. return
  967. } else if len(player.GameRoom.Players) > player.GameRoom.MaxNum {
  968. BroadErrorMsg("不能超过房间人数", ws)
  969. return
  970. } else {
  971. player.LastHeartbeat=time.Now()
  972. req := &message.RoomStartGameRsp{}
  973. rData, _ := proto.Marshal(req) //回包
  974. log.Info("广播开始玩游戏", player.GameRoom.Players)
  975. player.GameRoom.StartPlayerNum = len(player.GameRoom.Players)
  976. for _, v := range player.GameRoom.Players {
  977. v.Status=14
  978. // for i := 0; i < len(player.GameRoom.GzPlayers); i++ {
  979. // if player.GameRoom.GzPlayers[i].Uid == v.Uid {
  980. // player.GameRoom.GzPlayers = append(player.GameRoom.GzPlayers[:i], player.GameRoom.GzPlayers[i+1:]...)
  981. // //MatchGameplayers = append(MatchGameplayers[:j], MatchGameplayers[j+1:]...)
  982. // }
  983. // }
  984. }
  985. go countdonw2(player, 3)
  986. BroadInfoRoom(player.GameRoom, int16(message.MSGID_Room_Start_Game_Rsp), rData)
  987. // result := Utils.ChatRoomBroadcast(player.GameRoom.ChatRoomId, BroadcastInvisit, player.GameRoom.Id)
  988. // if result.Code != 200 {
  989. // log.Printf("通知语聊房开始游戏失败")
  990. // }
  991. }
  992. //通知到开始游戏 广播游戏客户端
  993. }
  994. func BroadErrorMsg(msg string, ws *websocket.Conn) {
  995. rsp := &message.PbErrorRsp{}
  996. rsp.MessageData = msg
  997. rData, _ := proto.Marshal(rsp) //回包
  998. sendData(ws, int16(message.MSGID_Pb_Error_Rsp), rData)
  999. }
  1000. func DissolveTheGame(ws *websocket.Conn, data []byte) {
  1001. //房主解散游戏房间
  1002. // req := &message.DissolveTheGameReq{}
  1003. uid := ClientUserIds[ws]
  1004. player := GamePlayers[uid]
  1005. if player == nil {
  1006. BroadErrorMsg("玩家不存在", ws)
  1007. return
  1008. }
  1009. if player.GameRoom == nil {
  1010. BroadErrorMsg("该房间不存在", ws)
  1011. return
  1012. }
  1013. log.Printf("解散房间", player.GameRoom.RoomType)
  1014. if player.GameRoom.RoomType == 1 {
  1015. Dissolve_The_Game(player)
  1016. }
  1017. }
  1018. // 游戏匹配
  1019. // 玩家登陆,上传头像金币信息
  1020. var MatchGameplayers = []*GamePlayer{}
  1021. var lock2 sync.Mutex
  1022. var lock3 sync.Mutex
  1023. var lock5 sync.Mutex
  1024. var lock6 sync.Mutex
  1025. // 玩家第一次选择模式
  1026. func CreateGameReq(ws *websocket.Conn, data []byte) {
  1027. req := &message.CreateGameReq{}
  1028. proto.Unmarshal(data, req) //解包
  1029. log.Info("data", req)
  1030. //player := gamePlayers[clientUserIds[ws]]
  1031. UserClientIds[req.Uid] = ws
  1032. ClientUserIds[ws] = req.Uid
  1033. var player *GamePlayer = GamePlayers[req.Uid]
  1034. if nil != player {
  1035. if player.Status != 0 {
  1036. BroadErrorMsg("该玩家已经在匹配或者已开房", ws)
  1037. return
  1038. }
  1039. } else {
  1040. player = createPlayer2(req.Uid, ws,req.HeadImg,req.Name,*req.Money)
  1041. if nil == player {
  1042. BroadErrorMsg("没有该玩家信息", ws)
  1043. return
  1044. }
  1045. }
  1046. player.LastHeartbeat=time.Now()
  1047. player.AdmissionFee = uint32(req.AdmissionFee)
  1048. // if player.Money < player.AdmissionFee {
  1049. // BroadErrorMsg("该玩家入场费不足", ws)
  1050. // return
  1051. // }
  1052. // Rcfcz(player.Uid, uint32(player.AdmissionFee), 2)
  1053. player.MaxNum = int(req.PlayerNum)
  1054. player.Mode = int(req.Mode)
  1055. if req.Mode == uint32(ModeMatch) {
  1056. //player.Status2 = uint32(PlayerMatching)
  1057. match(player, ws, int(req.AdmissionFee), int(req.PlayerNum))
  1058. } else if req.Mode == uint32(ModeClass) {
  1059. //选择经典开房模式
  1060. //lock5.Lock()
  1061. room := GetIdleRoom(req.AdmissionFee, int(req.PlayerNum), int(req.Mode))
  1062. //player.GameRoom = room
  1063. player.Status = 12
  1064. uid, _ := strconv.Atoi(player.Uid)
  1065. room.Homeowner = uid
  1066. room.RoomPlayers[player.Uid] = player
  1067. //player.GameRoom = room
  1068. rsp3 := &message.CreateGameRsp{}
  1069. var rid uint32 = uint32(room.Id)
  1070. rsp3.RoomId = &rid
  1071. rData3, _ := proto.Marshal(rsp3) //回包
  1072. //broadInfo(player.GameRoom, int16(message.MSGID_Create_Game_Rsp), rData3)
  1073. sendData(ws, int16(message.MSGID_Create_Game_Rsp), rData3)
  1074. room.RoomType = RoomOut
  1075. //log.Printf("chatRoomId=====", *req.ChatRoomId)
  1076. if req.ChatRoomId != nil {
  1077. //
  1078. //room.Type = 1
  1079. room.RoomType = RoomIn
  1080. room.ChatRoomId = int(*req.ChatRoomId)
  1081. //房间内开房的话,则直接拉人
  1082. result := Utils.ChatRoomBroadcast(int(*req.ChatRoomId), BroadcastInvisit, room.Id)
  1083. if result.Code != 200 {
  1084. log.Printf("发送邀请失败,通知客户端再发起邀请")
  1085. }
  1086. } else {
  1087. room.RoomType = RoomOut
  1088. //room.Players[0]=player
  1089. }
  1090. //lock5.Unlock()
  1091. }
  1092. }
  1093. func Rcfcz(uid string, adminfee uint32, Type int) int {
  1094. // 扣除玩家入场费
  1095. var userInfos = []UserInfo{}
  1096. uid2, _ := strconv.Atoi(uid)
  1097. var userInfo = UserInfo{
  1098. UserId: uid2,
  1099. GoldCoins: int(adminfee),
  1100. Type: Type,
  1101. }
  1102. userInfos = append(userInfos, userInfo)
  1103. var userInfoReq = UserInfoReq{GameType: 1, UserWallets: userInfos}
  1104. jsonData2, _ := json.Marshal(userInfoReq)
  1105. result := Utils.PostData(jsonData2)
  1106. return result
  1107. }
  1108. //player
  1109. //players map
  1110. // 房间踢人
  1111. func RoomKickUserReq(ws *websocket.Conn, data []byte) {
  1112. req := &message.RoomKickUserReq{}
  1113. proto.Unmarshal(data, req) //解包
  1114. //uid := clientUserIds[ws]
  1115. player := GamePlayers[req.Uid]
  1116. uid1 := ClientUserIds[ws]
  1117. uid, _ := strconv.Atoi(uid1)
  1118. if uid != player.GameRoom.Homeowner {
  1119. log.Printf("房主id", player.GameRoom.Homeowner)
  1120. log.Printf("uid", uid)
  1121. BroadErrorMsg("不是房主不能踢人", ws)
  1122. return
  1123. }
  1124. for key, value := range player.GameRoom.Players {
  1125. if value.GameRoom.GameEnd == GameNotStarted {
  1126. if req.Uid == value.Uid {
  1127. if player.Status != uint32(message.GameStatus_Player_Status_10) {
  1128. Rcfcz(player.Uid, player.GameRoom.AdmissionFee, Plus)
  1129. }
  1130. rsp := &message.RoomKickUserRsp{}
  1131. rsp.Uid = req.Uid
  1132. rData3, _ := proto.Marshal(rsp) //回包
  1133. BroadInfoRoom(player.GameRoom, int16(message.MSGID_Room_Kick_User_Rsp), rData3)
  1134. //player.GameRoom.Players = append(player.GameRoom.Players[:i], player.GameRoom.Players[i+1:]...)
  1135. delete(player.GameRoom.Players, key)
  1136. log.Println("开始踢人,查看剩余房间人数", req.Uid, player.GameRoom.Players)
  1137. delete(GamePlayers, player.Uid)
  1138. delete(ClientUserIds, UserClientIds[player.Uid])
  1139. delete(UserClientIds, player.Uid)
  1140. break
  1141. }
  1142. } else {
  1143. BroadErrorMsg("游戏已经开始不能踢人", ws)
  1144. break
  1145. }
  1146. }
  1147. }
  1148. // 退出房间或者房主解散房间 或者取消匹配
  1149. func RoomQuitReq(ws *websocket.Conn, data []byte) {
  1150. req := &message.RoomQuitReq{}
  1151. proto.Unmarshal(data, req) //解包
  1152. uid := ClientUserIds[ws]
  1153. player := GamePlayers[uid]
  1154. if player == nil {
  1155. BroadErrorMsg("改玩家已退出", ws)
  1156. return
  1157. }
  1158. QuitRoom(player, ws, 11)
  1159. }
  1160. // 请求关闭客户端
  1161. func RoomCloseReq(ws *websocket.Conn, data []byte) {
  1162. req := &message.RoomQuitReq{}
  1163. proto.Unmarshal(data, req) //解包
  1164. uid := ClientUserIds[ws]
  1165. player := GamePlayers[uid]
  1166. if player == nil {
  1167. BroadErrorMsg("改玩家已退出", ws)
  1168. return
  1169. }
  1170. QuitRoom(player, ws, 0)
  1171. }
  1172. func QuitRoom(player *GamePlayer, ws *websocket.Conn, status uint32) {
  1173. //log.Info("玩家请求退出房间", player.GameRoom)
  1174. uuid, _ := strconv.Atoi(player.Uid)
  1175. if player.Status == 2 {
  1176. player.Status = 10
  1177. if player.GameRoom.Homeowner == uuid {
  1178. //广播退出游戏
  1179. var key2 int
  1180. for key, value := range player.GameRoom.Players {
  1181. if player.Uid == value.Uid {
  1182. // player.GameRoom.Players = append(player.GameRoom.Players[:i], player.GameRoom.Players[i+1:]...)
  1183. key2 = key
  1184. delete(player.GameRoom.Players, key)
  1185. break
  1186. }
  1187. }
  1188. //player.GameRoom.PlayerNum = player.GameRoom.PlayerNum - 1
  1189. Rcfcz(player.Uid, player.GameRoom.AdmissionFee, Plus)
  1190. rsp := &message.RoomQuitUserRsp{}
  1191. rsp.Uid = player.Uid
  1192. rData3, _ := proto.Marshal(rsp) //回包
  1193. BroadInfoRoom(player.GameRoom, int16(message.MSGID_Room_Quit_Rsp), rData3)
  1194. //player.Status = status
  1195. //如果是房间开房玩家就移交下一个玩家,如果房间内没有玩家就解散房间
  1196. if len(player.GameRoom.Players) == 0 {
  1197. //解散房间
  1198. Dissolve_The_Game(player)
  1199. } else {
  1200. //if player.GameRoom.Mode == 1 {
  1201. if key2 == 0 {
  1202. if player.GameRoom.Players[1] != nil {
  1203. uid, _ := strconv.Atoi(player.GameRoom.Players[1].Uid)
  1204. player.GameRoom.Homeowner = uid
  1205. } else if player.GameRoom.Players[2] != nil {
  1206. uid, _ := strconv.Atoi(player.GameRoom.Players[2].Uid)
  1207. player.GameRoom.Homeowner = uid
  1208. } else if player.GameRoom.Players[3] != nil {
  1209. uid, _ := strconv.Atoi(player.GameRoom.Players[3].Uid)
  1210. player.GameRoom.Homeowner = uid
  1211. }
  1212. } else if key2 == 1 {
  1213. if player.GameRoom.Players[2] != nil {
  1214. uid, _ := strconv.Atoi(player.GameRoom.Players[2].Uid)
  1215. player.GameRoom.Homeowner = uid
  1216. } else if player.GameRoom.Players[3] != nil {
  1217. uid, _ := strconv.Atoi(player.GameRoom.Players[3].Uid)
  1218. player.GameRoom.Homeowner = uid
  1219. } else if player.GameRoom.Players[0] != nil {
  1220. uid, _ := strconv.Atoi(player.GameRoom.Players[0].Uid)
  1221. player.GameRoom.Homeowner = uid
  1222. }
  1223. } else if key2 == 2 {
  1224. if player.GameRoom.Players[3] != nil {
  1225. uid, _ := strconv.Atoi(player.GameRoom.Players[3].Uid)
  1226. player.GameRoom.Homeowner = uid
  1227. } else if player.GameRoom.Players[0] != nil {
  1228. uid, _ := strconv.Atoi(player.GameRoom.Players[0].Uid)
  1229. player.GameRoom.Homeowner = uid
  1230. } else if player.GameRoom.Players[1] != nil {
  1231. uid, _ := strconv.Atoi(player.GameRoom.Players[1].Uid)
  1232. player.GameRoom.Homeowner = uid
  1233. }
  1234. } else if key2 == 3 {
  1235. if player.GameRoom.Players[0] != nil {
  1236. uid, _ := strconv.Atoi(player.GameRoom.Players[0].Uid)
  1237. player.GameRoom.Homeowner = uid
  1238. } else if player.GameRoom.Players[1] != nil {
  1239. uid, _ := strconv.Atoi(player.GameRoom.Players[1].Uid)
  1240. player.GameRoom.Homeowner = uid
  1241. } else if player.GameRoom.Players[2] != nil {
  1242. uid, _ := strconv.Atoi(player.GameRoom.Players[2].Uid)
  1243. player.GameRoom.Homeowner = uid
  1244. }
  1245. }
  1246. //player.Status = status
  1247. uuod := strconv.Itoa(player.GameRoom.Homeowner)
  1248. log.Print("玩家是否移交房主", player.GameRoom.Mode)
  1249. if player.GameRoom.Mode == 1 {
  1250. rsp2 := &message.RoomChangeOfHomeownerRsp{}
  1251. rsp2.Uid = uuod
  1252. rData4, _ := proto.Marshal(rsp2) //回包
  1253. log.Print("广播玩家移交房主", player.GameRoom.Mode)
  1254. BroadInfoRoom(player.GameRoom, int16(message.MSGID_Room_Change_Of_Homeowner_Rsp), rData4)
  1255. } else if player.GameRoom.Mode == 2 {
  1256. for key, value := range player.GameRoom.Players {
  1257. if uuod == value.Uid {
  1258. // player.GameRoom.Players = append(player.GameRoom.Players[:i], player.GameRoom.Players[i+1:]...)
  1259. player.GameRoom.Players[key].Status = uint32(message.GameStatus_Player_Status_1)
  1260. MatchGameplayers = append(MatchGameplayers, value)
  1261. break
  1262. }
  1263. }
  1264. }
  1265. //player.GameRoom.RoomPlayers
  1266. //delete(GamePlayers, player.Uid)
  1267. //delete(ClientUserIds, ws)
  1268. //delete(UserClientIds, player.Uid)
  1269. // }
  1270. }
  1271. } else {
  1272. //不是房主的情况下
  1273. for key, value := range player.GameRoom.Players {
  1274. if player.Uid == value.Uid {
  1275. // player.GameRoom.Players = append(player.GameRoom.Players[:i], player.GameRoom.Players[i+1:]...)
  1276. delete(player.GameRoom.Players, key)
  1277. break
  1278. }
  1279. }
  1280. //player.Status = status
  1281. //player.GameRoom.PlayerNum = player.GameRoom.PlayerNum - 1
  1282. Rcfcz(player.Uid, player.GameRoom.AdmissionFee, Plus)
  1283. // delete(GamePlayers, player.Uid)
  1284. // delete(ClientUserIds, ws)
  1285. // delete(UserClientIds, player.Uid)
  1286. rsp := &message.RoomQuitUserRsp{}
  1287. rsp.Uid = player.Uid
  1288. rData3, _ := proto.Marshal(rsp) //回包
  1289. BroadInfoRoom(player.GameRoom, int16(message.MSGID_Room_Quit_Rsp), rData3)
  1290. }
  1291. //delete(GamePlayers, player.Uid)
  1292. //delete(ClientUserIds, ws)
  1293. //log.Printf("有玩家退出了清除了对应的ws", ClientUserIds[ws])
  1294. //delete(UserClientIds, player.Uid)
  1295. } else if player.Status == 10 {
  1296. for i := 0; i < len(player.GameRoom.GzPlayers); i++ {
  1297. if player.GameRoom.GzPlayers[i].Uid == player.Uid {
  1298. player.GameRoom.GzPlayers = append(player.GameRoom.GzPlayers[:i], player.GameRoom.GzPlayers[i+1:]...)
  1299. i-- // 删除元素后,需要减少索引
  1300. break
  1301. }
  1302. }
  1303. delete(GamePlayers, player.Uid)
  1304. delete(ClientUserIds, ws)
  1305. delete(UserClientIds, player.Uid)
  1306. rsp3 := &message.QuitRsp{}
  1307. rsp3.SeatID = &player.SeatId
  1308. rsp3.PlayerStatus = player.Status
  1309. rsp3.Uid=player.Uid
  1310. rData4, _ := proto.Marshal(rsp3) //回包
  1311. BroadInfo(player.GameRoom, int16(message.MSGID_Quit_Rsq), rData4)
  1312. rsp := &message.RoomQuitUserRsp{}
  1313. rsp.Uid = player.Uid
  1314. rData3, _ := proto.Marshal(rsp) //回包
  1315. BroadInfoRoom(player.GameRoom, int16(message.MSGID_Room_Quit_Rsp), rData3)
  1316. } else if player.Status == 1 {
  1317. CancenMatch(player.Uid)
  1318. } else if player.Status == 11 || player.Status == 12 {
  1319. player.Status = 0
  1320. delete(GamePlayers, player.Uid)
  1321. delete(ClientUserIds, ws)
  1322. delete(UserClientIds, player.Uid)
  1323. } else if player.Status == 14 {
  1324. Dissolve_The_Game(player)
  1325. } else {
  1326. //在游戏里退出直接退出游戏
  1327. Quit(player.Uid)
  1328. //delete(ClientUserIds, ws)
  1329. //delete(UserClientIds, player.Uid)
  1330. }
  1331. }
  1332. func Dissolve_The_Game(player *GamePlayer) {
  1333. //广播解散游戏
  1334. rsp := &message.DissolveTheGameRsp{}
  1335. //rsp.Uid = req.Uid
  1336. rData3, _ := proto.Marshal(rsp) //回包
  1337. if player==nil{
  1338. return
  1339. }
  1340. if player.GameRoom==nil{
  1341. return
  1342. }
  1343. BroadInfoRoom(player.GameRoom, int16(message.MSGID_Dissolve_The_Game_Rsp), rData3)
  1344. log.Printf("通知解散房间")
  1345. UpdateRoom(player.GameRoom)
  1346. var userInfos = []UserInfo{}
  1347. for _, value := range player.GameRoom.Players {
  1348. uid2, _ := strconv.Atoi(value.Uid)
  1349. var userInfo = UserInfo{
  1350. UserId: uid2,
  1351. GoldCoins: int(player.GameRoom.AdmissionFee),
  1352. Type: Plus,
  1353. }
  1354. //delete(userRoom, room.Players[j].Uid)
  1355. userInfos = append(userInfos, userInfo)
  1356. delete(GamePlayers, value.Uid)
  1357. delete(ClientUserIds, UserClientIds[value.Uid])
  1358. delete(UserClientIds, value.Uid)
  1359. }
  1360. for j := 0; j < len(player.GameRoom.GzPlayers); j++ {
  1361. delete(GamePlayers, player.GameRoom.GzPlayers[j].Uid)
  1362. delete(ClientUserIds, UserClientIds[player.GameRoom.GzPlayers[j].Uid])
  1363. delete(UserClientIds, player.GameRoom.GzPlayers[j].Uid)
  1364. }
  1365. var userInfoReq = UserInfoReq{GameType: 1, UserWallets: userInfos}
  1366. jsonData2, _ := json.Marshal(userInfoReq)
  1367. if len(userInfos)>0{
  1368. Utils.PostData(jsonData2)
  1369. }
  1370. // uid := player.Uid
  1371. if player != nil {
  1372. delete(GamePlayers, player.Uid)
  1373. delete(ClientUserIds, UserClientIds[player.Uid])
  1374. delete(UserClientIds, player.Uid)
  1375. }
  1376. }
  1377. // 取消匹配
  1378. func CancelMatchReq(ws *websocket.Conn, data []byte) {
  1379. req := &message.CancelMatchReq{}
  1380. proto.Unmarshal(data, req) //解包
  1381. uid := ClientUserIds[ws]
  1382. CancenMatch(uid)
  1383. }
  1384. func CancenMatch(uid string) {
  1385. var player = GamePlayers[uid]
  1386. if player != nil {
  1387. if player.GameRoom.GameEnd != GameNotStarted {
  1388. //
  1389. BroadErrorMsg("游戏已开始,不能取消匹配", UserClientIds[uid])
  1390. return
  1391. }
  1392. // 先取消匹配,
  1393. player.Status = 0
  1394. //匹配状态下取消匹配并且更新房间状态
  1395. //var key2 int = 0
  1396. // player.GameRoom.PlayerNum = player.GameRoom.PlayerNum - 1
  1397. Rcfcz(player.Uid, player.GameRoom.AdmissionFee, Plus)
  1398. for key, player2 := range player.GameRoom.Players {
  1399. if player2.Uid == player.Uid {
  1400. for j := 0; j < len(MatchGameplayers); j++ {
  1401. if player2.Uid == MatchGameplayers[j].Uid {
  1402. //MatchGameplayers[i].Status = uint32(message.GameStatus_Player_Status_2)
  1403. MatchGameplayers = append(MatchGameplayers[:j], MatchGameplayers[j+1:]...)
  1404. }
  1405. }
  1406. delete(player.GameRoom.Players, key)
  1407. break
  1408. }
  1409. }
  1410. rsp := &message.RoomQuitUserRsp{}
  1411. rsp.Uid = player.Uid
  1412. rData3, _ := proto.Marshal(rsp) //回包
  1413. BroadInfoRoom(player.GameRoom, int16(message.MSGID_Room_Quit_Rsp), rData3)
  1414. //让房主开启匹配
  1415. delete(GamePlayers, player.Uid)
  1416. delete(ClientUserIds, UserClientIds[uid])
  1417. delete(UserClientIds, player.Uid)
  1418. if len(player.GameRoom.Players) == 0 {
  1419. //解散房间
  1420. log.Info("开始解散房间")
  1421. player.GameRoom.ticker.Stop()
  1422. for _, player2 := range player.GameRoom.Players {
  1423. for j := 0; j < len(MatchGameplayers); j++ {
  1424. if player2.Uid == MatchGameplayers[j].Uid {
  1425. //MatchGameplayers[i].Status = uint32(message.GameStatus_Player_Status_2)
  1426. MatchGameplayers = append(MatchGameplayers[:j], MatchGameplayers[j+1:]...)
  1427. }
  1428. }
  1429. }
  1430. rsp := &message.RoomQuitUserRsp{}
  1431. rsp.Uid = player.Uid
  1432. rData3, _ := proto.Marshal(rsp) //回包
  1433. BroadInfoRoom(player.GameRoom, int16(message.MSGID_Room_Quit_Rsp), rData3)
  1434. Dissolve_The_Game(player)
  1435. }
  1436. }
  1437. }
  1438. // 客户端邀请房间内玩家
  1439. func RoomInviteReq(ws *websocket.Conn, data []byte) {
  1440. req := &message.RoomInviteReq{}
  1441. proto.Unmarshal(data, req) //解包
  1442. result2 := Utils.ChatRoomBroadcast(int(*req.ChatRoomId), BroadcastInvisit, int(*req.RoomId))
  1443. if result2.Code != 200 {
  1444. log.Printf("发送邀请失败,通知客户端再发起邀请")
  1445. }
  1446. rsp := &message.RoomInviteRsp{}
  1447. var result uint32 = 0
  1448. rsp.Result = &result
  1449. rData3, _ := proto.Marshal(rsp) //回包
  1450. sendData(ws, int16(message.MSGID_Room_Invite_Rsp), rData3)
  1451. }
  1452. func DiscReq(ws *websocket.Conn, data []byte) {
  1453. req := &message.DiscReq{}
  1454. proto.Unmarshal(data, req) //解包
  1455. room := gameRooms[int(req.RoomId)]
  1456. if room.Status == RoomBusy {
  1457. // if room.GameEnd == 0 {
  1458. // } else if room.GameEnd == 1 {
  1459. // }
  1460. // rsp := &message.DiscR{}
  1461. // var game = uint32(room.GameEnd)
  1462. // rsp.RoomStatus = &game
  1463. // rData3, _ := proto.Marshal(rsp) //回包
  1464. // sendData(ws, int16(message.MSGID_Disc_Rsp), rData3)
  1465. }
  1466. }
  1467. func SendDataReq(ws *websocket.Conn, data []byte) {
  1468. req := &message.SendMsgReq{}
  1469. proto.Unmarshal(data, req) //解包
  1470. uid := ClientUserIds[ws]
  1471. var player = GamePlayers[uid]
  1472. rsp := &message.SendMsgRsp{}
  1473. rsp.Uid=uid
  1474. rsp.Data = req.Data
  1475. rData3, _ := proto.Marshal(rsp) //解包
  1476. // room := gameRooms[int(req.RoomId)]
  1477. //BroadInfo()
  1478. BroadInfo(player.GameRoom, int16(message.MSGID_Send_Msg_Rsp), rData3)
  1479. //sendData(ws, int16(message.MSGID_Send_Msg_Rsp), rData3)
  1480. }
  1481. func Gettoken(c *gin.Context) SqlResponse {
  1482. appID := "53c2ddc63bbc41228d916e1bea9a566a"
  1483. appCertificate := "ccefdf8d574d42a6a042a9415fceb18c"
  1484. channelName := "1"
  1485. uid := uint32(1)
  1486. //uidStr := "2882341273"
  1487. tokenExpirationInSeconds := uint32(3600)
  1488. privilegeExpirationInSeconds := uint32(3600)
  1489. // joinChannelPrivilegeExpireInSeconds := uint32(3600)
  1490. // pubAudioPrivilegeExpireInSeconds := uint32(3600)
  1491. // pubVideoPrivilegeExpireInSeconds := uint32(3600)
  1492. // pubDataStreamPrivilegeExpireInSeconds := uint32(3600)
  1493. result, err := rtctokenbuilder.BuildTokenWithUid(appID, appCertificate, channelName, uid, rtctokenbuilder.RoleSubscriber, tokenExpirationInSeconds, privilegeExpirationInSeconds)
  1494. if err != nil {
  1495. fmt.Println(err)
  1496. } else {
  1497. fmt.Printf("Token with int uid: %s\n", result)
  1498. }
  1499. log.Printf("result==", result)
  1500. var sql = SqlResponse{
  1501. Code: 200,
  1502. Message: "成功",
  1503. Data: result,
  1504. }
  1505. return sql
  1506. //c.JSON(http.StatusOK, sql)
  1507. // result, err = rtctokenbuilder.BuildTokenWithUserAccount(appID, appCertificate, channelName, uidStr, rtctokenbuilder.RoleSubscriber, tokenExpirationInSeconds, privilegeExpirationInSeconds)
  1508. // if err != nil {
  1509. // fmt.Println(err)
  1510. // } else {
  1511. // fmt.Printf("Token with user account: %s\n", result)
  1512. // }
  1513. // result, err1 := rtctokenbuilder.BuildTokenWithUidAndPrivilege(appID, appCertificate, channelName, uid,
  1514. // tokenExpirationInSeconds, joinChannelPrivilegeExpireInSeconds, pubAudioPrivilegeExpireInSeconds, pubVideoPrivilegeExpireInSeconds, pubDataStreamPrivilegeExpireInSeconds)
  1515. // if err1 != nil {
  1516. // fmt.Println(err1)
  1517. // } else {
  1518. // fmt.Printf("Token with int uid and privilege: %s\n", result)
  1519. // }
  1520. // result, err1 = rtctokenbuilder.BuildTokenWithUserAccountAndPrivilege(appID, appCertificate, channelName, uidStr,
  1521. // tokenExpirationInSeconds, joinChannelPrivilegeExpireInSeconds, pubAudioPrivilegeExpireInSeconds, pubVideoPrivilegeExpireInSeconds, pubDataStreamPrivilegeExpireInSeconds)
  1522. // if err1 != nil {
  1523. // fmt.Println(err1)
  1524. // } else {
  1525. // fmt.Printf("Token with user account and privilege: %s\n", result)
  1526. // }
  1527. }