コード例 #1
0
        public static void RewardPlayer(Player player, RewardData rewardData, GameRoom room)
        {
            if (player == null || rewardData == null || room == null)
            {
                return;
            }

            // TODO : 살짝 문제가 있긴 하다...
            // 1) DB에다가 저장 요청
            // 2) DB 저장 OK
            // 3) 메모리에 적용
            int?slot = player.Inven.GetEmptySlot();

            if (slot == null)
            {
                return;
            }

            ItemDb itemDb = new ItemDb()
            {
                TemplateId = rewardData.itemId,
                Count      = rewardData.count,
                Slot       = slot.Value,
                OwnerDbId  = player.PlayerDbId
            };

            // You
            Instance.Push(() =>
            {
                using (AppDbContext db = new AppDbContext())
                {
                    db.Items.Add(itemDb);
                    bool success = db.SaveChangesEx();
                    if (success)
                    {
                        // Me
                        room.Push(() =>
                        {
                            Item newItem = Item.MakeItem(itemDb);
                            player.Inven.Add(newItem);

                            // Client Noti
                            {
                                S_AddItem itemPacket = new S_AddItem();
                                ItemInfo itemInfo    = new ItemInfo();
                                itemInfo.MergeFrom(newItem.Info);
                                itemPacket.Items.Add(itemInfo);

                                player.Session.Send(itemPacket);
                            }
                        });
                    }
                }
            });
        }