-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
53 lines (40 loc) · 849 Bytes
/
map.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
package main
import (
"encoding/json"
"log"
)
type Map struct {
Width int `json:"width"`
Height int `json:"height"`
Walls []int `json:"walls"`
Crates []int `json:"crates"`
Targets []int `json:"targets"`
Player int `json:"player"`
}
type MapEntry struct {
ID int `json:"id"`
Data Map `json:"data"`
}
type MapDatabaseEntry struct {
id int
data string
published int
}
func GetAllMaps() []MapEntry {
rtn := make([]MapEntry, 0)
result, err := db.Query("SELECT * FROM map WHERE published=1")
if err != nil {
log.Fatal(err)
}
for result.Next() {
var row MapDatabaseEntry
var mapData Map
result.Scan(&row.id, &row.data, &row.published)
err = json.Unmarshal([]byte(row.data), &mapData)
if err != nil {
log.Fatal(err)
}
rtn = append(rtn, MapEntry{row.id, mapData})
}
return rtn
}