/// <summary> /// Initializes a new instance of the <see cref="DemoClient"/> class. /// </summary> public RobotClient(bool createGameLoopThread, int clientId, RoomShared roomShared = null) : base(ConnectionProtocol.Tcp) { this.roomShared = roomShared; this.clientId = clientId; this.MasterServerAddress = "192.168.2.202:4532"; //this.loadBalancingPeer.DebugOut = DebugLevel.INFO; //this.loadBalancingPeer.TrafficStatsEnabled = true; if (createGameLoopThread) { this.updateThread = new Thread(this.UpdateLoop); this.updateThread.IsBackground = true; this.updateThread.Start(); } this.NickName = "Player_" + this.clientId; this.LocalPlayer.SetCustomProperties(new Hashtable() { { "class", "tank" + (SupportClass.ThreadSafeRandom.Next() % 99) }, { "nickName", this.NickName } }); // insert your game's AppID (replace <your appid>). hosting yourself: use any name. using Photon Cloud: use your cloud subscription's appID this.AppId = "f0e09630-c30e-4d9d-8d60-64d91ebf642b"; this.AppVersion = "1.0"; // this demo uses the Name Server to connect to the EU region unless you set this.MasterServerAddress // setting a MasterServerAddress is required, when you host Photon Servers yourself (using Photon OnPremise) // this.MasterServerAddress = "<your server address>"; bool couldConnect = false; if (!string.IsNullOrEmpty(this.MasterServerAddress)) { couldConnect = this.Connect(); } else { couldConnect = this.ConnectToRegionMaster("eu"); } if (!couldConnect) { this.DebugReturn(DebugLevel.ERROR, "Can't connect to: " + this.CurrentServerAddress); } }
public override void OnEvent(EventData photonEvent) { switch (photonEvent.Code) { case EventCode.AppStats: break; case EventCode.LobbyStats: this.RoomInfoList.Keys.ToList().ForEach(roomName => { var roomProp = this.RoomInfoList[roomName] as RoomInfo; var playerCount = roomProp.PlayerCount; var maxPlayers = roomProp.MaxPlayers; var isOpen = roomProp.IsOpen; if (isOpen && playerCount < maxPlayers) { RoomShared roomShared = new RoomShared(); for (int i = playerCount, clientId = 0; i < maxPlayers; ++i, ++clientId) { RobotClient client = new RobotClient(true, clientId, roomShared); client.OpJoinRoom(roomName); } } }); break; case EventCode.GameListUpdate: // todo: Enter a room automatically. if (photonEvent.Parameters[ParameterCode.GameList] is Hashtable) { var hashtable = photonEvent.Parameters[ParameterCode.GameList] as Hashtable; this.RoomInfoList.Keys.ToList().ForEach(roomName => { var roomNames = hashtable.Keys.OfType <string>(); if (roomNames.Count() > 0 && roomNames.Contains(roomName)) { var roomProp = hashtable[roomName] as Hashtable; var playerCount = Convert.ToInt32(roomProp[GamePropertyKey.PlayerCount]); var maxPlayers = Convert.ToInt32(roomProp[GamePropertyKey.MaxPlayers]); var isOpen = Convert.ToBoolean(roomProp[GamePropertyKey.IsOpen]); if (isOpen && playerCount < maxPlayers) { RoomShared roomShared = new RoomShared(); for (int i = playerCount, clientId = 0; i < maxPlayers; ++i, ++clientId) { RobotClient client = new RobotClient(true, clientId, roomShared); client.OnStateChangeAction += (ClientState obj) => { if (obj == ClientState.JoinedLobby) { client.OpJoinRoom(roomName); } }; } } } }); } break; } base.OnEvent(photonEvent); }