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) }
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 }) }
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 }) }