Ejemplo n.º 1
0
func (t *Manager) Merge(teams Merger) {

	team1 := fmt.Sprintf("team:%v:users", teams.TeamId1)
	team2 := fmt.Sprintf("team:%v:users", teams.TeamId2)

	// delete members
	defer db.Redis.Del(team2)
	defer delete(t.Roster, teams.TeamId2)

	db.Redis.SUnionStore(team1, team1, team2)

	for _, idStr := range db.Redis.SMembers(team2).Val() {
		userId, _ := strconv.Atoi(idStr)
		// update user id
		teamKey := fmt.Sprintf("uid:%v:team", userId)
		db.Redis.Set(teamKey, strconv.Itoa(teams.TeamId1))
		// tell xna new team id
		t.memberChangeTeam(userId, teams.TeamId1, teams.TeamId2)
	}

	// move members to team 1
	t.Roster[teams.TeamId1] = append(t.Roster[teams.TeamId1], t.Roster[teams.TeamId2]...)

	// everybody, celebrate merge!
	for _, idStr := range db.Redis.SMembers(team1).Val() {
		userId, _ := strconv.Atoi(idStr)
		helper.SendBadge("firstMerge", userId)

		if len(t.Roster[teams.TeamId1]) >= 8 {
			helper.SendBadge("theOcho", userId)
		}
	}
}
Ejemplo n.º 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
}
Ejemplo n.º 3
0
func (t *Manager) addMember(m Member) (teamId int, err error) {

	if len(t.Roster) < t.MaxTeams() {
		teamId = t.createNewTeam()
		t.Roster[teamId] = []Member{m}
	} else {
		smallest := t.getSmallestTeam()
		// add users to existing team
		t.Roster[smallest] = append(t.Roster[smallest], m)
		teamId = smallest
	}

	if len(t.Roster[teamId]) >= 8 {
		for _, m := range t.Roster[teamId] {
			helper.SendBadge("theOcho", m.User.Id)
		}
	}

	m.User.TeamId = teamId

	// add user to team list in DB
	key := fmt.Sprintf("team:%v:users", teamId)
	db.Redis.SAdd(key, strconv.Itoa(m.User.Id))

	key = fmt.Sprintf("uid:%v:team", m.User.Id)
	db.Redis.Set(key, strconv.Itoa(teamId))

	return
}
Ejemplo n.º 4
0
func (t Manager) Analytics() {
	for _, team := range t.Roster {
		for _, member := range team {
			userShotKey := fmt.Sprintf("uid:%v:shotsFired", member.User.Id)
			shotsFired, _ := strconv.Atoi(db.Redis.Get(userShotKey).Val())
			if shotsFired > 2 {
				helper.SendBadge("triggerHappy", member.User.Id)
			}
			db.Redis.Del(userShotKey)
		}
	}
}
Ejemplo n.º 5
0
func onUserJoin(e events.Event) interface{} {
	defer func() {
		if r := recover(); r != nil {
			fmt.Printf("[ERROR]\t%v\n", r)
		}
	}()

	// get incoming data in format of user.Id
	u := user.User{}
	if err := json.Unmarshal(e.Args, &u); err != nil {
		fmt.Printf("[ERROR]\tCould not unmarshal new user. " + err.Error() + "\n")
	}

	u.Name = client.Sanitizer.ReplaceAllString(u.Name, "")

	if err, err2 := u.Save(); err != nil || err2 != nil {
		fmt.Printf("[ERROR]\tCould not save user. " + err.Error() + " " + err2.Error() + "\n")
	}

	// assign to team
	member := team.Member{User: u, Conn: e.Sender}
	teams.Queue <- member
	// get team id - blocking
	assignedTeamId := <-teams.LastId
	member.User.TeamId = assignedTeamId

	// forward to xna
	msg := struct {
		Name     string `tcp:"name"`
		Id       int    `tcp:"id"`
		Username string `tcp:"username"`
		TeamId   int    `tcp:"teamId"`
		Display  int    `tcp:"display"`
	}{"user:new", u.Id, strings.ToUpper(u.Name), assignedTeamId, u.Display}
	network.TcpClients.Broadcast <- msg

	helper.SendBadge("join", u.Id)

	// reply to sencha with user data
	return packet.Out{ "user:info", member.User }
}