// For type VOID, we will skip any damage calculation if (player_card.type == VOID || ai_card.type == VOID) return;
int player_attack_point = calculate_attack_point(player_card, ai_card); int ai_attack_point = calculate_attack_point(ai_card, player_card);
// Damage calculation if (player_attack_point > ai_attack_point) { // Deal damage to the AI if the AI card's attack point is higher int diff = player_attack_point - ai_attack_point; game_data.life_lost_ai = diff; game_data.life_ai -= diff; } elseif (ai_attack_point > player_attack_point) { // Deal damage to the player if the player card's attack point is higher int diff = ai_attack_point - player_attack_point; game_data.life_lost_player = diff; game_data.life_player -= diff; } }
打开cardgame.cpp文件:
1
vi cardgame.cpp
编辑代码,添加代码如下:
1 2 3 4 5 6 7 8
game_data.hand_ai[ai_card_idx] = 0;
//在上面代码行后添加如下代码 //===下面为添加代码===
resolve_selected_cards(game_data);
//======
2.2 部署智能合约覆盖原合约
编译智能合约
编译wast文件
1
eosiocpp -o cardgame.wast cardgame.cpp
编译abi文件
1
eosiocpp -g cardgame.abi cardgame.cpp
解锁钱包
1
cleos wallet unlock -n gamewallet
部署智能合约
1
cleos -u https://api-kylin.eosasia.one set contract 123123gogogo /eos-work/contracts/cardgame -p 123123gogogo@active
至此,后端的合约重新部署完成。
3 编写前端代码
3.1 下载Resolution组件
1 2 3
cd /eos-work/frontend/src/components/Game/components/
import Card from './Card'; import GameInfo from './GameInfo'; import GameMat from './GameMat'; import HandCards from './HandCards'; import PlayerInfo from './PlayerInfo'; import PlayerProfile from './PlayerProfile'; import Resolution from './Resolution';
// Get latest user object from blockchain loadUser() { // Extract `setUser` of `UserAction` and `user.name` of UserReducer from redux const { setUser, user: { name } } = this.props; // Send request the blockchain by calling the ApiService, // Get the user object and store the `win_count`, `lost_count` and `game_data` object return ApiService.getUserByName(name).then(user => { setUser({ win_count: user.win_count, lost_count: user.lost_count, game: user.game_data, }); }); }
handleStartGame() { // Send a request to API (blockchain) to start game // And call `loadUser` again for react to render latest game status to UI return ApiService.startGame().then(()=>{ returnthis.loadUser(); }); }
handlePlayCard(cardIdx) { // Extract `user.game` of `UserReducer` from redux const { user: { game } } = this.props;
// If it is an empty card, not going to do anything if (game.hand_player[cardIdx] === 0) { return; }
// Send a request to API (blockchain) to play card with card index // And call `loadUser` again for react to render latest game status to UI return ApiService.playCard(cardIdx).then(()=>{ returnthis.loadUser(); }); }
render() { // Extract data from user data of `UserReducer` from redux const { user: { name, win_count, lost_count, game } } = this.props;
// Flag to indicate if the game has started or not // By checking if the deckCard of AI is still 17 (max card) const isGameStarted = game && game.deck_ai.length !== 17;