-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
210 lines (177 loc) · 4.71 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"encoding/json"
"fmt"
"golang.org/x/net/websocket"
"io"
"net/http"
)
type Player struct {
conn *websocket.Conn
username string
score int
pos [2]float32
}
type IMessage struct {
Kind string `json:"type"`
Username string `json:"username"`
Score int `json:"score"`
ArenaId int `json:"arenaId"`
Players [2]string `json:"players"`
Scores [2]int `json:"scores"`
PlayerOnePos [2]float32 `json:"playerOnePos"`
PlayerTwoPos [2]float32 `json:"playerTwoPos"`
BulletPos [2]float32 `json:"bulletPos"`
BulletFrom int `json:"from"`
BulletId int `json:"bulletId"`
Winner string `json:"winner"`
}
type OMessage struct {
Kind string `json:"type"`
Message string `json:"message"`
ArenaId int `json:"arenaId"`
LenGlobal int `json:"lenGlobal"`
Players [2]string `json:"players"`
Scores [2]int `json:"scores"`
PlayerOnePos [2]float32 `json:"playerOnePos"`
PlayerTwoPos [2]float32 `json:"playerTwoPos"`
}
const maxArenaSession int = 20
var arenas [maxArenaSession][2]Player
func enterArena(ws *websocket.Conn, imessage *IMessage) (int, bool) {
for i := 0; i < maxArenaSession; i++ {
count := 0
for _, player := range arenas[i] {
if player.conn != nil {
count++
}
}
if count < 2 {
for j := 0; j < 2; j++ {
if arenas[i][j].conn == nil {
arenas[i][j].conn = ws
arenas[i][j].username = imessage.Username
arenas[i][j].score = imessage.Score
if j == 0 {
arenas[i][j].pos = imessage.PlayerOnePos
} else {
arenas[i][j].pos = imessage.PlayerTwoPos
}
return i, true
}
}
}
}
fmt.Println("Error: No available arenas")
return -1, false
}
type Server struct {
conns map[*websocket.Conn]bool
}
func NewServer() *Server {
return &Server{
conns: make(map[*websocket.Conn]bool),
}
}
func (s *Server) handleWS(ws *websocket.Conn) {
fmt.Println("New connection from", ws.RemoteAddr())
s.processNewConn(ws)
}
func (s *Server) broadcastMessage(data []byte, arenaId int) {
conns := [2]*websocket.Conn{arenas[arenaId][0].conn, arenas[arenaId][1].conn}
for i := 0; i < len(conns); i++ {
if conns[i] != nil {
conns[i].Write(data)
}
}
}
func (s *Server) sendToOpponent(data []byte, arenaId int, ws *websocket.Conn) {
conns := [2]*websocket.Conn{arenas[arenaId][0].conn, arenas[arenaId][1].conn}
for i := 0; i < len(conns); i++ {
if conns[i] != nil && conns[i] != ws {
conns[i].Write(data)
}
}
}
func (s *Server) processNewConn(ws *websocket.Conn) {
// Read incoming messages
buf := make([]byte, 1024)
for {
n, err := ws.Read(buf)
if err != nil {
if err == io.EOF {
fmt.Println("Connection closed by", ws.RemoteAddr())
break
}
fmt.Println("Read error:", err)
continue
}
var imessage IMessage
err = json.Unmarshal(buf[:n], &imessage)
if err != nil {
fmt.Println(err)
}
// Process in case of a new connection
// Enter to Arena
if imessage.Kind == "new conn" {
arenaId, ok := enterArena(ws, &imessage)
var res string
if !ok {
res = fmt.Sprintf("Can't join the arena due to some error!")
} else {
res = fmt.Sprintf("%s has join the arena (ID: %d)", imessage.Username, arenaId)
s.conns[ws] = true
}
// This data gets broadcaster to every player in the arena
// whenever a new player joins
arena := arenas[arenaId]
surround := OMessage{
Kind: "new conn",
Message: res,
ArenaId: arenaId,
Players: [2]string{
arena[0].username,
arena[1].username,
},
LenGlobal: len(s.conns),
}
data, _ := json.Marshal(&surround)
s.broadcastMessage(data, arenaId)
}
// Process when not a new connection: Change in player's position
if imessage.Kind == "pos" {
// Update Player positions
arenaId := imessage.ArenaId
arena := arenas[arenaId]
arena[0].pos = imessage.PlayerOnePos
arena[1].pos = imessage.PlayerTwoPos
// Broadcast Player positions
metric := OMessage{
Kind: "pos",
PlayerOnePos: arena[0].pos,
PlayerTwoPos: arena[1].pos,
}
data, _ := json.Marshal(&metric)
s.broadcastMessage(data, arenaId)
}
// On a new fire event
if imessage.Kind == "bullet" {
arenaId := imessage.ArenaId
data, _ := json.Marshal(&imessage)
s.sendToOpponent(data, arenaId, ws)
}
// Game over event
if imessage.Kind == "game-over" {
arenaId := imessage.ArenaId
data, _ := json.Marshal(&imessage)
s.broadcastMessage(data, arenaId)
}
}
}
func main() {
const PORT string = ":5000"
server := NewServer()
http.Handle("/ws", websocket.Handler(server.handleWS))
fmt.Println("Listening on ws://localhost" + PORT)
http.ListenAndServe(PORT, nil)
}