Advertisement
Guest User

BlaBlaa

a guest
Apr 20th, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 44.73 KB | None | 0 0
  1. // ----------------------------------------------------------------------------
  2. // <copyright file="LoadbalancingPeer.cs" company="Exit Games GmbH">
  3. // Loadbalancing Framework for Photon - Copyright (C) 2011 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. // Provides the operations needed to use the loadbalancing server app(s).
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // ----------------------------------------------------------------------------
  10.  
  11. using ExitGames.Client.Photon;
  12. using ExitGames.Client.Photon.Lite;
  13. using System;
  14. using System.Collections.Generic;
  15. using Hashtable = ExitGames.Client.Photon.Hashtable;
  16.  
  17.  
  18. /// <summary>
  19. /// Internally used by PUN, a LoadbalancingPeer provides the operations and enum
  20. /// definitions needed to use the Photon Loadbalancing server (or the Photon Cloud).
  21. /// </summary>
  22. /// <remarks>
  23. /// The LoadBalancingPeer does not keep a state, instead this is done by a LoadBalancingClient.
  24. /// </remarks>
  25. internal class LoadbalancingPeer : PhotonPeer
  26. {
  27. public LoadbalancingPeer(IPhotonPeerListener listener, ConnectionProtocol protocolType) : base(listener, protocolType)
  28. {
  29. }
  30.  
  31. public virtual bool OpGetRegions(string appId)
  32. {
  33. Dictionary<byte, object> parameters = new Dictionary<byte, object>();
  34. parameters[(byte)ParameterCode.ApplicationId] = appId;
  35.  
  36. return this.OpCustom(OperationCode.GetRegions, parameters, true, 0, true);
  37. }
  38.  
  39. /// <summary>
  40. /// Joins the lobby on the Master Server, where you get a list of RoomInfos of currently open rooms.
  41. /// This is an async request which triggers a OnOperationResponse() call.
  42. /// </summary>
  43. /// <returns>If the operation could be sent (has to be connected).</returns>
  44. public virtual bool OpJoinLobby(TypedLobby lobby)
  45. {
  46. if (this.DebugOut >= DebugLevel.INFO)
  47. {
  48. this.Listener.DebugReturn(DebugLevel.INFO, "OpJoinLobby()");
  49. }
  50.  
  51. Dictionary<byte, object> parameters = null;
  52. if (lobby != null && !lobby.IsDefault)
  53. {
  54. parameters = new Dictionary<byte, object>();
  55. parameters[(byte)ParameterCode.LobbyName] = lobby.Name;
  56. parameters[(byte)ParameterCode.LobbyType] = (byte)lobby.Type;
  57. }
  58.  
  59. return this.OpCustom(OperationCode.JoinLobby, parameters, true);
  60. }
  61.  
  62. /// <summary>
  63. /// Leaves the lobby on the Master Server.
  64. /// This is an async request which triggers a OnOperationResponse() call.
  65. /// </summary>
  66. /// <returns>If the operation could be sent (has to be connected).</returns>
  67. public virtual bool OpLeaveLobby()
  68. {
  69. if (this.DebugOut >= DebugLevel.INFO)
  70. {
  71. this.Listener.DebugReturn(DebugLevel.INFO, "OpLeaveLobby()");
  72. }
  73.  
  74. return this.OpCustom(OperationCode.LeaveLobby, null, true);
  75. }
  76.  
  77.  
  78. /// <summary>
  79. /// Don't use this method directly, unless you know how to cache and apply customActorProperties.
  80. /// The PhotonNetwork methods will handle player and room properties for you and call this method.
  81. /// </summary>
  82. public virtual bool OpCreateRoom(string roomName, RoomOptions roomOptions, TypedLobby lobby, Hashtable playerProperties, bool onGameServer)
  83. {
  84. if (this.DebugOut >= DebugLevel.INFO)
  85. {
  86. this.Listener.DebugReturn(DebugLevel.INFO, "OpCreateRoom()");
  87. }
  88.  
  89. Dictionary<byte, object> op = new Dictionary<byte, object>();
  90.  
  91. if (!string.IsNullOrEmpty(roomName))
  92. {
  93. op[ParameterCode.RoomName] = roomName;
  94. }
  95. if (lobby != null)
  96. {
  97. op[ParameterCode.LobbyName] = lobby.Name;
  98. op[ParameterCode.LobbyType] = (byte)lobby.Type;
  99. }
  100.  
  101. if (onGameServer)
  102. {
  103. if (playerProperties != null && playerProperties.Count > 0)
  104. {
  105. op[ParameterCode.PlayerProperties] = playerProperties;
  106. op[ParameterCode.Broadcast] = true; // TODO: check if this also makes sense when creating a room?! // broadcast actor properties
  107. }
  108.  
  109.  
  110. if (roomOptions == null)
  111. {
  112. roomOptions = new RoomOptions();
  113. }
  114.  
  115. Hashtable gameProperties = new Hashtable();
  116. op[ParameterCode.GameProperties] = gameProperties;
  117. gameProperties.MergeStringKeys(roomOptions.customRoomProperties);
  118.  
  119. gameProperties[GameProperties.IsOpen] = roomOptions.isOpen; // TODO: check default value. dont send this then
  120. gameProperties[GameProperties.IsVisible] = roomOptions.isVisible; // TODO: check default value. dont send this then
  121. gameProperties[GameProperties.PropsListedInLobby] = roomOptions.customRoomPropertiesForLobby;
  122. if (roomOptions.maxPlayers > 0)
  123. {
  124. gameProperties[GameProperties.MaxPlayers] = roomOptions.maxPlayers;
  125. }
  126. if (roomOptions.cleanupCacheOnLeave)
  127. {
  128. op[ParameterCode.CleanupCacheOnLeave] = true; // this is actually setting the room's config
  129. gameProperties[GameProperties.CleanupCacheOnLeave] = true; // this is only informational for the clients which join
  130. }
  131. }
  132.  
  133. // UnityEngine.Debug.Log("CreateGame: " + SupportClass.DictionaryToString(op));
  134. return this.OpCustom(OperationCode.CreateGame, op, true);
  135. }
  136.  
  137.  
  138. /// <summary>LoadBalancingPeer.OpJoinRoom</summary>
  139. public virtual bool OpJoinRoom(string roomName, RoomOptions roomOptions, TypedLobby lobby, bool createIfNotExists, Hashtable playerProperties, bool onGameServer)
  140. {
  141. Dictionary<byte, object> op = new Dictionary<byte, object>();
  142.  
  143. if (!string.IsNullOrEmpty(roomName))
  144. {
  145. op[ParameterCode.RoomName] = roomName;
  146. }
  147. if (createIfNotExists)
  148. {
  149. op[ParameterCode.CreateIfNotExists] = true;
  150. if (lobby != null)
  151. {
  152. op[ParameterCode.LobbyName] = lobby.Name;
  153. op[ParameterCode.LobbyType] = (byte)lobby.Type;
  154. }
  155. }
  156.  
  157. if (onGameServer)
  158. {
  159. if (playerProperties != null && playerProperties.Count > 0)
  160. {
  161. op[ParameterCode.PlayerProperties] = playerProperties;
  162. op[ParameterCode.Broadcast] = true; // broadcast actor properties
  163. }
  164.  
  165.  
  166. if (createIfNotExists)
  167. {
  168. if (roomOptions == null)
  169. {
  170. roomOptions = new RoomOptions();
  171. }
  172.  
  173. Hashtable gameProperties = new Hashtable();
  174. op[ParameterCode.GameProperties] = gameProperties;
  175. gameProperties.MergeStringKeys(roomOptions.customRoomProperties);
  176.  
  177. gameProperties[GameProperties.IsOpen] = roomOptions.isOpen;
  178. gameProperties[GameProperties.IsVisible] = roomOptions.isVisible;
  179. gameProperties[GameProperties.PropsListedInLobby] = roomOptions.customRoomPropertiesForLobby;
  180. if (roomOptions.maxPlayers > 0)
  181. {
  182. gameProperties[GameProperties.MaxPlayers] = roomOptions.maxPlayers;
  183. }
  184. if (roomOptions.cleanupCacheOnLeave)
  185. {
  186. op[ParameterCode.CleanupCacheOnLeave] = true; // this is actually setting the room's config
  187. gameProperties[GameProperties.CleanupCacheOnLeave] = true; // this is only informational for the clients which join
  188. }
  189. }
  190. }
  191.  
  192. // UnityEngine.Debug.Log("JoinGame: " + SupportClass.DictionaryToString(op));
  193. return this.OpCustom(OperationCode.JoinGame, op, true);
  194. }
  195.  
  196.  
  197. /// <summary>
  198. /// Operation to join a random, available room. Overloads take additional player properties.
  199. /// This is an async request which triggers a OnOperationResponse() call.
  200. /// If all rooms are closed or full, the OperationResponse will have a returnCode of ErrorCode.NoRandomMatchFound.
  201. /// If successful, the OperationResponse contains a gameserver address and the name of some room.
  202. /// </summary>
  203. /// <param name="expectedCustomRoomProperties">Optional. A room will only be joined, if it matches these custom properties (with string keys).</param>
  204. /// <param name="expectedMaxPlayers">Filters for a particular maxplayer setting. Use 0 to accept any maxPlayer value.</param>
  205. /// <param name="playerProperties">This player's properties (custom and well known).</param>
  206. /// <param name="matchingType">Selects one of the available matchmaking algorithms. See MatchmakingMode enum for options.</param>
  207. /// <returns>If the operation could be sent currently (requires connection).</returns>
  208. public virtual bool OpJoinRandomRoom(Hashtable expectedCustomRoomProperties, byte expectedMaxPlayers, Hashtable playerProperties, MatchmakingMode matchingType, TypedLobby typedLobby, string sqlLobbyFilter)
  209. {
  210. if (this.DebugOut >= DebugLevel.INFO)
  211. {
  212. this.Listener.DebugReturn(DebugLevel.INFO, "OpJoinRandomRoom()");
  213. }
  214.  
  215. Hashtable expectedRoomProperties = new Hashtable();
  216. expectedRoomProperties.MergeStringKeys(expectedCustomRoomProperties);
  217. if (expectedMaxPlayers > 0)
  218. {
  219. expectedRoomProperties[GameProperties.MaxPlayers] = expectedMaxPlayers;
  220. }
  221.  
  222. Dictionary<byte, object> opParameters = new Dictionary<byte, object>();
  223. if (expectedRoomProperties.Count > 0)
  224. {
  225. opParameters[ParameterCode.GameProperties] = expectedRoomProperties;
  226. }
  227.  
  228. if (playerProperties != null && playerProperties.Count > 0)
  229. {
  230. opParameters[ParameterCode.PlayerProperties] = playerProperties;
  231. }
  232.  
  233. if (matchingType != MatchmakingMode.FillRoom)
  234. {
  235. opParameters[ParameterCode.MatchMakingType] = (byte)matchingType;
  236. }
  237.  
  238. if (typedLobby != null)
  239. {
  240. opParameters[ParameterCode.LobbyName] = typedLobby.Name;
  241. opParameters[ParameterCode.LobbyType] = (byte)typedLobby.Type;
  242. }
  243.  
  244. if (!string.IsNullOrEmpty(sqlLobbyFilter))
  245. {
  246. opParameters[ParameterCode.Data] = sqlLobbyFilter;
  247. }
  248.  
  249. // UnityEngine.Debug.LogWarning("OpJoinRandom: " + opParameters.ToStringFull());
  250. return this.OpCustom(OperationCode.JoinRandomGame, opParameters, true);
  251. }
  252.  
  253. /// <summary>
  254. /// Request the rooms and online status for a list of friends (each client must set a unique username via OpAuthenticate).
  255. /// </summary>
  256. /// <remarks>
  257. /// Used on Master Server to find the rooms played by a selected list of users.
  258. /// Users identify themselves by using OpAuthenticate with a unique username.
  259. /// The list of usernames must be fetched from some other source (not provided by Photon).
  260. ///
  261. /// The server response includes 2 arrays of info (each index matching a friend from the request):
  262. /// ParameterCode.FindFriendsResponseOnlineList = bool[] of online states
  263. /// ParameterCode.FindFriendsResponseRoomIdList = string[] of room names (empty string if not in a room)
  264. /// </remarks>
  265. /// <param name="friendsToFind">Array of friend's names (make sure they are unique).</param>
  266. /// <returns>If the operation could be sent (requires connection).</returns>
  267. public virtual bool OpFindFriends(string[] friendsToFind)
  268. {
  269. Dictionary<byte, object> opParameters = new Dictionary<byte, object>();
  270. if (friendsToFind != null && friendsToFind.Length > 0)
  271. {
  272. opParameters[ParameterCode.FindFriendsRequestList] = friendsToFind;
  273. }
  274.  
  275. return this.OpCustom(OperationCode.FindFriends, opParameters, true);
  276. }
  277.  
  278. public bool OpSetCustomPropertiesOfActor(int actorNr, Hashtable actorProperties, bool broadcast, byte channelId)
  279. {
  280. return this.OpSetPropertiesOfActor(actorNr, actorProperties.StripToStringKeys(), broadcast, channelId);
  281. }
  282.  
  283. protected bool OpSetPropertiesOfActor(int actorNr, Hashtable actorProperties, bool broadcast, byte channelId)
  284. {
  285. if (this.DebugOut >= DebugLevel.INFO)
  286. {
  287. this.Listener.DebugReturn(DebugLevel.INFO, "OpSetPropertiesOfActor()");
  288. }
  289.  
  290. if (actorNr <= 0 || actorProperties == null)
  291. {
  292. if (this.DebugOut >= DebugLevel.INFO)
  293. {
  294. this.Listener.DebugReturn(DebugLevel.INFO, "OpSetPropertiesOfActor not sent. ActorNr must be > 0 and actorProperties != null.");
  295. }
  296. return false;
  297. }
  298.  
  299. Dictionary<byte, object> opParameters = new Dictionary<byte, object>();
  300. opParameters.Add(ParameterCode.Properties, actorProperties);
  301. opParameters.Add(ParameterCode.ActorNr, actorNr);
  302. if (broadcast)
  303. {
  304. opParameters.Add(ParameterCode.Broadcast, broadcast);
  305. }
  306.  
  307. return this.OpCustom((byte)OperationCode.SetProperties, opParameters, broadcast, channelId);
  308. }
  309.  
  310. protected void OpSetPropertyOfRoom(byte propCode, object value)
  311. {
  312. Hashtable properties = new Hashtable();
  313. properties[propCode] = value;
  314. this.OpSetPropertiesOfRoom(properties, true, (byte)0);
  315. }
  316.  
  317. public bool OpSetCustomPropertiesOfRoom(Hashtable gameProperties, bool broadcast, byte channelId)
  318. {
  319. return this.OpSetPropertiesOfRoom(gameProperties.StripToStringKeys(), broadcast, channelId);
  320. }
  321.  
  322. public bool OpSetPropertiesOfRoom(Hashtable gameProperties, bool broadcast, byte channelId)
  323. {
  324. if (this.DebugOut >= DebugLevel.INFO)
  325. {
  326. this.Listener.DebugReturn(DebugLevel.INFO, "OpSetPropertiesOfRoom()");
  327. }
  328.  
  329. Dictionary<byte, object> opParameters = new Dictionary<byte, object>();
  330. opParameters.Add(ParameterCode.Properties, gameProperties);
  331. if (broadcast)
  332. {
  333. opParameters.Add(ParameterCode.Broadcast, broadcast);
  334. }
  335.  
  336. return this.OpCustom((byte)OperationCode.SetProperties, opParameters, broadcast, channelId);
  337. }
  338.  
  339. /// <summary>
  340. /// Sends this app's appId and appVersion to identify this application server side.
  341. /// This is an async request which triggers a OnOperationResponse() call.
  342. /// </summary>
  343. /// <remarks>
  344. /// This operation makes use of encryption, if that is established before.
  345. /// See: EstablishEncryption(). Check encryption with IsEncryptionAvailable.
  346. /// This operation is allowed only once per connection (multiple calls will have ErrorCode != Ok).
  347. /// </remarks>
  348. /// <param name="appId">Your application's name or ID to authenticate. This is assigned by Photon Cloud (webpage).</param>
  349. /// <param name="appVersion">The client's version (clients with differing client appVersions are separated and players don't meet).</param>
  350. /// <param name="userId"></param>
  351. /// <param name="authValues"></param>
  352. /// <param name="regionCode">When authenticating for a specific region, a NameServer will forward you to that region's MasterServer.</param>
  353. /// <returns>If the operation could be sent (has to be connected).</returns>
  354. public virtual bool OpAuthenticate(string appId, string appVersion, string userId, AuthenticationValues authValues, string regionCode)
  355. {
  356. if (this.DebugOut >= DebugLevel.INFO)
  357. {
  358. this.Listener.DebugReturn(DebugLevel.INFO, "OpAuthenticate()");
  359. }
  360.  
  361. Dictionary<byte, object> opParameters = new Dictionary<byte, object>();
  362. if (authValues != null && authValues.Secret != null)
  363. {
  364. opParameters[ParameterCode.Secret] = authValues.Secret;
  365. //return this.OpCustom(OperationCode.Authenticate, opParameters, true, (byte)0, false);
  366. }
  367.  
  368. opParameters[ParameterCode.AppVersion] = appVersion;
  369. opParameters[ParameterCode.ApplicationId] = appId;
  370.  
  371. if (!string.IsNullOrEmpty(regionCode))
  372. {
  373. opParameters[ParameterCode.Region] = regionCode;
  374. }
  375.  
  376. if (!string.IsNullOrEmpty(userId))
  377. {
  378. opParameters[ParameterCode.UserId] = userId;
  379. }
  380.  
  381.  
  382. if (authValues != null && authValues.AuthType != CustomAuthenticationType.None)
  383. {
  384. if (!this.IsEncryptionAvailable)
  385. {
  386. this.Listener.DebugReturn(DebugLevel.ERROR, "OpAuthenticate() failed. When you want Custom Authentication encryption is mandatory.");
  387. return false;
  388. }
  389.  
  390. opParameters[ParameterCode.ClientAuthenticationType] = (byte)authValues.AuthType;
  391. if (!string.IsNullOrEmpty(authValues.Secret))
  392. {
  393. opParameters[ParameterCode.Secret] = authValues.Secret;
  394. }
  395. //else
  396. //{
  397. if (!string.IsNullOrEmpty(authValues.AuthParameters))
  398. {
  399. opParameters[ParameterCode.ClientAuthenticationParams] = authValues.AuthParameters;
  400. }
  401. if (authValues.AuthPostData != null)
  402. {
  403. opParameters[ParameterCode.ClientAuthenticationData] = authValues.AuthPostData;
  404. }
  405. //}
  406. }
  407.  
  408. bool sent = this.OpCustom(OperationCode.Authenticate, opParameters, true, (byte)0, this.IsEncryptionAvailable);
  409. if (!sent)
  410. {
  411. this.Listener.DebugReturn(DebugLevel.ERROR, "Error calling OpAuthenticate! Did not work. Check log output, CustomAuthenticationValues and if you're connected.");
  412. }
  413. return sent;
  414. }
  415.  
  416.  
  417. /// <summary>
  418. /// Operation to handle this client's interest groups (for events in room).
  419. /// </summary>
  420. /// <remarks>
  421. /// Note the difference between passing null and byte[0]:
  422. /// null won't add/remove any groups.
  423. /// byte[0] will add/remove all (existing) groups.
  424. /// First, removing groups is executed. This way, you could leave all groups and join only the ones provided.
  425. /// </remarks>
  426. /// <param name="groupsToRemove">Groups to remove from interest. Null will not remove any. A byte[0] will remove all.</param>
  427. /// <param name="groupsToAdd">Groups to add to interest. Null will not add any. A byte[0] will add all current.</param>
  428. /// <returns></returns>
  429. public virtual bool OpChangeGroups(byte[] groupsToRemove, byte[] groupsToAdd)
  430. {
  431. if (this.DebugOut >= DebugLevel.ALL)
  432. {
  433. this.Listener.DebugReturn(DebugLevel.ALL, "OpChangeGroups()");
  434. }
  435.  
  436. Dictionary<byte, object> opParameters = new Dictionary<byte, object>();
  437. if (groupsToRemove != null)
  438. {
  439. opParameters[(byte)LiteOpKey.Remove] = groupsToRemove;
  440. }
  441. if (groupsToAdd != null)
  442. {
  443. opParameters[(byte)LiteOpKey.Add] = groupsToAdd;
  444. }
  445.  
  446. return this.OpCustom((byte)LiteOpCode.ChangeGroups, opParameters, true, 0);
  447. }
  448.  
  449.  
  450. /// <summary>
  451. /// Send an event with custom code/type and any content to the other players in the same room.
  452. /// </summary>
  453. /// <remarks>This override explicitly uses another parameter order to not mix it up with the implementation for Hashtable only.</remarks>
  454. /// <param name="eventCode">Identifies this type of event (and the content). Your game's event codes can start with 0.</param>
  455. /// <param name="customEventContent">Any serializable datatype (including Hashtable like the other OpRaiseEvent overloads).</param>
  456. /// <param name="sendReliable">If this event has to arrive reliably (potentially repeated if it's lost).</param>
  457. /// <param name="raiseEventOptions">Contains (slightly) less often used options. If you pass null, the default options will be used.</param>
  458. /// <returns>If operation could be enqueued for sending. Sent when calling: Service or SendOutgoingCommands.</returns>
  459. public virtual bool OpRaiseEvent(byte eventCode, object customEventContent, bool sendReliable, RaiseEventOptions raiseEventOptions)
  460. {
  461. Dictionary<byte, object> opParameters = new Dictionary<byte, object>();
  462. opParameters[(byte)LiteOpKey.Code] = (byte)eventCode;
  463. if (customEventContent != null)
  464. {
  465. opParameters[(byte) LiteOpKey.Data] = customEventContent;
  466. }
  467.  
  468. if (raiseEventOptions == null)
  469. {
  470. raiseEventOptions = RaiseEventOptions.Default;
  471. }
  472. else
  473. {
  474. if (raiseEventOptions.CachingOption != EventCaching.DoNotCache)
  475. {
  476. opParameters[(byte) LiteOpKey.Cache] = (byte) raiseEventOptions.CachingOption;
  477. }
  478. if (raiseEventOptions.Receivers != ReceiverGroup.Others)
  479. {
  480. opParameters[(byte) LiteOpKey.ReceiverGroup] = (byte) raiseEventOptions.Receivers;
  481. }
  482. if (raiseEventOptions.InterestGroup != 0)
  483. {
  484. opParameters[(byte) LiteOpKey.Group] = (byte) raiseEventOptions.InterestGroup;
  485. }
  486. if (raiseEventOptions.TargetActors != null)
  487. {
  488. opParameters[(byte) LiteOpKey.ActorList] = raiseEventOptions.TargetActors;
  489. }
  490. if (raiseEventOptions.ForwardToWebhook)
  491. {
  492. opParameters[(byte) ParameterCode.EventForward] = true; //TURNBASED
  493. }
  494. }
  495.  
  496. return this.OpCustom((byte)LiteOpCode.RaiseEvent, opParameters, sendReliable, raiseEventOptions.SequenceChannel, false);
  497. }
  498.  
  499. }
  500.  
  501. /// <summary>
  502. /// Class for constants. These (int) values represent error codes, as defined and sent by the Photon LoadBalancing logic.
  503. /// Pun uses these constants internally.
  504. /// </summary>
  505. /// <remarks>Codes from the Photon Core are negative. Default-app error codes go down from short.max.</remarks>
  506. public class ErrorCode
  507. {
  508. /// <summary>(0) is always "OK", anything else an error or specific situation.</summary>
  509. public const int Ok = 0;
  510.  
  511. // server - Photon low(er) level: <= 0
  512.  
  513. /// <summary>
  514. /// (-3) Operation can't be executed yet (e.g. OpJoin can't be called before being authenticated, RaiseEvent cant be used before getting into a room).
  515. /// </summary>
  516. /// <remarks>
  517. /// Before you call any operations on the Cloud servers, the automated client workflow must complete its authorization.
  518. /// In PUN, wait until State is: JoinedLobby (with AutoJoinLobby = true) or ConnectedToMaster (AutoJoinLobby = false)
  519. /// </remarks>
  520. public const int OperationNotAllowedInCurrentState = -3;
  521.  
  522. /// <summary>(-2) The operation you called is not implemented on the server (application) you connect to. Make sure you run the fitting applications.</summary>
  523. public const int InvalidOperationCode = -2;
  524.  
  525. /// <summary>(-1) Something went wrong in the server. Try to reproduce and contact Exit Games.</summary>
  526. public const int InternalServerError = -1;
  527.  
  528. // server - PhotonNetwork: 0x7FFF and down
  529. // logic-level error codes start with short.max
  530.  
  531. /// <summary>(32767) Authentication failed. Possible cause: AppId is unknown to Photon (in cloud service).</summary>
  532. public const int InvalidAuthentication = 0x7FFF;
  533.  
  534. /// <summary>(32766) GameId (name) already in use (can't create another). Change name.</summary>
  535. public const int GameIdAlreadyExists = 0x7FFF - 1;
  536.  
  537. /// <summary>(32765) Game is full. This rarely happens when some player joined the room before your join completed.</summary>
  538. public const int GameFull = 0x7FFF - 2;
  539.  
  540. /// <summary>(32764) Game is closed and can't be joined. Join another game.</summary>
  541. public const int GameClosed = 0x7FFF - 3;
  542.  
  543. [Obsolete("No longer used, cause random matchmaking is no longer a process.")]
  544. public const int AlreadyMatched = 0x7FFF - 4;
  545.  
  546. /// <summary>(32762) Not in use currently.</summary>
  547. public const int ServerFull = 0x7FFF - 5;
  548.  
  549. /// <summary>(32761) Not in use currently.</summary>
  550. public const int UserBlocked = 0x7FFF - 6;
  551.  
  552. /// <summary>(32760) Random matchmaking only succeeds if a room exists thats neither closed nor full. Repeat in a few seconds or create a new room.</summary>
  553. public const int NoRandomMatchFound = 0x7FFF - 7;
  554.  
  555. /// <summary>(32758) Join can fail if the room (name) is not existing (anymore). This can happen when players leave while you join.</summary>
  556. public const int GameDoesNotExist = 0x7FFF - 9;
  557.  
  558. /// <summary>(32757) Authorization on the Photon Cloud failed because the concurrent users (CCU) limit of the app's subscription is reached.</summary>
  559. /// <remarks>
  560. /// Unless you have a plan with "CCU Burst", clients might fail the authentication step during connect.
  561. /// Affected client are unable to call operations. Please note that players who end a game and return
  562. /// to the master server will disconnect and re-connect, which means that they just played and are rejected
  563. /// in the next minute / re-connect.
  564. /// This is a temporary measure. Once the CCU is below the limit, players will be able to connect an play again.
  565. ///
  566. /// OpAuthorize is part of connection workflow but only on the Photon Cloud, this error can happen.
  567. /// Self-hosted Photon servers with a CCU limited license won't let a client connect at all.
  568. /// </remarks>
  569. public const int MaxCcuReached = 0x7FFF - 10;
  570.  
  571. /// <summary>(32756) Authorization on the Photon Cloud failed because the app's subscription does not allow to use a particular region's server.</summary>
  572. /// <remarks>
  573. /// Some subscription plans for the Photon Cloud are region-bound. Servers of other regions can't be used then.
  574. /// Check your master server address and compare it with your Photon Cloud Dashboard's info.
  575. /// https://cloud.exitgames.com/dashboard
  576. ///
  577. /// OpAuthorize is part of connection workflow but only on the Photon Cloud, this error can happen.
  578. /// Self-hosted Photon servers with a CCU limited license won't let a client connect at all.
  579. /// </remarks>
  580. public const int InvalidRegion = 0x7FFF - 11;
  581.  
  582. /// <summary>
  583. /// (32755) Custom Authentication of the user failed due to setup reasons (see Cloud Dashboard) or the provided user data (like username or token). Check error message for details.
  584. /// </summary>
  585. public const int CustomAuthenticationFailed = 0x7FFF - 12;
  586. }
  587.  
  588.  
  589. /// <summary>
  590. /// Class for constants. These (byte) values define "well known" properties for an Actor / Player.
  591. /// Pun uses these constants internally.
  592. /// </summary>
  593. /// <remarks>
  594. /// "Custom properties" have to use a string-type as key. They can be assigned at will.
  595. /// </remarks>
  596. public class ActorProperties
  597. {
  598. /// <summary>(255) Name of a player/actor.</summary>
  599. public const byte PlayerName = 255; // was: 1
  600. }
  601.  
  602. /// <summary>
  603. /// Class for constants. These (byte) values are for "well known" room/game properties used in Photon Loadbalancing.
  604. /// Pun uses these constants internally.
  605. /// </summary>
  606. /// <remarks>
  607. /// "Custom properties" have to use a string-type as key. They can be assigned at will.
  608. /// </remarks>
  609. public class GameProperties
  610. {
  611. /// <summary>(255) Max number of players that "fit" into this room. 0 is for "unlimited".</summary>
  612. public const byte MaxPlayers = 255;
  613. /// <summary>(254) Makes this room listed or not in the lobby on master.</summary>
  614. public const byte IsVisible = 254;
  615. /// <summary>(253) Allows more players to join a room (or not).</summary>
  616. public const byte IsOpen = 253;
  617. /// <summary>(252) Current count of players in the room. Used only in the lobby on master.</summary>
  618. public const byte PlayerCount = 252;
  619. /// <summary>(251) True if the room is to be removed from room listing (used in update to room list in lobby on master)</summary>
  620. public const byte Removed = 251;
  621. /// <summary>(250) A list of the room properties to pass to the RoomInfo list in a lobby. This is used in CreateRoom, which defines this list once per room.</summary>
  622. public const byte PropsListedInLobby = 250;
  623. /// <summary>Equivalent of Operation Join parameter CleanupCacheOnLeave.</summary>
  624. public const byte CleanupCacheOnLeave = 249;
  625. }
  626.  
  627. /// <summary>
  628. /// Class for constants. These values are for events defined by Photon Loadbalancing.
  629. /// Pun uses these constants internally.
  630. /// </summary>
  631. /// <remarks>They start at 255 and go DOWN. Your own in-game events can start at 0.</remarks>
  632. public class EventCode
  633. {
  634. /// <summary>(230) Initial list of RoomInfos (in lobby on Master)</summary>
  635. public const byte GameList = 230;
  636. /// <summary>(229) Update of RoomInfos to be merged into "initial" list (in lobby on Master)</summary>
  637. public const byte GameListUpdate = 229;
  638. /// <summary>(228) Currently not used. State of queueing in case of server-full</summary>
  639. public const byte QueueState = 228;
  640. /// <summary>(227) Currently not used. Event for matchmaking</summary>
  641. public const byte Match = 227;
  642. /// <summary>(226) Event with stats about this application (players, rooms, etc)</summary>
  643. public const byte AppStats = 226;
  644. /// <summary>(224) This event provides a list of lobbies with their player and game counts.</summary>
  645. public const byte TypedLobbyStats = 224;
  646. /// <summary>(210) Internally used in case of hosting by Azure</summary>
  647. [Obsolete("TCP routing was removed after becoming obsolete.")]
  648. public const byte AzureNodeInfo = 210;
  649. /// <summary>(255) Event Join: someone joined the game. The new actorNumber is provided as well as the properties of that actor (if set in OpJoin).</summary>
  650. public const byte Join = (byte)LiteEventCode.Join;
  651. /// <summary>(254) Event Leave: The player who left the game can be identified by the actorNumber.</summary>
  652. public const byte Leave = (byte)LiteEventCode.Leave;
  653. /// <summary>(253) When you call OpSetProperties with the broadcast option "on", this event is fired. It contains the properties being set.</summary>
  654. public const byte PropertiesChanged = (byte)LiteEventCode.PropertiesChanged;
  655. /// <summary>(253) When you call OpSetProperties with the broadcast option "on", this event is fired. It contains the properties being set.</summary>
  656. [Obsolete("Use PropertiesChanged now.")]
  657. public const byte SetProperties = (byte)LiteEventCode.PropertiesChanged;
  658. }
  659.  
  660. /// <summary>
  661. /// Class for constants. Codes for parameters of Operations and Events.
  662. /// Pun uses these constants internally.
  663. /// </summary>
  664. public class ParameterCode
  665. {
  666. /// <summary>(237) Optional parameter to suppress events Join and Leave for a room (which might be used as lobby/chat room then).</summary>
  667. public const byte SuppressRoomEvents = 237;
  668. /// <summary>(234) Optional parameter of OpRaiseEvent to forward the event to some web-service.</summary>
  669. public const byte EventForward = 234;
  670. /// <summary>(230) Address of a (game) server to use.</summary>
  671. public const byte Address = 230;
  672. /// <summary>(229) Count of players in rooms (connected to game servers for this application, used in stats event)</summary>
  673. public const byte PeerCount = 229;
  674. /// <summary>(228) Count of games in this application (used in stats event)</summary>
  675. public const byte GameCount = 228;
  676. /// <summary>(227) Count of players on the master server (connected to master server for this application, looking for games, used in stats event)</summary>
  677. public const byte MasterPeerCount = 227;
  678. /// <summary>(225) User's ID</summary>
  679. public const byte UserId = 225;
  680. /// <summary>(224) Your application's ID: a name on your own Photon or a GUID on the Photon Cloud</summary>
  681. public const byte ApplicationId = 224;
  682. /// <summary>(223) Not used (as "Position" currently). If you get queued before connect, this is your position</summary>
  683. public const byte Position = 223;
  684. /// <summary>(223) Modifies the matchmaking algorithm used for OpJoinRandom. Allowed parameter values are defined in enum MatchmakingMode.</summary>
  685. public const byte MatchMakingType = 223;
  686. /// <summary>(222) List of RoomInfos about open / listed rooms</summary>
  687. public const byte GameList = 222;
  688. /// <summary>(221) Internally used to establish encryption</summary>
  689. public const byte Secret = 221;
  690. /// <summary>(220) Version of your application</summary>
  691. public const byte AppVersion = 220;
  692.  
  693. // Codes for Azure / TCP Proxy are removed
  694.  
  695. /// <summary>(255) Code for the gameId/roomName (a unique name per room). Used in OpJoin and similar.</summary>
  696. public const byte RoomName = (byte)LiteOpKey.GameId;
  697. /// <summary>(250) Code for broadcast parameter of OpSetProperties method.</summary>
  698. public const byte Broadcast = (byte)LiteOpKey.Broadcast;
  699. /// <summary>(252) Code for list of players in a room. Currently not used.</summary>
  700. public const byte ActorList = (byte)LiteOpKey.ActorList;
  701. /// <summary>(254) Code of the Actor of an operation. Used for property get and set.</summary>
  702. public const byte ActorNr = (byte)LiteOpKey.ActorNr;
  703. /// <summary>(249) Code for property set (Hashtable).</summary>
  704. public const byte PlayerProperties = (byte)LiteOpKey.ActorProperties;
  705. /// <summary>(245) Code of data/custom content of an event. Used in OpRaiseEvent.</summary>
  706. public const byte CustomEventContent = (byte)LiteOpKey.Data;
  707. /// <summary>(245) Code of data of an event. Used in OpRaiseEvent.</summary>
  708. public const byte Data = (byte)LiteOpKey.Data;
  709. /// <summary>(244) Code used when sending some code-related parameter, like OpRaiseEvent's event-code.</summary>
  710. /// <remarks>This is not the same as the Operation's code, which is no longer sent as part of the parameter Dictionary in Photon 3.</remarks>
  711. public const byte Code = (byte)LiteOpKey.Code;
  712. /// <summary>(248) Code for property set (Hashtable).</summary>
  713. public const byte GameProperties = (byte)LiteOpKey.GameProperties;
  714. /// <summary>
  715. /// (251) Code for property-set (Hashtable). This key is used when sending only one set of properties.
  716. /// If either ActorProperties or GameProperties are used (or both), check those keys.
  717. /// </summary>
  718. public const byte Properties = (byte)LiteOpKey.Properties;
  719. /// <summary>(253) Code of the target Actor of an operation. Used for property set. Is 0 for game</summary>
  720. public const byte TargetActorNr = (byte)LiteOpKey.TargetActorNr;
  721. /// <summary>(246) Code to select the receivers of events (used in Lite, Operation RaiseEvent).</summary>
  722. public const byte ReceiverGroup = (byte)LiteOpKey.ReceiverGroup;
  723. /// <summary>(247) Code for caching events while raising them.</summary>
  724. public const byte Cache = (byte)LiteOpKey.Cache;
  725. /// <summary>(241) Bool parameter of CreateGame Operation. If true, server cleans up roomcache of leaving players (their cached events get removed).</summary>
  726. public const byte CleanupCacheOnLeave = (byte)241;
  727.  
  728. /// <summary>(240) Code for "group" operation-parameter (as used in Op RaiseEvent).</summary>
  729. public const byte Group = LiteOpKey.Group;
  730. /// <summary>(239) The "Remove" operation-parameter can be used to remove something from a list. E.g. remove groups from player's interest groups.</summary>
  731. public const byte Remove = LiteOpKey.Remove;
  732. /// <summary>(238) The "Add" operation-parameter can be used to add something to some list or set. E.g. add groups to player's interest groups.</summary>
  733. public const byte Add = LiteOpKey.Add;
  734.  
  735. /// <summary>(217) This key's (byte) value defines the target custom authentication type/service the client connects with. Used in OpAuthenticate</summary>
  736. public const byte ClientAuthenticationType = 217;
  737.  
  738. /// <summary>(216) This key's (string) value provides parameters sent to the custom authentication type/service the client connects with. Used in OpAuthenticate</summary>
  739. public const byte ClientAuthenticationParams = 216;
  740.  
  741. /// <summary>(215) Makes the server create a room if it doesn't exist. OpJoin uses this to always enter a room, unless it exists and is full/closed.</summary>
  742. public const byte CreateIfNotExists = 215;
  743.  
  744. /// <summary>(214) This key's (string or byte[]) value provides parameters sent to the custom authentication service setup in Photon Dashboard. Used in OpAuthenticate</summary>
  745. public const byte ClientAuthenticationData = 214;
  746.  
  747. /// <summary>(213) Used in matchmaking-related methods and when creating a room to name a lobby (to join or to attach a room to).</summary>
  748. public const byte LobbyName = (byte)213;
  749.  
  750. /// <summary>(212) Used in matchmaking-related methods and when creating a room to define the type of a lobby. Combined with the lobby name this identifies the lobby.</summary>
  751. public const byte LobbyType = (byte)212;
  752.  
  753. /// <summary>(211) This (optional) parameter can be sent in Op Authenticate to turn on Lobby Stats (info about lobby names and their user- and game-counts). See: PhotonNetwork.Lobbies</summary>
  754. public const byte LobbyStats = (byte)211;
  755.  
  756. /// <summary>(1) Used in Op FindFriends request. Value must be string[] of friends to look up.</summary>
  757. public const byte FindFriendsRequestList = (byte)1;
  758.  
  759. /// <summary>(1) Used in Op FindFriends response. Contains bool[] list of online states (false if not online).</summary>
  760. public const byte FindFriendsResponseOnlineList = (byte)1;
  761.  
  762. /// <summary>(2) Used in Op FindFriends response. Contains string[] of room names ("" where not known or no room joined).</summary>
  763. public const byte FindFriendsResponseRoomIdList = (byte)2;
  764.  
  765. /// <summary>(210) Used for region values in OpAuth and OpGetRegions.</summary>
  766. public const byte Region = (byte)210;
  767. }
  768.  
  769. /// <summary>
  770. /// Class for constants. Contains operation codes.
  771. /// Pun uses these constants internally.
  772. /// </summary>
  773. public class OperationCode
  774. {
  775. /// <summary>(230) Authenticates this peer and connects to a virtual application</summary>
  776. public const byte Authenticate = 230;
  777. /// <summary>(229) Joins lobby (on master)</summary>
  778. public const byte JoinLobby = 229;
  779. /// <summary>(228) Leaves lobby (on master)</summary>
  780. public const byte LeaveLobby = 228;
  781. /// <summary>(227) Creates a game (or fails if name exists)</summary>
  782. public const byte CreateGame = 227;
  783. /// <summary>(226) Join game (by name)</summary>
  784. public const byte JoinGame = 226;
  785. /// <summary>(225) Joins random game (on master)</summary>
  786. public const byte JoinRandomGame = 225;
  787.  
  788. // public const byte CancelJoinRandom = 224; // obsolete, cause JoinRandom no longer is a "process". now provides result immediately
  789.  
  790. /// <summary>(254) Code for OpLeave, to get out of a room.</summary>
  791. public const byte Leave = (byte)LiteOpCode.Leave;
  792. /// <summary>(253) Raise event (in a room, for other actors/players)</summary>
  793. public const byte RaiseEvent = (byte)LiteOpCode.RaiseEvent;
  794. /// <summary>(252) Set Properties (of room or actor/player)</summary>
  795. public const byte SetProperties = (byte)LiteOpCode.SetProperties;
  796. /// <summary>(251) Get Properties</summary>
  797. public const byte GetProperties = (byte)LiteOpCode.GetProperties;
  798.  
  799. /// <summary>(248) Operation code to change interest groups in Rooms (Lite application and extending ones).</summary>
  800. public const byte ChangeGroups = (byte)LiteOpCode.ChangeGroups;
  801.  
  802. /// <summary>(222) Request the rooms and online status for a list of friends (by name, which should be unique).</summary>
  803. public const byte FindFriends = 222;
  804.  
  805. /// <summary>(221) Request statistics about a specific list of lobbies (their user and game count).</summary>
  806. public const byte GetLobbyStats = 221;
  807.  
  808. /// <summary>(220) Get list of regional servers from a NameServer.</summary>
  809. public const byte GetRegions = 220;
  810. }
  811.  
  812. /// <summary>
  813. /// Options for matchmaking rules for OpJoinRandom.
  814. /// </summary>
  815. public enum MatchmakingMode : byte
  816. {
  817. /// <summary>Fills up rooms (oldest first) to get players together as fast as possible. Default.</summary>
  818. /// <remarks>Makes most sense with MaxPlayers > 0 and games that can only start with more players.</remarks>
  819. FillRoom = 0,
  820. /// <summary>Distributes players across available rooms sequentially but takes filter into account. Without filter, rooms get players evenly distributed.</summary>
  821. SerialMatching = 1,
  822. /// <summary>Joins a (fully) random room. Expected properties must match but aside from this, any available room might be selected.</summary>
  823. RandomMatching = 2
  824. }
  825.  
  826. /// <summary>
  827. /// Options for optional "Custom Authentication" services used with Photon. Used by OpAuthenticate after connecting to Photon.
  828. /// </summary>
  829. public enum CustomAuthenticationType : byte
  830. {
  831. /// <summary>Use a custom authentification service. Currently the only implemented option.</summary>
  832. Custom = 0,
  833.  
  834. /// <summary>Authenticates users by their Steam Account. Set auth values accordingly!</summary>
  835. Steam = 1,
  836.  
  837. /// <summary>Authenticates users by their Facebook Account. Set auth values accordingly!</summary>
  838. Facebook = 2,
  839.  
  840. /// <summary>Disables custom authentification. Same as not providing any AuthenticationValues for connect (more precisely for: OpAuthenticate).</summary>
  841. None = byte.MaxValue
  842. }
  843.  
  844. /// <summary>
  845. /// Container for "Custom Authentication" values in Photon (default: user and token). Set AuthParameters before connecting - all else is handled.
  846. /// </summary>
  847. /// <remarks>
  848. /// Custom Authentication lets you verify end-users by some kind of login or token. It sends those
  849. /// values to Photon which will verify them before granting access or disconnecting the client.
  850. ///
  851. /// The Photon Cloud Dashboard will let you enable this feature and set important server values for it.
  852. /// https://cloud.exitgames.com/dashboard
  853. /// </remarks>
  854. public class AuthenticationValues
  855. {
  856. /// <summary>The type of custom authentication provider that should be used. Currently only "Custom" or "None" (turns this off).</summary>
  857. public CustomAuthenticationType AuthType = CustomAuthenticationType.Custom;
  858.  
  859. /// <summary>This string must contain any (http get) parameters expected by the used authentication service. By default, username and token.</summary>
  860. /// <remarks>Standard http get parameters are used here and passed on to the service that's defined in the server (Photon Cloud Dashboard).</remarks>
  861. public string AuthParameters; // { get { return a; } set { a = value; UnityEngine.Debug.LogWarning("AuthParameters set: " + value + " server: " + PhotonNetwork.ServerAddress); } }
  862. //private string a;
  863.  
  864. /// <summary>After initial authentication, Photon provides a secret for this client / user, which is subsequently used as (cached) validation.</summary>
  865. public string Secret; // { get { return s; } set { s = value; UnityEngine.Debug.LogWarning("Secret set: " + value + " server: " + PhotonNetwork.ServerAddress); } }
  866. //private string s;
  867.  
  868. /// <summary>Data to be passed-on to the auth service via POST. Default: null (not sent). Either string or byte[] (see setters).</summary>
  869. public object AuthPostData { get; private set; }
  870.  
  871. /// <summary>Sets the data to be passed-on to the auth service via POST.</summary>
  872. /// <param name="byteData">Binary token / auth-data to pass on. Empty string will set AuthPostData to null.</param>
  873. public virtual void SetAuthPostData(string stringData)
  874. {
  875. this.AuthPostData = (string.IsNullOrEmpty(stringData)) ? null : stringData;
  876. }
  877.  
  878. /// <summary>Sets the data to be passed-on to the auth service via POST.</summary>
  879. /// <param name="byteData">Binary token / auth-data to pass on.</param>
  880. public virtual void SetAuthPostData(byte[] byteData)
  881. {
  882. this.AuthPostData = byteData;
  883. }
  884.  
  885. /// <summary>Creates the default parameter string from a user and token value, escaping both. Alternatively set AuthParameters yourself.</summary>
  886. /// <remarks>The default parameter string is: "username={user}&token={token}"</remarks>
  887. /// <param name="user">Name or other end-user ID used in custom authentication service.</param>
  888. /// <param name="token">Token provided by authentication service to be used on initial "login" to Photon.</param>
  889. public virtual void SetAuthParameters(string user, string token)
  890. {
  891. this.AuthParameters = "username=" + System.Uri.EscapeDataString(user) + "&token=" + System.Uri.EscapeDataString(token);
  892. }
  893.  
  894. public override string ToString()
  895. {
  896. return AuthParameters + " s: " + Secret;
  897. }
  898. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement