-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartGame.vue
More file actions
170 lines (159 loc) · 4.03 KB
/
StartGame.vue
File metadata and controls
170 lines (159 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<template>
<v-main id="StartGame">
<v-container>
<v-card fluid>
<v-toolbar
dark
color="primary"
>
<v-toolbar-title>Islanders</v-toolbar-title>
<v-spacer />
<v-btn-toggle
v-model="toggle"
class="transparent"
>
<v-btn
:value="2"
text
@click="isCreatingGame = true"
>
Create
</v-btn>
<v-btn
text
@click="isCreatingGame = false"
>
Join
</v-btn>
</v-btn-toggle>
</v-toolbar>
<v-alert
:value="error"
type="error"
transition="slide-y-transition"
dense
tile
>
{{ errorMessage }}
</v-alert>
<v-card-text>
<v-text-field
ref="playerName"
v-model="playerName"
autofocus
outlined
name="playerName"
dense
label="Enter your player name"
type="text"
:rules="rules"
counter="14"
/>
<v-text-field
v-show="!isCreatingGame"
v-model="gameId"
name="gameid"
label="To join a game, enter a Game Code"
type="text"
outlined
dense
counter="24"
/>
</v-card-text>
<v-card-actions>
<v-btn
v-if="!isCreatingGame"
color="primary"
:disabled="!validatePlayerName"
@click="joinGame"
@on:keyup.enter="joinGame"
>
Join Game
</v-btn>
<v-btn
v-else
color="primary"
:disabled="!validatePlayerName"
@click="createGame"
>
Create Game
</v-btn>
</v-card-actions>
</v-card>
</v-container>
</v-main>
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
@Component({
components: {},
})
export default class StartGame extends Vue {
public error = false;
public errorMessage = 'Could not create game';
public rules = [(v:string): boolean | string => v.length <= 25 || 'Max 25 characters'];
public isCreatingGame = true;
public playerName = '';
public gameId = '';
public toggle = 2;
public async createGame (): Promise<void> {
if (!this.validatePlayerName) {
this.error = true
this.errorMessage = 'You need to enter a valid player name'
return
}
await this.$store.dispatch('createGame', this.playerName)
if (this.$store.state.game.gameId) {
this.$parent.$parent.$emit('gameChosen')
this.$store.commit('game/setError', '')
} else {
this.error = true;
this.errorMessage = this.$store.getters['game/getError']
}
}
public async joinGame (): Promise<void> {
if (!this.validateGameId) {
this.error = true
this.errorMessage = 'You need to enter a Game Code'
return
} if (!this.validatePlayerName) {
this.error = true
this.errorMessage = 'You need to enter a valid player name'
return
}
try {
this.$store.commit('game/setError', '')
await this.$store.dispatch('joinGame', {
gameId: this.gameId,
playerName: this.playerName,
})
} catch (ex) {
this.error = true
this.errorMessage = ex.message
console.warn(ex)
return
}
if (this.$store.state.game.gameId) {
this.$parent.$parent.$emit('gameChosen')
this.$store.commit('game/setError', '')
} else {
this.error = true;
this.errorMessage = this.$store.getters['game/getError']
}
}
get validateGameId (): boolean {
if (!this.gameId) {
return false
}
return true
}
get validatePlayerName (): boolean {
if (!this.playerName || this.playerName.length >= 25) {
return false
}
return true
}
}
</script>
<style lang="scss" scoped>
</style>