mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-26 10:16:14 +00:00
提交升级
This commit is contained in:
@@ -17,6 +17,7 @@ import com.google.gson.JsonObject;
|
||||
public final class TbGRoleBaseAttribute extends AbstractBean {
|
||||
public TbGRoleBaseAttribute(JsonObject _buf) {
|
||||
id = _buf.get("id").getAsInt();
|
||||
Blood = _buf.get("Blood").getAsInt();
|
||||
Attack = _buf.get("Attack").getAsInt();
|
||||
Defend = _buf.get("Defend").getAsInt();
|
||||
}
|
||||
@@ -29,6 +30,10 @@ public final class TbGRoleBaseAttribute extends AbstractBean {
|
||||
* 角色Id (0 则默认)
|
||||
*/
|
||||
public final int id;
|
||||
/**
|
||||
* 默认血量
|
||||
*/
|
||||
public final int Blood;
|
||||
/**
|
||||
* 默认攻击属性
|
||||
*/
|
||||
@@ -47,6 +52,7 @@ public final class TbGRoleBaseAttribute extends AbstractBean {
|
||||
public String toString() {
|
||||
return "{ "
|
||||
+ "(format_field_name __code_style field.name):" + id + ","
|
||||
+ "(format_field_name __code_style field.name):" + Blood + ","
|
||||
+ "(format_field_name __code_style field.name):" + Attack + ","
|
||||
+ "(format_field_name __code_style field.name):" + Defend + ","
|
||||
+ "}";
|
||||
|
@@ -17,7 +17,7 @@ import com.google.gson.JsonObject;
|
||||
public final class TbGRoleUpLevel extends AbstractBean {
|
||||
public TbGRoleUpLevel(JsonObject _buf) {
|
||||
grade = _buf.get("grade").getAsInt();
|
||||
{ com.google.gson.JsonArray _json0_ = _buf.get("materials").getAsJsonArray(); int __n0 = _json0_.size(); materials = new cfg.TbGEntity.Resource[__n0]; int __index0=0; for(JsonElement __e0 : _json0_) { cfg.TbGEntity.Resource __v0; __v0 = cfg.TbGEntity.Resource.deserialize(__e0.getAsJsonObject()); materials[__index0++] = __v0; } }
|
||||
{ com.google.gson.JsonArray _json0_ = _buf.get("materials").getAsJsonArray(); int __n0 = _json0_.size(); materials = new cfg.TbGEntity.TResource[__n0]; int __index0=0; for(JsonElement __e0 : _json0_) { cfg.TbGEntity.TResource __v0; __v0 = cfg.TbGEntity.TResource.deserialize(__e0.getAsJsonObject()); materials[__index0++] = __v0; } }
|
||||
}
|
||||
|
||||
public static TbGRoleUpLevel deserialize(JsonObject _buf) {
|
||||
@@ -31,7 +31,7 @@ public final class TbGRoleUpLevel extends AbstractBean {
|
||||
/**
|
||||
* 消耗的材料
|
||||
*/
|
||||
public final cfg.TbGEntity.Resource[] materials;
|
||||
public final cfg.TbGEntity.TResource[] materials;
|
||||
|
||||
public static final int __ID__ = -877225318;
|
||||
|
||||
|
@@ -17,14 +17,14 @@ import com.google.gson.JsonObject;
|
||||
/**
|
||||
* 资源信息
|
||||
*/
|
||||
public final class Resource extends AbstractBean {
|
||||
public Resource(JsonObject _buf) {
|
||||
public final class TResource extends AbstractBean {
|
||||
public TResource(JsonObject _buf) {
|
||||
id = _buf.get("id").getAsInt();
|
||||
value = _buf.get("value").getAsInt();
|
||||
}
|
||||
|
||||
public static Resource deserialize(JsonObject _buf) {
|
||||
return new cfg.TbGEntity.Resource(_buf);
|
||||
public static TResource deserialize(JsonObject _buf) {
|
||||
return new cfg.TbGEntity.TResource(_buf);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,7 +36,7 @@ public final class Resource extends AbstractBean {
|
||||
*/
|
||||
public final int value;
|
||||
|
||||
public static final int __ID__ = -215001824;
|
||||
public static final int __ID__ = 125240784;
|
||||
|
||||
@Override
|
||||
public int getTypeId() { return __ID__; }
|
@@ -4,7 +4,10 @@ import cn.jisol.game.controller.argsresolver.PlayerMethodArgumentResolver;
|
||||
import cn.jisol.game.controller.argsresolver.UserMethodArgumentResolver;
|
||||
import cn.jisol.game.controller.game.GPlayerController;
|
||||
import cn.jisol.game.interceptor.LoginInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
@@ -28,10 +31,22 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/game/**");
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数解析
|
||||
* @param resolvers
|
||||
*/
|
||||
@Override
|
||||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
|
||||
WebMvcConfigurer.super.addArgumentResolvers(resolvers);
|
||||
resolvers.add(new UserMethodArgumentResolver());
|
||||
resolvers.add(new PlayerMethodArgumentResolver(this.playerController));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package cn.jisol.game.controller.exception;
|
||||
|
||||
import cn.jisol.ngame.util.NewsContext;
|
||||
import org.springframework.retry.RetryException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
@@ -17,6 +18,13 @@ public class HTTPExceptionHandler {
|
||||
return NewsContext.onMessage("Token 失效",null,LOGIN_TOKEN);
|
||||
}
|
||||
|
||||
//并发异常
|
||||
@ExceptionHandler(value = RetryException.class)
|
||||
@ResponseBody
|
||||
public NewsContext<String> retryExceptionHandler(RetryException e){
|
||||
return NewsContext.onFail("服务器忙碌啦~ 一会再试试.");
|
||||
}
|
||||
|
||||
//全局异常
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
@ResponseBody
|
||||
|
@@ -51,7 +51,7 @@ public class GNoviceController {
|
||||
return NewsContext.onFail("你已经有宠物了!");
|
||||
}else{
|
||||
//选择宠物
|
||||
if (playerPetService.save(PlayerPet.builder().petPlayerId(player.getPlayerId()).petTbId(petId).petGrade(0).build())){
|
||||
if (playerPetService.save(PlayerPet.builder().petPlayerId(player.getPlayerId()).petTbId(petId).petLevel(0).build())){
|
||||
return NewsContext.onSuccess("选择宠物成功");
|
||||
}else{
|
||||
return NewsContext.onSuccess("选择宠物失败");
|
||||
|
@@ -1,10 +1,14 @@
|
||||
package cn.jisol.game.controller.game;
|
||||
|
||||
import cfg.TB.TbGRoleUpLevel;
|
||||
import cfg.TbGEntity.TResource;
|
||||
import cn.jisol.game.controller.argsresolver.CurrentPlayer;
|
||||
import cn.jisol.game.data.TD;
|
||||
import cn.jisol.game.entity.game.Player;
|
||||
import cn.jisol.game.entity.game.PlayerPet;
|
||||
import cn.jisol.game.entity.game.Resource;
|
||||
import cn.jisol.game.service.PlayerPetService;
|
||||
import cn.jisol.game.service.ResourceService;
|
||||
import cn.jisol.game.vo.news.NewsResource;
|
||||
import cn.jisol.game.vo.news.ov.ResourceUpdateOV;
|
||||
import cn.jisol.game.vo.pet.PetUpStarOV;
|
||||
@@ -15,6 +19,8 @@ import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.retry.RetryException;
|
||||
import org.springframework.retry.annotation.Retryable;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@@ -34,6 +40,8 @@ public class GPlayerPetController {
|
||||
|
||||
@Autowired
|
||||
PlayerPetService playerPetService;
|
||||
@Autowired
|
||||
ResourceService resourceService;
|
||||
|
||||
//获取玩家宠物列表
|
||||
@ApiImplicitParams({})
|
||||
@@ -54,6 +62,7 @@ public class GPlayerPetController {
|
||||
@ApiOperation(value = "提升宠物星")
|
||||
@PostMapping("/up/star")
|
||||
@Transactional
|
||||
@Retryable(value = RetryException.class)
|
||||
public NewsResource<PlayerPet> onUpStar(@RequestBody PetUpStarOV info, @CurrentPlayer Player player){
|
||||
|
||||
//判断参数
|
||||
@@ -99,8 +108,8 @@ public class GPlayerPetController {
|
||||
pet.setPetStar(pet.getPetStar() + 1);
|
||||
}
|
||||
|
||||
//保存宠物信息
|
||||
playerPetService.updateById(pet);
|
||||
//保存宠物信息 (保存失败则重试)
|
||||
if(!playerPetService.updateById(pet)) throw new RetryException("");
|
||||
|
||||
//删除被合成的宠物
|
||||
playerPetService.removeByIds(info.getPets());
|
||||
@@ -122,15 +131,41 @@ public class GPlayerPetController {
|
||||
@ApiImplicitParams({})
|
||||
@ApiOperation(value = "提升宠物等级")
|
||||
@PostMapping("/up/level/{petId}")
|
||||
@Transactional
|
||||
@Retryable(value = RetryException.class)
|
||||
public NewsResource<PlayerPet> onUpLevel(@PathVariable Integer petId, @CurrentPlayer Player player){
|
||||
|
||||
//整理刷新数据
|
||||
ArrayList<ResourceUpdateOV> resources = new ArrayList<>();
|
||||
|
||||
//获取需要升级的宠物
|
||||
PlayerPet pet = playerPetService.getById(petId);
|
||||
if(Objects.isNull(pet) || !Objects.equals(pet.getPetPlayerId(), player.getPlayerId())){
|
||||
return NewsResource.onFail("你未拥有这个宠物");
|
||||
}
|
||||
|
||||
return NewsResource.onFail("升级成功");
|
||||
TbGRoleUpLevel resource = TD.DATA.getTbGRoleUpLevel().get(pet.getPetLevel() + 1);
|
||||
|
||||
//扣除玩家材料
|
||||
for (int i = 0; i < resource.materials.length; i++) {
|
||||
TResource res = resource.materials[i];
|
||||
Resource data = resourceService.addResourceValue(player.getPlayerId(), res.id, -((long) res.value));
|
||||
if (Objects.isNull(data)){
|
||||
return NewsResource.onFail("材料不够");
|
||||
}else{
|
||||
//刷新资源
|
||||
resources.add(ResourceUpdateOV.onUpdate(data)); //刷新资源
|
||||
}
|
||||
}
|
||||
|
||||
//升级
|
||||
pet.setPetLevel(pet.getPetLevel() + 1);
|
||||
|
||||
//保存
|
||||
if(!playerPetService.updateById(pet)) throw new RetryException("");
|
||||
resources.add(ResourceUpdateOV.onPet(ResourceUpdateOV.ResourceUpdateType.UPDATE,pet)); //刷新宠物
|
||||
|
||||
return NewsResource.onSuccess("升级成功",pet,resources.toArray(new ResourceUpdateOV[0]));
|
||||
|
||||
}
|
||||
|
||||
|
@@ -100,7 +100,7 @@ public class GOnHookController {
|
||||
|
||||
if(Objects.nonNull(creeps.remove(creepId))){
|
||||
//增加玩家金币 (默认添加100)
|
||||
Resource res = resourceService.addResourceValue(player.getPlayerId(), ResourceId.Gold, 100L);
|
||||
Resource res = resourceService.addResourceValue(player.getPlayerId(), ResourceId.Gold.id, 100L);
|
||||
return NewsResource.onSuccess(
|
||||
"卖出成功",
|
||||
true,
|
||||
@@ -135,7 +135,7 @@ public class GOnHookController {
|
||||
}
|
||||
|
||||
//捕捉
|
||||
PlayerPet pet = PlayerPet.builder().petPlayerId(player.getPlayerId()).petTbId(onHookPet.getPetTbId()).petGrade(0).build();
|
||||
PlayerPet pet = PlayerPet.builder().petPlayerId(player.getPlayerId()).petTbId(onHookPet.getPetTbId()).petLevel(0).build();
|
||||
if(playerPetService.save(pet)){
|
||||
return NewsContext.onSuccess("捕捉成功",pet);
|
||||
}else{
|
||||
|
@@ -3,6 +3,7 @@ package cn.jisol.game.entity.game;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.annotation.Version;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@@ -17,7 +18,9 @@ public class PlayerPet {
|
||||
private Long petId; //宠物唯一Id
|
||||
private Long petPlayerId; //宠物的玩家Id
|
||||
private Integer petTbId; //宠物配置表Id
|
||||
private Integer petGrade; //宠物等级
|
||||
private Integer petLevel; //宠物等级
|
||||
private Integer petStar; //宠物星级
|
||||
private Integer petStarExp; //宠物星级经验
|
||||
@Version
|
||||
private Integer version;
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ package cn.jisol.game.entity.game;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.annotation.Version;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@@ -19,5 +20,7 @@ public class Resource {
|
||||
private Long playerId; //玩家Id
|
||||
private int resourceTbId; //资源配置表Id
|
||||
private Long resourceValue; //资源数量
|
||||
@Version
|
||||
private Integer version;
|
||||
|
||||
}
|
||||
|
@@ -8,10 +8,10 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
||||
public interface ResourceService extends IService<Resource> {
|
||||
|
||||
//添加某个资源的数量
|
||||
public Resource addResourceValue(Long playerId, ResourceId type, Long value);
|
||||
public Resource addResourceValue(Long playerId, int type, Long value);
|
||||
|
||||
//初始化某个资源
|
||||
public Resource initResource(Long playerId, ResourceId type);
|
||||
public Resource initResource(Long playerId, int type);
|
||||
public Resource initResource(Resource resource);
|
||||
|
||||
}
|
||||
|
@@ -7,6 +7,8 @@ import cn.jisol.game.mapper.ResourceMapper;
|
||||
import cn.jisol.game.service.ResourceService;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.retry.RetryException;
|
||||
import org.springframework.retry.annotation.Retryable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@@ -22,15 +24,14 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> i
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public Resource addResourceValue(Long playerId, ResourceId type, Long value) {
|
||||
@Retryable(value = RetryException.class)
|
||||
public Resource addResourceValue(Long playerId, int type, Long value) {
|
||||
|
||||
//获取玩家资源
|
||||
Resource res = this.getOne(
|
||||
Wrappers.lambdaQuery(Resource.class)
|
||||
.eq(Resource::getPlayerId, playerId) //玩家
|
||||
.eq(Resource::getResourceTbId, type.id) //资源
|
||||
.last(" for update") //加锁
|
||||
.eq(Resource::getResourceTbId, type) //资源
|
||||
);
|
||||
|
||||
//如果没有资源则默认给一个0的资源
|
||||
@@ -42,16 +43,20 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> i
|
||||
//添加资源
|
||||
res.setResourceValue(res.getResourceValue() + value);
|
||||
|
||||
//保存资源
|
||||
this.saveOrUpdate(res);
|
||||
if(res.getResourceValue() <= 0){
|
||||
return null;
|
||||
}
|
||||
|
||||
//保存资源 (保存失败重试)
|
||||
if (!this.saveOrUpdate(res)) throw new RetryException("");
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
//初始化某个资源
|
||||
@Override
|
||||
public Resource initResource(Long playerId, ResourceId type) {
|
||||
Resource resource = Resource.builder().playerId(playerId).resourceTbId(type.id).resourceValue(0L).build();
|
||||
public Resource initResource(Long playerId, int type) {
|
||||
Resource resource = Resource.builder().playerId(playerId).resourceTbId(type).resourceValue(0L).build();
|
||||
this.initResource(resource);
|
||||
return resource;
|
||||
}
|
||||
|
@@ -8,5 +8,10 @@
|
||||
"id": 110002,
|
||||
"name": "防御",
|
||||
"sign": "Defend"
|
||||
},
|
||||
{
|
||||
"id": 110003,
|
||||
"name": "血量",
|
||||
"sign": "Blood"
|
||||
}
|
||||
]
|
@@ -1,6 +1,7 @@
|
||||
[
|
||||
{
|
||||
"id": 0,
|
||||
"Blood": 100,
|
||||
"Attack": 10,
|
||||
"Defend": 5
|
||||
}
|
||||
|
Reference in New Issue
Block a user