Merge branch 'master' of http://60.1.1.230/backend/jieling_server
# Conflicts: # serverlogic/src/main/java/com/ljsd/jieling/logic/dao/User.java # serverlogic/src/main/java/com/ljsd/jieling/logic/dao/UserManager.java # serverlogic/src/main/java/com/ljsd/jieling/logic/map/MapLogic.java # serverlogic/src/main/java/com/ljsd/jieling/util/MessageUtil.javaback_recharge
commit
32155b1435
|
|
@ -0,0 +1,390 @@
|
||||||
|
package com.ljsd.common.mogodb;
|
||||||
|
|
||||||
|
|
||||||
|
import com.mongodb.BasicDBList;
|
||||||
|
import com.mongodb.BasicDBObject;
|
||||||
|
import com.mongodb.DBObject;
|
||||||
|
|
||||||
|
import java.lang.reflect.*;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
||||||
|
public class BeanUtil {
|
||||||
|
private static Map<Class<?>, Field[]> fieldMap = new ConcurrentHashMap<>();
|
||||||
|
//是否是byte[]
|
||||||
|
private static boolean isByteArray(Object obj){
|
||||||
|
return obj instanceof byte[];
|
||||||
|
}
|
||||||
|
//是否是数组
|
||||||
|
private static boolean isArray(Object obj){
|
||||||
|
if(isByteArray(obj)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return obj.getClass().isArray();
|
||||||
|
}
|
||||||
|
//将数组对象转为DBObject
|
||||||
|
private static DBObject array2DBObject(Object obj) throws IllegalAccessException {
|
||||||
|
Object[] arr = (Object[]) obj;
|
||||||
|
BasicDBList dbList = new BasicDBList();
|
||||||
|
for(int i=0; i<arr.length; i++){
|
||||||
|
Object dbObject = bean2DBObject(arr[i]);
|
||||||
|
dbList.add(dbObject);
|
||||||
|
}
|
||||||
|
return dbList;
|
||||||
|
}
|
||||||
|
//是否是list
|
||||||
|
private static boolean isList(Object obj){
|
||||||
|
return obj instanceof List;
|
||||||
|
}
|
||||||
|
//将list对象转为DBObject
|
||||||
|
private static DBObject list2DBObject(Object obj) throws IllegalAccessException {
|
||||||
|
List list = (List) obj;
|
||||||
|
BasicDBList dbList = new BasicDBList();
|
||||||
|
for(int i=0; i<list.size(); i++){
|
||||||
|
Object dbObject = bean2DBObject(list.get(i));
|
||||||
|
dbList.add(dbObject);
|
||||||
|
}
|
||||||
|
return dbList;
|
||||||
|
}
|
||||||
|
//是否是map对象
|
||||||
|
private static boolean isMap(Object obj){
|
||||||
|
return obj instanceof Map;
|
||||||
|
}
|
||||||
|
//将map对象转为DBObject
|
||||||
|
private static DBObject map2DBObject(Object obj) throws IllegalAccessException {
|
||||||
|
Map<Object, Object> map = (Map) obj;
|
||||||
|
DBObject dbObject = new BasicDBObject();
|
||||||
|
for (Map.Entry<Object, Object> entry : map.entrySet()) {
|
||||||
|
Object dbObject1 = bean2DBObject(entry.getValue());
|
||||||
|
dbObject.put(entry.getKey().toString(), dbObject1);
|
||||||
|
}
|
||||||
|
return dbObject;
|
||||||
|
}
|
||||||
|
//是否是集合对象
|
||||||
|
private static boolean isSet(Object obj){
|
||||||
|
return obj instanceof Set;
|
||||||
|
}
|
||||||
|
//将集合对象转为DBObject
|
||||||
|
private static DBObject set2DBObject(Object obj) throws IllegalAccessException {
|
||||||
|
Set<Object> set = (Set) obj;
|
||||||
|
BasicDBList dbList = new BasicDBList();
|
||||||
|
for(Object val: set){
|
||||||
|
Object dbObject = bean2DBObject(val);
|
||||||
|
dbList.add(dbObject);
|
||||||
|
}
|
||||||
|
return dbList;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> boolean isBaseType(T param){
|
||||||
|
if (param instanceof Integer) {//判断变量的类型
|
||||||
|
return true;
|
||||||
|
} else if (param instanceof String) {
|
||||||
|
return true;
|
||||||
|
} else if (param instanceof Double) {
|
||||||
|
return true;
|
||||||
|
} else if (param instanceof Float) {
|
||||||
|
return true;
|
||||||
|
} else if (param instanceof Long) {
|
||||||
|
return true;
|
||||||
|
} else if (param instanceof Boolean) {
|
||||||
|
return true;
|
||||||
|
} else if (param instanceof Date) {
|
||||||
|
return true;
|
||||||
|
} else if (param instanceof Byte) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
private static Field[] getFields(Class<?> clazz){
|
||||||
|
Field[] fields = fieldMap.get(clazz);
|
||||||
|
if(fields == null) {
|
||||||
|
fields = clazz.getDeclaredFields();
|
||||||
|
fieldMap.put(clazz, fields);
|
||||||
|
}
|
||||||
|
return fields;
|
||||||
|
}
|
||||||
|
public static <T> Object bean2DBObject(T bean) throws IllegalArgumentException,
|
||||||
|
IllegalAccessException {
|
||||||
|
if (bean == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if(isBaseType(bean)){
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
DBObject dbObject = new BasicDBObject();
|
||||||
|
if (isMap(bean)) {
|
||||||
|
return map2DBObject(bean);
|
||||||
|
} else if (isSet(bean)) {
|
||||||
|
return set2DBObject(bean);
|
||||||
|
} else if (isArray(bean)) {
|
||||||
|
return array2DBObject(bean);
|
||||||
|
} else if (isList(bean)) {
|
||||||
|
return list2DBObject(bean);
|
||||||
|
} else if (isByteArray(bean)) {
|
||||||
|
return dbObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取对象对应类中的所有属性域
|
||||||
|
Class<?> clazz = bean.getClass();
|
||||||
|
while(clazz != null) {
|
||||||
|
Field[] fields = getFields(clazz);
|
||||||
|
|
||||||
|
for (Field field : fields) {
|
||||||
|
// 获取属性名
|
||||||
|
String varName = field.getName();
|
||||||
|
if(varName.startsWith("_")){
|
||||||
|
continue;
|
||||||
|
} else if(varName.equals("root")){
|
||||||
|
continue;
|
||||||
|
} else if (varName.equals("id")) {
|
||||||
|
varName = "_id";
|
||||||
|
}
|
||||||
|
// 修改访问控制权限
|
||||||
|
boolean accessFlag = field.isAccessible();
|
||||||
|
if (!accessFlag) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
}
|
||||||
|
Object param = field.get(bean);
|
||||||
|
if (param == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (isBaseType(param)) {
|
||||||
|
dbObject.put(varName, param);
|
||||||
|
} else {
|
||||||
|
DBObject rdbObject = (DBObject) bean2DBObject(param);
|
||||||
|
dbObject.put(varName, rdbObject);
|
||||||
|
}
|
||||||
|
// 恢复访问控制权限
|
||||||
|
field.setAccessible(accessFlag);
|
||||||
|
}
|
||||||
|
|
||||||
|
clazz = clazz.getSuperclass();
|
||||||
|
}
|
||||||
|
return dbObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
//从基础类型中取得值
|
||||||
|
private static Object getBaseObj(Object object, Class<?> clazz){
|
||||||
|
if(clazz == String.class){
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
if(clazz == int.class){
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
if(clazz == long.class){
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
if( clazz.equals(float.class)){
|
||||||
|
return ((Double)object).floatValue();
|
||||||
|
}
|
||||||
|
if(clazz == double.class){
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
if(clazz == byte.class){
|
||||||
|
return ((Integer)object).byteValue();
|
||||||
|
}
|
||||||
|
if(clazz == boolean.class){
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
if(clazz == Integer.class){
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
if(clazz == Long.class){
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
if( clazz.equals(Float.class)){
|
||||||
|
return ((Double)object).floatValue();
|
||||||
|
}
|
||||||
|
if(clazz == Double.class){
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
if(clazz == Byte.class){
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
if(clazz == Boolean.class){
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//是否是map对象
|
||||||
|
private static boolean isMap(Class<?> clazz){
|
||||||
|
if( clazz == Map.class){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//取得key对应的值
|
||||||
|
private static Object getKey(String key, Class<?> clazz){
|
||||||
|
if(clazz == Integer.class){
|
||||||
|
return Integer.valueOf(key);
|
||||||
|
}
|
||||||
|
if(clazz == Long.class){
|
||||||
|
return Long.valueOf(key);
|
||||||
|
}
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
//将DBObject转为map
|
||||||
|
private static <K, V> Map<K, V> dbObject2Map(DBObject dbObject, Field field, MongoRoot root) throws Exception {
|
||||||
|
ParameterizedType pt = (ParameterizedType) field.getGenericType();
|
||||||
|
Type[] types = pt.getActualTypeArguments();
|
||||||
|
|
||||||
|
Map map = new HashMap<K, V>();
|
||||||
|
BasicDBObject dbObj = (BasicDBObject)dbObject;
|
||||||
|
for (Map.Entry<String, Object> entry : dbObj.entrySet()) {
|
||||||
|
Object obj = getObj(entry.getValue(), (Class)types[1], root);
|
||||||
|
// Object value = ((Class)types[1]).newInstance();
|
||||||
|
// Object obj = dbObject2Bean((DBObject) entry.getValue(), value, root);
|
||||||
|
Object key = getKey(entry.getKey(), (Class)types[0]);
|
||||||
|
map.put(key, obj);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
//是否是集合对象
|
||||||
|
private static boolean isSet(Class<?> clazz){
|
||||||
|
if(clazz == Set.class){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//将dbobj转为普通对象
|
||||||
|
private static Object getObj(Object dbobj, Class<?> clazz, MongoRoot root) throws Exception {
|
||||||
|
Object obj = getBaseObj(dbobj, clazz);
|
||||||
|
if(obj == null){
|
||||||
|
Constructor constructor = clazz.getDeclaredConstructor();
|
||||||
|
boolean isAccessible = constructor.isAccessible();
|
||||||
|
constructor.setAccessible(true);
|
||||||
|
obj = constructor.newInstance();
|
||||||
|
constructor.setAccessible(isAccessible);
|
||||||
|
dbObject2Bean((DBObject) dbobj, obj, root);
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
//将DBObject转为set
|
||||||
|
private static <T> Set<T> dbObject2Set(DBObject dbObject, Field field, MongoRoot root) throws Exception {
|
||||||
|
ParameterizedType pt = (ParameterizedType) field.getGenericType();
|
||||||
|
Type[] types = pt.getActualTypeArguments();
|
||||||
|
Set set = ConcurrentHashMap.<T> newKeySet();
|
||||||
|
BasicDBList dbList = (BasicDBList)dbObject;
|
||||||
|
for (Object dbobj : dbList) {
|
||||||
|
Object obj = getObj(dbobj, (Class)types[0], root);
|
||||||
|
set.add(obj);
|
||||||
|
}
|
||||||
|
return set;
|
||||||
|
}
|
||||||
|
//是否是byte[]
|
||||||
|
private static boolean isByteArray(Class<?> clazz){
|
||||||
|
return clazz == byte[].class;
|
||||||
|
}
|
||||||
|
//是否是数组
|
||||||
|
private static boolean isArray(Class<?> clazz){
|
||||||
|
if(isByteArray(clazz)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return clazz.isArray();
|
||||||
|
}
|
||||||
|
//将DBObject转换为数组
|
||||||
|
private static Object[] dbObject2Array(DBObject dbObject, Class<?> clazz, MongoRoot root) throws Exception {
|
||||||
|
BasicDBList basicDBList = (BasicDBList)dbObject;
|
||||||
|
Object[] objs = (Object[])Array.newInstance(clazz.getComponentType(), basicDBList.size());
|
||||||
|
for(int i=0; i<basicDBList.size(); i++){
|
||||||
|
Object dbobj = (Object)basicDBList.get(i);
|
||||||
|
Object obj = getObj(dbobj, clazz.getComponentType(), root);
|
||||||
|
objs[i] = obj;
|
||||||
|
}
|
||||||
|
return objs;
|
||||||
|
}
|
||||||
|
//是否是list
|
||||||
|
private static boolean isList(Class<?> clazz){
|
||||||
|
if(clazz == List.class){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(clazz == ArrayList.class){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(clazz == CopyOnWriteArrayList.class){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//将DBObject转为list
|
||||||
|
private static <T> List<T> dbObject2List(DBObject dbObject, Field field, MongoRoot root) throws Exception {
|
||||||
|
ParameterizedType pt = (ParameterizedType) field.getGenericType();
|
||||||
|
Type[] types = pt.getActualTypeArguments();
|
||||||
|
List list = new CopyOnWriteArrayList<T>();
|
||||||
|
BasicDBList dbList = (BasicDBList)dbObject;
|
||||||
|
for (Object dbobj : dbList) {
|
||||||
|
Object obj = getObj(dbobj, (Class)types[0], root);
|
||||||
|
list.add(obj);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
//DBObject转换为Bean对象
|
||||||
|
public static Object dbObject2Bean(DBObject dbObject, Object bean, MongoRoot root) throws Exception {
|
||||||
|
if (bean == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Class<?> clazz = bean.getClass();
|
||||||
|
while (clazz != null) {
|
||||||
|
Field[] fields = getFields(clazz);
|
||||||
|
for (Field field : fields) {
|
||||||
|
String varName = field.getName();
|
||||||
|
Object object = null;
|
||||||
|
if (varName.equals("id")) {
|
||||||
|
object = dbObject.get("_id");
|
||||||
|
} else if(varName.equals("_root")){
|
||||||
|
((MongoBase)bean).setRoot((MongoRoot) root);
|
||||||
|
continue;
|
||||||
|
} else if(varName.equals("bsave")){
|
||||||
|
((MongoRoot)bean).setBsave(true);
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
object = dbObject.get(varName);
|
||||||
|
}
|
||||||
|
if (object == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
boolean accessFlag = field.isAccessible();
|
||||||
|
if (!accessFlag) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
//System.out.println("varName=" + varName);
|
||||||
|
Class<?> fieldClazz = field.getType();
|
||||||
|
Object obj = getBaseObj(object, fieldClazz);
|
||||||
|
if (obj != null) {
|
||||||
|
field.set(bean, obj);
|
||||||
|
} else if (isMap(fieldClazz)) {
|
||||||
|
obj = dbObject2Map((DBObject) object, field, root);
|
||||||
|
} else if (isSet(fieldClazz)) {
|
||||||
|
obj = dbObject2Set((DBObject) object, field, root);
|
||||||
|
} else if (isArray(fieldClazz)) {
|
||||||
|
obj = dbObject2Array((DBObject) object, fieldClazz, root);
|
||||||
|
} else if (isList(fieldClazz)) {
|
||||||
|
obj = dbObject2List((DBObject) object, field, root);
|
||||||
|
} else if (isByteArray(fieldClazz)) {
|
||||||
|
obj = object;
|
||||||
|
} else {
|
||||||
|
obj = fieldClazz.newInstance();
|
||||||
|
obj = dbObject2Bean((DBObject) object, obj, root);
|
||||||
|
}
|
||||||
|
field.set(bean, obj);
|
||||||
|
// 恢复访问控制权限
|
||||||
|
field.setAccessible(accessFlag);
|
||||||
|
}
|
||||||
|
clazz = clazz.getSuperclass();
|
||||||
|
}
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T dbObject2Bean(DBObject dbObject, Class<T> clazz) throws Exception {
|
||||||
|
if(dbObject == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
T bean = clazz.newInstance();
|
||||||
|
dbObject2Bean(dbObject, bean, (MongoRoot)bean);
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,6 +5,8 @@ import org.springframework.data.mongodb.core.MongoTemplate;
|
||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -27,24 +29,19 @@ public class LjsdMongoTemplate {
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> List<T> findAll(String collectionName, Class<T> clazz) throws Exception {
|
public <T> List<T> findAll(String collectionName, Class<T> clazz) throws Exception {
|
||||||
List<T> all = mongoTemplate.findAll(clazz, collectionName);
|
DBCollection coll = mongoTemplate.getCollection(collectionName);
|
||||||
if(all!=null && !all.isEmpty()){
|
DBCursor dbCursor = coll.find();
|
||||||
T t = all.get(0);
|
List<T> list= new ArrayList<>(dbCursor.count());
|
||||||
if(t.getClass().getSuperclass() == MongoRoot.class){
|
for (Iterator<DBObject> it = dbCursor.iterator(); it.hasNext(); ) {
|
||||||
for(T t1 : all){
|
list.add(BeanUtil.dbObject2Bean(it.next(), clazz));
|
||||||
dynamicChangeTheMongoRoot(t1, (MongoRoot) t1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return all;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T findById(String collectionName, String id, Class<T> clazz) throws Exception {
|
public <T> T findById(String collectionName, String id, Class<T> clazz) throws Exception {
|
||||||
T result = mongoTemplate.findById(id, clazz, collectionName);
|
DBCollection coll = mongoTemplate.getCollection(collectionName);
|
||||||
if(result.getClass().getSuperclass() == MongoRoot.class){
|
DBObject doc = coll.findOne(id);
|
||||||
dynamicChangeTheMongoRoot(result,(MongoRoot) result);
|
return BeanUtil.dbObject2Bean(doc, clazz);
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void chnage(Object obj){
|
public void chnage(Object obj){
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package com.ljsd.common.mogodb.test;
|
||||||
|
|
||||||
import com.ljsd.common.mogodb.LjsdMongoTemplate;
|
import com.ljsd.common.mogodb.LjsdMongoTemplate;
|
||||||
import com.ljsd.common.mogodb.test.pojo.BaseInfoForTest;
|
import com.ljsd.common.mogodb.test.pojo.BaseInfoForTest;
|
||||||
|
import com.ljsd.common.mogodb.test.pojo.CardTestInfo;
|
||||||
import com.ljsd.common.mogodb.test.pojo.PlayerForTest;
|
import com.ljsd.common.mogodb.test.pojo.PlayerForTest;
|
||||||
import com.mongodb.MongoClientURI;
|
import com.mongodb.MongoClientURI;
|
||||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||||
|
|
@ -20,7 +21,7 @@ public class TestMongo {
|
||||||
try {
|
try {
|
||||||
ljsdMongoTemplate = getLjsdMongoTemplate();
|
ljsdMongoTemplate = getLjsdMongoTemplate();
|
||||||
PlayerForTest.init(ljsdMongoTemplate);
|
PlayerForTest.init(ljsdMongoTemplate);
|
||||||
//save();
|
// save();
|
||||||
PlayerForTest one = findOne();
|
PlayerForTest one = findOne();
|
||||||
update(one);
|
update(one);
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
@ -44,10 +45,14 @@ public class TestMongo {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void save() throws Exception {
|
public static void save() throws Exception {
|
||||||
|
CardTestInfo cardTestInfo = new CardTestInfo();
|
||||||
|
cardTestInfo.setId("33");
|
||||||
|
cardTestInfo.setLevel("555");
|
||||||
BaseInfoForTest baseInfoForTest = new BaseInfoForTest();
|
BaseInfoForTest baseInfoForTest = new BaseInfoForTest();
|
||||||
baseInfoForTest.setAge(10);
|
baseInfoForTest.setAge(10);
|
||||||
baseInfoForTest.setName("test");
|
baseInfoForTest.setName("test");
|
||||||
PlayerForTest playerForTest = new PlayerForTest(1,baseInfoForTest);
|
PlayerForTest playerForTest = new PlayerForTest(1,baseInfoForTest);
|
||||||
|
baseInfoForTest.addCard(2,cardTestInfo);
|
||||||
ljsdMongoTemplate.save(playerForTest);
|
ljsdMongoTemplate.save(playerForTest);
|
||||||
System.out.println(ljsdMongoTemplate);
|
System.out.println(ljsdMongoTemplate);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,16 @@ public class BaseInfoForTest extends MongoBase {
|
||||||
private int age;
|
private int age;
|
||||||
|
|
||||||
private Map<String,String> otherInfoMsg = new ConcurrentHashMap<>();
|
private Map<String,String> otherInfoMsg = new ConcurrentHashMap<>();
|
||||||
|
private Map<Integer,CardTestInfo> cardTestInfoMap;
|
||||||
|
|
||||||
public BaseInfoForTest(){
|
public BaseInfoForTest(){
|
||||||
|
cardTestInfoMap = new ConcurrentHashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addCard(int cardId,CardTestInfo cardTestInfo) throws Exception {
|
||||||
|
cardTestInfo.init(getRoot(),getMongoKey() + ".cardTestInfoMap." + cardId);
|
||||||
|
updateString("cardMap." +cardId, cardTestInfo);
|
||||||
|
cardTestInfoMap.put(cardId,cardTestInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.ljsd.common.mogodb.test.pojo;
|
||||||
|
|
||||||
|
import com.ljsd.common.mogodb.MongoBase;
|
||||||
|
|
||||||
|
public class CardTestInfo extends MongoBase {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
private String level;
|
||||||
|
|
||||||
|
public void setId(String id) throws Exception {
|
||||||
|
updateString("id",id);
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLevel(String level) throws Exception {
|
||||||
|
updateString("level",level);
|
||||||
|
this.level = level;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLevel() {
|
||||||
|
return level;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,6 +3,8 @@ package com.ljsd.common.mogodb.test.pojo;
|
||||||
import com.ljsd.common.mogodb.LjsdMongoTemplate;
|
import com.ljsd.common.mogodb.LjsdMongoTemplate;
|
||||||
import com.ljsd.common.mogodb.MongoRoot;
|
import com.ljsd.common.mogodb.MongoRoot;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class PlayerForTest extends MongoRoot {
|
public class PlayerForTest extends MongoRoot {
|
||||||
private final static String _COLLECTION_NAME = "play_test";
|
private final static String _COLLECTION_NAME = "play_test";
|
||||||
private BaseInfoForTest baseInfoForTest;
|
private BaseInfoForTest baseInfoForTest;
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ public class GameApplication {
|
||||||
for (BaseHandler handler : gameHanderMap.values()) {
|
for (BaseHandler handler : gameHanderMap.values()) {
|
||||||
protocolsManager.addHandler(handler);
|
protocolsManager.addHandler(handler);
|
||||||
}
|
}
|
||||||
STableManager.initialize("com.ljsd.jieling.models");
|
STableManager.initialize("com.ljsd.jieling.config");
|
||||||
final NettyServerAutoConfiguration netServerConfig = configurableApplicationContext.getBean(NettyServerAutoConfiguration.class);
|
final NettyServerAutoConfiguration netServerConfig = configurableApplicationContext.getBean(NettyServerAutoConfiguration.class);
|
||||||
NettyProperties properties = netServerConfig.getNettyProperties();
|
NettyProperties properties = netServerConfig.getNettyProperties();
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package com.ljsd.jieling.models;
|
package com.ljsd.jieling.config;
|
||||||
|
|
||||||
public interface BaseConfig {
|
public interface BaseConfig {
|
||||||
void init() throws Exception;
|
void init() throws Exception;
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package com.ljsd.jieling.models;
|
package com.ljsd.jieling.config;
|
||||||
|
|
||||||
|
|
||||||
import com.ljsd.jieling.logic.STableManager;
|
import com.ljsd.jieling.logic.STableManager;
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package com.ljsd.jieling.models;
|
package com.ljsd.jieling.config;
|
||||||
|
|
||||||
import com.ljsd.jieling.logic.STableManager;
|
import com.ljsd.jieling.logic.STableManager;
|
||||||
import com.ljsd.jieling.logic.Table;
|
import com.ljsd.jieling.logic.Table;
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package com.ljsd.jieling.models;
|
package com.ljsd.jieling.config;
|
||||||
|
|
||||||
|
|
||||||
import com.ljsd.jieling.logic.STableManager;
|
import com.ljsd.jieling.logic.STableManager;
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.ljsd.jieling.core;
|
||||||
|
|
||||||
|
public interface GlobalsDef {
|
||||||
|
String DEFAULT_NAME = "无名妖灵师";
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package com.ljsd.jieling.handler;
|
package com.ljsd.jieling.handler;
|
||||||
|
|
||||||
|
import com.ljsd.jieling.logic.dao.User;
|
||||||
|
import com.ljsd.jieling.logic.dao.UserManager;
|
||||||
import com.ljsd.jieling.netty.cocdex.PacketNetData;
|
import com.ljsd.jieling.netty.cocdex.PacketNetData;
|
||||||
import com.ljsd.jieling.network.session.ISession;
|
import com.ljsd.jieling.network.session.ISession;
|
||||||
import com.ljsd.jieling.protocols.CommonProto;
|
import com.ljsd.jieling.protocols.CommonProto;
|
||||||
|
|
@ -33,11 +35,12 @@ public class LoginRequestHandler extends BaseHandler {
|
||||||
|
|
||||||
LOGGER.info("processMessage->uid={},token={},msgId={},msgIndex={},messageNum={},messageStr={}",
|
LOGGER.info("processMessage->uid={},token={},msgId={},msgIndex={},messageNum={},messageStr={}",
|
||||||
userId, token,msgId,msgIndex,getPlayerInfoRequest.getNum(),getPlayerInfoRequest.getStr());
|
userId, token,msgId,msgIndex,getPlayerInfoRequest.getNum(),getPlayerInfoRequest.getStr());
|
||||||
|
User userForLogin = UserManager.getUserForLogin(userId);
|
||||||
|
|
||||||
|
|
||||||
CommonProto.Common.Player player = CommonProto.Common.Player
|
CommonProto.Common.Player player = CommonProto.Common.Player
|
||||||
.newBuilder()
|
.newBuilder()
|
||||||
.setUid(12345)
|
.setUid(userId)
|
||||||
.setGold(10000)
|
.setGold(10000)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,127 @@
|
||||||
|
package com.ljsd.jieling.logic.dao;
|
||||||
|
|
||||||
|
import com.ljsd.common.mogodb.MongoBase;
|
||||||
|
|
||||||
|
public class Hero extends MongoBase {
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
private int templateId; //模板ID
|
||||||
|
|
||||||
|
private int heroType;
|
||||||
|
|
||||||
|
private int level;//等级
|
||||||
|
|
||||||
|
private int star;
|
||||||
|
|
||||||
|
private int quality;
|
||||||
|
|
||||||
|
private int hp;
|
||||||
|
|
||||||
|
private int attack;
|
||||||
|
|
||||||
|
private int pDefence;
|
||||||
|
|
||||||
|
private int mDefence;
|
||||||
|
|
||||||
|
private int speed;
|
||||||
|
|
||||||
|
|
||||||
|
public void setId(String id) throws Exception {
|
||||||
|
updateString("id",id);
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTemplateId(int templateId) throws Exception {
|
||||||
|
updateString("templateId",templateId);
|
||||||
|
this.templateId = templateId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHeroType(int heroType) throws Exception {
|
||||||
|
updateString("heroType",heroType);
|
||||||
|
this.heroType = heroType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLevel(int level) throws Exception {
|
||||||
|
updateString("level",level);
|
||||||
|
this.level = level;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStar(int star) throws Exception {
|
||||||
|
updateString("star",star);
|
||||||
|
this.star = star;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setQuality(int quality) throws Exception {
|
||||||
|
updateString("quality",quality);
|
||||||
|
this.quality = quality;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHp(int hp) throws Exception {
|
||||||
|
updateString("hp",hp);
|
||||||
|
this.hp = hp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAttack(int attack) throws Exception {
|
||||||
|
updateString("attack",attack);
|
||||||
|
this.attack = attack;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setpDefence(int pDefence) throws Exception {
|
||||||
|
updateString("pDefence",pDefence);
|
||||||
|
this.pDefence = pDefence;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setmDefence(int mDefence) throws Exception {
|
||||||
|
updateString("mDefence",mDefence);
|
||||||
|
this.mDefence = mDefence;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSpeed(int speed) throws Exception {
|
||||||
|
updateString("speed",speed);
|
||||||
|
this.speed = speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTemplateId() {
|
||||||
|
return templateId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHeroType() {
|
||||||
|
return heroType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLevel() {
|
||||||
|
return level;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getStar() {
|
||||||
|
return star;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getQuality() {
|
||||||
|
return quality;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHp() {
|
||||||
|
return hp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAttack() {
|
||||||
|
return attack;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getpDefence() {
|
||||||
|
return pDefence;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getmDefence() {
|
||||||
|
return mDefence;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSpeed() {
|
||||||
|
return speed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,37 @@ package com.ljsd.jieling.logic.dao;
|
||||||
|
|
||||||
import com.ljsd.common.mogodb.MongoBase;
|
import com.ljsd.common.mogodb.MongoBase;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class HeroManager extends MongoBase {
|
public class HeroManager extends MongoBase {
|
||||||
|
private Map<String, Hero> heroMap;
|
||||||
|
|
||||||
|
public HeroManager() {
|
||||||
|
heroMap = new HashMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Hero> getItemMap() {
|
||||||
|
return heroMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setItemMap(Map<String, Hero> heroMap) {
|
||||||
|
this.heroMap = heroMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addHero(Hero hero) throws Exception {
|
||||||
|
//绑定关系
|
||||||
|
hero.init(getRoot(), getMongoKey() + ".heroMap." + hero.getId());
|
||||||
|
updateString("heroMap." + hero.getTemplateId(), hero);
|
||||||
|
heroMap.put(hero.getId(), hero);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Hero getHero(String heroId) {
|
||||||
|
return heroMap.get(heroId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeHero(String heroId) {
|
||||||
|
heroMap.remove(heroId);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.ljsd.jieling.logic.dao;
|
||||||
|
|
||||||
|
import com.ljsd.common.mogodb.MongoBase;
|
||||||
|
|
||||||
|
public class Item extends MongoBase {
|
||||||
|
private int templateId;
|
||||||
|
private int overlap;
|
||||||
|
private long haveTime;
|
||||||
|
|
||||||
|
|
||||||
|
public void setTemplateId(int templateId) throws Exception {
|
||||||
|
updateString("templateId",templateId);
|
||||||
|
this.templateId = templateId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOverlap(int overlap) throws Exception {
|
||||||
|
updateString("overlap",overlap);
|
||||||
|
this.overlap = overlap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHaveTime(long haveTime) throws Exception {
|
||||||
|
updateString("haveTime",haveTime);
|
||||||
|
this.haveTime = haveTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTemplateId() {
|
||||||
|
return templateId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getOverlap() {
|
||||||
|
return overlap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getHaveTime() {
|
||||||
|
return haveTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,7 +2,36 @@ package com.ljsd.jieling.logic.dao;
|
||||||
|
|
||||||
import com.ljsd.common.mogodb.MongoBase;
|
import com.ljsd.common.mogodb.MongoBase;
|
||||||
|
|
||||||
public class ItemManager extends MongoBase {
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ItemManager extends MongoBase {
|
||||||
|
private Map<Integer, Item> itemMap;
|
||||||
|
|
||||||
|
public ItemManager() {
|
||||||
|
itemMap = new HashMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Integer, Item> getItemMap() {
|
||||||
|
return itemMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setItemMap(Map<Integer, Item> itemMap) {
|
||||||
|
this.itemMap = itemMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addItem(Item item) throws Exception {
|
||||||
|
item.init(getRoot(), getMongoKey() + ".itemMap." + item.getTemplateId());
|
||||||
|
updateString("itemMap." + item.getTemplateId(), item);
|
||||||
|
itemMap.put(item.getTemplateId(), item);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Item getItem(Integer key) {
|
||||||
|
return itemMap.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeItem(Integer key) {
|
||||||
|
itemMap.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,13 +15,33 @@ public class User extends MongoRoot {
|
||||||
|
|
||||||
private MapManager mapManager;
|
private MapManager mapManager;
|
||||||
|
|
||||||
public User(int uid) {
|
public User(int uid,PlayerInfoManager playerInfoManager, ItemManager itemManager,
|
||||||
setId(String.valueOf(uid));
|
HeroManager heroManager, MapManager mapManager){
|
||||||
playerInfoManager = new PlayerInfoManager();
|
setMyMongoDBPool(_myMongoDBPool);
|
||||||
itemManager = new ItemManager();
|
setId(Integer.toString(uid));
|
||||||
heroManager = new HeroManager();
|
this.playerInfoManager = playerInfoManager;
|
||||||
|
this.itemManager = itemManager;
|
||||||
|
this.heroManager = heroManager;
|
||||||
|
this.mapManager = mapManager;
|
||||||
|
//綁定关系
|
||||||
|
playerInfoManager.init(this, "playerInfoManager", false);
|
||||||
|
itemManager.init(this, "itemManager", false);
|
||||||
|
heroManager.init(this, "heroManager", false);
|
||||||
|
mapManager.init(this, "mapManager", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public User(){
|
||||||
|
setMyMongoDBPool(_myMongoDBPool);
|
||||||
|
this.playerInfoManager = new PlayerInfoManager();
|
||||||
|
this.itemManager = new ItemManager();
|
||||||
|
this.heroManager = new HeroManager();
|
||||||
|
this.mapManager = new MapManager();
|
||||||
|
//綁定关系
|
||||||
|
playerInfoManager.init(this, "playerInfoManager", false);
|
||||||
|
itemManager.init(this, "itemManager", false);
|
||||||
|
heroManager.init(this, "heroManager", false);
|
||||||
|
mapManager.init(this, "mapManager", false);
|
||||||
|
}
|
||||||
|
|
||||||
public static void init(LjsdMongoTemplate ljsdMongoTemplate) {
|
public static void init(LjsdMongoTemplate ljsdMongoTemplate) {
|
||||||
_myMongoDBPool = ljsdMongoTemplate;
|
_myMongoDBPool = ljsdMongoTemplate;
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
public class UserManager {
|
public class UserManager {
|
||||||
private static Map<Integer, User> userMap = new ConcurrentHashMap<>();
|
private static Map<Integer, User> userMap = new ConcurrentHashMap<>();
|
||||||
private static LjsdMongoTemplate ljsdMongoTemplate;
|
public static LjsdMongoTemplate ljsdMongoTemplate;
|
||||||
public static void init(ConfigurableApplicationContext configurableApplicationContext) {
|
public static void init(ConfigurableApplicationContext configurableApplicationContext) {
|
||||||
ljsdMongoTemplate =configurableApplicationContext.getBean(LjsdMongoTemplate.class);
|
ljsdMongoTemplate =configurableApplicationContext.getBean(LjsdMongoTemplate.class);
|
||||||
User.init(ljsdMongoTemplate);
|
User.init(ljsdMongoTemplate);
|
||||||
|
|
@ -25,13 +25,24 @@ public class UserManager {
|
||||||
user = ljsdMongoTemplate.findById(User.getCollectionName(), Integer.toString(uid), User.class);
|
user = ljsdMongoTemplate.findById(User.getCollectionName(), Integer.toString(uid), User.class);
|
||||||
}
|
}
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
user = new User(uid);
|
user = registerUser(uid);
|
||||||
UserManager.addUser(user);
|
}else{
|
||||||
ljsdMongoTemplate.save(user);
|
refreshUserLoginInfo(user);
|
||||||
}
|
}
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void refreshUserLoginInfo(User user) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static User registerUser(int uid) throws Exception {
|
||||||
|
User user = new User(uid,new PlayerInfoManager(),new ItemManager(),new HeroManager(),new MapManager());
|
||||||
|
UserManager.addUser(user);
|
||||||
|
ljsdMongoTemplate.save(user);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static User getUser(int uid) throws Exception {
|
public static User getUser(int uid) throws Exception {
|
||||||
User user = userMap.get(uid);
|
User user = userMap.get(uid);
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,12 @@ package com.ljsd.jieling.logic.map;
|
||||||
import com.ljsd.jieling.logic.dao.MapManager;
|
import com.ljsd.jieling.logic.dao.MapManager;
|
||||||
import com.ljsd.jieling.logic.dao.User;
|
import com.ljsd.jieling.logic.dao.User;
|
||||||
import com.ljsd.jieling.logic.dao.UserManager;
|
import com.ljsd.jieling.logic.dao.UserManager;
|
||||||
import com.ljsd.jieling.models.SCMap;
|
|
||||||
import com.ljsd.jieling.models.SCMapEvent;
|
|
||||||
import com.ljsd.jieling.network.session.ISession;
|
import com.ljsd.jieling.network.session.ISession;
|
||||||
import com.ljsd.jieling.protocols.MapInfoProto;
|
import com.ljsd.jieling.protocols.MapInfoProto;
|
||||||
import com.ljsd.jieling.protocols.MessageTypeProto;
|
import com.ljsd.jieling.protocols.MessageTypeProto;
|
||||||
import com.ljsd.jieling.util.MessageUtil;
|
import com.ljsd.jieling.util.MessageUtil;
|
||||||
|
import com.ljsd.jieling.config.SCMap;
|
||||||
|
import com.ljsd.jieling.config.SCMapEvent;
|
||||||
import com.ljsd.jieling.util.ToolsUtil;
|
import com.ljsd.jieling.util.ToolsUtil;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package com.ljsd.jieling.util;
|
package com.ljsd.jieling.util;
|
||||||
|
|
||||||
import com.google.protobuf.GeneratedMessage;
|
import com.google.protobuf.GeneratedMessage;
|
||||||
|
import com.ljsd.jieling.logic.dao.UserManager;
|
||||||
import com.ljsd.jieling.netty.PackageConstant;
|
import com.ljsd.jieling.netty.PackageConstant;
|
||||||
import com.ljsd.jieling.netty.cocdex.Tea;
|
import com.ljsd.jieling.netty.cocdex.Tea;
|
||||||
import com.ljsd.jieling.network.session.ISession;
|
import com.ljsd.jieling.network.session.ISession;
|
||||||
|
|
@ -46,6 +47,7 @@ public class MessageUtil {
|
||||||
|
|
||||||
public static void sendMessage(ISession session, int result, int msgId, GeneratedMessage generatedMessage, boolean flush) throws Exception {
|
public static void sendMessage(ISession session, int result, int msgId, GeneratedMessage generatedMessage, boolean flush) throws Exception {
|
||||||
byte[] byteBuf = wrappedBuffer(session.getUid(), session.getToken(), session.getIndex(), result, msgId, generatedMessage);
|
byte[] byteBuf = wrappedBuffer(session.getUid(), session.getToken(), session.getIndex(), result, msgId, generatedMessage);
|
||||||
|
UserManager.ljsdMongoTemplate.lastUpdate();
|
||||||
if (flush) {
|
if (flush) {
|
||||||
session.writeAndFlush(byteBuf);
|
session.writeAndFlush(byteBuf);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue