Esempio n. 1
0
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)
}
Esempio n. 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
}
Esempio n. 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
	})
}
Esempio n. 4
0
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
}
Esempio n. 5
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
	})
}
Esempio n. 6
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)
	})
}