invite system start

This commit is contained in:
Michael Wain 2024-02-23 17:16:27 +03:00
parent 0ece55e87f
commit 9b6c3d525d
9 changed files with 74 additions and 6 deletions

View File

@ -57,7 +57,7 @@ public class LongPoll {
});
getLongPollingQueue().forEach(longPollingSession -> {
try {
if( !map.containsKey(longPollingSession.getUserId())) map.put(longPollingSession.getUserId(), new LongPollConfig(0L,new ArrayList<>(), 0, Hash.rnd(), new ArrayList<>(), System.currentTimeMillis()));
if( !map.containsKey(longPollingSession.getUserId())) map.put(longPollingSession.getUserId(), new LongPollConfig(0L,new ArrayList<>(), 0, Hash.rnd(), new ArrayList<>(), System.currentTimeMillis(), new ArrayList<>()));
LongPollConfig config = map.get(longPollingSession.getUserId());
LongPollResult result = process(longPollingSession.getUserId(), config);
if( !result.getRooms().isEmpty() )
@ -67,7 +67,7 @@ public class LongPoll {
config.setSession_pass(config.getSession_pass()+1);
if( !result.getFriends().isEmpty() || !result.getRooms().isEmpty() || !result.getMessages().isEmpty() || config.getSession_pass() >= iterations) {
if( !result.getInvites().isEmpty() || !result.getFriends().isEmpty() || !result.getRooms().isEmpty() || !result.getMessages().isEmpty() || config.getSession_pass() >= iterations) {
longPollingSession.getDeferredResult().setResult(result);
config.setSession_pass(0);
}
@ -196,7 +196,7 @@ public class LongPoll {
.collect(Collectors.toList());
}
return new LongPollResult(onlineCount, results, users, roomResults, friendsResult);
return new LongPollResult(onlineCount, results, users, roomResults, friendsResult, config.getInvites().stream().map(i -> new InviteResult(i.getRoomId(), i.getUserId(), i.getUsername())).collect(Collectors.toList()));
}
private Boolean isEqual(RoomResult r1, RoomResult r2) {

View File

@ -1,5 +1,6 @@
package com.alterdekim.game.component;
import com.alterdekim.game.dto.GameInvite;
import com.alterdekim.game.dto.RoomResult;
import com.alterdekim.game.dto.UserResult;
import lombok.AllArgsConstructor;
@ -20,4 +21,5 @@ public class LongPollConfig {
private String poll_token;
private List<UserResult> friends_online;
private Long lastRequest;
private List<GameInvite> invites;
}

View File

@ -51,6 +51,9 @@ public class APIController {
@Autowired
private LongPoll longPoll;
@Autowired
private FriendServiceImpl friendService;
@GetMapping("/api/v1/chat/history/{count}/")
public ResponseEntity<ChatResult> chatList(@PathVariable Integer count ) {
List<Chat> results = chatService.getLastChats(count);
@ -133,6 +136,25 @@ public class APIController {
return ResponseEntity.accepted().build();
}
@PostMapping("/api/v1/rooms/invite/")
public ResponseEntity<String> inviteToRoom( @RequestParam("friend_id") Long friend_id ) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Long userId = userService.findByUsername(((org.springframework.security.core.userdetails.User) auth.getPrincipal()).getUsername()).getId();
if( friendService.getFriendsOfUserId(userId).stream().anyMatch( p -> p.longValue() == friend_id.longValue()) &&
roomPlayerService.hasUserId(userId) != null) {
LongPollConfig config = longPoll.getMap().get(friend_id);
if( config != null ) {
List<GameInvite> l = config.getInvites();
Long roomId = roomPlayerService.hasUserId(userId);
l.add(new GameInvite(roomId, userId, ((org.springframework.security.core.userdetails.User) auth.getPrincipal()).getUsername()));
config.setInvites(l);
longPoll.getMap().put(friend_id, config);
return ResponseEntity.ok().build();
}
}
return ResponseEntity.badRequest().build();
}
@PostMapping("/async/notify/get/")
@ResponseBody
public DeferredResult<LongPollResult> getNotify(@RequestParam("last_chat_id") Long last_chat_id,
@ -152,7 +174,7 @@ public class APIController {
if( longPoll.getMap().containsKey(userId) ){
LongPollConfig c = longPoll.getMap().get(userId);
if( !c.getPoll_token().equals(poll_token) ) {
c = new LongPollConfig(last_chat_id, new ArrayList<>(), 0, poll_token, new ArrayList<>(), System.currentTimeMillis());
c = new LongPollConfig(last_chat_id, new ArrayList<>(), 0, poll_token, new ArrayList<>(), System.currentTimeMillis(), new ArrayList<>());
longPoll.getLongPollingQueue().removeIf(q -> q.getUserId().longValue() == userId.longValue());
}
c.setLast_chat_id(last_chat_id);
@ -160,7 +182,7 @@ public class APIController {
c.setLastRequest(System.currentTimeMillis());
longPoll.getMap().put(userId, c);
} else {
longPoll.getMap().put(userId, new LongPollConfig(last_chat_id, new ArrayList<>(), 0, poll_token, new ArrayList<>(), System.currentTimeMillis()));
longPoll.getMap().put(userId, new LongPollConfig(last_chat_id, new ArrayList<>(), 0, poll_token, new ArrayList<>(), System.currentTimeMillis(), new ArrayList<>()));
}
longPoll.getLongPollingQueue().add(new LongPollingSession(userId, deferredResult));
return deferredResult;

View File

@ -0,0 +1,14 @@
package com.alterdekim.game.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Getter
public class GameInvite {
private Long roomId;
private Long userId;
private String username;
}

View File

@ -0,0 +1,14 @@
package com.alterdekim.game.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Getter
public class InviteResult {
private Long roomId;
private Long userId;
private String username;
}

View File

@ -16,4 +16,5 @@ public class LongPollResult {
private List<UserResult> users;
private List<RoomResultV2> rooms;
private List<FriendResult> friends;
private List<InviteResult> invites;
}

View File

@ -21,4 +21,7 @@ public interface RoomPlayerRepository extends JpaRepository<RoomPlayer, Long> {
@Modifying
@Query(value = "DELETE FROM RoomPlayer r WHERE r.userId = :userId")
void deleteAllByUserId(@Param("userId") Long userId);
@Query(value = "SELECT r.roomId FROM RoomPlayer r WHERE r.userId = :userId ORDER BY r.roomId ASC LIMIT 1")
Long hasUserId(@Param("userId") Long userId);
}

View File

@ -32,4 +32,8 @@ public class RoomPlayerServiceImpl implements RoomPlayerService{
public void leaveByUserId(Long userId) {
repository.deleteAllByUserId(userId);
}
public Long hasUserId(Long userId) {
return repository.hasUserId(userId);
}
}

View File

@ -16,7 +16,15 @@ function createRoom() {
}
function sendInviteMessage(uid) {
alert(uid);
$.ajax({
url: "/api/v1/rooms/invite/",
method: "POST",
data: {
friend_id: uid
}
}).done(function(data) {
console.log(data);
});
}
function successPolling(data) {