private void ProcessJoinResult(JoinRoomResult result, MatchMakingPlayer oldestPlayer) { switch (result.Result) { case RoomOperationResult.OK: foreach (var player in _matchmakingPlayers) { ProcessSuccess(player, result); } break; default: foreach (var player in _matchmakingPlayers) { ProcessFailed(player, result.Result.ToString()); } break; } _matchmakingPlayers.Clear(); if (oldestPlayer != null) { TrackMmTime(oldestPlayer); } _isGroupWorking = false; }
public async Task CreateRoomNoServersTest() { _serverProvider = new FakeServerProvider(false, true); _roomManager = new RoomManager(_serverProvider, _logger, _taskSchedulerFactory, _roomApiProvider); var players = new Dictionary <Guid, Dictionary <byte, object> >(); var properties = new Dictionary <byte, object>(); var success = true; Exception ex = null; JoinRoomResult result = null; properties.Add(PropertyCode.RoomProperties.TotalPlayersNeeded, 3); try { result = await _roomManager.CreateRoom(_group1Id, players, properties); } catch (Exception e) { success = false; ex = e; } Assert.IsNotNull(result); Assert.AreEqual(result.Result, RoomOperationResult.ServerNotFound); Assert.AreEqual(true, success); Assert.AreEqual(null, ex); Assert.AreEqual(0, _roomManager.GetRoomsCount()); }
private void PlayerJoinRoom(IHanabiPlayer player, RequestJoinRoom request) { RoomInfo roomInfo = new RoomInfo(); JoinRoomResult result = JoinRoomResult.Fail; if (player.RoomStatus == PlayerRoomStatus.Idle) { RoomIndexType roomIndex = new RoomIndexType(request.RoomIndex); if (Rooms.ContainsKey(roomIndex)) { Room room = Rooms[roomIndex]; if (room.Players.Count < room.Setting.MaxPlayers) { room.Players.Add(player); roomInfo = room.Info; player.OnJoinRoom(room); result = JoinRoomResult.Success; } } } ResponseJoinRoom response = new ResponseJoinRoom(); response.Result = result; response.Room = roomInfo; SendCommand(player, ActionType.JoinRoom, response); }
public override void OnReceive(object session, Stream stream) { this.Result = (JoinRoomResult)stream.ReadInt(); this.Room = stream.ReadObject <GameRoom>(); this.SelfPlayer = stream.ReadObject <RoomPlayer>(); this.OtherPlayers = stream.ReadObject <List <object> >(); }
public void JoinRoom(string roomId) { JoinRoomResult result = _gameClient.Get <JoinRoomParameter, JoinRoomResult>(new JoinRoomParameter { RoomId = roomId }); _sessionId = result.SessionId; }
private void ProcessSuccess(MatchMakingPlayer player, JoinRoomResult result) { _logger.Debug($"Sending join info to {player.Id}"); var joinInfo = new JoinInfo(result.Address, result.Port, result.RoomId, JoinStatus.RoomIsReady, GetCurrentPlayersWeight(), _totalPlayersNeeded, true); _messageSender.Send(new JoinInfoEvent(joinInfo), player.Peer); _playersManager.Remove(player.Id); }
public async Task CreateRoomTest() { var players = new Dictionary <Guid, Dictionary <byte, object> > { { Guid.NewGuid(), new Dictionary <byte, object>() } }; var properties = new Dictionary <byte, object>(); var success = true; Exception ex = null; JoinRoomResult result = null; try { result = await _roomManager.CreateRoom(_group1Id, players, properties); } catch (Exception e) { success = false; ex = e; } Assert.IsNull(result); Assert.AreEqual(false, success); Assert.AreEqual("MatchMakingGroup ctr error: there is no TotalPlayersNeeded property", ex.Message); Assert.AreEqual(0, _roomManager.GetRoomsCount()); properties.Add(PropertyCode.RoomProperties.TotalPlayersNeeded, 3); success = true; ex = null; result = null; try { result = await _roomManager.CreateRoom(_group1Id, players, properties); } catch (Exception e) { success = false; ex = e; } Assert.IsNotNull(result); Assert.AreEqual(result.Result, RoomOperationResult.OK); Assert.AreEqual(result.Address, "0.0.0.0"); Assert.AreEqual(result.Port, 7777); Assert.AreEqual(true, success); Assert.AreEqual(null, ex); Assert.AreEqual(1, _roomManager.GetRoomsCount()); }
private bool VerifyJoinRoom(Command c, JoinRoomResult result, int userCount) { if (c.action != ActionType.JoinRoom.ToString()) { return(false); } ResponseJoinRoom response = JsonConvert.DeserializeObject <ResponseJoinRoom>(c.payload); if (response.result != result) { return(false); } return((response.result == JoinRoomResult.Fail) ? true : (response.room.players.Count == userCount)); }
private JoinInfo GetJoinInfo(JoinRoomResult joinResult) { var room = _roomManager.GetRoom(joinResult.RoomId); if (room == null) { throw new Exception($"CreateRoomFromClient error: room {joinResult.RoomId} is not exists"); } switch (joinResult.Result) { case RoomOperationResult.OK: return(new JoinInfo(joinResult.Address, joinResult.Port, joinResult.RoomId, JoinStatus.RoomIsReady, room.CurrentWeight, room.TotalWeightNeeded, true)); case RoomOperationResult.ServerNotFound: case RoomOperationResult.CreateRoomError: case RoomOperationResult.JoinRoomError: _logger.Error($"DirectJoin error: JoinResult = {joinResult.Result},"); return(new JoinInfo("", 0, Guid.Empty, JoinStatus.MatchMakingFailed, 0, 0)); default: throw new ArgumentException(); } }
public virtual void OnJoinRoomFailed(JoinRoomResult result) { }
public void HandleReceive() { lock (mutex) { do { if (offset < DataSizeOffset + DataSize) { break; } // 先读取包的size int packet_size = ByteOrderConverter.NetworkToHostOrder(BitConverter.ToInt32(buffer, DataSizeOffset)); int proto_total_len = packet_size + WrapperLen; if (offset < proto_total_len) { break; } // 读取包id int id = ByteOrderConverter.NetworkToHostOrder(BitConverter.ToInt16(buffer, HandlerIdOfffset)); // 处理具体的包协议 object proto_data = null; int proto_data_deserialize_offset = DataOffset; switch (id) { case ResponseId.JoinRoomResult: { proto_data = JoinRoomResult.Deserialize(buffer, ref proto_data_deserialize_offset); } break; case ResponseId.BattleStart: { proto_data = BattleStart.Deserialize(buffer, ref proto_data_deserialize_offset); } break; case ResponseId.Tick: { proto_data = TickList.Deserialize(buffer, ref proto_data_deserialize_offset); } break; default: break; } ; if (proto_data != null) { ProtoData data = new ProtoData(); data.id = id; data.proto = proto_data; proto_data_queue.Enqueue(data); } // 包体读取完毕 Array.Copy(buffer, proto_total_len, buffer, 0, offset - proto_total_len); offset = offset - proto_total_len; }while(true); } }
public override void OnReceive(object session, Stream stream) { this.Result = (JoinRoomResult)stream.ReadInt(); this.RoomServerHostPort = stream.ReadObject <HostPort>(); }
public void Start() { //schedule main task _mainTask = _taskScheduler.ScheduleOnInterval(async() => { _logger.Debug($"MmGroup tick"); if (_isGroupWorking) { return; } _isGroupWorking = true; try { lock (_queueSync) { var matchmakingPlayersCount = GetCurrentPlayersWeight(); var players = _playersManager.GetPlayers(Id, _totalPlayersNeeded - matchmakingPlayersCount, _maxMmWeight); foreach (var player in players) { _playersManager.SetOnMatchmaking(player.Id, true); AddPlayer(player); } matchmakingPlayersCount = GetCurrentPlayersWeight(); //if noone in collection - continue if (matchmakingPlayersCount == 0) { _isGroupWorking = false; return; } var oldestPlayer = GetOldestPlayer(); //try to add to existing room var room = _roomManager.GetRoom(Id, matchmakingPlayersCount, _matchmakingPlayers.Max(p => p.MmWeight), _matchmakingPlayers.Sum(p => p.MmWeight)); JoinRoomResult result = null; if (room != null) { //join to existing try { result = _roomManager.JoinRoom(room.Id, _matchmakingPlayers.ToDictionary(key => key.SessionId, value => value.Properties), _matchmakingPlayers.Max(p => p.MmWeight), _matchmakingPlayers.Sum(p => p.MmWeight)).Result; if (result.Result != RoomOperationResult.OK) { _logger.Error($"MM join room error (closing this room): {result.Result}"); room.UpdateState(RoomState.Closed); } } catch (Exception e) { // todo explore case _logger.Error($"MM join room error (closing this room): {e}"); room.UpdateState(RoomState.Closed); } } else if (EnoughWaiting(oldestPlayer) || IsEnoughPlayers()) { //players var playersDict = new Dictionary <Guid, Dictionary <byte, object> >(); foreach (var item in _matchmakingPlayers) { if (item != null && !playersDict.ContainsKey(item.SessionId)) { playersDict.TryAdd(item.SessionId, item.Properties); } } //add new room result = _roomManager.CreateRoom(Id, playersDict,//_matchmakingPlayers.ToDictionary(key => key.SessionId, value => value.Properties), RoomProperties).Result; } if (result != null) { ProcessJoinResult(result, oldestPlayer); } else { SendJoinInfoToCurrentMatchmakingGroup(); } } } catch (Exception ex) { _logger.Error($"MM tick error: {ex}"); } finally { _isGroupWorking = false; } }, 0, _matchMakingTickMs); }
public static JoinRoomResponseDto ToDto(this JoinRoomResult model) => new JoinRoomResponseDto { Success = model.Success };
public void MainThreadFunc(float delta_time) { // called every frame ProtoData proto_data = receiveBuffer.DequeueProtoData(); while (proto_data != null) { //Debug.Log("收到消息包 id : " + proto_data.id); switch (proto_data.id) { case ResponseId.JoinRoomResult: { JoinRoomResult result = proto_data.proto as JoinRoomResult; // 逻辑层更新 BL.BLTimelineController.Instance().OnJoinRoom(result.team_id); // 表现层更新 BattleField.battle_field.OnJoinRoom(result.team_id, result.team_id); } break; case ResponseId.BattleStart: { BattleStart result = proto_data.proto as BattleStart; BL.BLTimelineController.Instance().Start(); BattleFieldInputHandle.Instance().enabled = true; } break; case ResponseId.Tick: { TickList tick_list = proto_data.proto as TickList; //Debug.Log("收到同步帧 " + tick_list.frame); // 先把指令放入队列 for (int i = 0; i < tick_list.list.Count; ++i) { Tick tick = tick_list.list[i]; switch (tick.command_type) { case BL.TickCommandType.Move: { BL.BLIntVector3 dest_position; dest_position.x = tick.x; dest_position.y = 0; dest_position.z = tick.y; BL.BLCommandBase command = BL.BLCommandManager.Instance().CreateMove2PositionCommand(tick.cast_id, tick_list.frame, dest_position); BL.BLCommandManager.Instance().AddCommand(command.cast_frame, command); } break; case BL.TickCommandType.PUT_HERO: { BL.BLCommandBase command = BL.BLCommandManager.Instance().CreatePutHeroCommand(tick.team_id, tick_list.frame, tick.hero_index); BL.BLCommandManager.Instance().AddCommand(command.cast_frame, command); } break; case BL.TickCommandType.PURSUE_TARGET: { } break; } ; } // 时间轴控制进行一帧, 分发指令 BL.BLTimelineController.Instance().frame_received++; } break; default: break; } ; proto_data = receiveBuffer.DequeueProtoData(); } }