Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson 8 #8

Open
wants to merge 169 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 61 commits
Commits
Show all changes
169 commits
Select commit Hold shift + click to select a range
82affe4
Lesson 0
rickykung Oct 9, 2018
1e306be
Lesson 1
rickykung Sep 4, 2018
af2b28f
Lesson 2
rickykung Sep 4, 2018
920ac91
Lesson 3
rickykung Sep 4, 2018
ca1972e
Lesson 4
rickykung Sep 4, 2018
2bd9f38
Lesson 5
rickykung Sep 10, 2018
603c8bd
Lesson 6
rickykung Sep 10, 2018
fe56545
Lesson 7
rickykung Sep 10, 2018
a4b118c
Lesson 8
rickykung Sep 4, 2018
77a69fe
Update lesson 0 to support eosio.cdt
Nov 20, 2018
937cb5b
Merge branch 'lesson-0' into lesson-1
Nov 20, 2018
a30c5a3
Update lesson 1 to support eosio.cdt
Nov 20, 2018
49ba5d8
Merge branch 'lesson-1' into lesson-2
Nov 20, 2018
2cc520b
Update lesson 2 to support eosio.cdt
Nov 20, 2018
5ec42ae
Merge branch 'lesson-2' into lesson-3
Nov 20, 2018
c989fb8
Merge branch 'lesson-3' into lesson-4
Nov 20, 2018
01d4987
Update lesson 4 to support eosio.cdt
Nov 20, 2018
594f4de
Merge branch 'lesson-4' into lesson-5
Nov 20, 2018
af145db
Update lesson 5 to support eosio.cdt
Nov 20, 2018
4954f36
Merge branch 'lesson-5' into lesson-6
Nov 20, 2018
e966c01
Update lesson 6 to support eosio.cdt
Nov 20, 2018
e72a2fb
Merge branch 'lesson-6' into lesson-7
Nov 20, 2018
f952549
Update lesson 7 to support eosio.cdt
Nov 20, 2018
eebe253
Merge branch 'lesson-7' into lesson-8
Nov 20, 2018
4b9f52e
Add gitignore
Nov 20, 2018
45b2624
Merge branch 'master' into staging-lesson-0
Nov 20, 2018
0ad0c62
Merge branch 'staging-lesson-0' into staging-lesson-1
Nov 20, 2018
2905bd5
Merge branch 'staging-lesson-1' into staging-lesson-2
Nov 20, 2018
b8303fc
Merge branch 'staging-lesson-2' into staging-lesson-3
Nov 20, 2018
f1c3552
Merge branch 'staging-lesson-3' into staging-lesson-4
Nov 20, 2018
c4d9fc9
Merge branch 'staging-lesson-4' into staging-lesson-5
Nov 20, 2018
3c9a3f4
Merge branch 'staging-lesson-5' into staging-lesson-6
Nov 20, 2018
0ae92d7
Merge branch 'staging-lesson-6' into staging-lesson-7
Nov 20, 2018
7932b06
Merge branch 'staging-lesson-7' into staging-lesson-8
Nov 20, 2018
b25189f
Build own docker image
Jan 17, 2019
6aed09c
Merge branch 'staging-lesson-1' into staging-lesson-2
Jan 17, 2019
025abd7
Update eosjs
Jan 17, 2019
bf23b3d
Merge branch 'staging-lesson-2' into staging-lesson-3
Jan 17, 2019
88fbc4e
Update to eosjs beta3
Jan 17, 2019
382f72d
Merge branch 'staging-lesson-3' into staging-lesson-4
Jan 17, 2019
8821c46
Merge branch 'staging-lesson-4' into staging-lesson-5
Jan 17, 2019
3530769
Merge branch 'staging-lesson-5' into staging-lesson-6
Jan 17, 2019
b995ff3
Merge branch 'staging-lesson-6' into staging-lesson-7
Jan 17, 2019
47bc764
Merge branch 'staging-lesson-7' into staging-lesson-8
Jan 17, 2019
3d64f4e
Update to eos 1.7.3 and eosio.cdt 1.6.1
May 23, 2019
1e4bc3e
Merge branch 'staging-lesson-1' into staging-lesson-2
May 23, 2019
3e55027
Update to eosjs v20
May 23, 2019
af502bc
Merge branch 'staging-lesson-2' into staging-lesson-3
May 23, 2019
b5b53bf
Merge branch 'staging-lesson-3' into staging-lesson-4
May 23, 2019
f8e6a97
Merge branch 'staging-lesson-4' into staging-lesson-5
May 23, 2019
6b2248d
Merge branch 'staging-lesson-5' into staging-lesson-6
May 23, 2019
d373253
Merge branch 'staging-lesson-6' into staging-lesson-7
May 23, 2019
f6ce10b
Merge branch 'staging-lesson-7' into staging-lesson-8
May 23, 2019
116d188
Updated to EOSIO v1.8.0
Jul 2, 2019
0f32534
Merge branch 'staging-lesson-1' into staging-lesson-2
Jul 2, 2019
b884014
Merge branch 'staging-lesson-2' into staging-lesson-3
Jul 2, 2019
43cacc9
Merge branch 'staging-lesson-3' into staging-lesson-4
Jul 2, 2019
827ed62
Merge branch 'staging-lesson-4' into staging-lesson-5
Jul 2, 2019
e2b1be6
Merge branch 'staging-lesson-5' into staging-lesson-6
Jul 2, 2019
82c63f7
Merge branch 'staging-lesson-6' into staging-lesson-7
Jul 2, 2019
c078f02
Merge branch 'staging-lesson-7' into staging-lesson-8
Jul 2, 2019
7925dd3
Updated to EOSIO v1.8.1
Jul 11, 2019
4775554
Merge branch 'staging-lesson-1' into staging-lesson-2
Jul 11, 2019
544966b
Merge branch 'staging-lesson-2' into staging-lesson-3
Jul 11, 2019
e6f7134
Merge branch 'staging-lesson-3' into staging-lesson-4
Jul 11, 2019
76c5ac5
Merge branch 'staging-lesson-4' into staging-lesson-5
Jul 11, 2019
8025695
Merge branch 'staging-lesson-5' into staging-lesson-6
Jul 11, 2019
c71dc20
Merge branch 'staging-lesson-6' into staging-lesson-7
Jul 11, 2019
568e949
Merge branch 'staging-lesson-7' into staging-lesson-8
Jul 11, 2019
469fe87
Updated all dependencies
Oct 31, 2019
8cd0c1c
- Updated all dependencies
Oct 31, 2019
d5c2337
Merge branch 'staging-lesson-2' into staging-lesson-3
Oct 31, 2019
a54995c
Updated package-lock.json
Nov 1, 2019
00d5af9
Merge branch 'staging-lesson-3' into staging-lesson-4
Nov 1, 2019
b9f0ec1
Merge branch 'staging-lesson-4' into staging-lesson-5
Nov 1, 2019
38ad4b4
Merge branch 'staging-lesson-5' into staging-lesson-6
Nov 1, 2019
f02c0a0
Merge branch 'staging-lesson-6' into staging-lesson-7
Nov 1, 2019
45e7dcb
Merge branch 'staging-lesson-7' into staging-lesson-8
Nov 1, 2019
830fcd1
Updated sample.cpp
Nov 7, 2019
12fd145
Merge branch 'staging-lesson-0' into staging-lesson-1
Nov 7, 2019
2104e69
Merge branch 'staging-lesson-1' into staging-lesson-2
Nov 7, 2019
2452ffe
Merge branch 'staging-lesson-2' into staging-lesson-3
Nov 7, 2019
9bc7bc6
Merge branch 'staging-lesson-3' into staging-lesson-4
Nov 7, 2019
9e12fc4
Merge branch 'staging-lesson-4' into staging-lesson-5
Nov 7, 2019
5c4ddae
Merge branch 'staging-lesson-5' into staging-lesson-6
Nov 7, 2019
904a39c
Merge branch 'staging-lesson-6' into staging-lesson-7
Nov 7, 2019
78a331b
Merge branch 'staging-lesson-7' into staging-lesson-8
Nov 7, 2019
d7cd9c1
- Updated .cpp files to use 'eosio' rather than 'eosiolib'
Nov 26, 2019
c0f7f01
Merge branch 'staging-lesson-1' into staging-lesson-2
Nov 26, 2019
329858f
Updated package-lock
Nov 26, 2019
258ca72
Merge branch 'staging-lesson-2' into staging-lesson-3
Nov 26, 2019
70ad79e
Merge branch 'staging-lesson-3' into staging-lesson-4
Nov 26, 2019
620e9a3
Changed .cpp import back to eosiolib
Nov 26, 2019
23afb8b
Merge branch 'staging-lesson-1' into staging-lesson-2
Nov 26, 2019
03cf8c9
Merge branch 'staging-lesson-2' into staging-lesson-3
Nov 26, 2019
9dea2b2
Merge branch 'staging-lesson-3' into staging-lesson-4
Nov 26, 2019
183dafb
Merge branch 'staging-lesson-4' into staging-lesson-5
Nov 26, 2019
43a3028
Merge branch 'staging-lesson-5' into staging-lesson-6
Nov 26, 2019
09af8bb
Merge branch 'staging-lesson-6' into staging-lesson-7
Nov 26, 2019
52ebd12
Merge branch 'staging-lesson-7' into staging-lesson-8
Nov 26, 2019
6a84c98
Update to cdt 1.6.3 and fix abi not found issue
Dec 4, 2019
21f7899
Merge branch 'staging-lesson-1' into staging-lesson-2
Dec 4, 2019
54427f3
Set abi once abi is generated in lesson 2
Dec 4, 2019
6b1b559
Merge branch 'staging-lesson-2' into staging-lesson-3
Dec 4, 2019
b4682a9
Merge branch 'staging-lesson-3' into staging-lesson-4
Dec 4, 2019
e3b3e81
Update eosio_assert and now() to support the latest eosio library
Dec 4, 2019
c524f08
Merge branch 'staging-lesson-4' into staging-lesson-5
Dec 4, 2019
2814643
Merge branch 'staging-lesson-5' into staging-lesson-6
Dec 4, 2019
344f603
Merge branch 'staging-lesson-6' into staging-lesson-7
Dec 4, 2019
25e61f0
Change eosio_assert to check
Dec 4, 2019
0ce4c69
Merge branch 'staging-lesson-7' into staging-lesson-8
Dec 4, 2019
ff27c86
- Updated eos to version 2.0.0
Jan 17, 2020
309c508
Merge branch 'staging-lesson-1' into staging-lesson-2
Jan 17, 2020
8a0c0dc
Merge branch 'staging-lesson-2' into staging-lesson-3
Jan 17, 2020
755eaf1
Merge branch 'staging-lesson-3' into staging-lesson-4
Jan 17, 2020
ea4683a
Merge branch 'staging-lesson-4' into staging-lesson-5
Jan 17, 2020
3e45b43
Merge branch 'staging-lesson-5' into staging-lesson-6
Jan 17, 2020
b3e76a2
Merge branch 'staging-lesson-6' into staging-lesson-7
Jan 17, 2020
10e75dd
Merge branch 'staging-lesson-7' into staging-lesson-8
Jan 17, 2020
5f94ea7
Updated deploy_contract script to set contract if an abi file is foun…
Feb 3, 2020
fcaa687
Merge branch 'staging-lesson-1' into staging-lesson-2
Feb 3, 2020
6a4b548
Merge branch 'staging-lesson-2' into staging-lesson-3
Feb 3, 2020
b8844c3
Merge branch 'staging-lesson-3' into staging-lesson-4
Feb 3, 2020
9d90d21
Merge branch 'staging-lesson-4' into staging-lesson-5
Feb 3, 2020
792e9d7
Merge branch 'staging-lesson-5' into staging-lesson-6
Feb 3, 2020
04ff4d5
Merge branch 'staging-lesson-6' into staging-lesson-7
Feb 3, 2020
a12223b
Merge branch 'staging-lesson-7' into staging-lesson-8
Feb 3, 2020
5d9e44c
Removed EOSIO_DISPATCH from cardgame.cpp
Feb 5, 2020
d1123b2
Merge branch 'staging-lesson-1' into staging-lesson-2
Feb 5, 2020
c4aeab6
Merge branch 'staging-lesson-2' into staging-lesson-3
Feb 5, 2020
ac30ae4
Merge branch 'staging-lesson-3' into staging-lesson-4
Feb 5, 2020
dd0aca2
Merge branch 'staging-lesson-4' into staging-lesson-5
Feb 5, 2020
8010fef
Merge branch 'staging-lesson-5' into staging-lesson-6
Feb 5, 2020
401e114
Merge branch 'staging-lesson-6' into staging-lesson-7
Feb 5, 2020
3fe4cad
Merge branch 'staging-lesson-7' into staging-lesson-8
Feb 5, 2020
6343641
- Moved 'contracts' folder from 'eosio_docker' to root
Mar 31, 2020
4082e6f
Merge branch 'staging-lesson-1' into staging-lesson-2
Mar 31, 2020
ba6e306
Merge branch 'staging-lesson-2' into staging-lesson-3
Mar 31, 2020
de3d084
Merge branch 'staging-lesson-3' into staging-lesson-4
Mar 31, 2020
52a6ee8
Merge branch 'staging-lesson-4' into staging-lesson-5
Mar 31, 2020
ddc8ba8
Merge branch 'staging-lesson-5' into staging-lesson-6
Mar 31, 2020
bcc6fb6
Merge branch 'staging-lesson-6' into staging-lesson-7
Mar 31, 2020
41364f2
Merge branch 'staging-lesson-7' into staging-lesson-8
Mar 31, 2020
5993812
- Updated eos to version 2.0.5
Apr 24, 2020
c9fd8a1
Merge branch 'staging-lesson-1' into staging-lesson-2
Apr 24, 2020
e0d0249
Merge branch 'staging-lesson-2' into staging-lesson-3
Apr 24, 2020
1096026
Merge branch 'staging-lesson-3' into staging-lesson-4
Apr 24, 2020
3f0028e
Merge branch 'staging-lesson-4' into staging-lesson-5
Apr 24, 2020
ec3ce15
Merge branch 'staging-lesson-5' into staging-lesson-6
Apr 24, 2020
8191818
Merge branch 'staging-lesson-6' into staging-lesson-7
Apr 24, 2020
e2ba983
Merge branch 'staging-lesson-7' into staging-lesson-8
Apr 24, 2020
30338b7
- Updated README
Apr 24, 2020
874f1f2
Merge branch 'staging-lesson-0' into staging-lesson-1
Apr 24, 2020
afaefa4
Merge branch 'staging-lesson-1' into staging-lesson-2
Apr 24, 2020
4aad4cd
Merge branch 'staging-lesson-2' into staging-lesson-3
Apr 24, 2020
b527ea6
Merge branch 'staging-lesson-3' into staging-lesson-4
Apr 24, 2020
dcbe256
Merge branch 'staging-lesson-4' into staging-lesson-5
Apr 24, 2020
9abb61f
Merge branch 'staging-lesson-5' into staging-lesson-6
Apr 24, 2020
e2ea311
Merge branch 'staging-lesson-6' into staging-lesson-7
Apr 24, 2020
798bd9b
Merge branch 'staging-lesson-7' into staging-lesson-8
Apr 24, 2020
90c2d8a
Updated license link in Contributing.md
Apr 27, 2020
45e90bf
Merge branch 'staging-lesson-0' into staging-lesson-1
Apr 27, 2020
37971f8
Merge branch 'staging-lesson-1' into staging-lesson-2
Apr 27, 2020
ab3396d
Merge branch 'staging-lesson-2' into staging-lesson-3
Apr 27, 2020
a8ab639
Merge branch 'staging-lesson-3' into staging-lesson-4
Apr 27, 2020
c2e80c3
Merge branch 'staging-lesson-4' into staging-lesson-5
Apr 27, 2020
f13ddc9
Merge branch 'staging-lesson-5' into staging-lesson-6
Apr 27, 2020
ab175d7
Merge branch 'staging-lesson-6' into staging-lesson-7
Apr 27, 2020
4fb77b1
Merge branch 'staging-lesson-7' into staging-lesson-8
Apr 27, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# dependencies
node_modules/

