-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcast.go
119 lines (96 loc) · 2.72 KB
/
broadcast.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
package main
import "fmt"
func broadcastEndGame(room *Room, player1 float64, player2 float64) {
// delete room first, then send endgame to clients
clientRoomsLock.Lock()
delete(clientRooms, room.RoomName)
clientRoomsLock.Unlock()
gameOverMessage := "GAME OVER!"
if room.Player1MissedTurns == 3 {
gameOverMessage = gameOverMessage + "\nPlayer 1 Missed 3 Consecutive Turns"
} else if room.Player2MissedTurns == 3 {
gameOverMessage = gameOverMessage + "\nPlayer 2 Missed 3 Consecutive Turns"
} else {
gameOverMessage = gameOverMessage + "\nAll possible words found!"
}
sendMessage(room, gameOverMessage)
if room.Player1WS != nil {
room.Player1WS.Conn.WriteJSON(map[string]interface{}{
"type": "endgame",
"player1": player1,
"player2": player2,
})
}
if room.Player2WS != nil {
room.Player2WS.Conn.WriteJSON(map[string]interface{}{
"type": "endgame",
"player1": player1,
"player2": player2,
})
}
if room.KafkaWriter != nil {
deleteTopic(room.RoomName)
room.KafkaWriter.Close()
}
}
func broadcastDisconnect(roomName string) {
clientRoomsLock.RLock()
defer clientRoomsLock.RUnlock()
room, exists := clientRooms[roomName]
if !exists {
return
}
room.Player1WS.Conn.WriteJSON(map[string]interface{}{
"type": "disconnected",
})
room.Player2WS.Conn.WriteJSON(map[string]interface{}{
"type": "disconnected",
})
sendMessage(room, "Someone disconnected! This game has ended.")
if room.KafkaWriter != nil {
deleteTopic(room.RoomName)
room.KafkaWriter.Close()
}
}
func broadcastSwitch(roomName string, curr_player int, next_player int, word string) {
clientRoomsLock.RLock()
defer clientRoomsLock.RUnlock()
room, exists := clientRooms[roomName]
if !exists {
return
}
room.Player1WS.Conn.WriteJSON(map[string]interface{}{
"type": "switch",
"player": next_player,
"word": word,
})
room.Player2WS.Conn.WriteJSON(map[string]interface{}{
"type": "switch",
"player": next_player,
"word": word,
})
if word == "" {
sendMessage(room, fmt.Sprintf("Player %d could not find word. Switching from Player %d to %d", curr_player, curr_player, next_player))
} else {
sendMessage(room, fmt.Sprintf("Player %d found word %s. Switching from Player %d to %d", curr_player, word, curr_player, next_player))
}
}
func broadcastStart(roomName string) {
clientRoomsLock.RLock()
defer clientRoomsLock.RUnlock()
room, exists := clientRooms[roomName]
if !exists {
return
}
room.Player1WS.Conn.WriteJSON(map[string]interface{}{
"type": "start",
"countdown": [2]int{3, 0},
"gameInfo": *room,
})
room.Player2WS.Conn.WriteJSON(map[string]interface{}{
"type": "start",
"countdown": [2]int{3, 0},
"gameInfo": *room,
})
sendMessage(room, "Game start!")
}