func (gh *game_hub) processNewGameRequests() { for ng := range gh.gameRequests { // look for an existing game to satisfy the new request gm := gh.findGame(ng) // create a game if one can't be found if gm == nil { logger.Info("Couldn't find an available game. Creating a new one") gm = gh.makeGame(ng.NumPlayers) } else { logger.Info("Found existing game. Slotting in") } gm.proxy.slotClientConnection(gm.currentPlayers, ng.cconn) gm.currentPlayers += 1 if gm.currentPlayers == gm.numPlayers { gh.commitGame(gm) } } }
func (gh *game_hub) handleDisconnection(message string, cconn *clientConnection) { // Find the connection and kill it, cleaning up its game if necessary logger.Info("Client Disconnected. Cleaning up...") for np, game := range gh.uncommittedGames { for i := 0; i < game.currentPlayers; i++ { if game.proxy.proxyConns[i].info.Id == cconn.info.Id { game.proxy.removeClientConnection(i) game.currentPlayers -= 1 if game.currentPlayers == 0 { logger.Infof("%d player uncommitted game empty. Dropping", np) delete(gh.uncommittedGames, np) } break } } } }
func (gh *game_hub) commitGame(game *game) { delete(gh.uncommittedGames, game.numPlayers) // make connection to server conn, err := connectToServer() if err != nil { logger.Fatalf("Could not connect to server, dying...") return } game.proxy.server = &serverConnection{conn: conn} game.channelInHandler(game.proxy) go game.proxy.serverReadPump() game.proxy.sendInitialGameInfo() logger.Info("Committed a game, proxying its messages") gh.committedGames.PushBack(game) }