Beispiel #1
0
func onCollectorMerge(e events.Event) interface{} {
	// e.g. /name=collector:merge/team_1=0/team_2=1$

	toMerge := team.Merger{}
	if err := tcp.Unmarshal(e.Args, &toMerge); err != nil {
		panic(err.Error())
	}

	teams.Merge(toMerge)

	return nil
}
Beispiel #2
0
func onCollectorBurst(e events.Event) interface{} {
	// e.g. /name=collector:burst/id=0/points=155/complete=1$

	type collector struct {
		//Name   	string `tcp:"name"`
		Id     		int 	`tcp:"id"`
		Points 		int 	`tcp:"points"`
		Complete 	int		`tcp:"complete"`
	}
	c := collector{}
	tcp.Unmarshal(e.Args, &c)

	// give points!
	if team, ok := teams.Roster[c.Id]; ok {
		pts := c.Points / len(team)

		for _, member := range team {
			
			userHarvestKey := fmt.Sprintf("uid:%v:harvests", member.User.Id)

			if (c.Complete > 0) {
				helper.SendBadge("firstComplete", member.User.Id)
				rounds := db.Redis.Incr(userHarvestKey).Val()
				if (rounds >= 3) {
					helper.SendBadge("bumperCrop", member.User.Id)
				}
			} else {
				db.Redis.Set(userHarvestKey, "0")
			}

			helper.SendPoints(pts, member.User.Id)
			userKey := fmt.Sprintf("uid:%v:points", member.User.Id)
			totalPts := db.Redis.IncrBy(userKey, int64(pts))

			toApp := packet.Out{
				Name:    "user:getPoints",
				Message: totalPts.Val(),
			}

			encoded, err := json.Marshal(toApp)
			if err != nil {
				panic(err)
			}

			member.Conn.Write(encoded)
		}

		teams.ReturnToQueue(c.Id)
	}

	return nil
}
Beispiel #3
0
func onCollectorHeartbeat(e events.Event) interface{} {
	// e.g. /name=collector:heartbeat/id=0/health=90/capacity=100/fill=43/color=#FFAA99$

	type collector struct {
		Id     		int	`tcp:"id"`
		Health 		int `tcp:"health"`
		Fill		int `tcp:"fill"`
		Capacity	int `tcp:"capacity"`
		Color		string `tcp:"color"`
	}
	c := collector{}
	tcp.Unmarshal(e.Args, &c)

	teamPrefix := fmt.Sprintf("team:%v:", c.Id)

	db.Redis.Set(teamPrefix + "health", strconv.Itoa(c.Health))
	db.Redis.Set(teamPrefix + "fill", strconv.Itoa(c.Fill))
	db.Redis.Set(teamPrefix + "capacity", strconv.Itoa(c.Capacity))
	db.Redis.Set(teamPrefix + "color", c.Color)

	return nil
}
Beispiel #4
0
func onUserShoot(e events.Event) interface{} {
	// e.g. /name=user:shoot/id=1$
	defer func() {
		if r := recover(); r != nil {
			fmt.Printf("[ERROR]\t%v\n", r)
		}
	}()
	
	u := struct {
		Id int `tcp:"id"`
	}{}

	if err := tcp.Unmarshal(e.Args, &u); err != nil {
		panic(err.Error())
	}

	userShotKey := fmt.Sprintf("uid:%v:shotsFired", u.Id)
	db.Redis.Incr(userShotKey)
	fmt.Printf("Shots fired!: %v\n", u.Id)

	return nil
}