# production
frontend/build

#compile contract files
**/*.abi
**/*.wasm
**/*.wast

#blockchain data
**/data/*

# misc
.DS_Store

npm-debug.log*
yarn-debug.log*
yarn-error.log*
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v8.11.3
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM ubuntu:18.04

RUN apt-get update && apt-get install -y wget sudo curl
RUN wget https://github.com/EOSIO/eosio.cdt/releases/download/v1.6.1/eosio.cdt_1.6.1-1_amd64.deb
RUN apt-get update && sudo apt install -y ./eosio.cdt_1.6.1-1_amd64.deb
RUN wget https://github.com/EOSIO/eos/releases/download/v1.8.0/eosio_1.8.0-1-ubuntu-18.04_amd64.deb
RUN apt-get update && sudo apt install -y ./eosio_1.8.0-1-ubuntu-18.04_amd64.deb
48 changes: 43 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
# Elemental Battles Tutorial Lessons
# Elemental Battles Tutorial Lesson 8

The Elemental Battles Tutorial is divided into easy to follow lessons that take you through the process of creating your own fully-functional blockchain-based dApp.
- *Account*: `player1`
- *Private Key*: `5KFyaxQW8L6uXFB6wSgC44EsAbzC7ideyhhQ68tiYfdKQp69xKo`
The account information is available in [eosio_docker/scripts/accounts.json](eosio_docker/scripts/accounts.json). The key pair in this file is generated **FOR TESTING ONLY** so please **DO NOT** use them for any other purposes.

Each lesson will introduce new concepts and showcase how to include them in the existing code. Additionally, we will display explanations and source code side-by-side to make following the tutorial easy.
## Prerequisites

## About this repository
Make sure Docker and Node.js are installed

This repository contains the source code used in each of the tutorial lesson. Each lesson's code are stored in a branch. You can find all the lesson branches from the list below.
* Install Docker: https://docs.docker.com/docker-for-mac/install/
* Install Node.js: https://nodejs.org/en/

The DApp and eosio will occupy the ports 3000, 8888 and 9876. Make sure nothing else is already running on these ports.

Clone the repository:
```sh
git clone https://github.com/EOSIO/eosio-card-game-repo.git
```

The following guide assumes you are using macOS.

## Quick start - Run the DApp

In this section we provide a single command script to run all the commands needed to start both the blockchain and UI. For more detail on each component see the `Detailed guide` below.

**To start**
```sh
./quick_start.sh
```

The above command will execute the following in sequence:

1. `first_time_setup.sh`
2. `start_eosio_docker.sh`
3. `start_frontend.sh`

- Login with the following credentials:

**To stop**, press `ctrl+c` on your keyboard, and execute:
```sh
docker stop eosio_cardgame_container
```

## Detailed guide

Please refer to [eosio-project-boilerplate-simple - Detailed guide](https://github.com/EOSIO/eosio-project-boilerplate-simple/blob/master/README.md#detailed-guide) for more information. This repository is using the similar structure as that.

## Lesson List

Expand Down
108 changes: 108 additions & 0 deletions eosio_docker/contracts/cardgame/cardgame.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include "gameplay.cpp"

void cardgame::login(name username) {
// Ensure this action is authorized by the player
require_auth(username);

// Create a record in the table if the player doesn't exist in our app yet
auto user_iterator = _users.find(username.value);
if (user_iterator == _users.end()) {
user_iterator = _users.emplace(username, [&](auto& new_user) {
new_user.username = username;
});
}
}

void cardgame::startgame(name username) {
// Ensure this action is authorized by the player
require_auth(username);

auto& user = _users.get(username.value, "User doesn't exist");

_users.modify(user, username, [&](auto& modified_user) {
// Create a new game
game game_data;

// Draw 4 cards each for the player and the AI
for (uint8_t i = 0; i < 4; i++) {
draw_one_card(game_data.deck_player, game_data.hand_player);
draw_one_card(game_data.deck_ai, game_data.hand_ai);
}

// Assign the newly created game to the player
modified_user.game_data = game_data;
});
}

void cardgame::endgame(name username) {
// Ensure this action is authorized by the player
require_auth(username);

// Get the user and reset the game
auto& user = _users.get(username.value, "User doesn't exist");
_users.modify(user, username, [&](auto& modified_user) {
modified_user.game_data = game();
});
}

void cardgame::playcard(name username, uint8_t player_card_idx) {
// Ensure this action is authorized by the player
require_auth(username);

// Checks that selected card is valid
eosio_assert(player_card_idx < 4, "playcard: Invalid hand index");

auto& user = _users.get(username.value, "User doesn't exist");

// Verify game status is suitable for the player to play a card
eosio_assert(user.game_data.status == ONGOING,
"playcard: This game has ended. Please start a new one");
eosio_assert(user.game_data.selected_card_player == 0,
"playcard: The player has played his card this turn!");

_users.modify(user, username, [&](auto& modified_user) {
game& game_data = modified_user.game_data;

// Assign the selected card from the player's hand
game_data.selected_card_player = game_data.hand_player[player_card_idx];
game_data.hand_player[player_card_idx] = 0;

// AI picks a card
int ai_card_idx = ai_choose_card(game_data);
game_data.selected_card_ai = game_data.hand_ai[ai_card_idx];
game_data.hand_ai[ai_card_idx] = 0;

resolve_selected_cards(game_data);

update_game_status(modified_user);
});
}

void cardgame::nextround(name username) {
// Ensure this action is authorized by the player
require_auth(username);

auto& user = _users.get(username.value, "User doesn't exist");

// Verify game status
eosio_assert(user.game_data.status == ONGOING,
"nextround: This game has ended. Please start a new one.");
eosio_assert(user.game_data.selected_card_player != 0 && user.game_data.selected_card_ai != 0,
"nextround: Please play a card first.");

_users.modify(user, username, [&](auto& modified_user) {
game& game_data = modified_user.game_data;

// Reset selected card and damage dealt
game_data.selected_card_player = 0;
game_data.selected_card_ai = 0;
game_data.life_lost_player = 0;
game_data.life_lost_ai = 0;

// Draw card for the player and the AI
if (game_data.deck_player.size() > 0) draw_one_card(game_data.deck_player, game_data.hand_player);
if (game_data.deck_ai.size() > 0) draw_one_card(game_data.deck_ai, game_data.hand_ai);
});
}

EOSIO_DISPATCH(cardgame, (login)(startgame)(playcard)(nextround)(endgame))
132 changes: 132 additions & 0 deletions eosio_docker/contracts/cardgame/cardgame.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#include <eosiolib/eosio.hpp>

using namespace std;
using namespace eosio;
class [[eosio::contract]] cardgame : public eosio::contract {

private:

enum game_status: int8_t {
ONGOING = 0,
PLAYER_WON = 1,
PLAYER_LOST = -1
};

enum card_type: uint8_t {
EMPTY = 0, // Represents empty slot in hand
FIRE = 1,
WOOD = 2,
WATER = 3,
NEUTRAL = 4,
VOID = 5
};

struct card {
uint8_t type;
uint8_t attack_point;
};

const map<uint8_t, card> card_dict = {
{ 0, {EMPTY, 0} },
{ 1, {FIRE, 1} },
{ 2, {FIRE, 1} },
{ 3, {FIRE, 2} },
{ 4, {FIRE, 2} },
{ 5, {FIRE, 3} },
{ 6, {WOOD, 1} },
{ 7, {WOOD, 1} },
{ 8, {WOOD, 2} },
{ 9, {WOOD, 2} },
{ 10, {WOOD, 3} },
{ 11, {WATER, 1} },
{ 12, {WATER, 1} },
{ 13, {WATER, 2} },
{ 14, {WATER, 2} },
{ 15, {WATER, 3} },
{ 16, {NEUTRAL, 3} },
{ 17, {VOID, 0} }
};

struct game {
int8_t life_player = 5;
int8_t life_ai = 5;
vector<uint8_t> deck_player = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
vector<uint8_t> deck_ai = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
vector<uint8_t> hand_player = {0, 0, 0, 0};
vector<uint8_t> hand_ai = {0, 0, 0, 0};
uint8_t selected_card_player = 0;
uint8_t selected_card_ai = 0;
uint8_t life_lost_player = 0;
uint8_t life_lost_ai = 0;
int8_t status = ONGOING;
};

struct [[eosio::table]] user_info {
name username;
uint16_t win_count = 0;
uint16_t lost_count = 0;
game game_data;

auto primary_key() const { return username.value; }
};

struct [[eosio::table]] seed {
uint64_t key = 1;
uint32_t value = 1;

auto primary_key() const { return key; }
};

typedef eosio::multi_index<name("users"), user_info> users_table;

typedef eosio::multi_index<name("seed"), seed> seed_table;

users_table _users;

seed_table _seed;

void draw_one_card(vector<uint8_t>& deck, vector<uint8_t>& hand);

int calculate_attack_point(const card& card1, const card& card2);

int ai_best_card_win_strategy(const int ai_attack_point, const int player_attack_point);

int ai_min_loss_strategy(const int ai_attack_point, const int player_attack_point);

int ai_points_tally_strategy(const int ai_attack_point, const int player_attack_point);

int ai_loss_prevention_strategy(const int8_t life_ai, const int ai_attack_point, const int player_attack_point);

int calculate_ai_card_score(const int strategy_idx, const int8_t life_ai,
const card& ai_card, const vector<uint8_t> hand_player);

int ai_choose_card(const game& game_data);

void resolve_selected_cards(game& game_data);

void update_game_status(user_info& user);

int random(const int range);

public:

cardgame( name receiver, name code, datastream<const char*> ds ):contract(receiver, code, ds),
_users(receiver, receiver.value),
_seed(receiver, receiver.value) {}

[[eosio::action]]
void login(name username);

[[eosio::action]]
void startgame(name username);

[[eosio::action]]
void endgame(name username);

[[eosio::action]]
void playcard(name username, uint8_t player_card_idx);

[[eosio::action]]
void nextround(name username);

};
Loading