Exemple #1
0
func (self *Message) Updated(d *kol.DB, old *Message) {
	g := Game{Id: self.GameId}
	if err := d.Get(&g); err != nil {
		panic(err)
	}
	d.EmitUpdate(&g)
}
Exemple #2
0
func (self Members) Disallows(d *kol.DB, asking *user.User) (result bool, err error) {
	var askerList map[string]bool
	if askerList, err = asking.Blacklistings(d); err != nil {
		return
	}
	for _, member := range self {
		if askerList[member.UserId.String()] {
			result = true
			return
		}
	}
	for _, member := range self {
		memberUser := &user.User{Id: member.UserId}
		if err = d.Get(memberUser); err != nil {
			return
		}
		var memberList map[string]bool
		if memberList, err = memberUser.Blacklistings(d); err != nil {
			return
		}
		if memberList[asking.Id.String()] {
			result = true
			return
		}
	}
	return
}
Exemple #3
0
func (self *Member) Created(d *kol.DB) {
	g := Game{Id: self.GameId}
	if err := d.Get(&g); err != nil {
		panic(err)
	}
	d.EmitUpdate(&g)
}
Exemple #4
0
func (self *Game) Messages(d *kol.DB) (result Messages, err error) {
	if err = d.Query().Where(kol.Equals{"GameId", self.Id}).All(&result); err != nil {
		return
	}
	sort.Sort(result)
	return
}
Exemple #5
0
func (self *Member) Deleted(d *kol.DB) {
	g := Game{Id: self.GameId}
	if err := d.Get(&g); err == nil {
		d.EmitUpdate(&g)
	} else if err != kol.NotFound {
		panic(err)
	}
}
Exemple #6
0
func (self *Game) Member(d *kol.DB, email string) (result *Member, err error) {
	var member Member
	var found bool
	if found, err = d.Query().Where(kol.And{kol.Equals{"GameId", self.Id}, kol.Equals{"UserId", kol.Id(email)}}).First(&member); found && err == nil {
		result = &member
	}
	return
}
Exemple #7
0
func Set(d *kol.DB, at time.Duration) (err error) {
	epoch := &Epoch{
		Id: kol.Id(epochKey),
		At: at,
	}
	err = d.Set(epoch)
	return
}
Exemple #8
0
func (self *Member) Updated(d *kol.DB, old *Member) {
	if old != self {
		g := Game{Id: self.GameId}
		if err := d.Get(&g); err != nil {
			panic(err)
		}
		d.EmitUpdate(&g)
	}
}
Exemple #9
0
func (self *Game) Updated(d *kol.DB, old *Game) {
	if old != self {
		members, err := self.Members(d)
		if err == nil {
			for _, member := range members {
				d.EmitUpdate(&member)
			}
		}
	}
}
Exemple #10
0
func Get(d *kol.DB) (result time.Duration, err error) {
	epoch := &Epoch{
		Id: kol.Id(epochKey),
	}
	if err = d.Get(epoch); err != nil {
		if err == kol.NotFound {
			err = nil
		} else {
			return
		}
	}
	result = epoch.At + time.Now().Sub(time.Unix(0, atomic.LoadInt64(&deltaPoint)))
	return
}
Exemple #11
0
func (self *Member) ReliabilityDelta(d *kol.DB, i int) (err error) {
	user := &user.User{Id: self.UserId}
	if err = d.Get(user); err != nil {
		return
	}
	if i > 0 {
		user.HeldDeadlines += i
	} else {
		user.MissedDeadlines -= i
	}
	if err = d.Set(user); err != nil {
		return
	}
	return
}
Exemple #12
0
func (self *Game) Users(d *kol.DB) (result user.Users, err error) {
	members, err := self.Members(d)
	if err != nil {
		return
	}
	result = make(user.Users, len(members))
	for index, member := range members {
		user := user.User{Id: member.UserId}
		if err = d.Get(&user); err != nil {
			return
		}
		result[index] = user
	}
	return
}
Exemple #13
0
func (self *Member) ToState(d *kol.DB, g *Game, email string, isMember bool, isAdmin bool) (result *MemberState, err error) {
	result = &MemberState{
		Member: &Member{
			Id: self.Id,
		},
		User: &user.User{},
	}
	secretNation := false
	secretEmail := false
	secretNickname := false
	var flag common.SecretFlag
	switch g.State {
	case common.GameStateCreated:
		flag = common.SecretBeforeGame
	case common.GameStateStarted:
		flag = common.SecretDuringGame
	case common.GameStateEnded:
		flag = common.SecretAfterGame
	default:
		panic(fmt.Errorf("Unknown game state for %+v", g))
	}
	secretNation, secretEmail, secretNickname = g.SecretNation&flag == flag, g.SecretEmail&flag == flag, g.SecretNickname&flag == flag
	isMe := string(self.UserId) == email
	if isAdmin || isMe || !secretNation {
		result.Member.Nation = self.Nation
	}
	if isAdmin || isMe || !secretEmail || !secretNickname {
		foundUser := &user.User{Id: self.UserId}
		if err = d.Get(foundUser); err != nil {
			return
		}
		if isAdmin || (isMember && (isMe || !secretEmail)) {
			result.User.Email = foundUser.Email
		}
		if isAdmin || (isMe || !secretNickname) {
			result.User.Nickname = foundUser.Nickname
		}
		if isAdmin || isMe {
			result.Member.Committed = self.Committed
			result.Member.Options = self.Options
			result.Member.NoOrders = self.NoOrders
		}
	}
	return
}
Exemple #14
0
func (self *Game) allocate(d *kol.DB, phase *Phase) (err error) {
	members, err := self.Members(d)
	if err != nil {
		return
	}
	switch self.AllocationMethod {
	case common.RandomString:
		for memberIndex, nationIndex := range rand.Perm(len(members)) {
			members[memberIndex].Nation = common.VariantMap[self.Variant].Nations[nationIndex]
		}
	case common.PreferencesString:
		prefs := make([][]dip.Nation, len(members))
		for index, member := range members {
			prefs[index] = member.PreferredNations
		}
		for index, nation := range optimizePreferences(prefs) {
			members[index].Nation = nation
		}
	default:
		return fmt.Errorf("Unknown allocation method %v", self.AllocationMethod)
	}
	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 = d.Set(&members[index]); err != nil {
			return
		}
	}
	return
}
Exemple #15
0
func (self *Game) Phases(d *kol.DB) (result Phases, err error) {
	err = d.Query().Where(kol.Equals{"GameId", self.Id}).All(&result)
	return
}
Exemple #16
0
func (self *Phase) Game(d *kol.DB) (result *Game, err error) {
	result = &Game{Id: self.GameId}
	err = d.Get(result)
	return
}