Skip to content

Commit

Permalink
feat: create user selection transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
martinkaintas committed Jul 27, 2022
1 parent 0a35a51 commit 61ebdf9
Show file tree
Hide file tree
Showing 13 changed files with 385 additions and 101 deletions.
1 change: 1 addition & 0 deletions client/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.md
build
coverage
dist
public
package.json
package-lock.json
12 changes: 9 additions & 3 deletions client/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
import ChannelInitialization from './components/ChannelInitialization.vue';
import TransactionsList from './components/TransactionsList.vue';
import Header from './components/Header.vue';
import RockPaperScissor from './components/RockPaperScissor.vue';
import RockPaperScissors from './components/RockPaperScissors.vue';
import PopUp from './components/PopUp.vue';
import { useChannelStore } from './stores/channel';
import { useGameStore } from './stores/game';
import { onBeforeUnmount, onMounted, toRaw } from 'vue';
import { getSdk, returnCoinsToFaucet } from './sdk/sdkService';
import { GameChannel } from './sdk/GameChannel';
import GameManager from './game/GameManager';
import { AeSdk } from '@aeternity/aepp-sdk';
const channelStore = useChannelStore();
const gameStore = useGameStore();
async function initChannel() {
if (!channelStore.channel) {
Expand All @@ -21,6 +24,7 @@ async function initChannel() {
onMounted(async () => {
channelStore.channel = new GameChannel(await getSdk());
gameStore.gameManager = new GameManager();
});
onBeforeUnmount(async () => {
Expand All @@ -34,10 +38,12 @@ onBeforeUnmount(async () => {
<PopUp />
<Header />
<ChannelInitialization
v-if="!channelStore.channel?.isOpen"
v-if="!channelStore.channel?.isOpen || !channelStore.channel?.contract"
@initializeChannel="initChannel()"
/>
<RockPaperScissor v-if="channelStore.channel?.isOpen" />
<RockPaperScissors
v-if="channelStore.channel?.isOpen && channelStore.channel.contract"
/>
<TransactionsList />
</template>

Expand Down
4 changes: 3 additions & 1 deletion client/src/components/ChannelInitialization.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const emit = defineEmits(['initializeChannel']);
const title = computed(() =>
!openChannelInitiated.value
? 'Start the game by open state channel'
: 'Setting ‘on-chain’ operations...'
: !channelStore.channel?.isOpen
? 'Setting ‘on-chain’ operations...'
: 'Waiting for contract to be deployed...'
);
const errorMessage = computed(() =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
<script setup lang="ts">
import { computed } from 'vue';
import { useGameStore, Selections } from '../stores/game';
import { usePopUpStore } from '../stores/popup';
import { useGameStore } from '../stores/game';
import { Selections } from '../game/GameManager';
const gameStore = useGameStore();
const popUpStore = usePopUpStore();
const userHasSelected = computed(() => {
return gameStore.userSelection != Selections.none ? true : false;
return gameStore.gameManager?.getUserSelection() != Selections.none
? true
: false;
});
const botIsMakingSelection = computed(() => {
return gameStore.userSelection != Selections.none
? gameStore.botSelection === Selections.none
return gameStore.gameManager?.getUserSelection() != Selections.none
? gameStore.gameManager?.getUserSelection() === Selections.none
? true
: false
: false;
});
const userSelection = computed(() =>
userHasSelected.value
? Selections[gameStore.gameManager?.getUserSelection() ?? Selections.none]
: ''
);
const botSelection = computed(() =>
gameStore.gameManager?.botSelection != Selections.none
? Selections[gameStore.gameManager?.botSelection ?? Selections.none]
: ''
);
const status = computed(() => {
if (!userHasSelected.value) {
return 'Choose one';
Expand All @@ -27,44 +40,33 @@ const status = computed(() => {
}
});
function makeSelection(selection: Selections) {
async function makeSelection(selection: Selections) {
if (userHasSelected.value) return;
popUpStore.showPopUp({
title: Selections[selection].toUpperCase(),
text: 'Confirm your selection',
mainBtnText: 'Confirm',
secBtnText: 'Cancel',
mainBtnAction: () => {
gameStore.userSelect(selection);
popUpStore.resetPopUp();
try {
await gameStore.gameManager?.setUserSelection(selection);
} catch (e) {
console.info((e as Error).message);
return;
}
// ! temporary placeholder for bot selection
setTimeout(() => {
gameStore.botSelection = Math.floor(Math.random() * 3);
}, 2000);
},
secBtnAction: () => {
popUpStore.resetPopUp();
},
});
// ! temporary placeholder for bot selection
setTimeout(() => {
if (gameStore.gameManager) {
gameStore.gameManager.botSelection = Selections.rock;
}
}, 2000);
}
</script>

<template>
<div class="rock-paper-scissor">
<div class="rock-paper-scissors">
<div class="header">
<div class="finalized-selection" data-testid="userSelection">
{{
gameStore.userSelection != 3
? Selections[gameStore.userSelection]
: ''
}}
{{ userSelection }}
</div>
<h1 class="title">{{ status }}</h1>
<div class="finalized-selection bot" data-testid="botSelection">
{{
gameStore.botSelection != 3 ? Selections[gameStore.botSelection] : ''
}}
{{ botSelection }}
</div>
</div>
<div class="selections">
Expand Down Expand Up @@ -94,9 +96,9 @@ function makeSelection(selection: Selections) {
'bot-selecting': botIsMakingSelection,
'user-selecting': !userHasSelected,
}"
@click="makeSelection(Selections.scissor)"
@click="makeSelection(Selections.scissors)"
>
SCISSOR
SCISSORS
</button>
</div>
</div>
Expand Down Expand Up @@ -197,7 +199,7 @@ function makeSelection(selection: Selections) {
cursor: default;
}
}
.rock-paper-scissor {
.rock-paper-scissors {
display: flex;
flex-direction: column;
justify-content: center;
Expand Down
44 changes: 44 additions & 0 deletions client/src/game/GameManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useChannelStore } from '../stores/channel';
import { PopUpData } from '../stores/popup';
import { sha256hash } from '@aeternity/aepp-sdk';

export enum Selections {
rock = 'rock',
paper = 'paper',
scissors = 'scissors',
none = 'none',
}

export default class GameManager {
private userSelection: Selections = Selections.none;
botSelection: Selections = Selections.none;
private hashKey = '';

async setUserSelection(selection: Selections) {
if (selection === Selections.none) {
throw new Error('Selection should not be none');
}
const channelStore = useChannelStore();
const popupData: Partial<PopUpData> = {
title: Selections[selection].toUpperCase(),
text: 'Confirm your selection',
};

const result = await channelStore.channel?.callContract(
'provide_hash',
[this.hashSelection(selection)],
popupData
);
if (result?.accepted) this.userSelection = selection;
else throw new Error('Selection was not accepted');
}

hashSelection(selection: Selections): Buffer {
this.hashKey = Math.random().toString(16).substring(2, 8);
return sha256hash(selection + this.hashKey);
}

getUserSelection(): Selections {
return this.userSelection;
}
}
Loading

0 comments on commit 61ebdf9

Please sign in to comment.