资源刷新 以及 出售

This commit is contained in:
DESKTOP-5RP3AKU\Jisol
2023-11-20 03:47:00 +08:00
parent cb64a6c25f
commit 98b7a52b45
41 changed files with 1483 additions and 263 deletions

View File

@@ -0,0 +1,55 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
package cfg.TB;
import luban.*;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
public final class TbGResource extends AbstractBean {
public TbGResource(JsonObject _buf) {
id = _buf.get("id").getAsInt();
name = _buf.get("name").getAsString();
tig = _buf.get("tig").getAsString();
}
public static TbGResource deserialize(JsonObject _buf) {
return new cfg.TB.TbGResource(_buf);
}
/**
* id
*/
public final int id;
/**
* 资源名称
*/
public final String name;
/**
* 描述
*/
public final String tig;
public static final int __ID__ = 1780642599;
@Override
public int getTypeId() { return __ID__; }
@Override
public String toString() {
return "{ "
+ "(format_field_name __code_style field.name):" + id + ","
+ "(format_field_name __code_style field.name):" + name + ","
+ "(format_field_name __code_style field.name):" + tig + ","
+ "}";
}
}

View File

@@ -33,6 +33,8 @@ public final class Tables
public cfg.TbGMap getTbGMap() { return _tbgmap; }
private final cfg.TbGOnHookGlobal _tbgonhookglobal;
public cfg.TbGOnHookGlobal getTbGOnHookGlobal() { return _tbgonhookglobal; }
private final cfg.TbGResource _tbgresource;
public cfg.TbGResource getTbGResource() { return _tbgresource; }
public Tables(IJsonLoader loader) throws java.io.IOException {
_tbgglobal = new cfg.TbGGlobal(loader.load("tbgglobal"));
@@ -42,6 +44,7 @@ public final class Tables
_tbgroleskill = new cfg.TbGRoleSkill(loader.load("tbgroleskill"));
_tbgmap = new cfg.TbGMap(loader.load("tbgmap"));
_tbgonhookglobal = new cfg.TbGOnHookGlobal(loader.load("tbgonhookglobal"));
_tbgresource = new cfg.TbGResource(loader.load("tbgresource"));
}
}

View File

@@ -0,0 +1,37 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
package cfg;
import luban.*;
import com.google.gson.JsonElement;
public final class TbGResource {
private final java.util.HashMap<Integer, cfg.TB.TbGResource> _dataMap;
private final java.util.ArrayList<cfg.TB.TbGResource> _dataList;
public TbGResource(JsonElement _buf) {
_dataMap = new java.util.HashMap<Integer, cfg.TB.TbGResource>();
_dataList = new java.util.ArrayList<cfg.TB.TbGResource>();
for (com.google.gson.JsonElement _e_ : _buf.getAsJsonArray()) {
cfg.TB.TbGResource _v;
_v = cfg.TB.TbGResource.deserialize(_e_.getAsJsonObject());
_dataList.add(_v);
_dataMap.put(_v.id, _v);
}
}
public java.util.HashMap<Integer, cfg.TB.TbGResource> getDataMap() { return _dataMap; }
public java.util.ArrayList<cfg.TB.TbGResource> getDataList() { return _dataList; }
public cfg.TB.TbGResource get(int key) { return _dataMap.get(key); }
}

View File

