-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (100 loc) · 2.92 KB
/
main.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
package main
import (
"fmt"
"html/template"
"math/rand"
"net/http"
"time"
"google.golang.org/appengine"
)
func main() {
fs := http.FileServer(http.Dir("assets/"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "static/playercount.html", 301)
})
http.HandleFunc("/setup", renderTileCounts)
appengine.Main()
//http.ListenAndServe(":8080", nil)
}
var tileLimits = []int{
24, //Grain
21, //Cheese
18, //Wine
15, //Wool
12, //Brocade
}
const (
mapSpacesFourPl = 52
mapSpacesThreePl = 48
mapSpacesTwoPl = 43
removeTilesFourPl = 0
removeTilesThreePl = 6
removeTilesTwoPl = 12
)
type tileSetup struct {
PlayerCount string
TilesUsed []int
TilesRemoved []int
Note string
}
func renderTileCounts(w http.ResponseWriter, r *http.Request) {
//TODO:move this out later for performance. Don't need to parse on each request. Nice for rapid dev though
var tmpl = template.Must(template.ParseFiles("assets/setup.html"))
playerCount := r.FormValue("playerCount")
var mapSpaces, removeTiles int
var note string
switch playerCount {
case "3":
mapSpaces = mapSpacesThreePl
removeTiles = removeTilesThreePl
note = "Remove 2 each of Farmer, Boatman, Craftsman Trader. Remove 3 each of Knight, Scholar, Monk"
case "2":
mapSpaces = mapSpacesTwoPl
removeTiles = removeTilesTwoPl
note = "Remove 4 each of Farmer, Boatman, Craftsman Trader. Remove 6 each of Knight, Scholar, Monk"
default:
playerCount = "4"
mapSpaces = mapSpacesFourPl
removeTiles = removeTilesFourPl
}
tilesUsed := calcTileNumbers(mapSpaces, tileLimits)
tilesRemoved := calcTileNumbers(removeTiles, subtract(tileLimits, tilesUsed))
tmpl.Execute(w, tileSetup{playerCount, tilesUsed, tilesRemoved, note})
}
// a - b elementwise
func subtract(a, b []int) []int {
if len(a) != len(b) {
panic("cannot subtract slices of different lengths")
}
result := make([]int, len(a))
for k := range a {
if a[k] < b[k] {
panic(fmt.Sprintf("Trying to subract %v from %v. Index %v too large", b, a, k))
}
result[k] = a[k] - b[k]
}
return result
}
// calcTileNumbers returns a slice containing the randomised starting
// number of tiles. Each slice entry represents a different type of tile.
// 'required' is the total number of required tiles.
// 'limits' represents the pool of available tiles to draw from
func calcTileNumbers(required int, limits []int) []int {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
tilePool := make([]int, 0, required)
for tileType := 0; tileType < len(limits); tileType++ {
for n := 0; n < limits[tileType]; n++ {
tilePool = append(tilePool, tileType)
}
}
if len(tilePool) < required {
panic("Too few tiles to sastisfy requirement")
}
randomTileIndices := r.Perm(len(tilePool))
output := make([]int, len(limits))
for i := 0; i < required; i++ {
output[tilePool[randomTileIndices[i]]]++
}
return output
}