Ai
3 Star 0 Fork 0

纸豪/CardGame

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ShopController.java 7.14 KB
一键复制 编辑 原始数据 按行查看 历史
package com.cardgame.controller;
import com.cardgame.dao.ArchivalDao;
import com.cardgame.dao.CardDao;
import com.cardgame.dao.RoleDao;
import com.cardgame.model.card.*;
import com.cardgame.model.role.Role;
import com.cardgame.model.archival.Archival;
import com.cardgame.model.shop.Shop;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import java.util.*;
import java.util.stream.Collectors;
@Controller
@RequestMapping("/api/shop")
public class ShopController {
private final Shop shop;
private final Archival archival;
private final CardDao cardDao;
private final RoleDao roleDao;
public ShopController(Shop shop, ArchivalDao archivalDao, CardDao cardDao, RoleDao roleDao) {
this.shop = shop;
this.archival = archivalDao.getArchival();
this.cardDao = cardDao;
this.roleDao = roleDao;
}
@GetMapping("/")
public String shopPage() {
return "character-shop.html";
}
@GetMapping("/available-characters")
@ResponseBody
public List<Map<String, Object>> getAvailableCharacters() {
// 获取所有角色ID
List<Integer> allRoleIds = shop.getRoles();
// 获取已拥有的角色ID
List<Integer> ownedRoleIds = archival.getRoles();
// 过滤出未拥有的角色
return allRoleIds.stream()
.filter(id -> !ownedRoleIds.contains(id))
.map(id -> {
Role role = roleDao.getRoleById(id);
Map<String, Object> character = new HashMap<>();
character.put("id", id);
character.put("name", role.getName());
character.put("introduction", role.getIntroduction());
character.put("hp", role.getHp());
character.put("mp", role.getMp());
character.put("price", role.getPrice());
character.put("imageUrl", role.getImageUrl());
Map<String, Object> skill = new HashMap<>();
skill.put("introduction", role.getSkill().getIntroduction());
skill.put("addSelfHp", role.getSkill().getAddSelfHp());
skill.put("addSelfMp", role.getSkill().getAddSelfMp());
skill.put("addSelfMpMax", role.getSkill().getAddSelfMpMax());
skill.put("addEnemyHp", role.getSkill().getAddEnemyHp());
skill.put("addEnemyMp", role.getSkill().getAddEnemyMp());
skill.put("cost", role.getSkill().getCost());
character.put("skill", skill);
return character;
})
.collect(Collectors.toList());
}
@GetMapping("/available-cards")
@ResponseBody
public List<Map<String, Object>> getAvailableCards() {
// 获取所有卡牌ID
List<Integer> allCardIds = shop.getCards();
// 过滤出未拥有的卡牌
return allCardIds.stream()
.map(id -> {
Card card_O = cardDao.getCardById(id);
if (card_O == null) {
System.err.println("❗ 警告:cardDao.getCardById(" + id + ") 返回 null!");
throw new NullPointerException("Card with ID " + id + " not found");
}
if (card_O.getType() == null) {
System.err.println("❗ 警告:卡牌 ID = " + id + " 类型为空!类 = " + card_O.getClass().getSimpleName());
}
Map<String, Object> card = new HashMap<>();
card.put("id", id);
card.put("name", card_O.getName());
card.put("introduction", card_O.getIntroduction());
card.put("type", card_O.getType());
card.put("price", card_O.getPrice());
card.put("imageUrl", card_O.getImageUrl());
card.put("cost", card_O.getCost());
// 根据卡牌类型添加不同的属性
switch (card_O.getType()) {
case "role":
card.put("hp", ((RoleCard) card_O).getHealth());
card.put("attack", ((RoleCard) card_O).getAttack());
break;
case "action":
card.put("addSelfHP", ((ActionCard) card_O).getAddSelfHP());
card.put("addSelfMP", ((ActionCard) card_O).getAddSelfMP());
card.put("addSelfMPMAX", ((ActionCard) card_O).getAddSelfMPMAX());
card.put("addEnemyHP", ((ActionCard) card_O).getAddEnemyHP());
card.put("addEnemyMP", ((ActionCard) card_O).getAddEnemyMP());
break;
case "total_weapon":
card.put("addHPMax", ((TotalWeaponCard) card_O).getAddHPMax());
break;
case "role_weapon":
card.put("addHPMax", ((RoleWeaponCard) card_O).getAddHPMax());
card.put("addAttack", ((RoleWeaponCard) card_O).getAddAttack());
break;
}
return card;
})
.collect(Collectors.toList());
}
@PostMapping("/purchase-character")
@ResponseBody
public ResponseEntity<Map<String, Object>> purchaseCharacter(@RequestBody Map<String, Integer> request) {
Integer characterId = request.get("characterId");
Map<String, Object> response = new HashMap<>();
if (characterId == null) {
response.put("success", false);
response.put("message", "角色ID不能为空");
return ResponseEntity.badRequest().body(response);
}
boolean success = shop.purchaseRole(characterId);
if (success) {
response.put("success", true);
response.put("message", "购买成功");
} else {
response.put("success", false);
response.put("message", "购买失败,可能是金钱不足或角色已拥有");
}
return ResponseEntity.ok(response);
}
@PostMapping("/purchase-card")
@ResponseBody
public ResponseEntity<Map<String, Object>> purchaseCard(@RequestBody Map<String, Integer> request) {
Integer cardId = request.get("cardId");
Map<String, Object> response = new HashMap<>();
if (cardId == null) {
response.put("success", false);
response.put("message", "卡牌ID不能为空");
return ResponseEntity.badRequest().body(response);
}
boolean success = shop.purchaseCard(cardId);
if (success) {
response.put("success", true);
response.put("message", "购买成功");
} else {
response.put("success", false);
response.put("message", "购买失败,可能是金钱不足或卡牌已拥有");
}
return ResponseEntity.ok(response);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhihao2023/card-game.git
git@gitee.com:zhihao2023/card-game.git
zhihao2023
card-game
CardGame
master

搜索帮助