add broadcasts
This commit is contained in:
parent
bff4f57e86
commit
6eb24636e5
@ -31,17 +31,19 @@ func receiveHandler(connection *websocket.Conn, output chan string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error in receive:", err)
|
log.Println("Error in receive:", err)
|
||||||
}
|
}
|
||||||
if resp.Result == coordinator.SessionRespJoined1 {
|
switch resp.Result {
|
||||||
|
case coordinator.SessionRespJoined1:
|
||||||
pid = game.SentinalID
|
pid = game.SentinalID
|
||||||
output <- "joined as sentinal"
|
output <- "joined as sentinal"
|
||||||
} else if resp.Result == coordinator.SessionRespJoined2 {
|
case coordinator.SessionRespJoined2:
|
||||||
pid = game.ScourgeID
|
pid = game.ScourgeID
|
||||||
output <- "joined as scourge"
|
output <- "joined as scourge"
|
||||||
} else if resp.Result == coordinator.SessionRespFound {
|
case coordinator.SessionRespFound:
|
||||||
matchID = resp.MatchID
|
matchID = resp.MatchID
|
||||||
output <- "game found"
|
output <- "game found"
|
||||||
} else if resp.Result == coordinator.SessionRespPlayed {
|
case coordinator.SessionRespPlayed:
|
||||||
if resp.GameResult != nil {
|
if resp.GameResult != nil {
|
||||||
|
output <- "played succesfully"
|
||||||
switch resp.GameResult.ResultType {
|
switch resp.GameResult.ResultType {
|
||||||
case game.ActCmd:
|
case game.ActCmd:
|
||||||
output <- resp.GameResult.ActionResult.String()
|
output <- resp.GameResult.ActionResult.String()
|
||||||
@ -50,12 +52,27 @@ func receiveHandler(connection *websocket.Conn, output chan string) {
|
|||||||
case game.DebugCmd:
|
case game.DebugCmd:
|
||||||
output <- resp.GameResult.DebugResult.String()
|
output <- resp.GameResult.DebugResult.String()
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
output <- "error playing"
|
||||||
}
|
}
|
||||||
} else if resp.Result == coordinator.SessionRespLeft {
|
case coordinator.SessionRespLeft:
|
||||||
output <- "game left"
|
output <- "game left"
|
||||||
break
|
break
|
||||||
} else if resp.Result == coordinator.SessionRespPlayed {
|
case coordinator.SessionRespBroadcastSenTurn:
|
||||||
output <- "played succesfully"
|
output <- "Sentinal may take their turn"
|
||||||
|
case coordinator.SessionRespBroadcastScoTrun:
|
||||||
|
output <- "Scourge may take their turn"
|
||||||
|
case coordinator.SessionRespBroadcastSenWin:
|
||||||
|
output <- "Sentinal wins!"
|
||||||
|
case coordinator.SessionRespBroadcastScoWin:
|
||||||
|
output <- "Scourge wins!"
|
||||||
|
case coordinator.SessionRespBroadcastNone:
|
||||||
|
|
||||||
|
case coordinator.SessionRespError:
|
||||||
|
output <- "generic error"
|
||||||
|
|
||||||
|
default:
|
||||||
|
output <- "Received a server response we don't know how to handle"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -82,7 +99,7 @@ func main() {
|
|||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
go receiveHandler(conn, output)
|
go receiveHandler(conn, output)
|
||||||
go GetCommand(id, cmd)
|
go GetCommand(id, cmd)
|
||||||
|
ticker := time.NewTicker(1 * time.Second)
|
||||||
// Our main loop for the client
|
// Our main loop for the client
|
||||||
// We send our relevant packets here
|
// We send our relevant packets here
|
||||||
for {
|
for {
|
||||||
@ -95,6 +112,9 @@ func main() {
|
|||||||
log.Println("Error during writing to websocket:", err)
|
log.Println("Error during writing to websocket:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if c.Command == coordinator.SessionCmdLeave {
|
||||||
|
break
|
||||||
|
}
|
||||||
case o = <-output:
|
case o = <-output:
|
||||||
fmt.Println(o)
|
fmt.Println(o)
|
||||||
case <-interrupt:
|
case <-interrupt:
|
||||||
@ -118,6 +138,18 @@ func main() {
|
|||||||
log.Println("Timeout in closing receiving channel. Exiting....")
|
log.Println("Timeout in closing receiving channel. Exiting....")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
if matchID != uuid.Nil {
|
||||||
|
err := conn.WriteJSON(coordinator.SessionCommand{
|
||||||
|
ID: id,
|
||||||
|
MatchID: matchID,
|
||||||
|
Command: coordinator.SessionCmdPoll,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error writing to websocket:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,12 +37,16 @@ func serveWs(c *coordinator.Coordinator, w http.ResponseWriter, r *http.Request)
|
|||||||
log.Println("Error during message reading:", err)
|
log.Println("Error during message reading:", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
log.Printf("Received: %s", cmd)
|
if cmd.Command != coordinator.SessionCmdPoll {
|
||||||
|
log.Printf("Received: %s", cmd)
|
||||||
|
}
|
||||||
resp := c.Coordinate(&cmd)
|
resp := c.Coordinate(&cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
log.Printf("sending: %v", resp)
|
if cmd.Command != coordinator.SessionCmdPoll {
|
||||||
|
log.Printf("sending: %v", resp)
|
||||||
|
}
|
||||||
err = conn.WriteJSON(resp)
|
err = conn.WriteJSON(resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error during message writing:", err)
|
log.Println("Error during message writing:", err)
|
||||||
|
@ -42,6 +42,7 @@ func (c *Coordinator) Start() {
|
|||||||
c.MatchLock.Unlock()
|
c.MatchLock.Unlock()
|
||||||
c.CallbackChan[p1] <- m.ID
|
c.CallbackChan[p1] <- m.ID
|
||||||
c.CallbackChan[p2] <- m.ID
|
c.CallbackChan[p2] <- m.ID
|
||||||
|
go MatchWatcher(m)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
go func() {
|
go func() {
|
||||||
@ -52,6 +53,7 @@ func (c *Coordinator) Start() {
|
|||||||
if v.Game == nil && v.Active {
|
if v.Game == nil && v.Active {
|
||||||
log.Println("clearing match with no game")
|
log.Println("clearing match with no game")
|
||||||
delete(c.Matches, v.ID)
|
delete(c.Matches, v.ID)
|
||||||
|
v.Watcher <- true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.MatchLock.Unlock()
|
c.MatchLock.Unlock()
|
||||||
@ -117,6 +119,24 @@ func (c *Coordinator) Coordinate(cmd *SessionCommand) *SessionCommandResult {
|
|||||||
Result: SessionRespPlayed,
|
Result: SessionRespPlayed,
|
||||||
GameResult: resp,
|
GameResult: resp,
|
||||||
}
|
}
|
||||||
|
case SessionCmdPoll:
|
||||||
|
m, exists := c.Matches[cmd.MatchID]
|
||||||
|
if exists {
|
||||||
|
select {
|
||||||
|
case res := <-m.Broadcasts[cmd.ID]:
|
||||||
|
return &SessionCommandResult{
|
||||||
|
ID: cmd.ID,
|
||||||
|
MatchID: m.ID,
|
||||||
|
Result: res,
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return &SessionCommandResult{
|
||||||
|
ID: cmd.ID,
|
||||||
|
MatchID: m.ID,
|
||||||
|
Result: SessionRespBroadcastNone,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return &SessionCommandResult{
|
return &SessionCommandResult{
|
||||||
ID: cmd.ID,
|
ID: cmd.ID,
|
||||||
@ -124,3 +144,45 @@ func (c *Coordinator) Coordinate(cmd *SessionCommand) *SessionCommandResult {
|
|||||||
Result: SessionRespError,
|
Result: SessionRespError,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MatchWatcher(m *Session) {
|
||||||
|
ticker := time.NewTicker(1 * time.Second)
|
||||||
|
old_turn := -1
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
if m.Active && m.Game != nil {
|
||||||
|
if m.Game.Status == game.StatusSentinalWin {
|
||||||
|
for _, v := range m.Broadcasts {
|
||||||
|
v <- SessionRespBroadcastSenWin
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if m.Game.Status == game.StatusScourgeWin {
|
||||||
|
for _, v := range m.Broadcasts {
|
||||||
|
v <- SessionRespBroadcastScoWin
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if old_turn != m.Game.CurrentTurn {
|
||||||
|
old_turn = m.Game.CurrentTurn
|
||||||
|
if old_turn == game.SentinalID {
|
||||||
|
for k, v := range m.pMap {
|
||||||
|
if v == game.SentinalID {
|
||||||
|
m.Broadcasts[k] <- SessionRespBroadcastSenTurn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if old_turn == game.ScourgeID {
|
||||||
|
for k, v := range m.pMap {
|
||||||
|
if v == game.ScourgeID {
|
||||||
|
m.Broadcasts[k] <- SessionRespBroadcastScoTrun
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case <-m.Watcher:
|
||||||
|
close(m.Watcher)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -15,35 +15,45 @@ const (
|
|||||||
SessionCmdJoin = "join"
|
SessionCmdJoin = "join"
|
||||||
SessionCmdLeave = "leave"
|
SessionCmdLeave = "leave"
|
||||||
SessionCmdPlay = "play"
|
SessionCmdPlay = "play"
|
||||||
|
SessionCmdPoll = "poll"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
SessionRespFound SessionResp = "found"
|
SessionRespFound SessionResp = "found"
|
||||||
SessionRespJoined1 = "joined p1"
|
SessionRespJoined1 = "joined p1"
|
||||||
SessionRespJoined2 = "joined p2"
|
SessionRespJoined2 = "joined p2"
|
||||||
SessionRespJoinError = "join error"
|
SessionRespJoinError = "join error"
|
||||||
SessionRespLeft = "left"
|
SessionRespLeft = "left"
|
||||||
SessionRespPlayed = "played"
|
SessionRespPlayed = "played"
|
||||||
SessionRespError = "generic error"
|
SessionRespError = "generic error"
|
||||||
|
SessionRespBroadcastSenTurn = "Sentinal turn"
|
||||||
|
SessionRespBroadcastScoTrun = "Scourge turn"
|
||||||
|
SessionRespBroadcastSenWin = "Sentinal wins"
|
||||||
|
SessionRespBroadcastScoWin = "Scourge wins"
|
||||||
|
SessionRespBroadcastNone = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
ID uuid.UUID
|
ID uuid.UUID
|
||||||
p1 uuid.UUID
|
p1 uuid.UUID
|
||||||
p2 uuid.UUID
|
p2 uuid.UUID
|
||||||
pMap map[uuid.UUID]int
|
pMap map[uuid.UUID]int
|
||||||
Active bool
|
Active bool
|
||||||
Game *game.Game
|
Game *game.Game
|
||||||
|
Watcher chan bool
|
||||||
|
Broadcasts map[uuid.UUID]chan SessionResp
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSession() *Session {
|
func NewSession() *Session {
|
||||||
return &Session{
|
return &Session{
|
||||||
ID: uuid.New(),
|
ID: uuid.New(),
|
||||||
p1: uuid.Nil,
|
p1: uuid.Nil,
|
||||||
p2: uuid.Nil,
|
p2: uuid.Nil,
|
||||||
pMap: make(map[uuid.UUID]int),
|
pMap: make(map[uuid.UUID]int),
|
||||||
Active: false,
|
Active: false,
|
||||||
Game: nil,
|
Game: nil,
|
||||||
|
Watcher: make(chan bool),
|
||||||
|
Broadcasts: make(map[uuid.UUID]chan SessionResp),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,10 +61,12 @@ func (s *Session) Join(id uuid.UUID) SessionResp {
|
|||||||
if s.p1 == uuid.Nil {
|
if s.p1 == uuid.Nil {
|
||||||
s.p1 = id
|
s.p1 = id
|
||||||
s.pMap[id] = game.SentinalID
|
s.pMap[id] = game.SentinalID
|
||||||
|
s.Broadcasts[id] = make(chan SessionResp)
|
||||||
return SessionRespJoined1
|
return SessionRespJoined1
|
||||||
} else if s.p2 == uuid.Nil {
|
} else if s.p2 == uuid.Nil {
|
||||||
s.p2 = id
|
s.p2 = id
|
||||||
s.pMap[id] = game.ScourgeID
|
s.pMap[id] = game.ScourgeID
|
||||||
|
s.Broadcasts[id] = make(chan SessionResp)
|
||||||
return SessionRespJoined2
|
return SessionRespJoined2
|
||||||
} else {
|
} else {
|
||||||
return SessionRespJoinError
|
return SessionRespJoinError
|
||||||
|
Loading…
Reference in New Issue
Block a user