match.go 1.9 KB

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