engine

package
v0.0.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2021 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DiscordToken = os.Getenv("DISCORD_TOKEN")
View Source
var Dsession *discordgo.Session
View Source
var Hclient *api.Client
View Source
var Iron *bolt.DB

Functions

func AddGuildToOrganization

func AddGuildToOrganization(platform string, platformID string, guild string) error

// AddFriendsToOrganization appends a new player uuid to the organizations list of tracked friend lists. First, it Gets the proper organization. Then it appends and Puts the new org structure into BoltDB. Returns an error.

func AddFriendsToOrganization(platform string, platformID string, uuid string) error {
	// put friends list
	err := PutFriends(uuid)
	if err != nil {
		log.Println("organizations.AddFriendsToOrganization: error putting friends", err)
	}
	// get proper organization
	org, err := GetPlatformOrganization(platform, platformID)
	if err != nil {
		log.Println("Unable to GetPlatformOrganization in AddFriendsToOrganization:", err)
		return err
	}
	newOrg := *org
	newOrg.Friends = append(newOrg.Friends, uuid)

	err = PutPlatformOrganization(newOrg)
	if err != nil {
		log.Println("Unable to Put newOrg in AddFriendsToOrganization:", err)
		return err
	}
	return nil
}

AddGuildToOrganization appends a new guild name to the organizations list of tracked guild lists. First, it Gets the proper organization. Then it appends and Puts the new org structure into BoltDB. Returns an error.

func AddPlayerToOrganization

func AddPlayerToOrganization(platform string, platformID string, uuid string) error

AddPlayerToOrganization appends a new player uuid to the organizations list of tracked players. First, it Gets the proper organization. Then it appends and Puts the new org structure into BoltDB. Returns an error.

func CreateAPIClient

func CreateAPIClient(key string) *api.Client

func DeleteFriendsFromOrganization

func DeleteFriendsFromOrganization(platform string, platformID string, uuid string) error

DeleteFriendsFromOrganization deletes a friend uuid from an organization's list of tracked friend list. Returns an error.

func DeleteGuildFromOrganization

func DeleteGuildFromOrganization(platform string, platformID string, guild string) error

DeleteGuildFromOrganization deletes a guild from an organization's list of tracked guilds. Returns an error.

func DeletePlayerFromOrganization

func DeletePlayerFromOrganization(platform string, platformID string, uuid string) error

DeletePlayerFromOrganization deletes a player uuid from an organization's list of tracked players. Returns an error.

func DeleteUsernames

func DeleteUsernames(username string) error

DeleteUsernames deletes the key matching the given username in the "usernames" bucket. Returns an error if anything goes wrong.

func EnsureBucketsExist

func EnsureBucketsExist(bucketList []string) error

EnsureBucketsExist creates buckets in BoltDB if they do not yet exist. Takes a slice of string bucket names. Returns an error if problem occurs.

func GetCachedPlayerStatus added in v0.0.11

func GetCachedPlayerStatus(uuid string) bool

TODO add error checking

func GetGuild

func GetGuild(name string) (*json.RawMessage, error)

GetGuild retrieves the raw JSON guild response to a guild name. If the database does not have it, GetGuild calls PutGuild() to add it to the database. Returns maybe the *json.RawMessage and error.

func GetGuildList

func GetGuildList(name string) ([]string, error)

GetGuildList returns a slice of strings with the UUIDs of one guild's members. Returns maybe a string slice and error.

func GetGuildNameWithPlayerUUID

func GetGuildNameWithPlayerUUID(uuid string) (string, error)

GetGuildNameWithPlayerUUID

func GetGuildWithPlayerUUID

func GetGuildWithPlayerUUID(uuid string) (*json.RawMessage, error)

GetGuildWithPlayerUUID

func GetPlayerStatus

func GetPlayerStatus(uuid string) (bool, error)

func GetRecentGame added in v0.0.19

func GetRecentGame(uuid string) (string, string, int, error)

