public static void OnOtherPlayerJoinedRoom(PlayerInRoom joinedPlayer) { foreach (var obs in _observers) { obs.OnOtherPlayerJoinedRoom(joinedPlayer); } }
/// <summary> /// Handles SevrerPacketId.RoomData /// </summary> private static void Packet_RoomData(byte[] data) { ByteBuffer buffer = new ByteBuffer(); buffer.WriteBytes(data); //Read packet id long packetId = buffer.ReadLong(); //Read maxPlayers int maxPlayers = buffer.ReadInteger(); //Init player array StaticRoomData.Players = new PlayerInRoom[maxPlayers]; StaticRoomData.MaxPlayers = maxPlayers; //Init slots dict Dictionary <int, long> slots = new Dictionary <int, long>(); //Read players count int playersCount = buffer.ReadInteger(); // Read players for (int i = 0; i < playersCount; i++) { //Read player id long playerId = buffer.ReadLong(); PlayerInRoom player = new PlayerInRoom(playerId); //Read slots number int slotN = buffer.ReadInteger(); player.SlotN = slotN; slots.Add(slotN, playerId); //if that part of data is about out player then set InRoomSlotNumber to read value if (playerId == FoolNetwork.LocalPlayer.ConnectionId) { FoolNetwork.LocalPlayer.InRoomSlotNumber = slotN; } //read player nickname player.Nickname = buffer.ReadStringUnicode(); //read player avatar url player.AvatarFile = buffer.ReadString(); StaticRoomData.Players[i] = player; } StaticRoomData.ConnectedPlayersCount = playersCount; StaticRoomData.OccupiedSlots = slots; //Invoke callback on observers FoolObservable.OnRoomDataUpdated(); }
/// <summary> /// 交换两个位置上的玩家/AI/空位 /// </summary> /// <param name="From">位置1</param> /// <param name="To">位置2</param> public void MovePlayer(int From, int To) { if (0 <= From && From < Capacity && 0 <= To && To < Capacity) { lock (PlayerList) { PlayerInRoom Temp = PlayerList[From]; PlayerList[From] = PlayerList[To]; PlayerList[To] = Temp; } } }
/// <summary> /// Replace slot with prefab of Occupied slot /// </summary> private void SetSlotAsOccupied(PlayerInRoom player) { int slotN = player.SlotN; SlotsScripts[slotN].DrawPlayerslot(StaticRoomData.Players[slotN]); if (idsSlots.ContainsKey(player.ConnectionId)) { idsSlots[player.ConnectionId] = SlotsScripts[slotN]; } else { idsSlots.Add(player.ConnectionId, SlotsScripts[slotN]); } }
public void SetSit(PlayerInRoom player, GameObject obj, int i) { SetAddAIButtonActive(i, false); //playerID[i] = obj.GetPhotonView().ownerId; playerInRooms[i] = player; obj.transform.SetParent(playerSits[i].transform); obj.GetComponent <RectTransform>().anchoredPosition = Vector2.zero; obj.transform.localScale = Vector3.one; sitHasPlayer[i] = false; PhotonNetwork.room.SetCustomProperties(new ExitGames.Client.Photon.Hashtable(2) { { "Map", MapList[nowMapIndex] }, { "PlayerCount", playerCount } }); }
public void SetPlayerSit(PlayerInRoom player, GameObject obj) { CheckList(); for (int i = 0; i < sitHasPlayer.Length; i++) { if (sitHasPlayer[i]) { SetAddAIButtonActive(i, false); //playerID[i] = obj.GetPhotonView().ownerId; SetSit(player, obj, i); return; } } }
/// <summary> /// Handles SevrerPacketId.OtherPlayerJoinedRoom /// </summary> private static void Packet_OtherPlayerJoinedRoom(byte[] data) { ByteBuffer buffer = new ByteBuffer(); buffer.WriteBytes(data); //Skip packetId buffer.ReadLong(); //Read id of newly joined player long joinedId = buffer.ReadLong(); PlayerInRoom joinedPlayer = new PlayerInRoom(joinedId); //Read slotN of newly joined player joinedPlayer.SlotN = buffer.ReadInteger(); //read name joinedPlayer.Nickname = buffer.ReadStringUnicode(); //read avatar joinedPlayer.AvatarFile = buffer.ReadString(); StaticRoomData.ConnectedPlayersCount++; // add to room if (StaticRoomData.OccupiedSlots.TryGetValue(joinedPlayer.SlotN, out long n)) { StaticRoomData.OccupiedSlots[joinedPlayer.SlotN] = joinedId; } else // else create new slot values { StaticRoomData.OccupiedSlots.Add(joinedPlayer.SlotN, joinedId); } StaticRoomData.Players[joinedPlayer.SlotN] = joinedPlayer; //Observable - room data changed FoolObservable.OnRoomDataUpdated(); //Invoke callback on observers FoolObservable.OnOtherPlayerJoinedRoom(joinedPlayer); }
public void CheckList(PlayerInRoom pl) { if (playerInRooms == null) { playerInRooms = new PlayerInRoom[7]; } bool hasSit = false; int nowPlayerCount = 1; for (int i = 0; i < playerInRooms.Length; i++) { if (playerInRooms[i] == null || playerInRooms[i] == pl) { sitHasPlayer[i] = true; SetAddAIButtonActive(i, true); hasSit = true; } else { sitHasPlayer[i] = false; SetAddAIButtonActive(i, false); } } if (PhotonNetwork.isMasterClient) { if (hasSit) { PhotonNetwork.room.IsOpen = true; } else { PhotonNetwork.room.IsOpen = false; } playerCount = nowPlayerCount; PhotonNetwork.room.SetCustomProperties(new ExitGames.Client.Photon.Hashtable(2) { { "Map", MapList[nowMapIndex] }, { "PlayerCount", playerCount } }); } }
/// <summary> /// Observer method: On Other Player Joined Room /// </summary> public override void OnOtherPlayerJoinedRoom(PlayerInRoom joinedPlayer) { SetSlotAsOccupied(joinedPlayer); }
/// <summary> /// Called on somebody expect you joins room. Also OnRoomData called. /// </summary> public virtual void OnOtherPlayerJoinedRoom(PlayerInRoom joinedPlayer) { }
public override void OnOtherPlayerJoinedRoom(PlayerInRoom joinedPlayer) { CheckIfAllPlayersJoined(); }
public async Task <string> SearchRandomChessGame(int gameDuration, string playerId) { float RaW = 0, RaD = 0, RaL = 0, RbW = 0, RbD = 0, RbL = 0; var currentPlayer = await _playerService .GetAsync(Context.User.Identity.Name); var currentPlayerInRoom = new PlayerInRoom(currentPlayer, gameDuration, this.Context.ConnectionId, SearchingGameTypEnum.random); Console.WriteLine("--> Player " + currentPlayer.Username + "was added to room"); if (_chessGameService.CountOpponent(gameDuration, currentPlayer.RatingElo, SearchingGameTypEnum.random) == 0) { await _chessGameService.AddToWaitingList(currentPlayerInRoom); Console.WriteLine("--> " + currentPlayer.Username + "was added to waiting list"); return("Added to waiting list"); } else { var opponent = await _chessGameService .GetPlayerFromWaitingList(gameDuration, currentPlayer.RatingElo, SearchingGameTypEnum.random); Console.WriteLine("--> " + currentPlayer.Username + "found opponent " + opponent.Username); await _chessGameService .RemoveFromWaitingList(opponent); Console.WriteLine("--> Player " + opponent.Username + " was removed from waiting list"); var room = Room.CreateRoom(); Console.WriteLine("--> Create new room with id " + room.Id.ToString()); Task t1 = Groups.AddToGroupAsync(this.Context.ConnectionId, room.Id.ToString()); Task t2 = Groups.AddToGroupAsync(opponent.ConnectionId.ToString(), room.Id.ToString()); await _chessGameService.InitMatch(currentPlayerInRoom, opponent, room.Id.ToString()); t1.Wait(); t2.Wait(); _eloProvider.CalcELORating(currentPlayer.RatingElo, opponent.RatingElo, ChessGameResultEnum.WIN, ref RaW, ref RbL); _eloProvider.CalcELORating(currentPlayer.RatingElo, opponent.RatingElo, ChessGameResultEnum.DRAW, ref RaD, ref RbD); _eloProvider.CalcELORating(currentPlayer.RatingElo, opponent.RatingElo, ChessGameResultEnum.LOSE, ref RaL, ref RbW); await Clients.Client(this.Context.ConnectionId) .SendAsync("GetNewRatingELO", RaW, RaD, RaL); await Clients.Client(opponent.ConnectionId) .SendAsync("GetNewRatingELO", RbW, RbD, RbL); var currentPieceColor = await _chessGameService .GetRandomColorPiece(); var opponentPieceColor = currentPieceColor .Equals(ChessboardPieceColorEnum.white.ToString()) ? ChessboardPieceColorEnum.black.ToString() : ChessboardPieceColorEnum.white.ToString(); await Clients.Client(this.Context.ConnectionId) .SendAsync("GetColorPiece", currentPieceColor); await Clients.Client(opponent.ConnectionId) .SendAsync("GetColorPiece", opponentPieceColor); await Clients.Client(this.Context.ConnectionId) .SendAsync("GetOpponentInformation", opponent.Username, opponent.RatingElo); await Clients.Client(opponent.ConnectionId) .SendAsync("GetOpponentInformation", currentPlayer.Username, currentPlayer.RatingElo); await Clients.Group(room.Id.ToString()) .SendAsync("ReceiveRoom", room.Id.ToString()); return("Player found game"); } }
/// <summary> /// Callback on somebody enters room /// </summary> public override void OnOtherPlayerJoinedRoom(PlayerInRoom joinedPlayer) { PlayerNumberChanged(); }
/// <summary> /// 删除一个玩家并重置其位置 /// </summary> /// <param name="Index">目标座位的序号</param> public void RemovePlayer(int Index) { lock (PlayerList) { PlayerList[Index] = new PlayerInRoom(); } }