public virtual Task <PlayRoom> FindRoomMatchRequest(RoomJoinRequest joinRequest) { var room = Rooms.FirstOrDefault(r => { var matched = false; if (!string.IsNullOrEmpty(joinRequest.Name)) { matched = r.Id == joinRequest.Name; } if (joinRequest.ExpectedUsers != null && r.ExpectedMemberPeerIds != null) { matched = r.ExpectedMemberPeerIds.SequenceEqual(joinRequest.ExpectedUsers); } if (joinRequest.ExpectAttributes != null && r.CustomRoomProperties != null) { matched = r.CustomRoomProperties.ValueEquals(joinRequest.ExpectAttributes); } return(matched); }); return(Task.FromResult(room)); }
public virtual async Task <Tuple <PlayGameServer, PlayRoom> > MatchAsync(RoomJoinRequest joinRequest) { foreach (var gameServer in GameServers) { var room = await gameServer.FindRoomMatchRequest(joinRequest); if (room != null) { return(new Tuple <PlayGameServer, PlayRoom>(gameServer, room)); } } var createIfNotFound = joinRequest.CreateIfNotFound.HasValue && joinRequest.CreateIfNotFound.Value; if (createIfNotFound) { var randomGameServer = GameServers.RandomOne(); var room = await randomGameServer.CreateEmptyRoomAsync(new RoomConfig() { Name = joinRequest.Name }); return(new Tuple <PlayGameServer, PlayRoom>(randomGameServer, room)); } return(await RandomMatchAsync()); }
/// <summary> /// Join a room. For internal use by the Games SDK only. Calling this method directly is unsupported. /// Documentation https://developers.google.com/games/v1/reference/rooms/join /// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. /// </summary> /// <param name="service">Authenticated Games service.</param> /// <param name="roomId">The ID of the room.</param> /// <param name="body">A valid Games v1 body.</param> /// <param name="optional">Optional paramaters.</param> /// <returns>RoomResponse</returns> public static Room Join(GamesService service, string roomId, RoomJoinRequest body, RoomsJoinOptionalParms optional = null) { try { // Initial validation. if (service == null) { throw new ArgumentNullException("service"); } if (body == null) { throw new ArgumentNullException("body"); } if (roomId == null) { throw new ArgumentNullException(roomId); } // Building the initial request. var request = service.Rooms.Join(body, roomId); // Applying optional parameters to the request. request = (RoomsResource.JoinRequest)SampleHelpers.ApplyOptionalParms(request, optional); // Requesting data. return(request.Execute()); } catch (Exception ex) { throw new Exception("Request Rooms.Join failed.", ex); } }
internal void SocketRoomJoin(int roomId) { SocketConnectionManager.GetInstance.ConnectToSocket(() => { RoomJoinRequest socketRequest = new RoomJoinRequest { room_id = roomId, user_id = int.Parse(GameVariables.userId), access_token = GameVariables.AccessToken }; SocketConnectionManager.GetInstance.SendSocketRequest(GameVariables.SocketRequestType.roomJoin, socketRequest); Debug.Log(_shouldgetRoomStatus); Invoke(nameof(RoomStatus), 0.1f); }); }
/// <summary> /// Handles the room join request of a user and adds him to the room if the password is correct /// </summary> /// <param name="joinRequest"></param> /// <returns></returns> private async Task HandleRoomJoinRequest(RoomJoinRequest joinRequest) { //send the user the result of his request (helper method to avoid duplicate code9 async Task sendRequestResult(bool success) { //send the user a message back that the password was wrong if (_sockets.TryGetValue(joinRequest.SenderGuid, out ChatPartner user)) { joinRequest.IsSuccessful = success; string serialized = JsonConvert.SerializeObject(joinRequest, Global.SerializerSettings); await SendStringAsync(user.WebSocket, user.Identifier, serialized, CancellationToken.None); } } //check if the room exists and if the correct password is supplied (if the room is password protected) if (_chatrooms.TryGetValue(joinRequest.RoomIdentifier, out ChatRoom joinRoom)) { if ((!joinRoom.IsPasswordProtected || joinRoom.Password == joinRequest.Password) && !joinRoom.ConnectedUsers.Contains(joinRequest.SenderGuid)) { joinRoom.ConnectedUsers.Add(joinRequest.SenderGuid); var joinMessage = new UserJoinMessage() { SenderGuid = joinRequest.SenderGuid, RoomIdentifier = joinRequest.RoomIdentifier, Timestamp = DateTime.Now }; await sendRequestResult(true); await InformUsersAboutJoin(joinMessage, joinRoom); } else { await sendRequestResult(false); } } }
/// <summary> /// Handles the received message depending on its message type /// </summary> /// <param name="message"></param> /// <returns></returns> private async Task HandleMessage(string messageString, string socketId) { Message message = null; JObject jsonMessage = null; try { jsonMessage = JObject.Parse(messageString); message = JsonConvert.DeserializeObject <Message>(messageString); } catch { } //if it's a valid "Message", we will continue if (message != null) { //the user's identifier should always be saved in the senderGuid-property jsonMessage["senderGuid"] = socketId; switch (message.MessageType) { //as long as rooms are not implemented, we can broadcast the message to the whole server case MessageType.ChatMessage: ChatMessage chatMessage = jsonMessage.ToObject <ChatMessage>();; await HandleChatMessage(socketId, chatMessage.RoomIdentifier, chatMessage.Recipient, jsonMessage.ToString()); break; case MessageType.RoomCreationRequest: RoomCreationRequest roomRequest = jsonMessage.ToObject <RoomCreationRequest>(); //ensure that the sender is added to the room if (!roomRequest.RequestedRoom.ConnectedUsers.Any(x => x == roomRequest.SenderGuid)) { roomRequest.RequestedRoom.ConnectedUsers.Add(roomRequest.SenderGuid); } roomRequest.RequestedRoom = CreateNewChatRoom(roomRequest.RequestedRoom); await InviteUsersToRoom(roomRequest, true); break; case MessageType.NicknameRequest: NicknameRequest nameRequest = jsonMessage.ToObject <NicknameRequest>(); nameRequest.WasSuccessful = false; lock (_sockets) { if (IsNicknameAvailable(nameRequest.RequestedNickname)) { nameRequest.WasSuccessful = true; _sockets.FirstOrDefault(x => x.Key == socketId).Value.Nickname = nameRequest.RequestedNickname; } } if (nameRequest.WasSuccessful) { await SendMessageIntoRoom(_globalRoom, JsonConvert.SerializeObject(nameRequest, Global.SerializerSettings)); } break; case MessageType.RoomJoinRequest: RoomJoinRequest joinRequest = jsonMessage.ToObject <RoomJoinRequest>(); await HandleRoomJoinRequest(joinRequest); break; } } }