match.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package ludo
  2. import (
  3. "server/msg"
  4. "github.com/name5566/leaf/gate"
  5. "github.com/name5566/leaf/log"
  6. )
  7. func RecvMatchLudo(args []interface{}) {
  8. log.Error("RecvMatchLudo!")
  9. m := args[0].(*msg.MatchLudo)
  10. a := args[1].(gate.Agent)
  11. ud := a.UserData()
  12. user_id := ud.(*msg.UserInfo).UserId
  13. log.Error("user_id:=%s , start match!", user_id)
  14. go matchRoom(m, user_id, a)
  15. }
  16. //根据玩家要匹配的对局,给玩家寻找和的房间
  17. func matchRoom(matchInfo *msg.MatchLudo, userId string, user_agent gate.Agent) {
  18. roomList := GetRoomsByLevel(matchInfo, msg.RoomStatus_AWAIT)
  19. if len(roomList) == 0 {
  20. // Create new room if none available
  21. room_info := CreateRoom(matchInfo, userId)
  22. log.Error("没找到房间匹配一个!")
  23. if room_info == nil {
  24. sendMatchErrorMsg(user_agent, 101, "not find player")
  25. }
  26. } else {
  27. log.Error("匹配到%d个房间", len(roomList))
  28. // 如果房间具备开始的条件
  29. isFullPlayer, err_info := JoinRoom(roomList[0], matchInfo, userId)
  30. if err_info != nil {
  31. if isFullPlayer {
  32. print("匹配成功开始游戏!")
  33. startGame(roomList[0])
  34. NotifyRoomPlayerMatch(roomList[0])
  35. }
  36. } else {
  37. sendMatchErrorMsg(user_agent, 101, "not find player")
  38. }
  39. }
  40. }
  41. // 通知房间玩家匹配成功
  42. func NotifyRoomPlayerMatch(room_info *msg.RoomInfo) {
  43. for i := range room_info.Colors {
  44. user_id := room_info.Colors[i].MId
  45. user_agent := getAgentByUserId(user_id)
  46. if user_agent != nil {
  47. user_agent.WriteMsg(&msg.ResMatchLudo{
  48. Room: room_info,
  49. Success: true,
  50. ErrMsg: nil,
  51. })
  52. }
  53. }
  54. }
  55. func sendMatchErrorMsg(user_agent gate.Agent, error_code int32, msg_str string) {
  56. if user_agent != nil {
  57. user_agent.WriteMsg(&msg.ResMatchLudo{
  58. Success: false,
  59. ErrMsg: &msg.MsgError{
  60. ErrorCode: error_code,
  61. ErrorMsg: msg_str,
  62. },
  63. })
  64. }
  65. }