Exemplo n.º 1
0
Arquivo: page.go Projeto: GuoJing/hugo
func (page *Page) handleJsonMetaData(datum []byte) (interface{}, error) {
	var f interface{}
	if err := json.Unmarshal(datum, &f); err != nil {
		return f, fmt.Errorf("Invalid JSON in %v \nError parsing page meta data: %s", page.FileName, err)
	}
	return f, nil
}
Exemplo n.º 2
0
func (quiz *QuizBowlGame) HandleGameMessage(msg string, h *GameHub) bool {
	var conn *connection
	var player *Player
	var handledMessage bool

	var data Message
	err := rjson.Unmarshal([]byte(msg), &data)
	if err != nil {
		fmt.Println("error:", err)
	}

	// Get sender's connection
	sender := data.Sender
	if sender != "" && sender != "Server" {
		player = h.players[sender]
		conn = player.conn
	}
	fmt.Println(conn)

	switch data.Operation {
	case "SendAnswer":
		quiz.Score(data.MessageMap, player)
		handledMessage = true
	}

	return handledMessage
}
Exemplo n.º 3
0
func (quiz *QuizBowlGame) loadQuestions() {
	bs, err := ioutil.ReadFile("json/sample-questions.json")
	if err != nil {
		return
	}

	questions := make([]Question, 0)

	err = rjson.Unmarshal(bs, &questions)
	if err != nil {
		fmt.Println("Error Loading Questions:", err)
	}
	log.Debug("Loaded questions")

	quiz.questions = questions
}
Exemplo n.º 4
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)
}
Exemplo n.º 5
0
func (h *GameHub) handleMessage(msg string) bool {
	var conn *connection
	var player *Player
	var handledMessage bool

	var data Message
	err := rjson.Unmarshal([]byte(msg), &data)
	if err != nil {
		fmt.Println("error:", err)
	}

	log.Debug(msg)

	// Get sender's connection
	sender := data.Sender
	if sender != "" && sender != "Server" {
		player = h.players[sender]
		conn = player.conn
	}

	switch data.Operation {
	case "CreateRoom":
		fmt.Println("CreateRoom")
		r := &GameRoom{players: make(map[string]bool)}
		r.roomId = uniuri.New()
		h.rooms[r.roomId] = r
		fmt.Println(h.getRoomList())

	// message to say room was created
	// move player to room
	case "ChangeNick":
		msg := player.ChangeNick(data.Message)
		h.sendBroadcastMessage(msg, "Lobby")
		// Send message to room saying nick has changed
		handledMessage = true
	case "JoinRoom":
		fmt.Println("JoinRoom")
		// TODO: Check if player is already in game room
		// Store gameroom data also in player object?
		roomId := data.Message
		room, err := h.rooms[roomId]
		if err != false {
			room.addPlayer(player)
			log.Debug("Player " + player.id + " added to room " + roomId)
			fmt.Println(room.players)
		}
	case "LeaveRoom":
		fmt.Println("LeaveRoom")
		// TODO: Verify player is already in game room
		roomId := data.Message
		room, err := h.rooms[roomId]
		if err != false {
			room.removePlayer(player)
			log.Debug("Player " + player.id + " left room " + roomId)
			fmt.Println(room.players)
		}

	case "StartGame":
		roomId := data.RoomID
		room := h.rooms[roomId]
		room.game.startGame(room, h)
	case "GetGameTypes":
		conn.send <- h.getGameTypes()
		handledMessage = true
	case "GetPlayerList":
		for _, v := range h.players {
			fmt.Println(v)
		}
	case "GetRoomList":
		conn.send <- h.getRoomList()
		handledMessage = true
	default:
		// Send to specific room for handling
		room := h.findRoomById(data.RoomID)
		if room != nil {
			fmt.Println(room)
			if room.game != nil {
				room.game.HandleGameMessage(msg, h)
			}
		}
	}

	fmt.Println(data.Message)
	return handledMessage
}