Ejemplo n.º 1
0
func (nm *V23Manager) getReadyToRun(ch chan bool) {
	defer nm.mu.Unlock()
	if nm.chatty {
		log.Printf("Calling v23.Init")
	}
	nm.ctx, nm.shutdown = v23.Init()
	if nm.shutdown == nil {
		log.Panic("shutdown nil")
	}
	if nm.chatty {
		log.Printf("Setting root to %v", nm.namespaceRoot)
	}
	v23.GetNamespace(nm.ctx).SetRoots(nm.namespaceRoot)

	nm.initialPlayerNumbers = nm.playerNumbers()
	if nm.chatty {
		log.Printf("Found %d players.", len(nm.initialPlayerNumbers))
	}
	sort.Ints(nm.initialPlayerNumbers)
	myId := 1
	if len(nm.initialPlayerNumbers) > 0 {
		myId = nm.initialPlayerNumbers[len(nm.initialPlayerNumbers)-1] + 1
	}

	if nm.isGameMaster {
		myId = 999
	}

	nm.relay = relay.MakeRelay()
	nm.myself = model.NewPlayer(myId)
	if nm.isGameMaster {
		if nm.chatty {
			log.Printf("I am game master.")
		}
		nm.isReady = true
		ch <- true
		return
	}
	if nm.chatty {
		log.Printf("I am player %v\n", nm.myself)
	}

	myName := nm.serverName(nm.Me().Id())
	if nm.chatty {
		log.Printf("Calling myself %s\n", myName)
	}
	ctx, s, err := v23.WithNewServer(nm.ctx, myName, ifc.GameServiceServer(nm.relay), MakeAuthorizer())
	if err != nil {
		log.Panic("Error creating server:", err)
		ch <- false
		return
	}
	saveEndpointToFile(s)
	nm.ctx = ctx
	nm.isReady = true
	ch <- true
}
Ejemplo n.º 2
0
func (nm *V23Manager) JoinGame(chBc <-chan model.BallCommand) {
	if nm.chatty {
		log.Println("Joining game.")
	}
	nm.chBallCommand = chBc
	for _, id := range nm.initialPlayerNumbers {
		nm.recognizeOther(model.NewPlayer(id))
	}
	if nm.chatty {
		log.Printf("I see %d players.\n", len(nm.players))
	}
	if nm.isGameMaster {
		if chBc != nil {
			log.Panic("game master should not have chBc")
		}
		return
	}
	nm.sayHelloToEveryone()
	nm.checkDoors()
	go nm.run()
	nm.isRunning = true
}