예제 #1
0
func (h *GameHub) sendBroadcastMessage(msg Message, roomId string) {
	room := h.rooms[roomId]
	b, err2 := rjson.Marshal(msg)
	if err2 != nil {
		return
	}

	for k, _ := range room.players {
		h.players[k].conn.send <- string(b)
	}
}
예제 #2
0
// Send Initial information back to Player
func (p *Player) SendPlayerIdentity() {
	msg := Message{
		Operation: "PlayerIdentity",
		Sender:    "Server",
		RoomID:    "Lobby",
		Message:   p.id,
	}
	b, err := rjson.Marshal(msg)
	if err != nil {
	}
	p.conn.send <- string(b)
}
예제 #3
0
func (h *GameHub) getGameTypes() string {
	gameTypes := []string{"BlackJack", "TicTacToe", "QuizBowl"}

	msg := Message{
		Operation:    "GetGameTypes",
		Sender:       "Server",
		RoomID:       "",
		MessageArray: gameTypes,
	}
	b, err := rjson.Marshal(msg)
	if err != nil {
	}
	return string(b)

}
예제 #4
0
func (p *Player) AnnouncePlayer(roomId string, isExiting bool) string {
	msg := Message{
		Operation: "ChatMessage",
		Sender:    "Server",
		RoomID:    roomId,
		Message:   "",
	}
	if isExiting == false {
		msg.Message = "Player " + p.id + " entered " + roomId + "."
	} else {
		msg.Message = "Player " + p.id + " exited " + roomId + "."
	}
	b, err := rjson.Marshal(msg)
	if err != nil {
	}
	return string(b)
}
예제 #5
0
func (quiz *QuizBowlGame) SendQuestion(roomId string, questionId string, h *GameHub) {
	q := quiz.findQuestionById(questionId)
	fmt.Println(q)
	q.CorrectAnswer = "-1"
	b, err := rjson.Marshal(q)
	if err != nil {
		fmt.Println("Dfdfs", err)
	}

	msg := Message{
		Operation:  "SendQuestion",
		Sender:     "Server",
		RoomID:     "",
		MessageMap: string(b),
	}

	h.sendBroadcastMessage(msg, roomId)
}
예제 #6
0
func (quiz *QuizBowlGame) Score(msg string, player *Player) {
	if player.data == nil {
		player.data = 0
	}

	var answer QuizBowlAnswer
	err := rjson.Unmarshal([]byte(msg), &answer)
	if err != nil {
		fmt.Println(err)
		log.Debug("Could not score answer")
	}

	var m string
	question := quiz.findQuestionById(answer.QuestionId)
	correctAnswer := question.CorrectAnswer
	answerText := quiz.findAnswerText(question, correctAnswer)
	fmt.Println(answerText)
	fmt.Println(correctAnswer, answer.AnswerId)
	if answer.AnswerId == correctAnswer {
		player.data = player.data.(int) + 200
		fmt.Println("Score:", player.data)
		m = "Correct Answer! \nCurrent Score: " + strconv.Itoa(player.data.(int))
	} else {
		m = "Wrong! The correct answer is " + answerText + "\n Current score: " + strconv.Itoa(player.data.(int))
	}

	msg2 := Message{
		Operation: "SendResult",
		Sender:    "Server",
		RoomID:    "",
		Message:   m,
	}

	b, err2 := rjson.Marshal(msg2)
	if err2 != nil {
		log.Debug("There was a marshalling error.")
		return
	}
	//Send message back to player
	player.conn.send <- string(b)
}
예제 #7
0
func (h *GameHub) getRoomList() string {
	rooms := h.rooms
	roomList := make([]string, len(rooms))

	i := 0
	for k, _ := range rooms {
		roomList[i] = k
		i++
	}

	msg := Message{
		Operation:    "GetRoomList",
		Sender:       "Server",
		RoomID:       "",
		MessageArray: roomList,
	}
	b, err := rjson.Marshal(msg)
	if err != nil {
	}
	return string(b)
}