back_recharge
parent
82aeab166e
commit
60bd86264b
|
@ -1,8 +1,10 @@
|
|||
package com.ljsd.common.mogodb;
|
||||
|
||||
import com.ljsd.common.mogodb.util.BlockingUniqueQueue;
|
||||
import com.mongodb.BasicDBObject;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
public class MongoUpdateCache {
|
||||
static class UpdateRequest {
|
||||
|
@ -21,7 +23,7 @@ public class MongoUpdateCache {
|
|||
|
||||
//key:String, collection + ":" + id
|
||||
private final Map<String, Map<String, UpdateRequest>> requestMap = new HashMap<>();
|
||||
|
||||
static final BlockingQueue<Map<String, Map<String, UpdateRequest>>> logQueue = new BlockingUniqueQueue<>();
|
||||
|
||||
protected void addUpdateRequest(MongoBase mongoBase, String fullKey, Object value, int opCode) {
|
||||
String key = mongoBase.getRootCollection() + ":" + mongoBase.getRootId();
|
||||
|
|
|
@ -0,0 +1,114 @@
|
|||
package com.ljsd.common.mogodb.util;
|
||||
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
/**
|
||||
* LinkedBlockingQueue的增强版(队伍内未消费的元素保证是不重复的)
|
||||
*/
|
||||
public class BlockingUniqueQueue<E> extends LinkedBlockingQueue<E> {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(BlockingUniqueQueue.class);
|
||||
private static final long serialVersionUID = 8351632564634804122L;
|
||||
|
||||
private Set<E> datas = new HashSet<>();
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return datas.contains(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> c) {
|
||||
return datas.containsAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public E take() {
|
||||
//该方法千万不要用 synchronized 修饰,不然一旦take()方法被阻塞了,另外一条线程就永远不能add了
|
||||
E head = null;
|
||||
try{
|
||||
head = super.take();
|
||||
}catch(Exception e){
|
||||
LOGGER.error("take->uid={},emsg={}", GlobalData.getLocalUserId(), e.getMessage(), e);
|
||||
}
|
||||
datas.remove(head);
|
||||
return head;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean add(E e) {
|
||||
if (contains(e)) {
|
||||
return false;
|
||||
}
|
||||
return super.add(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean addAll(Collection<? extends E> c) {
|
||||
boolean modified = false;
|
||||
for (E e : c) {
|
||||
if (add(e)) {
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean remove(Object o) {
|
||||
datas.remove(o);
|
||||
return super.remove(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean removeAll(Collection<?> c) {
|
||||
for (Object e:c) {
|
||||
datas.remove(e);
|
||||
}
|
||||
return super.removeAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return new Itr(super.iterator());
|
||||
}
|
||||
|
||||
private class Itr implements Iterator<E> {
|
||||
|
||||
private Iterator<E> it;
|
||||
|
||||
private E curr;
|
||||
|
||||
Itr(Iterator<E> it) {
|
||||
this.it = it;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return this.it.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
this.curr = this.it.next();
|
||||
return this.curr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
synchronized(BlockingUniqueQueue.this) {
|
||||
this.it.remove();
|
||||
BlockingUniqueQueue.this.datas.remove(this.curr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.ljsd.common.mogodb.util;
|
||||
|
||||
public class GlobalData {
|
||||
public static final int LOG_INTEVAL_TIME = 60*5*1000; //日志输出间隔,以便确定线程还正常
|
||||
|
||||
private static final ThreadLocal<Integer> threadLocalUid = new ThreadLocal<Integer>();
|
||||
|
||||
public static void setLocalUserId(int uid) {
|
||||
threadLocalUid.set(uid);
|
||||
}
|
||||
|
||||
public static int getLocalUserId() {
|
||||
Integer uid = threadLocalUid.get();
|
||||
if(uid == null){
|
||||
return -1;
|
||||
}
|
||||
return uid;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.ljsd.jieling.handler.friend;
|
||||
|
||||
import com.ljsd.jieling.handler.BaseHandler;
|
||||
import com.ljsd.jieling.logic.friend.FriendLogic;
|
||||
import com.ljsd.jieling.netty.cocdex.PacketNetData;
|
||||
import com.ljsd.jieling.network.session.ISession;
|
||||
import com.ljsd.jieling.protocols.MessageTypeProto;
|
||||
import com.ljsd.jieling.protocols.PlayerInfoProto;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Component
|
||||
public class AddFriendHandler extends BaseHandler {
|
||||
@Override
|
||||
public MessageTypeProto.MessageType getMessageCode() {
|
||||
return MessageTypeProto.MessageType.FRIEND_INVITE_REQUEST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(ISession iSession, PacketNetData netData) throws Exception {
|
||||
PlayerInfoProto.InviteFriendRequest inviteFriendRequest = PlayerInfoProto.InviteFriendRequest.parseFrom(netData.parseClientProtoNetData());
|
||||
List<Integer> inviteUidsList = inviteFriendRequest.getInviteUidsList();
|
||||
FriendLogic.getInstance().sendApply(iSession,inviteUidsList);
|
||||
}
|
||||
}
|
|
@ -1,19 +1,16 @@
|
|||
package com.ljsd.jieling.handler.friend;
|
||||
|
||||
import com.ljsd.jieling.handler.BaseHandler;
|
||||
import com.ljsd.jieling.logic.arena.ArenaLogic;
|
||||
import com.ljsd.jieling.logic.friend.FriendLogic;
|
||||
import com.ljsd.jieling.netty.cocdex.PacketNetData;
|
||||
import com.ljsd.jieling.network.session.ISession;
|
||||
import com.ljsd.jieling.protocols.ArenaInfoProto;
|
||||
import com.ljsd.jieling.protocols.ChatProto;
|
||||
import com.ljsd.jieling.protocols.MessageTypeProto;
|
||||
import com.ljsd.jieling.protocols.PlayerInfoProto;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
@Component
|
||||
public class getFriendInfoHandler extends BaseHandler {
|
||||
public class GetFriendInfoHandler extends BaseHandler {
|
||||
@Override
|
||||
public MessageTypeProto.MessageType getMessageCode() {
|
||||
return MessageTypeProto.MessageType.GET_FRIEND_INFO_REQUEST;
|
|
@ -1,13 +1,18 @@
|
|||
package com.ljsd.jieling.logic.dao;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import com.ljsd.common.mogodb.MongoBase;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
public class FriendManager extends MongoBase {
|
||||
private int uid;
|
||||
|
||||
private List<Integer> friends;
|
||||
private List<Integer> friends = new CopyOnWriteArrayList<>();
|
||||
|
||||
private Set<Integer> applyFriends = Sets.newConcurrentHashSet(); // 申请列表信息
|
||||
|
||||
public int getUid() {
|
||||
return uid;
|
||||
|
@ -25,4 +30,13 @@ public class FriendManager extends MongoBase {
|
|||
updateString("friends", friends);
|
||||
this.friends = friends;
|
||||
}
|
||||
|
||||
public Set<Integer> getApplyFriends() {
|
||||
return applyFriends;
|
||||
}
|
||||
|
||||
public void setApplyFriends(Set<Integer> applyFriends) {
|
||||
updateString("applyFriends", applyFriends);
|
||||
this.applyFriends = applyFriends;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ import com.ljsd.jieling.util.MathUtils;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public class PlayerManager extends MongoBase {
|
||||
|
||||
private String openId;
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
package com.ljsd.jieling.logic.friend;
|
||||
|
||||
import com.ljsd.jieling.db.mongo.MongoUtil;
|
||||
import com.ljsd.jieling.logic.OnlineUserManager;
|
||||
import com.ljsd.jieling.logic.dao.FriendManager;
|
||||
import com.ljsd.jieling.logic.dao.PlayerManager;
|
||||
import com.ljsd.jieling.logic.dao.RecommendFriend;
|
||||
import com.ljsd.jieling.logic.dao.UserManager;
|
||||
import com.ljsd.jieling.logic.dao.root.User;
|
||||
import com.ljsd.jieling.network.session.ISession;
|
||||
import com.ljsd.jieling.protocols.ChatProto;
|
||||
import com.ljsd.jieling.protocols.CommonProto;
|
||||
import com.ljsd.jieling.protocols.MessageTypeProto;
|
||||
import com.ljsd.jieling.protocols.PlayerInfoProto;
|
||||
|
@ -120,6 +118,48 @@ public class FriendLogic {
|
|||
*/
|
||||
public void getApplyFriends(ISession iSession) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 添加好友发送申请
|
||||
* @param iSession
|
||||
* @param inviteUidsList
|
||||
*/
|
||||
public void sendApply(ISession iSession, List<Integer> inviteUidsList) throws Exception {
|
||||
int msgId = MessageTypeProto.MessageType.FRIEND_INVITE_RESPONSE_VALUE;
|
||||
int uid = iSession.getUid();
|
||||
if (inviteUidsList.size() ==0){
|
||||
MessageUtil.sendErrorResponse(iSession,0,msgId,"没有申请的好友");
|
||||
}
|
||||
User user = UserManager.getUser(uid);
|
||||
List<Integer> friends = user.getFriendManager().getFriends();
|
||||
int maxFriends = 200;
|
||||
if (friends.size() >= maxFriends){
|
||||
MessageUtil.sendErrorResponse(iSession,0,msgId,"好友已达上限");
|
||||
}
|
||||
for (Integer inviteUid : inviteUidsList){
|
||||
if (inviteUid == uid){
|
||||
continue;
|
||||
}
|
||||
if (friends.contains(inviteUid)){
|
||||
continue;
|
||||
}
|
||||
User inviteUser = UserManager.getUser(inviteUid);
|
||||
if (inviteUser == null){
|
||||
continue;
|
||||
}
|
||||
FriendManager friendManager = inviteUser.getFriendManager();
|
||||
List<Integer> friends1 = friendManager.getFriends();
|
||||
Set<Integer> applyFriends = friendManager.getApplyFriends();
|
||||
if (applyFriends.contains(uid)){
|
||||
continue;
|
||||
}
|
||||
if (friends1.size() >= maxFriends){
|
||||
continue;
|
||||
}
|
||||
applyFriends.add(uid);
|
||||
}
|
||||
MessageUtil.sendMessage(iSession, 1, msgId, null, true);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue