This repository has been archived by the owner on May 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
194 lines (171 loc) · 7.45 KB
/
index.html
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Blackjack!</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous" />
<link rel="stylesheet" href="styles/style.css">
<script type="text/javascript">
let currencycode = "$";
var person = {userName:"", currency:currencycode, wallet:100};
function startGame() {
startCheck();
if(localStorage.getItem("blackjack_userDetails")){
window.location.href="game.html";
}
var userName=document.getElementById("currentUserName");
var storedUserDetails=JSON.parse(localStorage.getItem("blackjack_userDetails"));
userName.innerText = storedUserDetails.userName;
// userName.innerText = localStorage.getItem("blackjack_userName");
}
function loadFunctions() {
fetchCurrency();
setName();
startCheck();
}
function setName() {
var nameInput=document.getElementById("txtUsername");
var nameButton=document.getElementById("nameSaveButton");
var userName=document.getElementById("currentUserName");
var storedUserDetails=JSON.parse(localStorage.getItem("blackjack_userDetails"));
userName.innerText = storedUserDetails.userName;
startCheck();
}
function nameCheck() {
var nameInput = document.getElementById("txtUsername");
var nameButton = document.getElementById("nameSaveButton");
if (nameInput.value === "") {
nameButton.disabled = true;
}
else {
nameButton.disabled = false;
}
}
function SaveName() {
var nameInput=document.getElementById("txtUsername");
var nameButton=document.getElementById("nameSaveButton");
userName=document.getElementById("currentUserName");
if(nameInput.value !== ""){
person.userName=nameInput.value;
person.currency=currencycode;
localStorage.setItem("blackjack_userDetails",JSON.stringify(person));
// localStorage.setItem("blackjack_userName",nameInput.value);
userName.innerText = person.userName;
}
else{
nameButton.disabled = true;
}
startCheck();
}
async function fetchCurrency() {
var endpoint = 'http://ip-api.com/json/?fields=status,message,continent';
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var response = JSON.parse(this.responseText);
console.log(this.responseText);
if (response.status !== 'success') {
console.log('query failed: ' + response.message);
showError(error);
return
}
if (response.continent === "Europe") {
currencycode = "€";
}
}
};
xhr.open('GET', endpoint, true);
xhr.send();
}
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
var obj = document.getElementById("dvCurrencyChoice");
obj.classList.remove("d-none");
break;
case error.POSITION_UNAVAILABLE:
var obj = document.getElementById("dvCurrencyChoice");
obj.classList.remove("d-none");
break;
case error.TIMEOUT:
var obj = document.getElementById("dvCurrencyChoice");
obj.classList.remove("d-none");
break;
case error.UNKNOWN_ERROR:
var obj = document.getElementById("dvCurrencyChoice");
obj.classList.remove("d-none");
break;
}
startCheck();
}
//will determine if start button can be clicked and what errors are shown
function startCheck(){
//var currencyError=document.getElementById("currencyError");
var startButton=document.getElementById("startButton");
var nameError=document.getElementById("nameError");
var valid = true;
if(localStorage.getItem("blackjack_userDetails") && localStorage.getItem("blackjack_userDetails") != ""){
nameError.classList.add('d-none');
}
else{
valid = false;
nameError.classList.remove('d-none');
}
if(valid){
startButton.disabled = false;
}
else{
startButton.disabled = true;
}
}
//on rdoButtonChange
function rdoChange(radio){
currencycode = radio.value;
startCheck();
}
</script>
</head>
<body class="text-center" onload="loadFunctions()">
<div id="container">
<header class="masthead mb-auto">
<div>
<div class="col">
<h1 class="cover-heading">Blackjack!</h1>
</div>
</div>
</header>
<div>
<label>Current UserName: </label>
<label id="currentUserName"></label>
</div>
<div class="row pt-3">
<div class="col">
<label for="txtUsername">Desired Username: </label>
<input type="text" id="txtUsername" onkeyup="nameCheck()" />
<input type="button" class="btn btn-lg btn-secondary" id="nameSaveButton" value="Enter" onclick="SaveName()">
</div>
</div>
<div id="dvCurrencyChoice" class="row pt-3 d-none">
<div class="col">
<label for="rdoCurrency">Choose your Currency:</label>
<input type="radio" name="rdoCurrency" id="rdoUSD" onclick="rdoChange(this)" checked/> US Dollars ($)
<input type="radio" name="rdoCurrency" id="rdoEuro" onclick="rdoChange(this)"/> Euro (€)
</div>
</div>
<div class="row pt-3">
<div class="col">
<input type="button" class="btn btn-lg btn-secondary" id="startButton" value="Start Game" onclick="startGame()">
</div>
</div>
<div>
<p id="nameError" style="color: red; margin-bottom: 0;">* Please enter a Name</p>
</div>
<!-- <div>
<p id="currencyError" style="color: red; margin-bottom: 0;">* Please choose a Currency</p>
</div> -->
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
</body>
</html>