funcmap

package
v0.0.0-...-a408947 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2024 License: AGPL-3.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const PREMIUM_PRICE_PER_MONTH = 5.0

Variables

View Source
var FuncMap = template.FuncMap{
	"print": fmt.Println,

	"html": func(value interface{}) template.HTML {
		return template.HTML(fmt.Sprint(value))
	},

	"config": func(key string) interface{} {
		return services.ConfigMap[key]
	},

	"navbarItem": func(currentPath, name, path string) template.HTML {
		var act string
		if path == currentPath {
			act = "active "
		}
		return template.HTML(fmt.Sprintf(`<a class="%sitem" href="%s">%s</a>`, act, path, name))
	},

	"curryear": func() int {
		return time.Now().Year()
	},

	"hasAdmin": func(privs common.UserPrivileges) bool {
		return privs&common.AdminPrivilegeAccessRAP > 0
	},

	"isRAP": func(p string) bool {
		parts := strings.Split(p, "/")
		return len(parts) > 1 && parts[1] == "admin"
	},

	"favMode": func(favMode float64, current int) string {
		if int(favMode) == current {
			return "active "
		}
		return ""
	},

	"slice": func(els ...interface{}) []interface{} {
		return els
	},

	"int": func(f interface{}) int {
		if f == nil {
			return 0
		}
		switch f := f.(type) {
		case int:
			return f
		case float64:
			return int(f)
		case float32:
			return int(f)
		}
		return 0
	},

	"float": func(i int) float64 {
		return float64(i)
	},

	"atoi": func(s string) interface{} {
		i, err := strconv.Atoi(s)
		if err != nil {
			return nil
		}
		return float64(i)
	},

	"atoint": func(s string) int {
		i, _ := strconv.Atoi(s)
		return i
	},

	"parseUserpage": func(s string) template.HTML {
		return template.HTML(bbcode.ConvertBBCodeToHTML(s))
	},

	"time": func(s string) template.HTML {
		t, _ := time.Parse(time.RFC3339, s)
		return _time(s, t)
	},

	"timeFromTime": func(t time.Time) template.HTML {
		return _time(t.Format(time.RFC3339), t)
	},

	"timeAddDay": func(s string) template.HTML {
		t, _ := time.Parse(time.RFC3339, s)
		t = t.Add(time.Hour * 24)
		return _time(t.Format(time.RFC3339), t)
	},

	"nativeTime": func(s string) time.Time {
		t, _ := time.Parse(time.RFC3339, s)
		return t
	},
	"playtimeConv": func(input float64) string {

		days := math.Floor(float64(input) / 60 / 60 / 24)
		seconds := int(input) % (60 * 60 * 24)
		hours := math.Floor(float64(seconds) / 60 / 60)
		seconds = int(input) % (60 * 60)
		minutes := math.Floor(float64(seconds) / 60)
		seconds = int(input) % 60

		return fmt.Sprintf("%01dd %01dh %01dm", int(math.Floor(days)), int(math.Floor(hours)), int(math.Floor(minutes)))
	},

	"band": func(i1 int, i ...int) int {
		for _, el := range i {
			i1 &= el
		}
		return i1
	},

	"countryReadable":   geoloc.CountryReadable,
	"countryCodepoints": geoloc.CountryToCodepoints,
	"country": func(s string, name bool) template.HTML {
		var c string
		if name {
			c = geoloc.CountryReadable(s)
			if c == "" {
				return ""
			}
		}
		codepoints := geoloc.CountryToCodepoints(s)
		return template.HTML(fmt.Sprintf(`<img src="/static/images/flags/%s.svg" class="new-flag"></img>%s`, codepoints, c))
	},

	"humanize": func(f float64) string {
		return humanize.Commaf(f)
	},

	"levelPercent": func(l float64) string {
		_, f := math.Modf(l)
		f *= 100
		return fmt.Sprintf("%.0f", f)
	},

	"level": func(l float64) string {
		i, _ := math.Modf(l)
		return fmt.Sprintf("%.0f", i)
	},

	"faIcon": func(i string) string {
		classes := strings.Split(i, " ")
		for i, class := range classes {
			if v, ok := fasuimappings.Mappings[class]; ok {
				classes[i] = v
			}
		}
		return strings.Join(classes, " ")
	},

	"log": fmt.Printf,

	"has": func(priv1 interface{}, priv2 float64) bool {
		var p1 uint64
		switch priv1 := priv1.(type) {
		case common.UserPrivileges:
			p1 = uint64(priv1)
		case float64:
			p1 = uint64(priv1)
		case int:
			p1 = uint64(priv1)
		}
		return p1&uint64(priv2) == uint64(priv2)
	},

	"_range": func(x int, y ...int) ([]int, error) {
		switch len(y) {
		case 0:
			r := make([]int, x)
			for i := range r {
				r[i] = i
			}
			return r, nil
		case 1:
			nums, up := pos(y[0] - x)
			r := make([]int, nums)
			for i := range r {
				if up {
					r[i] = i + x + 1
				} else {
					r[i] = i + y[0]
				}
			}
			if !up {

				sort.Sort(sort.Reverse(sort.IntSlice(r)))
			}
			return r, nil
		}
		return nil, errors.New("y must be at maximum 1 parameter")
	},

	"blackfriday": func(m string) template.HTML {

		return template.HTML(
			blackfriday.Run(
				[]byte(
					m[strings.Index(m, "\n---\n")+5:],
				),
				blackfriday.WithExtensions(blackfriday.CommonExtensions),
			),
		)
	},

	"i": func(a bool, x, y interface{}) interface{} {
		if a {
			return x
		}
		return y
	},

	"modes": func() []string {
		return []string{
			"osu!",
			"Taiko",
			"Catch",
			"Mania",
		}
	},

	"cmodes": func() []string {
		return []string{
			"Vanilla",
			"Relax",
			"Autopilot",
		}
	},

	"_or": func(args ...interface{}) interface{} {
		for _, a := range args {
			if a != nil && a != false {
				return a
			}
		}
		return nil
	},

	"unixNano": func() string {
		return strconv.FormatInt(hanayoStarted, 10)
	},

	"playstyle": func(i float64, f *models.ProfileData) string {
		var parts []string

		p := int(i)
		for k, v := range playstyle.Styles {
			if p&(1<<uint(k)) > 0 {
				parts = append(parts, f.T(v))
			}
		}

		return strings.Join(parts, ", ")
	},

	"plus": func(i ...float64) float64 {
		var sum float64
		for _, i := range i {
			sum += i
		}
		return sum
	},
	"minus": func(i1 float64, i ...float64) float64 {
		for _, i := range i {
			i1 -= i
		}
		return i1
	},

	"rsin": func(i interface{}) interface{} {
		if i == nil {
			return []struct{}{}
		}
		return i
	},

	"loadjson": func(jsonfile string) interface{} {
		f, err := ioutil.ReadFile(jsonfile)
		if err != nil {
			return nil
		}
		var x interface{}
		err = json.Unmarshal(f, &x)
		if err != nil {
			return nil
		}
		return x
	},

	"teamJSON": func() map[string]interface{} {
		f, err := ioutil.ReadFile("team.json")
		if err != nil {
			return nil
		}
		var m map[string]interface{}
		json.Unmarshal(f, &m)
		return m
	},

	"in": func(a1 interface{}, as ...interface{}) bool {
		for _, a := range as {
			if a == a1 {
				return true
			}
		}
		return false
	},
	"capitalise": strings.Title,

	"servicePrefix": func(s string) string { return servicePrefixes[s] },

	"randomLogoColour": func() string {
		if rand.Int()%4 == 0 {
			return logoColours[rand.Int()%len(logoColours)]
		}
		return "pink"
	},

	"after": func(s string) bool {
		t, _ := time.Parse(time.RFC3339, s)
		return t.After(time.Now())
	},

	"qb": func(q string, p ...interface{}) map[string]qsql.String {
		r, err := services.QB.QueryRow(q, p...)
		if err != nil {
			slog.Error("Error in qb", "error", err.Error())
		}
		if r == nil {
			return make(map[string]qsql.String, 0)
		}
		return r
	},
	"qba": func(q string, p ...interface{}) []map[string]qsql.String {
		r, err := services.QB.Query(q, p...)
		if err != nil {
			slog.Error("Error in qba", "error", err.Error())
		}
		return r
	},
	"qbe": func(q string, p ...interface{}) int {
		i, _, err := services.QB.Exec(q, p...)
		if err != nil {
			slog.Error("Error in qbe", "error", err.Error())
		}
		return i
	},

	"bget": func(ept string, qs ...interface{}) map[string]interface{} {
		settings := settingsState.GetSettings()
		d, err := http.Get(fmt.Sprintf(settings.PUBLIC_BANCHO_SERVICE_BASE_URL+"/api/v1/"+ept, qs...))
		if err != nil {
			slog.Error("Error in bget", "error", err.Error())
			return nil
		}
		x := make(map[string]interface{})
		data, _ := ioutil.ReadAll(d.Body)
		json.Unmarshal(data, &x)
		return x
	},

	"styles": func() []string {
		return playstyle.Styles[:]
	},

	"shift": func(n1, n2 int) int {
		return n1 << uint(n2)
	},

	"calculatePremiumPrice": func(months float64) string {
		return fmt.Sprintf("%.2f", months*PREMIUM_PRICE_PER_MONTH)
	},

	"csrfGenerate": func(u int) template.HTML {
		return template.HTML(`<input type="hidden" name="csrf" value="` + misc.MustCSRFGenerate(u) + `">`)
	},

	"csrfURL": func(u int) template.URL {
		return template.URL("csrf=" + misc.MustCSRFGenerate(u))
	},

	"systemSettings": systemSettings,

	"authCodeURL": func(u int) string {
		return ""
	},

	"perc": func(i, total float64) string {
		return fmt.Sprintf("%.0f", i/total*100)
	},

	"atLeastOne": func(i int) int {
		if i < 1 {
			i = 1
		}
		return i
	},

	"ieForm": func(c *gin.Context) template.HTML {
		if !IsIE(c.Request.UserAgent()) {
			return ""
		}
		return ieUnfucker
	},

	"version": func() string {
		return version.Version
	},

	"rediget": func(k string) string {
		x := services.RD.Get(k)
		if x == nil {
			return ""
		}
		if err := x.Err(); err != nil {
			slog.Error("An error occurred", "error", err.Error())
		}
		return x.Val()
	},
	"languageInformation": func() []langInfo {
		return languageInformation
	},
	"languageInformationByNameShort": func(s string) langInfo {
		for _, lang := range languageInformation {
			if lang.NameShort == s {
				return lang
			}
		}
		return langInfo{}
	},
	"countryList": func(n int64) []string {
		return services.RD.ZRevRange("hanayo:country_list", 0, n-1).Val()
	},
	"documentationFiles": doc.GetDocs,
	"documentationData": func(slug string, language string) doc.File {
		if i, err := strconv.Atoi(slug); err == nil {
			slug = doc.SlugFromOldID(i)
		}
		return doc.GetFile(slug, language)
	},
	"privilegesToString": func(privs float64) string {
		return common.Privileges(privs).String()
	},
	"htmlescaper": template.HTMLEscaper,
}

funcMap contains useful functions for the various templates.

Functions

func IsIE

func IsIE(s string) bool

Types

This section is empty.

Jump to

Keyboard shortcuts

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