-
Notifications
You must be signed in to change notification settings - Fork 2
/
Websocket.go
64 lines (57 loc) · 1.61 KB
/
Websocket.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
package main
import (
"database/sql"
"github.com/gorilla/websocket"
_ "github.com/gorilla/websocket"
"log"
"math/rand"
"net/http"
"time"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 128,
WriteBufferSize: 128,
CheckOrigin: func(r *http.Request) bool { return true },
//Error: nil,
}
func StartWebSocketConnection(w http.ResponseWriter, r *http.Request, db *sql.DB) {
//Check if db connection is available
if err := db.Ping(); err != nil {
http.Error(w, INTERNAL_SERVER_ERROR_RESPONSE, http.StatusInternalServerError)
log.Println(err)
return
}
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
cookie, err := r.Cookie(COOKIE_NAME)
if err != nil {
http.Error(w, "No Cookie found", http.StatusNotFound)
log.Print(err)
return
}
user, dErr := GetUserFromSessionCookie(db, cookie.Value)
if dErr != nil {
http.Error(w, dErr.PublicError(), dErr.Status())
log.Println(dErr.Error())
return
}
rand.Seed(time.Now().UTC().UnixNano())
random := rand.Intn(4)
bomber := NewBomberman(user.UserID, 0, 0, user.Username)
if sessionRunning {
bomber.Kill()
findWinner()
}
GameMap.Fields[pixToArr(bomber.PositionX)][pixToArr(bomber.PositionY)].Player.PushBack(bomber)
if random == 0 {
bomber.teleportTo(19, 0, pixToArr(bomber.PositionX), pixToArr(bomber.PositionY))
} else if random == 1 {
bomber.teleportTo(0, 19, pixToArr(bomber.PositionX), pixToArr(bomber.PositionY))
} else if random == 2 {
bomber.teleportTo(19, 19, pixToArr(bomber.PositionX), pixToArr(bomber.PositionY))
}
StartPlayerLoop(NewSession(user, bomber, ws, time.Now()))
}