-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (102 loc) · 3.16 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
118
119
120
121
122
package main
import (
"fmt"
"html/template"
"io"
"net/http"
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
type Template struct {
templates *template.Template
}
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
var t = &Template{
templates: template.Must(template.ParseGlob("./templates/*.*html")),
}
var game Game
// Connection handles the connection of a player to the game.
// It checks if the player has already a session and if
// he is already in the game. If not, it saves the player to the game.
func connection(c echo.Context) error {
session, err := session.Get("session", c)
if err != nil {
fmt.Println(err)
return c.String(http.StatusInternalServerError, "Internal Server Error")
}
if session.Values["id"] != nil {
if game.isPlayerPresent(session.Values["id"].(int)) {
return c.Redirect(http.StatusFound, "/play")
}
}
session.Options = &sessions.Options{
Path: "/",
MaxAge: 86400 * 7,
HttpOnly: true,
}
// Check if name is empty
name := c.FormValue("name")
if name == "" {
return c.String(http.StatusBadRequest, "Name is empty")
}
// Save player to game
id := game.addPlayer(name)
// Save ID to session
session.Values["id"] = id
session.Values["name"] = name
session.Save(c.Request(), c.Response())
return c.Redirect(http.StatusFound, "/play")
}
// Checks if player has already a session
// If yes, redirect to play page
// If not, redirect to login page
func index(c echo.Context) error {
session, err := session.Get("session", c)
if err != nil {
fmt.Println(err)
return c.String(http.StatusInternalServerError, "Internal Server Error")
}
if session.Values["id"] != nil {
if game.isPlayerPresent(session.Values["id"].(int)) {
return c.Redirect(http.StatusFound, "/play")
}
}
return c.Render(http.StatusOK, "login", nil)
}
func playerToggleReady(c echo.Context) error {
session, err := session.Get("session", c)
if err != nil {
fmt.Println(err)
return c.String(http.StatusInternalServerError, "Internal Server Error")
}
if session.Values["id"] != nil {
for i, player := range game.Players {
if player.ID == session.Values["id"].(int) {
game.Players[i].toggleReady()
}
}
}
if game.shouldChangePhase() {
game.nextPhase()
}
return c.Redirect(http.StatusFound, "/play")
}
func main() {
game.initialize()
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(session.Middleware(sessions.NewCookieStore([]byte("secret"))))
e.Renderer = t
e.Static("/public", "public") // Serve static files, like images
e.GET("/play", play) // Play is the main page of the game
e.GET("/", index) // Index is the login page
e.POST("/connection", connection) // Connection handles the connection of a player to the game
e.GET("/ws", ws) // ws is the websocket connection
e.POST("/player/ready", playerToggleReady) // playerToggleReady toggles the ready status of a player
e.Logger.Fatal(e.Start(":1323"))
}