Exemplo n.º 1
0
func UnsubscribeEmails(c *common.HTTPContext) (err error) {
	unsubTag, err := common.DecodeUnsubscribeTag(c.Secret(), c.Vars()["unsubscribe_tag"])
	if err != nil {
		return
	}
	u := &user.User{Id: unsubTag.U}
	if err = c.DB().Get(u); err != nil {
		return
	}
	switch unsubTag.T {
	case common.UnsubscribeMessageEmail:
		u.MessageEmailDisabled = true
	case common.UnsubscribePhaseEmail:
		u.MessageEmailDisabled = true
	}
	if err = c.DB().Set(u); err != nil {
		return
	}
	switch unsubTag.T {
	case common.UnsubscribeMessageEmail:
		fmt.Fprintf(c.Resp(), "%v has successfully been unsubscribed from message emails.", u.Email)
	case common.UnsubscribePhaseEmail:
		fmt.Fprintf(c.Resp(), "%v has successfully been unsubscribed from phase emails.", u.Email)
	}
	return
}
Exemplo n.º 2
0
func Openid(c *common.HTTPContext) (err error) {
	redirect, email, ok, err := gopenid.VerifyAuth(c.Req())
	if err != nil {
		return
	}
	if ok {
		email = strings.ToLower(email)
		c.Session().Values[common.SessionEmail] = email
		u := &User{Id: kol.Id(email)}
		err = c.DB().Get(u)
		if err == kol.NotFound {
			err = nil
			u.Email = email
			u.Ranking = 1
		}
		if err == nil {
			u.Language = common.GetLanguage(c.Req())
			u.DiplicityHost = c.Req().Host
			u.LastLoginAt = time.Now()
			err = c.DB().Set(u)
		}
	} else {
		delete(c.Session().Values, common.SessionEmail)
	}
	c.Close()
	c.Resp().Header().Set("Location", redirect.String())
	c.Resp().WriteHeader(302)
	fmt.Fprintln(c.Resp(), redirect.String())
	return
}
Exemplo n.º 3
0
func AdminCreateUser(c *common.HTTPContext) (err error) {
	user := &User{}
	if err = json.NewDecoder(c.Req().Body).Decode(user); err != nil {
		return
	}
	err = c.DB().Set(user)
	return
}
Exemplo n.º 4
0
func AdminReindexGames(c *common.HTTPContext) (err error) {
	games := Games{}
	if err = c.DB().Query().All(&games); err != nil {
		return
	}
	for _, game := range games {
		if err = c.DB().Index(game); err != nil {
			return
		}
		fmt.Fprintf(c.Resp(), "Reindexed %#v\n", game.Id.String())
	}
	return
}
Exemplo n.º 5
0
func AdminGetGame(c *common.HTTPContext) (err error) {
	gameId, err := base64.URLEncoding.DecodeString(c.Vars()["game_id"])
	if err != nil {
		return
	}
	g := &Game{Id: gameId}
	if err = c.DB().Get(g); err != nil {
		return
	}
	members, err := g.Members(c.DB())
	if err != nil {
		return
	}
	memberStates, err := members.ToStates(c.DB(), g, "", true)
	if err != nil {
		return
	}
	phases, err := g.Phases(c.DB())
	if err != nil {
		return
	}
	sort.Sort(phases)
	return c.RenderJSON(AdminGameState{
		Game:    g,
		Phases:  phases,
		Members: memberStates,
	})
}
Exemplo n.º 6
0
func AdminSetRank1(c *common.HTTPContext) (err error) {
	users := Users{}
	if err = c.DB().Query().All(&users); err != nil {
		return
	}
	for _, user := range users {
		user.Ranking = 1
		if err = c.DB().Set(&user); err != nil {
			return
		}
		fmt.Fprintf(c.Resp(), "Set rank of %#v to 1\n", user.Email)
	}
	return

}
Exemplo n.º 7
0
func AdminGetOptions(c *common.HTTPContext) (err error) {
	gameId, err := base64.URLEncoding.DecodeString(c.Vars()["game_id"])
	if err != nil {
		return
	}
	game := &Game{Id: gameId}
	if err = c.DB().Get(game); err != nil {
		return
	}
	_, last, err := game.Phase(c.DB(), 0)
	if err != nil {
		return
	}
	opts, err := last.Options(dip.Nation(c.Vars()["nation"]))
	if err != nil {
		return
	}
	return c.RenderJSON(opts)
}
Exemplo n.º 8
0
func AdminRecalcOptions(c *common.HTTPContext) (err error) {
	gameId, err := base64.URLEncoding.DecodeString(c.Vars()["game_id"])
	if err != nil {
		return
	}
	g := &Game{Id: gameId}
	if err = c.DB().Get(g); err != nil {
		return
	}
	_, last, err := g.Phase(c.DB(), 0)
	if err != nil {
		return
	}
	members, err := g.Members(c.DB())
	if err != nil {
		return
	}
	for index, _ := range members {
		opts := dip.Options{}
		if opts, err = last.Options(members[index].Nation); err != nil {
			return
		}
		members[index].Options = opts
		if len(opts) == 0 {
			members[index].Committed = true
			members[index].NoOrders = true
		} else {
			members[index].Committed = false
			members[index].NoOrders = false
		}
		if err = c.DB().Set(&members[index]); err != nil {
			return
		}
	}
	return
}
Exemplo n.º 9
0
func AdminRollback(c *common.HTTPContext) (err error) {
	gameId, err := base64.URLEncoding.DecodeString(c.Vars()["game_id"])
	if err != nil {
		return
	}
	epoch, err := epoch.Get(c.DB())
	if err != nil {
		return
	}
	g := &Game{Id: gameId}
	if err = c.DB().Get(g); err != nil {
		return
	}
	ordinal, err := strconv.Atoi(c.Vars()["until"])
	if err != nil {
		return
	}
	members, err := g.Members(c.DB())
	if err != nil {
		return
	}
	phases, err := g.Phases(c.DB())
	if err != nil {
		return
	}
	sort.Sort(phases)
	for index, _ := range phases {
		phase := &phases[index]
		if phase.Ordinal == ordinal {
			phase.Resolutions = map[dip.Province]string{}
			phase.Resolved = false
			phase.Deadline = epoch + (time.Minute * time.Duration(g.Deadlines[phase.Type]))
			for index, _ := range members {
				opts := dip.Options{}
				if opts, err = phase.Options(members[index].Nation); err != nil {
					return
				}
				members[index].Options = opts
				if len(opts) == 0 {
					members[index].Committed = true
					members[index].NoOrders = true
				} else {
					members[index].Committed = false
					members[index].NoOrders = false
				}
				if err = c.DB().Set(&members[index]); err != nil {
					return
				}
			}
			if err = c.DB().Set(phase); err != nil {
				return
			}
		} else if phase.Ordinal > ordinal {
			if err = c.DB().Del(phase); err != nil {
				return
			}
		}
	}
	return
}