Exemple #1
0
func DeleteMember(c common.WSContext) error {
	return c.Transact(func(c common.WSContext) error {
		decodedId, err := kol.DecodeId(c.Match()[1])
		if err != nil {
			return err
		}
		game := &Game{Id: decodedId}
		if err := c.DB().Get(game); err != nil {
			return fmt.Errorf("Game not found: %v", err)
		}
		if game.State != common.GameStateCreated {
			return fmt.Errorf("%+v already started", game)
		}
		member := Member{}
		if _, err := c.DB().Query().Where(kol.And{kol.Equals{"GameId", decodedId}, kol.Equals{"UserId", kol.Id(c.Principal())}}).First(&member); err != nil {
			return err
		}
		if err := c.DB().Del(&member); err != nil {
			return err
		}
		left, err := game.Members(c.DB())
		if err != nil {
			return err
		}
		if len(left) == 0 {
			if err := c.DB().Del(game); err != nil {
				return err
			}
		}
		return nil
	})
}
Exemple #2
0
func SeeMessage(c common.WSContext) (result interface{}, err error) {
	messageId, err := base64.URLEncoding.DecodeString(c.Data().GetString("MessageId"))
	if err != nil {
		err = fmt.Errorf("Error trying to decode %#v: %v", c.Data().GetString("MessageId"), err)
		return
	}
	err = c.Transact(func(c common.WSContext) (err error) {
		message := &Message{Id: messageId}
		if err = c.DB().Get(message); err != nil {
			return
		}
		game := &Game{Id: message.GameId}
		if err = c.DB().Get(game); err != nil {
			return
		}
		var member *Member
		if member, err = game.Member(c.DB(), c.Principal()); err != nil {
			return
		}
		if message.SeenBy == nil {
			message.SeenBy = map[string]bool{}
		}
		if !message.SeenBy[member.Id.String()] {
			message.SeenBy[member.Id.String()] = true
			if err = c.DB().Set(message); err != nil {
				return
			}
		}
		return
	})
	return
}
Exemple #3
0
func AddMember(c common.WSContext) error {
	var state GameState
	c.Data().Overwrite(&state)
	return c.Transact(func(c common.WSContext) error {
		game := Game{Id: state.Game.Id}
		if err := c.DB().Get(&game); err != nil {
			return fmt.Errorf("Game not found")
		}
		if game.State != common.GameStateCreated {
			return fmt.Errorf("%+v already started")
		}
		variant, found := common.VariantMap[game.Variant]
		if !found {
			return fmt.Errorf("Unknown variant %v", game.Variant)
		}
		if alreadyMember, err := game.Member(c.DB(), c.Principal()); err != nil {
			return err
		} else if alreadyMember != nil {
			return fmt.Errorf("%+v is already member of %v", alreadyMember, game.Id)
		}
		me := &user.User{Id: kol.Id(c.Principal())}
		if err := c.DB().Get(me); err != nil {
			return err
		}
		if game.Disallows(me) {
			return fmt.Errorf("Is not allowed to join this game due to game settings")
		}
		already, err := game.Members(c.DB())
		if err != nil {
			return err
		}
		if disallows, err := already.Disallows(c.DB(), me); err != nil {
			return err
		} else if disallows {
			return fmt.Errorf("Is not allowed to join this game due to blacklistings")
		}
		if len(already) < len(variant.Nations) {
			member := Member{
				GameId:           state.Game.Id,
				UserId:           kol.Id(c.Principal()),
				PreferredNations: state.Members[0].PreferredNations,
			}
			if err := c.DB().Set(&member); err != nil {
				return err
			}
			if len(already) == len(variant.Nations)-1 {
				if err := game.start(c.Diet()); err != nil {
					return err
				}
				c.Infof("Started %v", game.Id)
			}
		}
		return nil
	})
}
Exemple #4
0
func setPhaseCommitted(c common.WSContext, commit bool) (err error) {
	phaseId, err := base64.URLEncoding.DecodeString(c.Data().GetString("PhaseId"))
	if err != nil {
		return
	}
	return c.Transact(func(c common.WSContext) (err error) {
		phase := &Phase{Id: phaseId}
		if err = c.DB().Get(phase); err != nil {
			return
		}
		game, err := phase.Game(c.DB())
		if err != nil {
			return
		}
		members, err := game.Members(c.DB())
		if err != nil {
			return
		}
		member := members.Get(c.Principal())
		if member == nil {
			err = fmt.Errorf("Not member of game")
			return
		}
		if member.NoOrders {
			c.Infof("%+v has no orders to give", member)
			err = fmt.Errorf("No orders to give")
			return
		}
		member.Committed = commit
		member.NoWait = false
		if err = c.DB().Set(member); err != nil {
			return
		}
		if !phase.Resolved {
			count := 0
			for _, m := range members {
				if m.Committed || m.NoWait {
					count++
				}
			}
			if count == len(members) {
				if err = game.resolve(c.Diet(), phase); err != nil {
					return
				}
				c.Infof("Resolved %v", game.Id)
				return
			}
		}
		err = c.DB().Set(phase)
		return
	})
}
Exemple #5
0
func Create(c common.WSContext) error {
	var state GameState
	c.Data().Overwrite(&state)

	game := &Game{
		Variant:               state.Game.Variant,
		EndYear:               state.Game.EndYear,
		Private:               state.Game.Private,
		SecretEmail:           state.Game.SecretEmail,
		SecretNickname:        state.Game.SecretNickname,
		SecretNation:          state.Game.SecretNation,
		Deadlines:             state.Game.Deadlines,
		ChatFlags:             state.Game.ChatFlags,
		AllocationMethod:      state.Game.AllocationMethod,
		NonCommitConsequences: state.Game.NonCommitConsequences,
		NMRConsequences:       state.Game.NMRConsequences,
		Ranking:               state.Game.Ranking,
	}

	if _, found := common.VariantMap[game.Variant]; !found {
		return fmt.Errorf("Unknown variant for %+v", game)
	}

	if _, found := common.AllocationMethodMap[game.AllocationMethod]; !found {
		return fmt.Errorf("Unknown allocation method for %+v", game)
	}

	member := &Member{
		UserId:           kol.Id(c.Principal()),
		PreferredNations: state.Members[0].PreferredNations,
	}
	return c.Transact(func(c common.WSContext) error {
		if err := c.DB().Set(game); err != nil {
			return err
		}
		member.GameId = game.Id
		return c.DB().Set(member)
	})
}