Beispiel #1
0
        private static async Task <string> update(string payload, Client client, GameContext context)
        {
            UpdateCampaignPayload?message;

            try
            {
                message = JsonConvert.DeserializeObject <UpdateCampaignPayload>(payload, new StringEnumConverter());
            }
            catch
            {
                return("CampaignManager update 5: Invalid JSON.");
            }
            if (message == null)
            {
                return("CampaignManager update 6: Null message.");
            }
            Campaign campaignToUpdate;

            try
            {
                campaignToUpdate = await context.campaigns
                                   .Include(c => c.dungeonMaster)
                                   .SingleAsync(c => c.ID == message.ID);
            }
            catch
            {
                return("CampaignManager update 7: Campaign not found.");
            }
            if (client.user.ID == campaignToUpdate.dungeonMaster.ID)
            {
                campaignToUpdate.name             = message.name;
                campaignToUpdate.log              = message.log;
                campaignToUpdate.turnIndex        = message.turnIndex;
                campaignToUpdate.joinable         = message.joinable;
                campaignToUpdate.maxPlayers       = message.maxPlayers;
                campaignToUpdate.password         = message.password ?? campaignToUpdate.password;
                campaignToUpdate.modificationDate = DateTime.Now;
                context.campaigns.Update(campaignToUpdate);
                await context.SaveChangesAsync();
            }
            else
            {
                return("CampaignManager update 8: You are not the Dungeon Master for this campaign.");
            }
            // We also send a new gameState to the other players (we know it's everyone but the dungeon master)
            List <string> clientIdsToUpdate = new List <string>();

            clientIdsToUpdate.Add(campaignToUpdate.dungeonMaster.ID);
            var charactersInCampaign = await context.characters
                                       .Include(c => c.owner)
                                       .Where(c => c.campaign != null && c.campaign.ID == campaignToUpdate.ID)
                                       .Select(c => c.owner.ID)
                                       .ToListAsync();

            clientIdsToUpdate.Concat(charactersInCampaign);
            foreach (var clientID in clientIdsToUpdate)
            {
                await ClientManager.GetInstance().sendGameState(clientID);
            }
            return(await GameState.gameStateFor(client));
        }
Beispiel #2
0
        public static async Task <string> accept(string payload, Client client)
        {
            DiceRollPayload message;

            try
            {
                message = JsonConvert.DeserializeObject <DiceRollPayload>(payload);
            }
            catch
            {
                return("DiceRollManager accept 0: Ya done did goof'd");
            }
            using (var context = GameContext.getNew())
            {
                Campaign campaign;
                try
                {
                    campaign = await context.campaigns
                               .Include(c => c.dungeonMaster)
                               .Where(c => c.ID == message.campaignID)
                               .SingleAsync();
                }
                catch
                {
                    return("DiceRollManager accept 1: Campaign not found");
                }
                if (message.diceType < 4 || message.diceType > 20)
                {
                    return("DiceRollManager accept 2: Hvad fanden laver du din prut (wrong diceType)");
                }
                var rnd  = new Random();
                var roll = new DiceRoll
                           (
                    message.diceType,
                    rnd.Next(1, message.diceType + 1),
                    DateTime.Now,
                    campaign
                           );
                await context.diceRolls.AddAsync(roll);

                await context.SaveChangesAsync();

                var charactersInCampaign = await context.characters
                                           .Include(c => c.campaign)
                                           .Include(c => c.owner)
                                           .Where(c => c.campaign != null && c.campaign.ID == campaign.ID)
                                           .Select(c => c.owner.ID)
                                           .ToListAsync();

                string dicePayload = JsonConvert.SerializeObject(new
                {
                    diceType = roll.diceType,
                    roll     = roll.roll,
                    date     = roll.date,
                    campaign = roll.campaign.ID
                });
                charactersInCampaign.Add(campaign.dungeonMaster.ID);
                foreach (var c in charactersInCampaign)
                {
                    await ClientManager.GetInstance().sendPayload(dicePayload, c);
                }
                return(await GameState.gameStateFor(client));
            }
        }