Skip to content

Commit 8ca1a2b

Browse files
committed
add match room unity相关逻辑
1 parent 033d3e0 commit 8ca1a2b

10 files changed

+244
-3
lines changed

CrazyCar/Assets/Scripts/CrazyCar.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ protected override void Init() {
1818
RegisterSystem<IKCPSystem>(new KCPSystem());
1919
RegisterSystem<IAddressableSystem>(new AddressableSystem());
2020
RegisterSystem<IGuidanceSystem>(new GuidanceSystem());
21+
RegisterSystem<IMatchRoomSystem>(new MatchRoomSystem());
2122
RegisterModel<IGameControllerModel>(new GameControllerModel());
2223
RegisterModel<IUserModel>(new UserModel());
2324
RegisterModel<IAvatarModel>(new AvatarModel());
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public class MatchRoomCreateOrJoinSuccEvent {
2+
3+
}

CrazyCar/Assets/Scripts/Event/MatchRoomCreateOrJoinSuccEvent.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public class MatchRoomStartEvent {
2+
3+
}

CrazyCar/Assets/Scripts/Event/MatchRoomStartEvent.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CrazyCar/Assets/Scripts/Game/NetworkController.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ public enum NetType {
1616

1717
public enum MsgType{
1818
CreatePlayer = 0,
19-
PlayerState,
20-
DelPlayer
19+
PlayerState = 1,
20+
DelPlayer = 2,
21+
MatchRoomCreate = 3,
22+
MatchRoomJoin = 4,
23+
MatchRoomStatus = 5,
24+
MatchRoomExit = 6,
25+
MatchRoomStart = 7
2126
}
2227

2328

CrazyCar/Assets/Scripts/Model/MatchModel.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,28 @@ public class MatchRankInfo {
2424
public int completeTime;
2525
}
2626

27+
public class MatchRoomMemberInfo {
28+
public string memberName;
29+
public int aid;
30+
public bool isHouseOwner;
31+
}
32+
2733
public interface IMatchModel : IModel {
2834
BindableProperty<int> RewardStar { get; }
2935
Dictionary<int, MatchInfo> MatchDic { get; set; }
3036
BindableProperty<MatchInfo> SelectInfo { get; }
3137
List<MatchRankInfo> MatchRankList { get; set; }
3238
BindableProperty<bool> IsComplete { get; }
3339
BindableProperty<int> CompleteTime { get; }
34-
3540
BindableProperty<long> StartTime { get; }
3641
BindableProperty<long> EndTime { get; }
3742
BindableProperty<bool> IsArriveLimitTime { get; }
3843
bool IsStartGame { get; }
3944
bool IsEndGame { get; }
4045
bool InGame { get; }
46+
BindableProperty<int> RoomId { get; }
47+
bool IsHouseOwner { get; }
48+
Dictionary<uint, MatchRoomMemberInfo> MemberInfoDic { get; set; }
4149

4250
int GetCompleteTime();
4351
void CleanData();
@@ -64,6 +72,13 @@ public class MatchModel : AbstractModel, IMatchModel {
6472
public bool IsEndGame { get { return IsComplete || IsArriveLimitTime;}}
6573
public bool InGame { get { return IsStartGame && !IsEndGame; } }
6674

75+
public BindableProperty<int> RoomId { get; } = new BindableProperty<int>();
76+
77+
public bool IsHouseOwner { get; }
78+
79+
public Dictionary<uint, MatchRoomMemberInfo> MemberInfoDic { get; set; }
80+
81+
6782
public void CleanData() {
6883
IsComplete.Value = false;
6984
IsArriveLimitTime.Value = false;

CrazyCar/Assets/Scripts/System/DataParseSystem.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public interface IDataParseSystem : ISystem {
1313
void ParseTimeTrialRank(JsonData jsonData, Action success = null);
1414
void ParseTimeTrialResult(JsonData jsonData, Action success = null);
1515
void ParseMatchClassData(JsonData jsonData, Action success = null);
16+
public void ParseSelectMatch(JsonData jsonData, Action success = null);
1617
void ParseMatchRank(JsonData data, Action success = null);
1718
void ParseEquipRes(JsonData jsonData, Action success = null);
1819
}
@@ -146,6 +147,19 @@ public void ParseMatchClassData(JsonData jsonData, Action success = null) {
146147
success?.Invoke();
147148
}
148149

150+
public void ParseSelectMatch(JsonData jsonData, Action success = null) {
151+
var info = this.GetModel<IMatchModel>().SelectInfo;
152+
info.Value.cid = (int)jsonData["cid"];
153+
info.Value.name = (string)jsonData["name"];
154+
info.Value.star = (int)jsonData["star"];
155+
info.Value.mapId = (int)jsonData["map_id"];
156+
info.Value.limitTime = (int)jsonData["limit_time"];
157+
info.Value.times = (int)jsonData["times"];
158+
info.Value.startTime = (long)jsonData["start_time"];
159+
info.Value.enrollTime = (long)jsonData["enroll_time"];
160+
success?.Invoke();
161+
}
162+
149163
public void ParseMatchRank(JsonData data, Action success = null) {
150164
var matchModel = this.GetModel<IMatchModel>();
151165
matchModel.MatchRankList.Clear();
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using QFramework;
5+
using System.Text;
6+
using LitJson;
7+
using Utils;
8+
9+
public interface IMatchRoomSystem : ISystem {
10+
void MatchRoomCreate();
11+
void MatchRoomJoin();
12+
void MatchRoomStatus();
13+
void MatchRoomEixt();
14+
void MatchRoomStart();
15+
void OnCreateMsg(JsonData recJD);
16+
void OnJoinMsg(JsonData recJD);
17+
void OnStatusMsg(JsonData recJD);
18+
void OnExitMsg(JsonData recJD);
19+
void OnStartMsg(JsonData recJD);
20+
}
21+
22+
public class MatchRoomSystem : AbstractSystem, IMatchRoomSystem {
23+
private void MatchRoomConnect() {
24+
string ws = "ws" + this.GetSystem<INetworkSystem>().HttpBaseUrl.Substring(4) +
25+
"websocket/MatchRoomWebSocket/" +
26+
this.GetModel<IUserModel>().Uid.Value + "," + this.GetModel<IMatchModel>().RoomId;
27+
28+
this.GetSystem<IWebSocketSystem>().Connect(ws);
29+
}
30+
31+
public void MatchRoomCreate() {
32+
MatchRoomConnect();
33+
StringBuilder sb = new StringBuilder();
34+
JsonWriter w = new JsonWriter(sb);
35+
w.WriteObjectStart();
36+
w.WritePropertyName("msg_type");
37+
w.Write((int)MsgType.MatchRoomCreate);
38+
w.WritePropertyName("timestamp");
39+
w.Write(Util.GetTime());
40+
w.WritePropertyName("room_id");
41+
w.Write(this.GetModel<IMatchModel>().RoomId);
42+
w.WritePropertyName("uid");
43+
w.Write(this.GetModel<IUserModel>().Uid);
44+
w.WriteObjectEnd();
45+
Debug.LogError("MatchRoomCreate : " + sb.ToString());
46+
this.GetSystem<IWebSocketSystem>().SendMsgToServer(sb.ToString());
47+
}
48+
49+
public void MatchRoomEixt() {
50+
this.GetSystem<IWebSocketSystem>().CloseConnect();
51+
}
52+
53+
public void MatchRoomJoin() {
54+
MatchRoomConnect();
55+
StringBuilder sb = new StringBuilder();
56+
JsonWriter w = new JsonWriter(sb);
57+
w.WriteObjectStart();
58+
w.WritePropertyName("msg_type");
59+
w.Write((int)MsgType.MatchRoomCreate);
60+
w.WritePropertyName("timestamp");
61+
w.Write(Util.GetTime());
62+
w.WritePropertyName("room_id");
63+
w.Write(this.GetModel<IMatchModel>().RoomId);
64+
w.WritePropertyName("uid");
65+
w.Write(this.GetModel<IUserModel>().Uid);
66+
w.WriteObjectEnd();
67+
Debug.LogError("MatchRoomJoin : " + sb.ToString());
68+
this.GetSystem<IWebSocketSystem>().SendMsgToServer(sb.ToString());
69+
}
70+
71+
public void MatchRoomStatus() {
72+
StringBuilder sb = new StringBuilder();
73+
JsonWriter w = new JsonWriter(sb);
74+
w.WriteObjectStart();
75+
w.WritePropertyName("msg_type");
76+
w.Write((int)MsgType.MatchRoomStatus);
77+
w.WritePropertyName("timestamp");
78+
w.Write(Util.GetTime());
79+
w.WritePropertyName("room_id");
80+
w.Write(this.GetModel<IMatchModel>().RoomId);
81+
w.WritePropertyName("uid");
82+
w.Write(this.GetModel<IUserModel>().Uid);
83+
w.WriteObjectEnd();
84+
Debug.LogError("MatchRoomStatus : " + sb.ToString());
85+
this.GetSystem<IWebSocketSystem>().SendMsgToServer(sb.ToString());
86+
}
87+
88+
public void MatchRoomStart() {
89+
CoroutineController.manager.StartCoroutine(this.GetSystem<INetworkSystem>().POSTHTTP(url: this.GetSystem<INetworkSystem>().HttpBaseUrl +
90+
RequestUrl.createMatchUrl,
91+
token: this.GetModel<IGameControllerModel>().Token.Value,
92+
succData: (data) => {
93+
int cid = (int)data["cid"];
94+
StringBuilder sb = new StringBuilder();
95+
JsonWriter w = new JsonWriter(sb);
96+
w.WriteObjectStart();
97+
w.WritePropertyName("msg_type");
98+
w.Write((int)MsgType.MatchRoomStart);
99+
w.WritePropertyName("timestamp");
100+
w.Write(Util.GetTime());
101+
w.WritePropertyName("room_id");
102+
w.Write(this.GetModel<IMatchModel>().RoomId);
103+
w.WritePropertyName("uid");
104+
w.Write(this.GetModel<IUserModel>().Uid);
105+
w.WritePropertyName("cid");
106+
w.Write(cid);
107+
w.WriteObjectEnd();
108+
Debug.LogError("MatchRoomStart : " + sb.ToString());
109+
this.GetSystem<IWebSocketSystem>().SendMsgToServer(sb.ToString());
110+
},
111+
code: (code) => {
112+
113+
}
114+
));
115+
}
116+
117+
public void OnCreateMsg(JsonData recJD) {
118+
int code = (int)recJD["code"];
119+
Debug.LogError("OnCreateMsg = " + code);
120+
if (code == 200) {
121+
this.SendEvent<MatchRoomCreateOrJoinSuccEvent>();
122+
} else if (code == 421) {
123+
this.GetModel<IGameControllerModel>().WarningAlert.ShowWithText("房间已存在");
124+
} else if (code == 422) {
125+
this.GetModel<IGameControllerModel>().WarningAlert.ShowWithText("房间数已达上限,请稍后再试");
126+
}
127+
}
128+
129+
public void OnExitMsg(JsonData recJD) {
130+
131+
}
132+
133+
public void OnJoinMsg(JsonData recJD) {
134+
int code = (int)recJD["code"];
135+
Debug.LogError("OnJoinMsg = " + code);
136+
if (code == 200) {
137+
this.SendEvent<MatchRoomCreateOrJoinSuccEvent>();
138+
} else if (code == 404) {
139+
this.GetModel<IGameControllerModel>().WarningAlert.ShowWithText("房间不存在");
140+
} else if (code == 422) {
141+
this.GetModel<IGameControllerModel>().WarningAlert.ShowWithText("房间人数已满");
142+
}
143+
}
144+
145+
public void OnStatusMsg(JsonData recJD) {
146+
JsonData players = recJD["players"];
147+
var infos = this.GetModel<IMatchModel>().MemberInfoDic;
148+
infos.Clear();
149+
for (int i = 0; i < players.Count; i++) {
150+
MatchRoomMemberInfo info = new MatchRoomMemberInfo();
151+
info.memberName = (string)players[i]["member_name"];
152+
info.isHouseOwner = ((uint)players[i]["type"] == 1);
153+
info.aid = (int)players[i]["aid"];
154+
}
155+
}
156+
157+
public void OnStartMsg(JsonData recJD) {
158+
if ((int)recJD["code"] == 200) {
159+
this.GetSystem<IDataParseSystem>().ParseSelectMatch(recJD);
160+
this.SendEvent<MatchRoomStartEvent>();
161+
}
162+
}
163+
164+
protected override void OnInit() {
165+
166+
}
167+
}

CrazyCar/Assets/Scripts/System/MatchRoomSystem.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)