-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameServices.cs
More file actions
691 lines (651 loc) · 31.1 KB
/
GameServices.cs
File metadata and controls
691 lines (651 loc) · 31.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
namespace TextGame
{
public class GameSession
{
public List<Room> Rooms { get; set; } = new List<Room>()
{
new StartRoom(),
};
public Room? CurrentRoom { get; set; }
public Weapon Weapon { get; set; } = Fists.DefaultFists;
public Helm? Helm { get; set; }
public Chestplate? Chestplate { get; set; }
public const int DefaultMaxHealth = 100;
public int MaxHealth { get; set; } = DefaultMaxHealth;
public int CurrentHealth { get; set; } = DefaultMaxHealth;
public int Coins { get; set; }
public int Keys { get; set; }
public List<Item> Inventory { get; set; } = new List<Item>();
public bool IsGameStarted { get; set; }
public bool IsInBattle { get; set; } = false;
public Chest? CurrentMimicChest { get; set; } = null;
public void RemoveWeapon()
{
Weapon = Fists.DefaultFists;
}
public void RemoveHelm()
{
Helm = null;
}
public void RemoveChestplate()
{
Chestplate = null;
}
}
public class CombatRepository : ICombatRepository
{
private GameSession Session;
private IGetEnemyByIdRepository GetEnemyByIdRepository;
private IGameInfoRepository GameInfoRepository;
public CombatRepository(
GameSession gameSession,
IGetEnemyByIdRepository getEnemyByIdRepository,
IGameInfoRepository gameInfoRepository
)
{
Session = gameSession;
GetEnemyByIdRepository = getEnemyByIdRepository;
GameInfoRepository = gameInfoRepository;
}
public BattleLog DealDamage()
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
int playerHealthBeforeAttack = Session.CurrentHealth;
Enemy enemy = GetEnemyByIdRepository.GetEnemyById();
int damage = Session.Weapon.Attack(Session);
int enemyHealthBeforeAttack = enemy.Health;
int enemyHealthAfterAttack = enemy.GetDamage(damage, Session.CurrentRoom!);
int playerHealthAfterAttack = playerHealthBeforeAttack - Session.CurrentHealth;
BattleLog battleLog = new BattleLog(enemy.Name!, damage, enemyHealthBeforeAttack, enemyHealthAfterAttack, "ИГРОК", playerHealthAfterAttack, playerHealthBeforeAttack, Session.CurrentHealth);
if (enemyHealthAfterAttack <= 0)
{
Session.CurrentRoom!.Enemies.Remove(enemy);
if (Session.CurrentMimicChest is not null)
{
Session.CurrentMimicChest.KillMimic();
Session.CurrentRoom.Items.Add(Session.CurrentMimicChest);
Session.CurrentMimicChest = null;
}
if (Session.CurrentHealth <= 0) throw new DefeatException("Вы погибли от своей же атаки. Как отчаянно.", GameInfoRepository.GetGameInfo());//дубль
if (!Session.CurrentRoom.Enemies.Any()) Session.IsInBattle = false;
throw new BattleWinException($"{enemy.Name!} повержен.", battleLog);
}
if (Session.CurrentHealth <= 0) throw new DefeatException("Вы погибли от своей же атаки. Как отчаянно.", GameInfoRepository.GetGameInfo()); //дубль
return battleLog;
}
public BattleLog GetDamage()
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
Enemy enemy = GetEnemyByIdRepository.GetEnemyById();
int enemyHealthBeforeAttack = enemy.Health;
int damage = enemy.Attack();
int helmBlock = Session.Helm != null ? Session.Helm.Block(Session) : 0;
int chestplateBlock = Session.Chestplate != null ? Session.Chestplate.Block(Session) : 0;
int damageAfterBlock = damage - helmBlock - chestplateBlock;
int playerHealthBeforeAttack = Session.CurrentHealth;
if (damageAfterBlock > 0) Session.CurrentHealth -= damageAfterBlock;
if (Session.CurrentHealth <= 0) throw new DefeatException($"Вы были повержены {enemy.Name}ОМ.", GameInfoRepository.GetGameInfo());
int enemyHealthAfterAttack = enemyHealthBeforeAttack - enemy.Health;
return new BattleLog("ИГРОК", damage, playerHealthBeforeAttack, Session.CurrentHealth, enemy.Name!, enemyHealthAfterAttack, enemyHealthBeforeAttack, enemy.Health);
}
}
public class GetEnemyByIdRepository : IGetEnemyByIdRepository
{
private GameSession Session;
private IGetRoomByIdRepository GetRoomByIdRepository;
public GetEnemyByIdRepository(
IGetRoomByIdRepository getRoomByIdRepository,
GameSession session)
{
GetRoomByIdRepository = getRoomByIdRepository;
Session = session;
}
//public List<Enemy> GetEnemies()
//{
// if (!Session.IsGameStarted) throw new UnstartedGameException();
// Room room = Session.CurrentRoom!;
// return room.Enemies;
//}
public Enemy GetEnemyById()
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
Room room = Session.CurrentRoom!;
//Enemy? enemy = room.Enemies.FirstOrDefault(e => e.Id == enemyId);
Enemy? enemy = room.Enemies.FirstOrDefault();
if (enemy == null) throw new NullEnemyIdException();
return enemy;
}
}
public class GetCurrentRoomRepository : IGetCurrentRoomRepository
{
private GameSession Session { get; set; }
public GetCurrentRoomRepository(GameSession session)
{
Session = session;
}
public Room GetCurrentRoom()
{
if (!Session.IsGameStarted && Session.Rooms.Count <= 1) throw new UnstartedGameException();
return Session.CurrentRoom!;
}
}
public class InventoryRepository : IInventoryRepository
{
private GameSession Session { get; set; }
public InventoryRepository(GameSession session)
{
Session = session;
}
public Item GetInventoryItem(int itemId)
{
Item? item = Session.Inventory.FirstOrDefault(i => i.Id == itemId);
if (item == null) throw new NullItemIdException();
return item;
}
public List<Equipment> GetEquipment()
{
List<Equipment> equipmentList = new List<Equipment>() { Session.Weapon };
if (Session.Helm != null) equipmentList.Add(Session.Helm);
if (Session.Chestplate != null) equipmentList.Add(Session.Chestplate);
return equipmentList;
}
public List<Equipment> EquipInventoryItem(int itemId)
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
Item item = GetInventoryItem(itemId);
if (item is not Equipment equipment) throw new InvalidIdException("NOT_EQUIPMENT", "Это не снаряжение.");
switch (equipment)
{
case Weapon weapon:
if (Session.Weapon != Fists.DefaultFists) Session.Inventory.Add(Session.Weapon);
Session.Weapon = weapon;
Session.Inventory.Remove(weapon);
break;
case Armor armor:
switch (armor)
{
case Helm helm:
if (Session.Helm != null) Session.Inventory.Add(Session.Helm);
Session.Helm = helm;
Session.Inventory.Remove(helm);
break;
case Chestplate chestplate:
if (Session.Chestplate != null) Session.Inventory.Add(Session.Chestplate);
Session.Chestplate = chestplate;
Session.Inventory.Remove(chestplate);
break;
}
break;
}
List<Equipment> equipmentList = new List<Equipment>() { Session.Weapon };
if (Session.Helm != null) equipmentList.Add(Session.Helm);
if (Session.Chestplate != null) equipmentList.Add(Session.Chestplate);
return equipmentList;
}
public List<Equipment> UnequipWeapon()
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
if (Session.Weapon == Fists.DefaultFists) throw new EmptyException();
Session.Inventory.Add(Session.Weapon);
Session.Weapon = Fists.DefaultFists;
List<Equipment> equipmentList = new List<Equipment>() { Session.Weapon };
if (Session.Helm != null) equipmentList.Add(Session.Helm);
if (Session.Chestplate != null) equipmentList.Add(Session.Chestplate);
return equipmentList;
}
public List<Equipment> UnequipHelm()
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
if (Session.Helm == null) throw new EmptyException();
Session.Inventory.Add(Session.Helm!);
Session.Helm = null;
List<Equipment> equipmentList = new List<Equipment>() { Session.Weapon };
if (Session.Helm != null) equipmentList.Add(Session.Helm);
if (Session.Chestplate != null) equipmentList.Add(Session.Chestplate);
return equipmentList;
}
public List<Equipment> UnequipChestplate()
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
if (Session.Chestplate == null) throw new EmptyException();
Session.Inventory.Add(Session.Chestplate!);
Session.Chestplate = null;
List<Equipment> equipmentList = new List<Equipment>() { Session.Weapon };
if (Session.Helm != null) equipmentList.Add(Session.Helm);
if (Session.Chestplate != null) equipmentList.Add(Session.Chestplate);
return equipmentList;
}
public void SellInventoryItem(int itemId)
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
if (Session.IsInBattle) throw new InBattleException();
if (Session.CurrentRoom is not Shop) throw new NotShopException();
Item item = GetInventoryItem(itemId);
if (item.Cost == null) throw new UnsellableItemException();
Session.Inventory.Remove(item);
Session.Coins += (int)item.Cost;
}
}
public class ChestRepository : IChestRepository
{
private GameSession Session;
private IGameInfoRepository GameInfoRepository;
private IGetRoomByIdRepository GetRoomByIdRepository;
private IGetItemByIdRepository GetItemByIdRepository;
private ICombatRepository CombatRepository;
private IEnemyFactory EnemyFactory;
public ChestRepository(
GameSession session,
IGameInfoRepository gameInfoRepository,
IGetRoomByIdRepository getRoomByIdRepository,
IGetItemByIdRepository getItemByIdRepository,
IEnemyFactory enemyFactory,
ICombatRepository combatRepository
)
{
Session = session;
GameInfoRepository = gameInfoRepository;
GetRoomByIdRepository = getRoomByIdRepository;
GetItemByIdRepository = getItemByIdRepository;
EnemyFactory = enemyFactory;
CombatRepository = combatRepository;
}
public ChestStateDTO ReturnChestDTO(Chest chest)
{
return new ChestStateDTO(chest.Name!, chest.Description!, chest.IsLocked, chest.IsClosed);
}
public ChestStateDTO ReturnChestDTO(int chestId)
{
Chest chest = GetChestById(chestId);
return new ChestStateDTO(chest.Name!, chest.Description!, chest.IsLocked, chest.IsClosed);
}
public Chest GetChestById(int chestId)
{
//Room room = GetRoomByIdRepository.GetRoomById(roomId);
Item item = GetItemByIdRepository.GetItemById(chestId, Session.CurrentRoom!.Items);
if (item is not Chest) throw new InvalidIdException("NOT_CHEST", "Это не сундук.");
return (Chest)item;
}
public BattleLog HitChest(int chestId)
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
if (Session.IsInBattle) throw new InBattleException();
Chest chest = GetChestById(chestId);
//Room room = GetRoomByIdRepository.GetRoomById(roomId);
BattleLog battleLog;
if (chest.Mimic is not null)
{
Session.CurrentMimicChest = chest;
Session.CurrentRoom!.Items.Remove(chest);
Session.CurrentRoom!.Enemies.Add(chest.Mimic);
Session.IsInBattle = true;
battleLog = CombatRepository.DealDamage();
}
else
{
int playerHealthBeforeAttack = Session.CurrentHealth;
int damage = Session.Weapon.Attack(Session);
int playerHealthAfterAttack = playerHealthBeforeAttack - Session.CurrentHealth;
battleLog = new BattleLog("СУНДУК", damage, null, null, "ИГРОК", playerHealthAfterAttack, playerHealthBeforeAttack, Session.CurrentHealth);
}
return battleLog;
}
public void OpenChest(int chestId)
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
if (Session.IsInBattle) throw new InBattleException();
Chest chest = GetChestById(chestId);
if (chest.IsLocked) throw new LockedException();
if (chest.Mimic is not null)
{
Session.IsGameStarted = false;
throw new DefeatException("НА ВАС НАПАЛ МИМИК! ВЫ БЫЛИ ПРОГЛОЧЕНЫ И ПЕРЕВАРЕНЫ!", GameInfoRepository.GetGameInfo());
}
chest.Open();
}
public void UnlockChest(int chestId)
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
if (Session.IsInBattle) throw new InBattleException();
Chest chest = GetChestById(chestId);
if (Session.Keys > 0) Session.Keys--;
else throw new NoKeyException();
chest.Unlock();
}
public List<Item> SearchChest(int chestId)
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
if (Session.IsInBattle) throw new InBattleException();
Chest chest = GetChestById(chestId);
if (chest.IsClosed) throw new ClosedException();
return chest.Search();
}
public void TakeItemFromChest(int chestId, int itemId)
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
if (Session.IsInBattle) throw new InBattleException();
Chest chest = GetChestById(chestId);
if (chest.IsLocked) throw new LockedException();
if (chest.IsClosed) throw new ClosedException();
Item item = GetItemByIdRepository.GetItemById(itemId, chest.Items);
if (item is BagOfCoins bagOfCoins) Session.Coins += (int)bagOfCoins.Cost!;
else if (item is Key) Session.Keys++;
else Session.Inventory.Add(item);
chest.Items.Remove(item);
}
public void TakeAllItemsFromChest(int chestId)
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
if (Session.IsInBattle) throw new InBattleException();
Chest chest = GetChestById(chestId);
if (chest.IsLocked) throw new LockedException();
if (chest.IsClosed) throw new ClosedException();
List<Item> carryableItems = chest.Items.Where(i => i.IsCarryable == true).ToList();
if (carryableItems.Count <= 0) throw new EmptyException();
foreach (Item item in carryableItems)
{
if (item is BagOfCoins bagOfCoins) Session.Coins += (int)bagOfCoins.Cost!;
else if (item is Key) Session.Keys++;
else Session.Inventory.Add(item);
}
chest.Items.RemoveAll(x => x.IsCarryable);
}
}
//public class InventoryRepository : IInventoryRepository
//{
// private GameSession Session;
// private IGetItemByIdRepository GetItemByIdRepository;
// public InventoryRepository(
// GameSession session,
// IGetItemByIdRepository getItemByIdRepository
// )
// {
// Session = session;
// GetItemByIdRepository = getItemByIdRepository;
// }
// public Item GetInventoryItem(int itemId)
// {
// return GetItemByIdRepository.GetItemById(itemId, Session.Inventory);
// }
// public List<Item> GetInventoryItems(List<int> itemsIds)
// {
// List<Item> items = new List<Item>();
// foreach (var itemId in itemsIds)
// {
// items.Add(GetInventoryItem(itemId));
// }
// return items;
// }
//}
public class GameInfoRepository : IGameInfoRepository
{
private GameSession Session;
public GameInfoRepository(GameSession session)
{
Session = session;
}
public GameInfoDTO GetGameInfo()
{
if (!Session.IsGameStarted && Session.Rooms.Count <= 1) throw new UnstartedGameException();
//RoomDTO roomDTO = new RoomDTO(Session.CurrentRoom!.Number, Session.CurrentRoom!.Name!, Session.CurrentRoom!.Description!, Session.CurrentRoom!.Enemies);
var roomDTO = GameObjectMapper.ToDTO(Session.CurrentRoom!);
WeaponDTO weaponDTO = (WeaponDTO)GameObjectMapper.ToDTO(Session.Weapon);
ArmorDTO? helmDTO = Session.Helm != null ? (ArmorDTO)GameObjectMapper.ToDTO(Session.Helm) : null;
ArmorDTO? chestplateDTO = Session.Chestplate != null ? (ArmorDTO)GameObjectMapper.ToDTO(Session.Chestplate) : null;
return new GameInfoDTO(roomDTO, weaponDTO, helmDTO, chestplateDTO, Session.MaxHealth, Session.CurrentHealth, Session.Coins, Session.Keys, GameObjectMapper.ToDTO(Session.Inventory));
}
}
public class GetRoomByIdRepository : IGetRoomByIdRepository
{
private GameSession Session;
IGameInfoRepository GameInfoRepository;
public GetRoomByIdRepository(
GameSession session,
IGameInfoRepository gameInfoRepository
)
{
Session = session;
GameInfoRepository = gameInfoRepository;
}
public Room GetRoomById(int roomId)
{
/*//Старый вариант
Room? room = Rooms.FirstOrDefault(r => r.Number == roomId);
if (room == null) throw new NullIdException("ROOM_NOT_FOUND", "Комната с таким номером не найдена.");*/
if (!Session.IsGameStarted && Session.Rooms.Count <= 1) throw new UnstartedGameException();
if (Session.IsInBattle) throw new InBattleException();
if (roomId < 0 || roomId > Session.Rooms.Count) throw new NullRoomIdException();
Room room = Session.Rooms[roomId];
if (!room.IsDiscovered) throw new UndiscoveredRoomException();
Session.CurrentRoom = room;
if (Session.CurrentRoom is EndRoom) throw new WinException(GameInfoRepository.GetGameInfo());
return room;
}
}
public class GetItemByIdRepository : IGetItemByIdRepository
{
public Item GetItemById(int itemId, List<Item> items)
{
Item? item = items.FirstOrDefault(i => i.Id == itemId);
if (item == null) throw new NullItemIdException();
return item;
}
}
public class GameControllerRepository : IGameControllerRepository
{
private GameSession Session;
private IGetCurrentRoomRepository GetCurrentRoomRepository;
private readonly IRoomNumberFactory RoomNumberFactory;
private readonly IRoomFactory RoomFactory;
private readonly IItemIdFactory ItemIdFactory;
private readonly IEnemyIdFactory EnemyIdFactory;
private readonly IInventoryRepository InventoryRepository;
private readonly IGameInfoRepository GameInfoRepository;
public GameControllerRepository(
GameSession session,
IGetCurrentRoomRepository getCurrentRoomRepository,
IRoomNumberFactory roomNumberFactory,
IRoomFactory roomFactory,
IItemIdFactory itemIdFactory,
IInventoryRepository inventoryRepository,
IEnemyIdFactory enemyIdFactory,
IGameInfoRepository gameInfoRepository
)
{
Session = session;
GetCurrentRoomRepository = getCurrentRoomRepository;
RoomNumberFactory = roomNumberFactory;
RoomFactory = roomFactory;
ItemIdFactory = itemIdFactory;
InventoryRepository = inventoryRepository;
EnemyIdFactory = enemyIdFactory;
GameInfoRepository = gameInfoRepository;
}
public Room GetCurrentRoom() => GetCurrentRoomRepository.GetCurrentRoom();
public void Start()
{
ResetGame();
Session.Rooms = GenerateMap();
Session.CurrentRoom = Session.Rooms[0];
Session.IsGameStarted = true;
}
public void ResetGame()
{
Session.Coins = 0;
Session.Keys = 0;
Session.Weapon = Fists.DefaultFists;
Session.Helm = null;
Session.Chestplate = null;
Session.Inventory = new List<Item>();
Session.Rooms = new List<Room>();
Session.MaxHealth = GameSession.DefaultMaxHealth;
Session.CurrentHealth = GameSession.DefaultMaxHealth;
Session.IsInBattle = false;
RoomNumberFactory.Reset();
ItemIdFactory.Reset();
EnemyIdFactory.Reset();
}
public List<Room> GenerateMap()
{
List<Room> rooms = new List<Room>()
{
new StartRoom(),
};
while (rooms.Last() is not EndRoom)
{
rooms.Add(RoomFactory.CreateRoom());
}
return rooms;
}
public List<Item> GetInventory()
{
if (!Session.IsGameStarted && Session.Rooms.Count <= 1) throw new UnstartedGameException();
return Session.Inventory;
}
public int GetCoins()
{
if (!Session.IsGameStarted && Session.Rooms.Count <= 1) throw new UnstartedGameException();
return Session.Coins;
}
public int GetKeys()
{
if (!Session.IsGameStarted && Session.Rooms.Count <= 1) throw new UnstartedGameException();
return Session.Keys;
}
public List<MapRoomDTO> GetMap()
{
if (!Session.IsGameStarted && Session.Rooms.Count <= 1) throw new UnstartedGameException();
if (!Session.Inventory.OfType<Map>().Any()) throw new NoMapException();
return Session.Rooms.Select(r => new MapRoomDTO(r.Number, r.Name ?? "НЕИЗВЕСТНО")).ToList();
}
public void UseInventoryItem(int itemId)
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
Item item = GetInventoryItem(itemId);
if (item is not Heal heal) throw new InvalidIdException("NOT_HEAL", "Это не предмет лечения.");
Session.Inventory.Remove(heal);
heal.Use(Session);
if (Session.CurrentHealth <= 0) throw new DefeatException($"{heal.Name} приводит Вас к гибели!", GetGameInfo());
}
public Item GetInventoryItem(int itemId) => InventoryRepository.GetInventoryItem(itemId);
public List<Equipment> GetEquipment() => InventoryRepository.GetEquipment();
public List<Equipment> EquipInventoryItem(int itemId) => InventoryRepository.EquipInventoryItem(itemId);
public List<Equipment> UnequipWeapon() => InventoryRepository.UnequipWeapon();
public List<Equipment> UnequipHelm() => InventoryRepository.UnequipHelm();
public List<Equipment> UnequipChestplate() => InventoryRepository.UnequipChestplate();
public GameInfoDTO GetGameInfo() => GameInfoRepository.GetGameInfo();
public void SellInventoryItem(int itemId) => InventoryRepository.SellInventoryItem(itemId);
}
public class RoomControllerRepository : IRoomControllerRepository
{
private GameSession Session;
private IGetCurrentRoomRepository GetCurrentRoomRepository;
private IChestRepository ChestRepository;
private IGameInfoRepository GameInfoRepository;
private IGetRoomByIdRepository GetRoomByIdRepository;
private IGetItemByIdRepository GetItemByIdRepository;
private IGetEnemyByIdRepository GetEnemyByIdRepository;
private ICombatRepository CombatRepository;
public RoomControllerRepository(
GameSession session,
IGetCurrentRoomRepository getCurrentRoomRepository,
IChestRepository chestRepository,
IGameInfoRepository gameInfoRepository,
IGetRoomByIdRepository getRoomByIdRepository,
IGetItemByIdRepository getItemByIdRepository,
IGetEnemyByIdRepository getEnemyByIdRepository,
ICombatRepository combatRepository
)
{
Session = session;
GetCurrentRoomRepository = getCurrentRoomRepository;
ChestRepository = chestRepository;
GameInfoRepository = gameInfoRepository;
GetRoomByIdRepository = getRoomByIdRepository;
GetItemByIdRepository = getItemByIdRepository;
CombatRepository = combatRepository;
GetEnemyByIdRepository = getEnemyByIdRepository;
}
public Room GetCurrentRoom() => GetCurrentRoomRepository.GetCurrentRoom();
public void GoNextRoom()
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
if (Session.IsInBattle) throw new InBattleException();
Session.CurrentRoom = Session.Rooms[Session.CurrentRoom!.Number + 1];
Session.CurrentRoom.IsDiscovered = true;
if (Session.CurrentRoom is EndRoom) throw new WinException(GameInfoRepository.GetGameInfo());
if (Session.CurrentRoom.Enemies.Any()) Session.IsInBattle = true;
}
public List<Item> Search()
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
if (Session.IsInBattle) throw new InBattleException();
//Room room = GetRoomById(roomId);
Room room = Session.CurrentRoom!;
room.IsSearched = true;
return room!.Items;
}
public void TakeItem(int itemId)
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
if (Session.IsInBattle) throw new InBattleException();
if (!Session.CurrentRoom!.IsSearched) throw new UnsearchedRoomException();
if (Session.CurrentRoom is Shop) throw new ImpossibleStealException();
//Room room = GetRoomById(roomId);
//Room room = Session.CurrentRoom!;
Item item = GetItemByIdRepository.GetItemById(itemId, Session.CurrentRoom!.Items);
if (!item.IsCarryable) throw new UncarryableException();
if (item is BagOfCoins bagOfCoins) Session.Coins += (int)bagOfCoins.Cost!;
else if (item is Key) Session.Keys++;
else Session.Inventory.Add(item);
Session.CurrentRoom!.Items.Remove(item);
}
public void TakeAllItems()
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
if (Session.IsInBattle) throw new InBattleException();
if (!Session.CurrentRoom!.IsSearched) throw new UnsearchedRoomException();
if (Session.CurrentRoom is Shop) throw new ImpossibleStealException();
//Room room = GetRoomById(roomId);
List<Item> carryableItems = Session.CurrentRoom!.Items.Where(i => i.IsCarryable == true).ToList();
if (carryableItems.Count <= 0) throw new EmptyException();
foreach (Item item in carryableItems)
{
if (!item.IsCarryable) continue;
if (item is BagOfCoins bagOfCoins) Session.Coins += (int)bagOfCoins.Cost!;
else if (item is Key) Session.Keys++;
else Session.Inventory.Add(item);
}
Session.CurrentRoom!.Items.RemoveAll(x => x.IsCarryable);
}
public void BuyItem(int itemId)
{
if (!Session.IsGameStarted) throw new UnstartedGameException();
if (Session.IsInBattle) throw new InBattleException();
if (!Session.CurrentRoom!.IsSearched) throw new UnsearchedRoomException();
if (Session.CurrentRoom is not Shop) throw new NotShopException();
Item item = GetItemByIdRepository.GetItemById(itemId, Session.CurrentRoom.Items);
if (item.Cost > Session.Coins) throw new NoMoneyException();
Session.Coins -= (int)item.Cost!;
Session.CurrentRoom.Items.Remove(item);
Session.Inventory.Add(item);
}
//public List<Enemy> GetEnemies(int roomId) => GetEnemyByIdRepository.GetEnemies();
public Enemy GetEnemyById() => GetEnemyByIdRepository.GetEnemyById();
public BattleLog DealDamage() => CombatRepository.DealDamage();
public BattleLog GetDamage() => CombatRepository.GetDamage();
public ChestStateDTO ReturnChestDTO(Chest chest) => ChestRepository.ReturnChestDTO(chest);
public ChestStateDTO ReturnChestDTO(int chestId) => ChestRepository.ReturnChestDTO(chestId);
public BattleLog HitChest(int chestId) => ChestRepository.HitChest(chestId);
public void OpenChest(int chestId) => ChestRepository.OpenChest(chestId);
public void UnlockChest(int chestId) => ChestRepository.UnlockChest(chestId);
public List<Item> SearchChest(int chestId) => ChestRepository.SearchChest(chestId);
public void TakeItemFromChest(int chestId, int itemId) => ChestRepository.TakeItemFromChest(chestId, itemId);
public void TakeAllItemsFromChest(int chestId) => ChestRepository.TakeAllItemsFromChest(chestId);
public Room GetRoomById(int roomId) => GetRoomByIdRepository.GetRoomById(roomId);
//public Item GetItemById(int itemId, List<Item> items) => GetItemByIdRepository.GetItemById(itemId, items);
//public Item GetInventoryItem(int itemId) => InventoryRepository.GetInventoryItem(itemId);
//public List<Item> GetInventoryItems(List<int> itemIds) => InventoryRepository.GetInventoryItems(itemIds);
public GameInfoDTO GetGameInfo() => GameInfoRepository.GetGameInfo();
}
}