## Vara Network 开发案例 <img src="https://hackmd.io/_uploads/By_O0JA7A.png" width="640" alt="GEAR"> ### Battleship 战舰游戏 主网: https://battleship.vara.network/ 测试网: https://stg-battleship.vara-network.io/ --- <img src="https://hackmd.io/_uploads/H1gPelRXR.png" width="600" alt="GEAR"> Gear Wiki: https://wiki.gear-tech.io/docs/examples/Gaming/Battleship/ --- <img src="https://hackmd.io/_uploads/BySy4xCmA.png" width="400" alt="GEAR"> --- <img src="https://hackmd.io/_uploads/S1XeSeRXA.png" width="400" alt="GEAR"> --- ### 代码 Program: https://github.com/gear-foundation/dapps/tree/master/contracts/battleship [![Open in Gitpod](https://img.shields.io/badge/Open_in-Gitpod-white?logo=gitpod)](https://gitpod.io/#FOLDER=battleship/https://github.com/gear-foundation/dapps) 前端: https://github.com/gear-foundation/dapps/tree/master/frontend/apps/battleship [![Open in Gitpod](https://img.shields.io/badge/Open_in-Gitpod-white?logo=gitpod)](https://gitpod.io/#FOLDER=battleship/https://github.com/gear-foundation/dapps/tree/master/frontend/apps/) 后端(可选): https://github.com/gear-foundation/dapps-battleship-backend [![Open in Gitpod](https://img.shields.io/badge/Open_in-Gitpod-white?logo=gitpod)](https://gitpod.io/#/https://github.com/gear-foundation/dapps-battleship-backend) --- 测试代币领取: https://idea.gear-tech.io/programs?node=wss%3A%2F%2Ftestnet.vara.network ![image](https://hackmd.io/_uploads/H1eczlR7A.png) Gasless: 通过后端接口向没有代币的用户发放 gas voucher ![image](https://hackmd.io/_uploads/B18nMxAmR.png) --- 向 Gasless 后端发送请求, 发放 gas voucher ``` curl -X POST -H "Content-Type: application/json" -d '{"account":"0x123..."}' http://localhost:3000/ ``` 配置 `.env` ``` PORT= # 本地端口 NODE_ADDRESS= # RPC Endpoint GAME_ADDRESS="" # 游戏合约地址 PATH_TO_KEYS="" # 私钥文件 KEYRING_PASSPHRASE="" # 私钥文件密码 ``` 后端逻辑: https://github.com/gear-foundation/dapps-battleship-backend/blob/master/src/app.ts#L28 --- 合约数据结构: ``` struct Battleship { pub games: HashMap<ActorId, Game>, pub msg_id_to_game_id: BTreeMap<MessageId, ActorId>, pub bot_address: ActorId, pub admin: ActorId, pub sessions: HashMap<ActorId, Session>, pub config: Config, } ``` --- ``` pub struct Game { pub player_board: Vec<Entity>, pub bot_board: Vec<Entity>, pub player_ships: Ships, pub bot_ships: Ships, pub turn: Option<BattleshipParticipants>, pub start_time: u64, pub end_time: u64, pub total_shots: u64, pub game_over: bool, pub game_result: Option<BattleshipParticipants>, } ``` --- ``` fn player_move(&mut self, step: u8, session_for_account: Option<ActorId>) { // ... game.turn = Some(BattleshipParticipants::Bot); let board = game.get_hidden_field(); let msg_id = msg::send_with_gas( self.bot_address, BotBattleshipAction::Turn(board), self.config.gas_for_move, 0, ) .expect("Error in sending a message"); self.msg_id_to_game_id.insert(msg_id, player); msg::reply(BattleshipReply::MessageSentToBot, 0).expect("Error in sending a reply"); ``` --- # Questions <img src="https://hackmd.io/_uploads/SyK8P72d2.jpg" width="240" alt="GEAR"> <img src="https://hackmd.io/_uploads/By_kgE3_2.png" width="240" alt="VARA"> 课后答疑频道: https://t.me/Gear_CN
{"description":"Battleship Game on Vara Network Testnet","title":"Battleship","slideOptions":"{\"theme\":\"dark\",\"spotlight\":{\"enabled\":false},\"width\":1600,\"height\":900}","contributors":"[{\"id\":\"94262fbf-81ae-4ed7-933c-561a41bd977a\",\"add\":3981,\"del\":723}]"}
    307 views