forked from jbowens/codenames
-
Notifications
You must be signed in to change notification settings - Fork 18
/
frontend.go
123 lines (107 loc) · 3.19 KB
/
frontend.go
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
package codenames
import (
"math/rand"
"net/http"
"path/filepath"
"strings"
)
const tpl = `
<!DOCTYPE html>
<html>
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-166905893-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-166905893-1');
</script>
<title>Codenames Pictures - Play Online</title>
<script src="/js/lib/browser.min.js"></script>
<script src="/js/lib/react.min.js"></script>
<script src="/js/lib/react-dom.min.js"></script>
<script src="/js/lib/jquery-3.0.0.min.js"></script>
<link rel="shortcut icon" type="image/png" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAwSURBVHgB7dJRDQAABEVRJFJBNvX8SEQINrO9G+B8XTaPokFCwwAsAPdxqmKk90ADdlUE2gRVHXcAAAAASUVORK5CYII="/>
<script type="text/babel">
{{if .SelectedGameID}}
window.selectedGameID = "{{.SelectedGameID}}";
{{end}}
window.autogeneratedGameID = "{{.AutogeneratedGameID}}";
</script>
{{range .JSScripts}}
<script type="text/babel" src="/js/{{ . }}"></script>
{{end}}
{{range .Stylesheets}}
<link rel="stylesheet" type="text/css" href="/css/{{ . }}" />
{{end}}
<style type="text/css">
html, body {
margin: 0;
font-family: 'Roboto', verdana, sans-serif;
}
#application { margin: 1em; }
#topbar {
padding: 0.5em;
margin-bottom: 0.5em;
}
#topbar a {
color: black;
text-decoration: none;
}
h1 {
text-transform: uppercase;
letter-spacing: 0.2em;
text-align: center;
font-family: "Courier New", monospace;
}
h1, h2, h3 {
font-family: 'Courier New', monospace;
}
#game-view, .loading {
width: 700px;
margin: 0 auto;
}
</style>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
</head>
<body>
<div id="app">
</div>
<script type="text/babel">
ReactDOM.render(<window.App />, document.getElementById('app'));
</script>
</body>
</html>
`
type templateParameters struct {
SelectedGameID string
AutogeneratedGameID string
JSLibs []string
JSScripts []string
Stylesheets []string
}
func (s *Server) handleIndex(rw http.ResponseWriter, req *http.Request) {
dir, id := filepath.Split(req.URL.Path)
if dir != "" && dir != "/" {
http.NotFound(rw, req)
return
}
autogeneratedID := ""
for {
autogeneratedID = strings.ToLower(s.gameIDWords[rand.Intn(len(s.gameIDWords))])
if _, ok := s.games[autogeneratedID]; !ok {
break
}
}
err := s.tpl.Execute(rw, templateParameters{
SelectedGameID: id,
AutogeneratedGameID: autogeneratedID,
JSLibs: s.jslib.RelativePaths(),
JSScripts: s.js.RelativePaths(),
Stylesheets: s.css.RelativePaths(),
})
if err != nil {
http.Error(rw, "error rendering", http.StatusInternalServerError)
}
}