~exprez135/go-hypixel

23f07b806e2e299045418602c73a61bc337e8d24 — Nate Ijams 3 years ago 0c8a060 v0.0.19
Add recent game feature.
3 files changed, 108 insertions(+), 2 deletions(-)

M api/hypixel.go
M engine/api.go
M engine/discord.go
M api/hypixel.go => api/hypixel.go +21 -2
@@ 231,7 231,26 @@ func (c *Client) player(parameterName string, parameterValue string) (*json.RawM
	}
	return result.Player, nil
}
/*
// RecentGamesByUUID queries a player's recent games by their uuid.
func (c *Client) RecentGamesByUUID(uuid string) (*json.RawMessage, error) {
	return c.recentgames("uuid", uuid)
}

// Internal helper method which queries for a player's recent games using a parameterName and a parameterValue. Returns a
// *json.RawMessage.
func (c *Client) recentgames(parameterName string, parameterValue string) (*json.RawMessage, error) {
	result := &RecentGamesResponse{}
	err := c.Query("recentgames", map[string]string{parameterName: parameterValue}, result)

	if err != nil {
		return nil, err
	} else if result.Success == false {
		return nil, errors.New(result.Cause)
	}
	return result.Games, nil
}
*/
// SessionByUUID queries a players session by its uuid.
func (c *Client) SessionByUUID(uuid string) (*json.RawMessage, error) {
	return c.session("uuid", uuid)


@@ 309,7 328,7 @@ func (c *Client) RecentGamesByUUID(uuid string) (*json.RawMessage, error) {
// Internal helper method which queries for a players recent games using a parameterName and a parameterValue. Returns a
// *json.RawMessage.
func (c *Client) recentGames(parameterName string, parameterValue string) (*json.RawMessage, error) {
	result := &SessionResponse{}
	result := &RecentGamesResponse{}
	err := c.Query("recentGames", map[string]string{parameterName: parameterValue}, result)

	if err != nil {


@@ 317,7 336,7 @@ func (c *Client) recentGames(parameterName string, parameterValue string) (*json
	} else if result.Success == false {
		return nil, errors.New(result.Cause)
	}
	return result.Session, nil
	return result.Games, nil
}

// WatchDogStats calls the API at /watchdogstats. It will returns an error or five *json.RawMessage

M engine/api.go => engine/api.go +40 -0
@@ 299,4 299,44 @@ func GetPlayerStatus(uuid string) (bool, error) {

// TODO: add functions for recentGames

// Returns: (mode string,
func GetRecentGame(uuid string) (string, string, int, error) {
	rawResponse, err := Hclient.RecentGamesByUUID(uuid)
	if err != nil {
		log.Println("Error fetching player's recent games:", err)
		return "", "", 0, err
	}

	// Returns []Result. 0 will be mode. 1 will be map. 2 will be ended.
	recentGamesResults := gjson.GetBytes(*rawResponse, `@this|#(gameType=="BEDWARS")`)

	if recentGamesResults.Type.String() == "Null" {
		log.Println("Error code 1004")
		return "", "", 0, errors.New("No bed wars games found.")
	}

	recentGamesResultsMore := gjson.GetManyBytes([]byte(recentGamesResults.Raw), `mode`, `map`, `ended`)

	modeRaw := recentGamesResultsMore[0].Str
	var modeClean string

	if modeRaw == "BEDWARS_FOUR_FOUR" {
		modeClean = "4v4v4v4"
	} else if modeRaw == "BEDWARS_FOUR_THREE" {
		modeClean = "3v3v3v3"
	} else if modeRaw == "BEDWARS_EIGHT_TWO" {
		modeClean = "2v2v2v2v2v2v2v2"
	} else {
		modeClean = ""
	}

	mapRaw := recentGamesResultsMore[1].Str

	endedRaw := int(recentGamesResultsMore[2].Num)
	// convert to int

	return modeClean, mapRaw, endedRaw, nil

}

// TODO: add general hypixel functions (boosters, playerCount, gameCounts, WatchdogStats, leaderboards)

M engine/discord.go => engine/discord.go +47 -0
@@ 5,6 5,7 @@ import (
	"math/rand"
	"os"
	"os/signal"
	"strconv"
	"strings"
	"syscall"
	"time"


@@ 254,6 255,51 @@ func playerStatus(ctx *exrouter.Context) {
	}
}

func playerRecent(ctx *exrouter.Context) {
	var message string
	arguments := ctx.Args
	arg := arguments.Get(1)
	uuid, err := UsernameToUUID(arg)
	if err != nil {
		log.Println("discord.trackplayer:", err)
		ctx.Reply("Unable to add " + arg + " to the tracked list. Did you spell the username correctly?")
	}
	online, err := GetPlayerStatus(uuid)
	if err != nil {
		ctx.Reply("Something went wrong fetching the status of " + arg)
		log.Println("discord.player:", err)
		return
	}

	gameMode, gameMap, gameTime, err := GetRecentGame(uuid)
	if err != nil {
		ctx.Reply(err)
		return
	}

	if online == true {
		message = arg + "is online!"
	} else {
		message = arg + "is offline!"
	}

	gameTime2 := time.Unix(0, int64(gameTime)*1000000)
	oneHour, _ := time.ParseDuration("60m")
	timeSince := time.Since(gameTime2)

	// check if gameTime is more than 1 hour ago
	if timeSince > oneHour {
		// game was too long ago
		message += " They haven't played any Bed Wars games in the last hour. :("
	} else {
		// game was within hour
		message += " Their last game in Bed Wars " + gameMode + " on the " + gameMap + " map ended " + strconv.FormatFloat(timeSince.Minutes(), 'f', 4, 64) + " minutes ago."
	}

	// put together message
	ctx.Reply(message)
}

func trackPlayer(ctx *exrouter.Context) {
	arguments := ctx.Args
	arg := arguments.Get(1)


@@ 261,6 307,7 @@ func trackPlayer(ctx *exrouter.Context) {
	if err != nil {
		log.Println("discord.trackplayer:", err)
		ctx.Reply("Unable to add " + arg + " to the tracked list. Did you spell the username correctly?")
		return
	}
	err = AddPlayerToOrganization(platform, ctx.Msg.GuildID, uuid)
	if err != nil {