添加道具公共接口
parent
713ecb6902
commit
358310d762
|
@ -0,0 +1,10 @@
|
||||||
|
Id Name Position Include
|
||||||
|
int string mut,float#float,1 mut,float#float,1
|
||||||
|
1 北部 null 101#102
|
||||||
|
2 西部 null 103#104
|
||||||
|
3 0.0 null null
|
||||||
|
4 0.0 null null
|
||||||
|
5 0.0 null null
|
||||||
|
6 0.0 null null
|
||||||
|
7 0.0 null null
|
||||||
|
8 0.0 null null
|
|
@ -0,0 +1,2 @@
|
||||||
|
Id BornItem
|
||||||
|
int mut,int#int,2
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.ljsd.jieling.config;
|
||||||
|
|
||||||
|
import com.ljsd.jieling.logic.STableManager;
|
||||||
|
import com.ljsd.jieling.logic.Table;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Table(name ="ArenaConfig")
|
||||||
|
public class SArenaConfig implements BaseConfig {
|
||||||
|
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private float[] position;
|
||||||
|
|
||||||
|
private float[] include;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float[] getPosition() {
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float[] getInclude() {
|
||||||
|
return include;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
package com.ljsd.jieling.config;
|
||||||
|
|
||||||
|
import com.ljsd.jieling.logic.STableManager;
|
||||||
|
import com.ljsd.jieling.logic.Table;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Table(name = "drop")
|
||||||
|
public class SDrop implements BaseConfig {
|
||||||
|
public static Map<Integer, SDrop> sDropMap;
|
||||||
|
private int id;
|
||||||
|
private int dropType; // 掉落type
|
||||||
|
private int[][] item;
|
||||||
|
private int[] weight;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() throws Exception {
|
||||||
|
sDropMap = STableManager.getConfig(SDrop.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[][] getItem() {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<Integer, SDrop> getsDropMap() {
|
||||||
|
return sDropMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDropType() {
|
||||||
|
return dropType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] getWeight() {
|
||||||
|
return weight;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.ljsd.jieling.config;
|
||||||
|
|
||||||
|
import com.ljsd.jieling.logic.STableManager;
|
||||||
|
import com.ljsd.jieling.logic.Table;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Table(name ="GameSetting")
|
||||||
|
public class SGameSetting implements BaseConfig {
|
||||||
|
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
private int[][] bornItem;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[][] getBornItem() {
|
||||||
|
return bornItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.ljsd.jieling.globals;
|
||||||
|
|
||||||
|
public interface GlobalItemType {
|
||||||
|
//掉落物品类型
|
||||||
|
int ALL_DROP = 0; //全部掉落
|
||||||
|
int WEIGHT_DROP = 1; //随机掉落
|
||||||
|
|
||||||
|
// 物品类型
|
||||||
|
int ITEM = 10; //道具类型
|
||||||
|
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ 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.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.GMCommandProto;
|
import com.ljsd.jieling.protocols.GMCommandProto;
|
||||||
import com.ljsd.jieling.protocols.MessageTypeProto;
|
import com.ljsd.jieling.protocols.MessageTypeProto;
|
||||||
import com.ljsd.jieling.util.ItemUtil;
|
import com.ljsd.jieling.util.ItemUtil;
|
||||||
|
@ -49,8 +50,9 @@ public class GMRequestHandler extends BaseHandler{
|
||||||
}
|
}
|
||||||
|
|
||||||
private void giveItem(User cUser,int itemId,int itemNum) throws Exception {
|
private void giveItem(User cUser,int itemId,int itemNum) throws Exception {
|
||||||
|
CommonProto.Drop.Builder dropBuilder = CommonProto.Drop.newBuilder();
|
||||||
Map<Integer,Integer> itemMap = new ConcurrentHashMap<>();
|
Map<Integer,Integer> itemMap = new ConcurrentHashMap<>();
|
||||||
itemMap.put(itemId,itemNum);
|
itemMap.put(itemId,itemNum);
|
||||||
ItemUtil.addItem(cUser,itemMap);
|
ItemUtil.addItem(cUser,itemMap,dropBuilder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package com.ljsd.jieling.util;
|
package com.ljsd.jieling.util;
|
||||||
|
|
||||||
|
import com.ljsd.jieling.config.SDrop;
|
||||||
import com.ljsd.jieling.config.SItem;
|
import com.ljsd.jieling.config.SItem;
|
||||||
|
import com.ljsd.jieling.globals.GlobalItemType;
|
||||||
import com.ljsd.jieling.logic.dao.Item;
|
import com.ljsd.jieling.logic.dao.Item;
|
||||||
import com.ljsd.jieling.logic.dao.ItemManager;
|
import com.ljsd.jieling.logic.dao.ItemManager;
|
||||||
import com.ljsd.jieling.logic.dao.User;
|
import com.ljsd.jieling.logic.dao.User;
|
||||||
|
@ -10,11 +12,56 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
||||||
public class ItemUtil {
|
public class ItemUtil {
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(ItemUtil.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(ItemUtil.class);
|
||||||
|
//掉落
|
||||||
|
public static CommonProto.Drop.Builder drop(User user, int dropId, CommonProto.Drop.Builder drop) throws Exception {
|
||||||
|
SDrop sDrop = SDrop.getsDropMap().get(dropId);
|
||||||
|
if (sDrop == null){
|
||||||
|
return drop;
|
||||||
|
}
|
||||||
|
int dropType = sDrop.getDropType();
|
||||||
|
Map<Integer, Integer> itemMap = new HashMap<>();
|
||||||
|
selectDrop(sDrop, dropType, itemMap);
|
||||||
|
|
||||||
|
addItem(user,itemMap,drop);
|
||||||
|
|
||||||
|
return drop;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void selectDrop(SDrop sDrop, int dropType, Map<Integer, Integer> itemMap) {
|
||||||
|
int[][] itemIds = sDrop.getItem();
|
||||||
|
if (dropType == GlobalItemType.ALL_DROP){
|
||||||
|
for (int[] itemId : itemIds) {
|
||||||
|
getMap(itemId, itemMap);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
int[] weights = sDrop.getWeight();
|
||||||
|
int index = MathUtils.randomForStrArray(weights);
|
||||||
|
getMap(itemIds[index], itemMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void getMap(int[] itemInfo, Map<Integer, Integer> itemMap) {
|
||||||
|
SItem sItem = SItem.getsItemMap().get(itemInfo[1]);
|
||||||
|
int itemType = sItem.getItemType();
|
||||||
|
switch (itemType) {
|
||||||
|
case GlobalItemType.ITEM:
|
||||||
|
putcountMap(itemInfo, itemMap);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void putcountMap(int[] itemInfo, Map<Integer, Integer> map) {
|
||||||
|
map.merge(itemInfo[0], itemInfo[1], (a, b) -> b + a);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加道具
|
* 添加道具
|
||||||
|
@ -22,7 +69,8 @@ public class ItemUtil {
|
||||||
* @param itemMap
|
* @param itemMap
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static void addItem(User user, Map<Integer, Integer> itemMap) throws Exception {
|
public static void addItem(User user, Map<Integer, Integer> itemMap,CommonProto.Drop.Builder dropBuilder) throws Exception {
|
||||||
|
List<CommonProto.Item> itemProtoList = new CopyOnWriteArrayList<>();
|
||||||
ItemManager itemManager = user.getItemManager();
|
ItemManager itemManager = user.getItemManager();
|
||||||
for (Map.Entry<Integer, Integer> entry : itemMap.entrySet()) {
|
for (Map.Entry<Integer, Integer> entry : itemMap.entrySet()) {
|
||||||
SItem sItem = SItem.getsItemMap().get(entry.getKey());
|
SItem sItem = SItem.getsItemMap().get(entry.getKey());
|
||||||
|
@ -34,7 +82,12 @@ public class ItemUtil {
|
||||||
item.setItemNum(item.getItemNum() + entry.getValue());
|
item.setItemNum(item.getItemNum() + entry.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (dropBuilder != null) {
|
||||||
|
CommonProto.Item itemProto = CommonProto.Item.newBuilder().setItemId(item.getItemId()).setItemNum(item.getItemNum()).build();
|
||||||
|
itemProtoList.add(itemProto);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
dropBuilder.addAllItemlist(itemProtoList);
|
||||||
//todo 为了测试 日后要删掉
|
//todo 为了测试 日后要删掉
|
||||||
UserManager.ljsdMongoTemplate.lastUpdate();
|
UserManager.ljsdMongoTemplate.lastUpdate();
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,85 @@
|
||||||
|
package com.ljsd.jieling.util;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by admin on 2014/11/14.
|
||||||
|
* 随机工具类
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class MathUtils {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(MathUtils.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 权重随机
|
||||||
|
* @param rateArray
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int randomForStrArray(int[] rateArray) {
|
||||||
|
return randomStrArray(rateArray, -1);
|
||||||
|
}
|
||||||
|
public static int randomStrArray(int[] rateArray, int minSum) {
|
||||||
|
if (null == rateArray) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"The random array must not be null!");
|
||||||
|
}
|
||||||
|
int arrayLength = rateArray.length;
|
||||||
|
if (arrayLength == 0) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"The random array's length must not be zero!");
|
||||||
|
}
|
||||||
|
// 依次累加的和
|
||||||
|
int rateSum = 0;
|
||||||
|
// 从头开始 依次累加之后的各个元素和 的临时数组
|
||||||
|
int[] rateSumArray = new int[arrayLength];
|
||||||
|
|
||||||
|
for (int i = 0; i < arrayLength; i++) {
|
||||||
|
|
||||||
|
if (rateArray[i] < 0) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"The array's element must not be equal or greater than zero!");
|
||||||
|
}
|
||||||
|
rateSum += rateArray[i];
|
||||||
|
rateSumArray[i] = rateSum;
|
||||||
|
}
|
||||||
|
if (rateSum <= 0) {
|
||||||
|
// 所有概率都为零,必然没有选中的元素,返回无效索引:-1
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
//如果有保底分母设置,当所有权值之和小于保底分母时,则启用保底分母
|
||||||
|
if(minSum < rateSum) {
|
||||||
|
minSum = rateSum;
|
||||||
|
}
|
||||||
|
int randomInt = MathUtils.random(1, minSum);
|
||||||
|
if(randomInt > rateSum) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int bingoIndex = -1;
|
||||||
|
for (int i = 0; i < arrayLength; i++) {
|
||||||
|
if (randomInt <= rateSumArray[i]) {
|
||||||
|
bingoIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (bingoIndex == -1) {
|
||||||
|
throw new IllegalStateException("Cannot find out bingo index!");
|
||||||
|
}
|
||||||
|
return bingoIndex;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 返回>=low, <=hi的整数随机数,均匀分布
|
||||||
|
*
|
||||||
|
* @param low
|
||||||
|
* @param hi
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int random(int low, int hi) {
|
||||||
|
// return (int) (low + (hi - low + 0.9) * Math.random());
|
||||||
|
return low + ThreadLocalRandom.current().nextInt(hi - low + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue