Exemple #1
0
func main() {
	if len(os.Args) != 3 {
		fmt.Fprintln(os.Stderr, "Usage: play <game pin> <nickname>")
		os.Exit(1)
	}
	gamePin, err := strconv.Atoi(os.Args[1])
	if err != nil {
		fmt.Fprintln(os.Stderr, "invalid game pin:", os.Args[1])
		os.Exit(1)
	}
	nickname := os.Args[2]

	conn, err := kahoot.NewConn(gamePin)
	if err != nil {
		fmt.Fprintln(os.Stderr, "failed to connect:", err)
		os.Exit(1)
	}
	if err := conn.Login(nickname); err != nil {
		fmt.Fprintln(os.Stderr, "failed to login:"******"Could not receive question:", err)
			}
			os.Exit(1)
		}
		if action.Type == kahoot.QuestionIntro {
			fmt.Println("Awaiting answers...")
		} else if action.Type == kahoot.QuestionAnswers {
			fmt.Print("Answer (0 through " + strconv.Itoa(action.NumAnswers-1) + "): ")
			answer := readNumberInput()
			if err := quiz.Send(answer); err != nil {
				fmt.Fprintln(os.Stderr, "Could not answer:", err)
				os.Exit(1)
			}
		}
	}
}
Exemple #2
0
func launchConnection(gamePin int, nickname string) {
	defer wg.Done()

	conn, err := kahoot.NewConn(gamePin)
	if err != nil {
		fmt.Fprintln(os.Stderr, "failed to connect:", err)
		os.Exit(1)
	}

	closed := make(chan bool, 1)
	closed <- false
	go func() {
		sigChan := make(chan os.Signal, 1)
		signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
		<-sigChan
		<-closed
		closed <- true
		conn.GracefulClose()
	}()

	if err := conn.Login(nickname); err != nil {
		fmt.Fprintln(os.Stderr, "failed to login:"******"Could not receive question:", err)
				os.Exit(1)
			}
		}
		if action.Type == kahoot.QuestionAnswers {
			quiz.Send(rand.Intn(action.NumAnswers))
		}
	}
}