本文整理汇总了Golang中github.com/zond/diplicity/common.WSContext类的典型用法代码示例。如果您正苦于以下问题:Golang WSContext类的具体用法?Golang WSContext怎么用?Golang WSContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WSContext类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: CreateMessage
func CreateMessage(c common.WSContext) (err error) {
// load the message provided by the client
message := &Message{}
c.Data().Overwrite(message)
if message.RecipientIds == nil {
message.RecipientIds = map[string]bool{}
}
message.Body = strings.TrimSpace(message.Body)
if message.Body == "" {
return
}
// and the game
game := &Game{Id: message.GameId}
if err := c.DB().Get(game); err != nil {
return err
}
// and the member
sender, err := game.Member(c.DB(), c.Principal())
if err != nil {
return
}
return message.Send(c.Diet(), game, sender)
}
开发者ID:JorenC,项目名称:diplicity,代码行数:26,代码来源:controller.go
示例2: SetOrder
func SetOrder(c common.WSContext) (result interface{}, err error) {
var base64DecodedId []byte
if base64DecodedId, err = base64.URLEncoding.DecodeString(c.Data().GetString("GameId")); err != nil {
return
}
err = c.DB().Transact(func(d *kol.DB) (err error) {
game := Game{Id: base64DecodedId}
if err = d.Get(&game); err != nil {
return
}
var member *Member
if member, err = game.Member(d, c.Principal()); err != nil {
return
}
var phase *Phase
if _, phase, err = game.Phase(d, 0); err != nil {
return
}
if phase == nil {
err = fmt.Errorf("No phase for %+v found", game)
return
}
nationOrders, found := phase.Orders[member.Nation]
if !found {
nationOrders = map[dip.Province][]string{}
phase.Orders[member.Nation] = nationOrders
}
order := c.Data().GetStringSlice("Order")
if len(order) == 1 {
delete(nationOrders, dip.Province(order[0]))
} else {
var parsedOrder dip.Order
parsedOrder, err = orders.Parse(order)
if err != nil {
return
}
var state *state.State
state, err = phase.State()
if err != nil {
return
}
if err = parsedOrder.Validate(state); err != nil {
return
}
nationOrders[dip.Province(order[0])] = order[1:]
}
if err = d.Set(phase); err != nil {
return
}
return
})
return
}
开发者ID:arlm,项目名称:diplicity,代码行数:53,代码来源:rpc.go
示例3: 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)
})
}
开发者ID:JorenC,项目名称:diplicity,代码行数:39,代码来源:controller.go
示例4: SubscribeMine
func SubscribeMine(c common.WSContext) error {
if c.Principal() == "" {
return websocket.JSON.Send(c.Conn(), gosubs.Message{
Type: gosubs.FetchType,
Object: &gosubs.Object{
URI: c.Match()[0],
},
})
}
s := c.Pack().New(c.Match()[0])
s.Query = s.DB().Query().Where(kol.Equals{"UserId", kol.Id(c.Principal())})
s.Call = func(i interface{}, op string) (err error) {
members := i.([]*Member)
var ep time.Duration
ep, err = epoch.Get(c.DB())
if err != nil {
return
}
states := GameStates{}
for _, member := range members {
if op == gosubs.DeleteType {
states = append(states, GameState{
Game: &Game{Id: member.GameId},
Members: []MemberState{MemberState{Member: member}},
})
} else {
game := &Game{Id: member.GameId}
if err = s.DB().Get(game); err != nil {
return
}
var gameMembers Members
if gameMembers, err = game.Members(c.DB()); err != nil {
return
}
var state GameState
if state, err = game.ToState(c.DB(), gameMembers, member); err != nil {
return
}
states = append(states, state)
}
}
if op == gosubs.FetchType || len(states) > 0 {
states = states.SortAndLimit(func(a, b GameState) bool {
urgencyA := time.Hour * 24 * 365
urgencyB := time.Hour * 24 * 365
switch a.State {
case common.GameStateStarted:
_, phase, err := a.Game.Phase(c.DB(), 0)
if err == nil {
urgencyA = phase.Deadline - ep
}
case common.GameStateCreated:
urgencyA -= 1
}
switch b.State {
case common.GameStateStarted:
_, phase, err := b.Game.Phase(c.DB(), 0)
if err == nil {
urgencyB = phase.Deadline - ep
}
case common.GameStateCreated:
urgencyB -= 1
}
if urgencyA != urgencyB {
return urgencyA < urgencyB
}
return a.CreatedAt.Before(b.CreatedAt)
}, 1024*16)
return s.Send(states, op)
}
return nil
}
return s.Subscribe(&Member{})
}
开发者ID:arlm,项目名称:diplicity,代码行数:74,代码来源:subscriptions.go
示例5: subscribeOthers
func subscribeOthers(c common.WSContext, filter kol.QFilter, preLimiter func(source Games) (result Games), postLimiter func(source GameStates) (result GameStates)) error {
if c.Principal() == "" {
return websocket.JSON.Send(c.Conn(), gosubs.Message{
Type: gosubs.FetchType,
Object: &gosubs.Object{
URI: c.Match()[0],
},
})
}
s := c.Pack().New(c.Match()[0])
s.Query = s.DB().Query().Where(filter)
s.Call = func(i interface{}, op string) error {
games := i.([]*Game)
if preLimiter != nil {
games = ([]*Game)(preLimiter(Games(games)))
}
states := GameStates{}
isMember := false
me := &user.User{Id: kol.Id(c.Principal())}
if err := c.DB().Get(me); err != nil {
return err
}
for _, game := range games {
if !game.Disallows(me) {
members, err := game.Members(c.DB())
if err != nil {
return err
}
if disallows, err := members.Disallows(c.DB(), me); err != nil {
return err
} else if !disallows {
isMember = members.Contains(c.Principal())
if !isMember {
state, err := game.ToState(c.DB(), members, nil)
if err != nil {
return err
}
states = append(states, state)
}
}
}
}
if op == gosubs.FetchType || len(states) > 0 {
if postLimiter != nil {
states = postLimiter(states)
}
return s.Send(states, op)
}
return nil
}
return s.Subscribe(&Game{})
}
开发者ID:arlm,项目名称:diplicity,代码行数:52,代码来源:subscriptions.go
示例6: SubscribeMessages
func SubscribeMessages(c common.WSContext) (err error) {
base64DecodedId, err := base64.URLEncoding.DecodeString(c.Match()[1])
if err != nil {
return err
}
game := &Game{Id: base64DecodedId}
if err = c.DB().Get(game); err != nil {
return
}
member, err := game.Member(c.DB(), c.Principal())
if err != nil && err != kol.NotFound {
return
}
memberId := ""
if member != nil {
memberId = member.Id.String()
}
s := c.Pack().New(c.Match()[0])
s.Query = s.DB().Query().Where(kol.Equals{"GameId", base64DecodedId})
s.Call = func(i interface{}, op string) (err error) {
messages := i.([]*Message)
result := Messages{}
for _, message := range messages {
if message.Public {
game := &Game{Id: message.GameId}
if err = c.DB().Get(game); err != nil {
return
}
var members Members
if members, err = game.Members(c.DB()); err != nil {
return
}
message.RecipientIds = map[string]bool{}
for _, memb := range members {
message.RecipientIds[memb.Id.String()] = true
}
}
if message.Public || message.RecipientIds[memberId] {
result = append(result, *message)
}
}
if op == gosubs.FetchType || len(result) > 0 {
sort.Sort(result)
return s.Send(result, op)
}
return
}
return s.Subscribe(&Message{})
}
开发者ID:arlm,项目名称:diplicity,代码行数:49,代码来源:subscriptions.go
示例7: SubscribeGamePhase
func SubscribeGamePhase(c common.WSContext) error {
base64DecodedId, err := base64.URLEncoding.DecodeString(c.Match()[1])
if err != nil {
return err
}
game := &Game{Id: base64DecodedId}
if err = c.DB().Get(game); err != nil {
return err
}
phaseOrdinal, err := strconv.Atoi(c.Match()[2])
if err != nil {
return err
}
members, err := game.Members(c.DB())
if err != nil {
return err
}
member := members.Get(c.Principal())
isMember := member != nil
if !game.Private || isMember {
state, err := game.ToStateWithPhaseOrdinal(c.DB(), members, member, phaseOrdinal)
if err != nil {
return err
}
return websocket.JSON.Send(c.Conn(), gosubs.Message{
Type: gosubs.FetchType,
Object: &gosubs.Object{
URI: c.Match()[0],
Data: state,
},
})
}
return nil
}
开发者ID:arlm,项目名称:diplicity,代码行数:34,代码来源:subscriptions.go
示例8: SubscribeGame
func SubscribeGame(c common.WSContext) error {
base64DecodedId, err := base64.URLEncoding.DecodeString(c.Match()[1])
if err != nil {
return err
}
s := c.Pack().New(c.Match()[0])
s.Call = func(i interface{}, op string) error {
game := i.(*Game)
members, err := game.Members(c.DB())
if err != nil {
return err
}
member := members.Get(c.Principal())
isMember := member != nil
if !game.Private || isMember {
state, err := game.ToState(c.DB(), members, member)
if err != nil {
return err
}
return s.Send(state, op)
} else if op == gosubs.FetchType {
return s.Send(GameState{}, op)
}
return nil
}
return s.Subscribe(&Game{Id: base64DecodedId})
}
开发者ID:arlm,项目名称:diplicity,代码行数:27,代码来源:subscriptions.go
示例9: 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
})
}
开发者ID:JorenC,项目名称:diplicity,代码行数:55,代码来源:controller.go
示例10: 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
})
}
开发者ID:JorenC,项目名称:diplicity,代码行数:32,代码来源:controller.go
示例11: SubscribeMessages
func SubscribeMessages(c common.WSContext) (err error) {
base64DecodedId, err := base64.URLEncoding.DecodeString(c.Match()[1])
if err != nil {
return err
}
game := &Game{Id: base64DecodedId}
if err = c.DB().Get(game); err != nil {
return
}
variant, found := common.VariantMap[game.Variant]
if !found {
err = fmt.Errorf("Unknown variant for %+v", game)
return
}
member, err := game.Member(c.DB(), c.Principal())
if err != nil && err != kol.NotFound {
return
}
memberId := ""
if member != nil {
memberId = member.Id.String()
}
s := c.Pack().New(c.Match()[0])
s.Query = s.DB().Query().Where(kol.Equals{"GameId", base64DecodedId})
s.Call = func(i interface{}, op string) (err error) {
messages := i.([]*Message)
result := Messages{}
for _, message := range messages {
if len(message.RecipientIds) == len(variant.Nations) || message.RecipientIds[memberId] {
result = append(result, *message)
}
}
if op == gosubs.FetchType || len(result) > 0 {
sort.Sort(result)
return s.Send(result, op)
}
return
}
return s.Subscribe(&Message{})
}
开发者ID:JorenC,项目名称:diplicity,代码行数:40,代码来源:subscriptions.go
示例12: 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
})
}
开发者ID:arlm,项目名称:diplicity,代码行数:52,代码来源:rpc.go
示例13: 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
}
开发者ID:arlm,项目名称:diplicity,代码行数:32,代码来源:rpc.go
注:本文中的github.com/zond/diplicity/common.WSContext类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论