-
Notifications
You must be signed in to change notification settings - Fork 0
/
rps.html
157 lines (140 loc) · 4.71 KB
/
rps.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>🤜 🖐 ✌</title>
<meta name="description" content="The HTML5 Herald">
<meta name="author" content="SitePoint">
<style>
.top-level {
display: flex;
justify-content: space-evenly;
flex-direction: column;
}
.container {
display: flex;
justify-content: center;
margin: 5px 5px 5px 5px;
flex-direction: column;
}
.big-selector {
height: 150px;
width: 450px;
font-size: 100px;
display: flex;
justify-content: center;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
</head>
<body class="top-level">
<div>
<button onclick="reset()" style="height:25px; background-color:#f77fbe;">Reset</button>
</div>
<div class="container">
<p style="text-align:center; font-size:25px;"> Choose your move: </p>
<div class="container" style="flex-direction:row; flex-wrap:wrap;">
<select id="pc" class="big-selector">
<option value="rock">🤜 🏔 🤛</option>
<option value="paper">🖐 🌊 🖐</option>
<option value="scissors">✌ 🌩 ✌</option>
</select>
</div>
<button onclick="submit()" style="height:100px; background-color:#00A693; font-size:75px;">Submit</button>
</div>
<script>
function compare(c1, c2){
if(c1 == c2){
return 0
} else if( (c1 == "rock" && c2 == "scissors") || (c1 == "paper" && c2 == "rock") || (c1 == "scissors" && c2 == "paper")) {
return 1
} else {
return -1
}
}
function submit_player1(){
var root= window.location.href;
var p1Choice = ["pc"]
.map(function (c){return document.getElementById(c)})
.map(function (sc){return CryptoJS.AES.encrypt(sc.options[sc.selectedIndex].value, localStorage.getItem("SECRET")).toString()});
Clipboard.copy(root + "?" + p1Choice.toString());
alert("Send this link below to a friend, (I already copied it for you" + String.fromCodePoint("0x1F970") + "):\n\n " + root + "?" + p1Choice.toString());
}
function reset(){
var root = window.location.href.split('?')[0]
localStorage.clear();
window.location = root;
}
function submit_player2(){
var root = window.location.href.split('?')[0]
var p2ChoiceObj = ["pc"]
.map(function (c){return document.getElementById(c)})
var p2Choice = p2ChoiceObj
.map(function (sc){return sc.options[sc.selectedIndex].value});
var p1Choice = localStorage
.getItem("p1Choice")
.split(',')
.map(function (c){
return CryptoJS.AES.decrypt(c, localStorage.getItem("SECRET")).toString(CryptoJS.enc.Utf8)
});
var opponentMessage = '\n' + 'Your opponent played '+p1Choice
var score = [0]
.map(function(index){return compare(p2Choice[index], p1Choice[index])})
.reduce(function(acc, currVal){return acc + currVal}, 0)
if(score > 0){
alert("YOU WIN!" + String.fromCodePoint("0x1F618") + opponentMessage);
} else if ( score == 0 ) {
alert("It's a Draw. " + String.fromCodePoint("0x1F388") + "Play again: " + root);
} else {
alert("YOU LOSE " + String.fromCodePoint("0x1F611") + opponentMessage);
}
}
window.Clipboard = (function(window, document, navigator) {
var textArea,
copy;
function isOS() {
return navigator.userAgent.match(/ipad|iphone/i);
}
function createTextArea(text) {
textArea = document.createElement('textArea');
textArea.value = text;
document.body.appendChild(textArea);
}
function selectText() {
var range,
selection;
if (isOS()) {
range = document.createRange();
range.selectNodeContents(textArea);
selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
textArea.setSelectionRange(0, 999999);
} else {
textArea.select();
}
}
function copyToClipboard() {
document.execCommand('copy');
document.body.removeChild(textArea);
}
copy = function(text) {
createTextArea(text);
selectText();
copyToClipboard();
};
return {
copy: copy
};
})(window, document, navigator);
localStorage.setItem("SECRET", "plum");
var p1Choice = window.location.search.slice(1).split(',');
if(p1Choice.length === 1 && p1Choice[0] !== ""){
var submit = submit_player2
localStorage.setItem("p1Choice", p1Choice);
} else {
var submit = submit_player1
}
</script>
</body>
</html>