Returns: (mode string,

func GetTrackedList

func GetTrackedList(platform string, platformKey string) ([]string, []string, []string, error)

func GetUsernamesUUID

func GetUsernamesUUID(username string) (string, error)

GetUsernamesUUID gets a uuid from a username in the "usernames" bucket.

func GuildCreate

func GuildCreate(s *discordgo.Session, event *discordgo.GuildCreate)

GuildCreate will be called (due to AddHandler above) every time a new guild is joined. It adds a new PlatformOrganization to BoltDB if this is a new guild.

func ListExistingTopBuckets

func ListExistingTopBuckets() error

ListExistingTopBuckets lists all the top level buckets in the open database to stdout. Returns an error if

func ListOnlinePlayers added in v0.0.15

func ListOnlinePlayers(guilds []string) []string

Returns a string array of the UUIDs of each online player. Currently limited to guild members only!

func Notify

func Notify(platform string, channelID string, uuid string, newStatus bool)

func PutGuild

func PutGuild(name string) error

// PutFriends fetches the raw JSON friends from the API and stores it in the "friends" bucket. Returns an error.

func PutFriends(uuid string) error {
	log.Println("STARTING PutFriends()...")
	rawFriends, err := Hclient.FriendsByUUID(uuid)
	if err != nil {
		log.Println("Error calling api.FriendsByUUID in PutFriends:", err)
		return err
	}
	log.Println("PutFriends(): *rawFriends is:", *rawFriends)
	err = Iron.Update(func(tx *bolt.Tx) error {
		log.Println("PutFriends(): opened Iron db")
		b := tx.Bucket([]byte("friends"))
		putErr := b.Put([]byte(uuid), *rawFriends)
		if putErr != nil {
			log.Println("PutFriends(): problem putting. putErr is:", putErr)
			return putErr
		}
		return nil
	})
	if err != nil {
		log.Println("Error putting friends:", err)
		return err
	}
	log.Println("About to return nil at end of PutFriends()")
	return nil
}

// GetFriends retrieves the raw JSON friends response to a UUID. If the database does not have it, GetFriends calls PutFriends() to add it to the database. Returns maybe the *json.RawMessage and error.

func GetFriends(uuid string) (*json.RawMessage, error) {
	var result json.RawMessage
	err := Iron.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("friends"))
		v := b.Get([]byte(uuid))
		if v == nil {
			return errors.New("404")
		}
		result = v
		return nil
	})
	if err != nil {
		log.Println("GetFriends() ~ UUID not found in BoltDB:", err)
		putErr := PutFriends(uuid)
		log.Println("putErr in GetFriends():", putErr)
		log.Println("GetFriends() ~ Should have just PutFriends():")
		if putErr != nil {
			log.Println("Unable to PutFriends in GetFriends:", err)
			return nil, putErr
		}
		err = Iron.View(func(tx *bolt.Tx) error {
			log.Println("Opened BoltDB in GetFriends()......")
			b := tx.Bucket([]byte("friends"))
			v := b.Get([]byte(uuid))
			if v == nil {
				return errors.New("404")
			}
			result = v
			//log.Println("In BoltDB in GetFriends(), the result is:", result)
			return nil
		})
		if err != nil {
			log.Println("Redundancy failed in GetFriends:", err)
			return nil, err
		}
		log.Println("GetFriends() ~ UUID *was* found in BoltDB after running PutFriends().")
		return &result, err
	}
	log.Println("GetFriends() returning at the very last. Huh?")
	return &result, err
}

// GetFriendsList returns a slice of strings with the UUIDs of one player's friends. Returns maybe a string slice and error.

func GetFriendsList(uuid string) ([]string, error) {
	friendsRaw, err := GetFriends(uuid)
	if err != nil {
		log.Println("GetFriends failed in GetFriendsList:", err)
		return nil, err
	}
	friendsResults := gjson.GetBytes(*friendsRaw, "#.uuid*")
	var friendsListNarrowed []string
	json.Unmarshal([]byte(friendsResults.Raw), &friendsListNarrowed)
	var friendsList []string
	for _, f := range friendsListNarrowed {
		if f != uuid {
			friendsList = append(friendsList, f)
		}
	}
	return friendsList, nil
}

PutGuild fetches the raw JSON guild from the API and stores it in the "guilds" bucket. Returns an error.

func PutPlatformOrganization

func PutPlatformOrganization(org PlatformOrganization) error

PutPlatformOrganization puts a PlatformOrganization struct into BoltDB.

func PutUsernames

func PutUsernames(username string) error

PutUsernames fetches the UUID associated with a username from the API and puts it in the "usernames" bucket. Returns an error.

func RemoveDuplicates added in v0.0.11

func RemoveDuplicates(elements []string) []string

RemoveDuplicates takes a slice of strings and removes any duplicate values. Returns a slice of strings.

func SendStatusNotifications

func SendStatusNotifications(uuid string, newStatus bool) error

SendStatusNotifications takes a uuid and new status and sends notifications to all guilds tracking the player directly, or by virtue of friends or guilds.

func SetOrganizationChannel

func SetOrganizationChannel(platform string, platformID string, channelID string) error

func StartBolt

func StartBolt(path string) error

StartBolt opens the Bolt database. Returns an error if opening goes wrong.

func StartDiscord

func StartDiscord()

StartDiscord creates the main Discord session, registers commands, and initializes the router. This function can be supplanted by, e.g. StartIRC(), to use a different (or multiple) bot(s). This will require multiple changes to other functions as well.

func StopBolt

func StopBolt()

StopBolt closes the database. This should be deferred in the main application.

func ToggleOrganizationNotifications

func ToggleOrganizationNotifications(platform string, platformID string) (bool, error)

func UpdateFriendGuildLists

func UpdateFriendGuildLists()

func UpdateStatuses

func UpdateStatuses() error

func UpdateTrackedPlayerList

func UpdateTrackedPlayerList() error

func UsernameToUUID

func UsernameToUUID(username string) (string, error)

UsernameToUUID guarantees returning a UUID from a username if no error occurs. First, it runs GetUsernamesUUID() to use cache in BoltDB. If the username is not in BoltDB, it will Put it there and then Get it.

Types

type PlatformOrganization

type PlatformOrganization struct {
	Platform       string // e.g. “discord” or “text message”
	PlatformID     string // e.g. discord guild id or phone number ~ unique BoltDB ID
	DefaultChannel string
	Notifications  bool
	Players        []string // slice of players to be notified of
	Friends        []string // slice of players whose friends are to be notified of
	Guilds         []string // slice of guilds whose members are to be notified of
	Active         bool
	CreatedAt      time.Time
	UpdatedAt      time.Time
}

PlatformOrganization is the basic structure for each end point (e.g. a Discord group). It stores the settings for that group as well as lists of its tracked entities (players, friend lists, and guilds).

func GetPlatformOrganization

func GetPlatformOrganization(platform string, platformID string) (*PlatformOrganization, error)

GetPlatformOrganization fetches a PlatformOrganization struct from BoltDB.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL