-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPhotonActor.cs
253 lines (214 loc) · 9.51 KB
/
PhotonActor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#define NO_NAMESERVER
using ExitGames.Client.Photon;
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Collections;
using System.Linq;
using System.Text;
namespace TurnbasedConsole
{
using ExitGames.Client.Photon.Lite;
using ExitGames.Client.Photon.LoadBalancing;
using System.Threading;
class PhotonActor : Actor
{
private PhotonTurnbasedClient photonClient;
public PhotonActor(ConcurrentQueue<Message> inbox, ConcurrentQueue<Message> outbox, PhotonTurnbasedClient photonClient)
: base(inbox, outbox)
{
this.photonClient = photonClient;
}
public override void DoInLoop()
{
Message message = null;
while (photonClient.loadBalancingPeer.DispatchIncomingCommands())
{
// You could count dispatch calls to limit them to X, if they take too much time of a single frame
}
while (inbox.TryDequeue(out message))
{
this.HandleMessage(message);
}
photonClient.loadBalancingPeer.SendOutgoingCommands();
}
public override void HandleMessage(Message message)
{
base.HandleMessage(message);
int actorNr = 0;
string roomname = string.Empty;
switch (message.id)
{
case MessageId.ListRooms:
PrintOpenRoomList();
break;
case MessageId.ChangeRoomName:
roomname = message.data;
if (photonClient.ChangeRoomName(roomname))
{
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("changed to room {0}", photonClient.RoomName)));
}
else
{
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("unchanged room {0}", photonClient.RoomName)));
}
break;
case MessageId.CreateRoom:
roomname = message.data;
photonClient.CreateRoomForMatchmaking(roomname);
break;
case MessageId.JoinRoom:
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("joining room:{0} with actor#:{1}", photonClient.RoomName, photonClient.ActorNr)));
photonClient.JoinRoom();
break;
case MessageId.LeaveRoom:
photonClient.LeaveRoom();
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("leaving room:{0}", photonClient.CurrentRoom)));
break;
case MessageId.GetMyGameList:
photonClient.GetMyGameList();
break;
case MessageId.GetGameList:
photonClient.GetGameList();
break;
case MessageId.GetGameState:
photonClient.GetGameState();
break;
case MessageId.UpdateGameState:
photonClient.UpdateGameState();
break;
case MessageId.PlayerSave:
photonClient.SaveMyPlayer(photonClient.LocalPlayer);
break;
case MessageId.PlayerLoad:
photonClient.GetMyPlayer();
break;
case MessageId.OperationRequest:
photonClient.OpRequest(message.data);
break;
case MessageId.ChangeActor:
int.TryParse(message.data, out actorNr);
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("actorNr: {0}", actorNr)));
if (photonClient.ChangeActor(actorNr))
{
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("set to actor#: {0}", photonClient.ActorNr)));
}
else
{
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("stay as actor#: {0}", photonClient.ActorNr)));
}
break;
case MessageId.GetUser:
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("user: {0}", photonClient.PlayerName)));
break;
case MessageId.ShowRoomActor:
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("room: {0}, actor#: {1}", photonClient.RoomName, photonClient.ActorNr)));
break;
case MessageId.SetRoomAndActor:
int index = -1;
int.TryParse(message.data, out index);
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("index: {0}", index)));
if (photonClient.SetRoomAndActorBy(index))
{
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("set to room:{0} actor#:{1}", photonClient.RoomName, photonClient.ActorNr)));
}
else
{
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("stay in room:{0} actor#:{1}", photonClient.RoomName, photonClient.ActorNr)));
}
break;
case MessageId.CurrentState:
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("state: {0}", photonClient.State)));
break;
case MessageId.Quit:
photonClient.QuitRoom();
break;
case MessageId.Connect:
photonClient.ConnectToMaster();
break;
case MessageId.Disconnect:
photonClient.Disconnect();
break;
default:
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("photon received unknown message: {0}", message.id.ToString())));
break;
}
}
public void OnGameList(List<string> games)
{
if (games.Count > 0)
{
StringBuilder rooms = new StringBuilder();
foreach (var room in games)
{
rooms.Append(String.Format("Got room:{0}\n", room));
}
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format(rooms.ToString())));
}
else
{
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format(@"N\A - empty list")));
}
}
public void OnPlayerLoad(string playerJson)
{
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("PlayerState: {0}", playerJson)));
}
public void OnPlayerSave()
{
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("PlayerState saved")));
}
public void OnGames(List<string> games)
{
if (games.Count > 0)
{
StringBuilder rooms = new StringBuilder();
foreach (var room in games)
{
rooms.Append(String.Format("Got room:{0}\n", room));
}
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format(rooms.ToString())));
}
else
{
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format(@"N\A - empty list")));
}
}
public void OnSetProperties()
{
PrintRoomCustomProperties();
}
private void PrintOpenRoomList()
{
if (photonClient.RoomInfoList.Count == 0)
{
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("no open rooms -> create a room")));
}
else
{
StringBuilder rooms = new StringBuilder();
foreach (KeyValuePair<string, RoomInfo> room in photonClient.RoomInfoList)
{
rooms.Append(String.Format("room:{0} info:{1}", room.Key, ""));
}
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format(rooms.ToString())));
}
}
private void PrintRoomCustomProperties()
{
if (photonClient.CurrentRoom.CustomProperties.Count == 0)
{
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format("no custom properties")));
}
else
{
StringBuilder properties = new StringBuilder();
foreach (DictionaryEntry property in photonClient.CurrentRoom.CustomProperties)
{
properties.Append(String.Format("key: {0} value: {1}\n", property.Key, property.Value));
}
outbox.Enqueue(new Message(MessageId.ConsoleMessage, String.Format(properties.ToString())));
}
}
}
}