-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdad001.html
151 lines (132 loc) · 3.86 KB
/
dad001.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
<body>
<script>
require("https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js", function () {
draw_get_url_and_direction()
})
//document.styleSheets[0].insertRule("button { font-size: 100px }", 0)
//document.styleSheets[0].insertRule("button { padding: 10px; }", 0)
function ff() {
var s = document.createElement('style')
s.innerHTML = 'button { padding: 10px; } button { font-size: 150% }'
document.head.append(s)
var m = document.createElement('meta')
m.name = "viewport"
m.content = "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"
document.getElementsByTagName('head')[0].appendChild(m)
}
ff()
console.log(localStorage)
function draw_get_url_and_direction() {
$('body').empty()
var url = $('<input type="text"/>')
window.addEventListener('message', function (e) {
if (e.origin == 'https://glittle.org') {
url.val(e.data)
async function blah(front) {
window.parent.postMessage({
cmd: 'set_url',
url: url.val()
}, 'https://glittle.org')
let cards = await load_cards_from_url(url.val())
play_flashcard_game(cards, front)
}
$('body').append(url).append($('<button/>').text('A -> B').click(() => {
blah(true)
})).append($('<button/>').text('B -> A').click(() => {
blah(false)
}))
}
}, false)
window.parent.postMessage({ cmd: 'get_url' }, 'https://glittle.org')
}
async function load_cards_from_url(url) {
var m = url.match(/spreadsheets\/d\/(.*?)\//)
if (!m) {
alert('please put a link to a google sheet that has been published')
return
}
url = `https://docs.google.com/spreadsheets/d/${m[1]}/gviz/tq?tqx=out:json`
let x = await fetch(url)
x = await x.text()
x = JSON.parse(x.substr(47).slice(0, -2))
return x.table.rows.map(x => {
return {
true: '' + x.c[0]?.v,
false: '' + x.c[1]?.v
}
})
}
function play_flashcard_game(cards, front) {
$('body').empty()
var game_div = $('<div/>')
$('body').append(game_div)
var main_pile = []
var good_pile = []
var bad_pile = []
each(cards, (x) => { bad_pile.push(x) })
function draw_game() {
if (main_pile.length == 0) {
if (bad_pile.length == 0) {
bad_pile = good_pile
good_pile = []
}
main_pile = bad_pile
shuffle(main_pile)
bad_pile = []
}
game_div.empty()
game_div.append($('<div/>').text('left: ' + main_pile.length + ' good: ' + good_pile.length + ' bad: ' + bad_pile.length))
game_div.append($('<div style="padding:10px;font-size:150%"/>').text(main_pile[0][front]))
game_div.append($('<div style="background:black;padding:10px;font-size:150%"/>').text(main_pile[0][!front]).click(function () {
$(this).css('background', 'white')
}))
game_div.append($('<button/>').text('good').click(() => {
good_pile.push(main_pile.shift())
draw_game()
}))
game_div.append($('<span>').html(' '))
game_div.append($('<button/>').text('bad').click(() => {
bad_pile.push(main_pile.shift())
draw_game()
}))
}
draw_game()
}
function each(o, cb) {
if (o instanceof Array) {
for (var i = 0; i < o.length; i++) {
if (cb(o[i], i, o) == false)
return false
}
} else {
for (var k in o) {
if (o.hasOwnProperty(k)) {
if (cb(o[k], k, o) == false)
return false
}
}
}
return true
}
function shuffle(a) {
var j, x, i;
for (i = a.length; i; i--) {
j = Math.floor(Math.random() * i);
x = a[i - 1];
a[i - 1] = a[j];
a[j] = x;
}
}
function to_array(o) {
var a = [];
each(o, (x) => { a.push(x) });
return a
}
function require(me, cb) {
var s = document.createElement('script')
s.onload = cb
s.src = me
document.head.appendChild(s)
}
</script>
</body>