本文整理汇总了Golang中github.com/zond/diplicity/common.WSContext.Transact方法的典型用法代码示例。如果您正苦于以下问题:Golang WSContext.Transact方法的具体用法?Golang WSContext.Transact怎么用?Golang WSContext.Transact使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/zond/diplicity/common.WSContext
的用法示例。
在下文中一共展示了WSContext.Transact方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: DeleteMember
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
})
}
示例2: SeeMessage
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
}
示例3: AddMember
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
})
}
示例4: setPhaseCommitted
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
})
}
示例5: Create
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)
})
}