@@ -1,10 +1,9 @@
package cn.jisol.game.controller.game;
import cfg.TB.TbGGlobal;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import cn.jisol.game.controller.argsresolver.CurrentPlayer;
import cn.jisol.game.data.GlobalIds;
import cn.jisol.game.data.GlobalId;
import cn.jisol.game.data.TD;
import cn.jisol.game.entity.game.Player;
import cn.jisol.game.entity.game.PlayerPet;
@@ -17,7 +16,6 @@ import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@@ -38,7 +36,7 @@ public class GNoviceController {
if(Objects.isNull(petId)) return NewsContext.onFail("请选择宠物");
//判断选择的宠物是否在配置表中
TbGGlobal info = TD.DATA.getTbGGlobal().get(GlobalIds.SELECT_PET_ID);
TbGGlobal info = TD.DATA.getTbGGlobal().get(GlobalId.SELECT_PET_ID);
//判断选择的宠物是否在其中
List<Integer> ids = JSONUtil.toList(info.args, Integer.class);
if (!ids.contains(petId)) return NewsContext.onFail("不可选择这个宠物!");

View File

@@ -0,0 +1,50 @@
package cn.jisol.game.controller.game;
import cn.jisol.game.controller.argsresolver.CurrentPlayer;
import cn.jisol.game.entity.game.Player;
import cn.jisol.game.entity.game.PlayerTactical;
import cn.jisol.game.entity.game.Resource;
import cn.jisol.game.service.ResourceService;
import cn.jisol.ngame.util.NewsContext;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.sql.Wrapper;
import java.util.List;
/**
* 资源
*/
@Api(value = "JNGameDemo - API", tags = {"资源 - API"})
@RestController
@RequestMapping("/game/resource")
@ResponseBody
public class GResourceController {
@Autowired
ResourceService resourceService;
//获取玩家的资源
@ApiImplicitParams({})
@ApiOperation(value = "获取玩家的资源")
@GetMapping("/get")
public NewsContext<List<Resource>> getInfo(@CurrentPlayer Player player){
//获取玩家全部资源列表
List<Resource> list = resourceService.list(
Wrappers.lambdaQuery(Resource.class)
.eq(Resource::getPlayerId, player.getPlayerId())
);
return NewsContext.onSuccess("获取成功",list);
}
}

View File

@@ -3,15 +3,19 @@ package cn.jisol.game.controller.game.mode;
import cfg.TB.TbGRole;
import cn.hutool.core.util.RandomUtil;
import cn.jisol.game.controller.argsresolver.CurrentPlayer;
import cn.jisol.game.data.GlobalIds;
import cn.jisol.game.data.GlobalId;
import cn.jisol.game.data.ResourceId;
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.proto.GOnHookMessage;
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.ngame.util.NewsContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
@@ -32,6 +36,8 @@ public class GOnHookController {
@Autowired
PlayerPetService playerPetService;
@Autowired
ResourceService resourceService;
//生成的野怪列表
public static Map<Long, Map<String, GOnHookMessage.GOnHookPet>> CREEPS = new HashMap<>();
@@ -54,7 +60,7 @@ public class GOnHookController {
}
//获取可生成的最大数量
int max = Integer.parseInt(TD.DATA.getTbGOnHookGlobal().get(GlobalIds.MODE_ON_HOOK_SPAWN_NUM).args);
int max = Integer.parseInt(TD.DATA.getTbGOnHookGlobal().get(GlobalId.MODE_ON_HOOK_SPAWN_NUM).args);
//获取配置表全部宠物
ArrayList<TbGRole> tdPets = TD.DATA.getTbGRole().getDataList();
@@ -83,8 +89,25 @@ public class GOnHookController {
@ApiImplicitParams({})
@ApiOperation(value = "出售野怪")
@PostMapping("/onSellCreeps/{creepId}")
public NewsContext<Boolean> onSellCreeps(@PathVariable String creepId){
return NewsContext.onSuccess("卖出成功");
public NewsResource<Boolean> onSellCreeps(@PathVariable String creepId, @CurrentPlayer Player player){
//玩家的野怪
Map<String, GOnHookMessage.GOnHookPet> creeps;
//找到野怪删除
if(Objects.isNull(creeps = CREEPS.get(player.getPlayerId()))) return NewsResource.onFail("卖出失败",false);
if(Objects.nonNull(creeps.remove(creepId))){
//增加玩家金币 (默认添加100)
Resource res = resourceService.addResourceValue(player.getPlayerId(), ResourceId.Gold, 100L);
return NewsResource.onSuccess(
"卖出成功",
true,
ResourceUpdateOV.onUpdate(res) //更新金币
);
}else{
return NewsResource.onFail("卖出失败",false);
}
}
//捕捉野怪

View File

@@ -1,11 +1,10 @@
package cn.jisol.game.data;
public interface GlobalIds {
public interface GlobalId {
//选择宠物配置表Id
int SELECT_PET_ID = 70001;
//OnHook模式 宠物生成数量
int MODE_ON_HOOK_SPAWN_NUM = 80001;

View File

@@ -0,0 +1,14 @@
package cn.jisol.game.data;
public enum ResourceId {
//金币资源
Gold(90001),
;
public int id;
ResourceId(int id) {
this.id = id;
}
}

View File

@@ -0,0 +1,23 @@
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 lombok.Builder;
import lombok.Data;
/**
* 资源类
*/
@Builder
@Data
@TableName("`resource`")
public class Resource {
@TableId(type = IdType.AUTO)
private Long resourceId; //资源Id
private Long playerId; //玩家Id
private int resourceTbId; //资源配置表Id
private Long resourceValue; //资源数量
}

View File

@@ -0,0 +1,11 @@
package cn.jisol.game.mapper;
import cn.jisol.game.entity.game.Player;
import cn.jisol.game.entity.game.Resource;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Component;
@Component
public interface ResourceMapper extends BaseMapper<Resource> {
}

View File

@@ -0,0 +1,17 @@
package cn.jisol.game.service;
import cn.jisol.game.data.ResourceId;
import cn.jisol.game.entity.game.PlayerPet;
import cn.jisol.game.entity.game.Resource;
import com.baomidou.mybatisplus.extension.service.IService;
public interface ResourceService extends IService<Resource> {
//添加某个资源的数量
public Resource addResourceValue(Long playerId, ResourceId type, Long value);
//初始化某个资源
public Resource initResource(Long playerId, ResourceId type);
public Resource initResource(Resource resource);
}

View File

@@ -0,0 +1,63 @@
package cn.jisol.game.service.impl;
import cn.jisol.game.data.ResourceId;
import cn.jisol.game.entity.game.Player;
import cn.jisol.game.entity.game.Resource;
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.stereotype.Service;
import java.util.Objects;
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements ResourceService {
/**
* @param playerId 玩家Id
* @param type 资源类型
* @param value 添加的数量
* @return
*/
@Override
public Resource addResourceValue(Long playerId, ResourceId type, Long value) {
//获取玩家资源
Resource res = this.getOne(
Wrappers.lambdaQuery(Resource.class)
.eq(Resource::getPlayerId, playerId) //玩家
.eq(Resource::getResourceTbId, type.id) //资源
);
//如果没有资源则默认给一个0的资源
if(Objects.isNull(res)){
//创建初始资源
res = this.initResource(playerId,type);
}
//添加资源
res.setResourceValue(res.getResourceValue() + value);
//保存资源
this.saveOrUpdate(res);
return res;
}
//初始化某个资源
@Override
public Resource initResource(Long playerId, ResourceId type) {
Resource resource = Resource.builder().playerId(playerId).resourceTbId(type.id).resourceValue(0L).build();
this.initResource(resource);
return resource;
}
@Override
public Resource initResource(Resource resource) {
this.saveOrUpdate(resource);
return resource;
}
}

View File

@@ -0,0 +1,53 @@
package cn.jisol.game.vo.news;
import cn.jisol.game.entity.game.Resource;
import cn.jisol.game.vo.news.ov.ResourceUpdateOV;
import cn.jisol.ngame.util.NewsContext;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.Builder;
import lombok.experimental.SuperBuilder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
//返回带资源更新的消息
@SuperBuilder
@ApiResponses({
@ApiResponse(code = 200,message = "成功"),
@ApiResponse(code = 500,message = "失败"),
})
public class NewsResource<T> extends NewsContext<T> {
//资源刷新
public List<ResourceUpdateOV> resources = new ArrayList<>();
//添加一个刷新资源数据
public void addUpdateResource(Resource resource){
this.resources.add(ResourceUpdateOV.onUpdate(resource));
}
public static <T> NewsResource<T> onSuccess(String msg){
return onMessage(msg,null,NEWS_STATE_SUCCESS,new ArrayList<>());
}
public static <T> NewsResource<T> onSuccess(String msg,T data){
return onMessage(msg,data,NEWS_STATE_SUCCESS,new ArrayList<>());
}
public static <T> NewsResource<T> onSuccess(String msg,T data,ResourceUpdateOV... resource){
return onMessage(msg,data,NEWS_STATE_SUCCESS, Arrays.asList(resource));
}
public static <T> NewsResource<T> onFail(String msg){
return onMessage(msg,null,NEWS_STATE_FAIL,new ArrayList<>());
}
public static <T> NewsResource<T> onFail(String msg, T data){
return onMessage(msg,data,NEWS_STATE_FAIL,new ArrayList<>());
}
public static <T> NewsResource<T> onMessage(String msg, T data, Integer state,List<ResourceUpdateOV> resource){
return (NewsResource<T>) NewsResource.builder().state(state).msg(msg).data(data).resources(resource).build();
}
}

View File

@@ -0,0 +1,29 @@
package cn.jisol.game.vo.news.ov;
import cn.jisol.game.entity.game.Resource;
import lombok.Builder;
import lombok.Data;
//资源更新类
@Builder
@Data
public class ResourceUpdateOV {
Resource resource;
//操作
int operation = 0;
//资源更新类型
public interface ResourceUpdateType{
static int UPDATE = 0; //刷新资源数量
static int MINUS = 1; //减少资源数量
static int ADD = 2; //添加资源数量
}
//返回一个刷新的资源更新类
public static ResourceUpdateOV onUpdate(Resource resource){
return ResourceUpdateOV.builder().operation(ResourceUpdateType.UPDATE).resource(resource).build();
}
}

View File

@@ -0,0 +1,7 @@
[
{
"id": 90001,
"name": "金币",
"tig": "金币 (目前叫做金币 名称还没有定) 游戏的基础资源 用于升级宠物"
}
]