Esempio n. 1
0
File: box.go Progetto: cubeee/go-sig
func (b BoxGoalGenerator) CreateSignature(req util.ParsedSignatureRequest) (util.Signature, error) {
	username := req.GetProperty("username").(string)
	skill := req.GetProperty("skill").(util.Skill)
	goal := req.GetProperty("goal").(int)
	goaltype := req.GetProperty("goaltype").(util.GoalType)

	stats, err := util.GetStats(username)
	if err != nil {
		var s util.Signature
		return s, errors.New(fmt.Sprintf("Failed to fetch stats for %s", username))
	}
	stat := util.GetStatBySkill(stats, skill)

	currentLevel := util.LevelFromXP(stat.Skill, stat.Xp)
	currentXP := stat.Xp
	var goalXP int
	var remainder int
	if goaltype == util.GoalXP {
		goalXP = goal
		remainder = goalXP - currentXP
	} else {
		goalXP = util.XPForLevel(stat.Skill, goal)
		remainder = util.XPToLevel(stat.Skill, currentXP, goal)
	}
	goalLevel := util.LevelFromXP(stat.Skill, goalXP)
	if remainder < 0 {
		remainder = 0
	}
	percent := int(float64(currentXP) / float64(goalXP) * 100.0)
	if percent > 100 {
		percent = 100
	}

	c := freetype.NewContext()

	createDrawer := func(img draw.Image, color *image.Uniform, f *truetype.Font,
		size float64, dpi float64, hinting font.Hinting) *font.Drawer {
		return &font.Drawer{
			Dst: img,
			Src: color,
			Face: truetype.NewFace(f, &truetype.Options{
				Size:    size,
				DPI:     dpi,
				Hinting: hinting,
			})}
	}

	baseImage := cloneImage(baseImage)

	drawer := createDrawer(baseImage, fontColor, baseFont, size, dpi,
		font.HintingFull)

	drawString := func(str string, x fixed.Int26_6, y int) {
		drawer.Dot = fixed.Point26_6{
			X: x,
			//Y: fixed.I(y + int((c.PointToFixed(size) >> 6))),
			Y: fixed.I(y + int(c.PointToFixed(size)>>6)),
		}
		drawer.DrawString(str)
	}

	drawRightAlignedString := func(str string, x, y int) {
		width := drawer.MeasureString(str)
		drawString(str, fixed.I(x)-width, y)
	}

	// Skill name and current level
	drawString(
		fmt.Sprintf("%s: %d/%d", skill.Name, currentLevel, goalLevel),
		fixed.I(7), 1)

	for _, label := range staticLabels {
		if label.str == "Target lvl:" && goaltype == util.GoalXP {
			label.str = "Target XP:"
		}

		drawString(label.str, fixed.I(label.x), label.y)
	}

	x, y := 150, 15

	// current xp
	drawRightAlignedString(util.Format(currentXP), x, y)
	y += 15

	// goal
	drawRightAlignedString(util.Format(goal), x, y)
	y += 15

	// remainder
	drawRightAlignedString(util.Format(remainder), x, y)
	y += 15

	// bar
	drawBar(baseImage, percent)

	// bar percentage
	x = 71
	y = 61
	color := image.White
	if percent >= 50 {
		color = image.Black
	}

	drawer = createDrawer(baseImage, color, baseFont, 11, dpi,
		font.HintingFull)
	drawString(fmt.Sprintf("%d%%", percent), fixed.I(x), y)

	return util.Signature{username, baseImage}, nil
}
Esempio n. 2
0
func (m MultiGoalGenerator) CreateSignature(req util.ParsedSignatureRequest) (util.Signature, error) {
	username := req.GetProperty("username").(string)
	goals := req.GetProperty("goals").([]MultiGoal)

	stats, err := util.GetStats(username)
	if err != nil {
		var s util.Signature
		return s, errors.New(fmt.Sprintf("Failed to fetch stats for %s", username))
	}

	c := freetype.NewContext()

	createDrawer := func(img draw.Image, color *image.Uniform, f *truetype.Font,
		size float64, dpi float64, hinting font.Hinting) *font.Drawer {
		return &font.Drawer{
			Dst: img,
			Src: color,
			Face: truetype.NewFace(f, &truetype.Options{
				Size:    size,
				DPI:     dpi,
				Hinting: hinting,
			})}
	}

	baseImage := loadbaseImage(len(goals))

	drawer := createDrawer(baseImage, fontColor, baseFont, size, dpi,
		font.HintingFull)

	drawString := func(str string, x fixed.Int26_6, y int) {
		drawer.Dot = fixed.Point26_6{
			X: x,
			Y: fixed.I(y + int(c.PointToFixed(size)>>6)),
		}
		drawer.DrawString(str)
	}

	drawRightAlignedString := func(str string, x, y int) {
		width := drawer.MeasureString(str)
		drawString(str, fixed.I(x)-width, y)
	}

	nameX, goalX := paddingSides, baseWidth-paddingSides
	y := paddingSides

	for _, goal := range goals {
		stat := util.GetStatBySkill(stats, goal.skill)

		currentLevel := util.LevelFromXP(stat.Skill, stat.Xp)
		currentXP := stat.Xp
		var goalXP int
		var remainder int
		if goal.goaltype == util.GoalXP {
			goalXP = goal.goal
			remainder = goalXP - currentXP
		} else {
			goalXP = util.XPForLevel(stat.Skill, goal.goal)
			remainder = util.XPToLevel(stat.Skill, currentXP, goal.goal)
		}
		goalLevel := util.LevelFromXP(stat.Skill, goalXP)
		if remainder < 0 {
			remainder = 0
		}
		percent := int(float64(currentXP) / float64(goalXP) * 100.0)
		if percent > 100 {
			percent = 100
		}

		// Skill name and current level
		drawString(
			fmt.Sprintf("%s: %d/%d", goal.skill.Name, currentLevel, goalLevel),
			fixed.I(nameX), y)

		// Current and goal xp
		drawRightAlignedString(util.Format(currentXP)+"/"+util.Format(goalXP), goalX, y)

		// Bar
		drawBar(baseImage, percent, nameX, y+20, baseWidth-5, 1)

		y += baseHeight
	}

	// Watermark
	y -= 5
	drawer = createDrawer(baseImage, fontColor, baseFont, 11, dpi,
		font.HintingFull)
	drawRightAlignedString("sig.scapelog.com", goalX, y)

	return util.Signature{username, baseImage}, nil
}