forked from googleforgames/space-agon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameEvents.go
118 lines (94 loc) · 2.73 KB
/
gameEvents.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
// controllers/books.go
package controllers
import (
"context"
"fmt"
"log"
"net/http"
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/mbychkowski/space-agon/leaderboard/models"
"cloud.google.com/go/spanner"
"google.golang.org/api/iterator"
)
func GetEvents(c *gin.Context) {
var gameEvents []models.GameEvent
ctx := context.Background()
client := models.ConnectDatabase(ctx)
defer client.Close()
sqlStr := `SELECT PlayerId, EventId, EventType, Timestamp, Data FROM gameevents`
statement := spanner.Statement{
SQL: sqlStr,
}
// Execute the query and retrieve the results
iter := client.Single().Query(ctx, statement)
defer iter.Stop()
for {
var eventId string
var playerId int64
var eventType string
var timestamp int64
var data interface{}
row, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
fmt.Println("error retrieving Spanner results:", err)
}
if err := row.Columns(&eventId, &playerId, &eventType, ×tamp, &data); err != nil {
fmt.Println("error parsing Spanner result:", err)
}
gameEvents = append(gameEvents, models.GameEvent{
EventId: eventId,
PlayerId: playerId,
EventType: eventType,
Timestamp: timestamp,
Data: data,
})
}
c.JSON(http.StatusOK, gin.H{"data": gameEvents})
}
func GetLeaderboard(c *gin.Context) {
var players []models.Player
ctx := context.Background()
client := models.ConnectDatabase(ctx)
defer client.Close()
sqlStr := `SELECT PlayerId, COUNT(EventType) FROM gameevents WHERE EventType="SpawnMissile" GROUP by PlayerId ORDER BY COUNT(EventType) DESC`
statement := spanner.Statement{
SQL: sqlStr,
}
// Execute the query and retrieve the results
iter := client.Single().Query(ctx, statement)
defer iter.Stop()
for {
var playerId string
var count int64
row, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
fmt.Println("error retrieving Spanner results:", err)
}
if err := row.Columns(&playerId, &count); err != nil {
fmt.Println("error parsing Spanner result:", err)
}
players = append(players, models.Player{
Name: playerId,
Score: count,
})
}
jsonPlayers, err := json.Marshal(&players)
if err != nil {
log.Fatal(err)
}
log.Println("Players Marshalled:", string(jsonPlayers))
c.IndentedJSON(http.StatusOK, gin.H{
"message": string(jsonPlayers),
})
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE")
}