-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
71 lines (64 loc) · 2.79 KB
/
Main.java
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
import java.util.UUID;
import data.ClanRepository;
import data.DataService;
import data.TransactionsRepository;
import models.Clan;
import service.ClanService;
import service.TaskService;
import service.UserTransactionService;
public class Main {
public static void main(String[] args) {
// Setting up game environment
DataService dataService= new DataService();
dataService.setUpDatabase();
ClanRepository clanRepository = new ClanRepository(dataService);
TransactionsRepository transactionsRepository = new TransactionsRepository(dataService);
ClanService clanService = new ClanService(clanRepository);
TaskService taskService = new TaskService(clanService);
UserTransactionService userTransactionService = new UserTransactionService(clanService, transactionsRepository);
// completed setup
//game starts
Clan punks = clanService.createClan("punks");
Clan raiders = clanService.createClan("raiders");
Clan prists = clanService.createClan("prists");
Clan yankees = clanService.createClan("yankees");
Clan faggots = clanService.createClan("faggots");
updateGoldConcurrently(userTransactionService, punks);
taskService.fightClan(punks, faggots); // must be a draw
taskService.fightClan(punks, prists); // punks gained exp and must win prists
taskService.fightClan(punks, faggots); // skilled punks come back to faggots and finish'em up
taskService.stealGold(punks, yankees, 40); // punks steal gold
}
/**
* Multiple users update clan's gold simultaneously
*
* @param userTransactionService
* @param clan
*/
private static void updateGoldConcurrently(UserTransactionService userTransactionService, Clan clan) {
Runnable user1Task = () -> {
UUID userId = UUID.randomUUID();
System.out.println("thread 1 executing..");
userTransactionService.takeGoldFromClan(userId, clan, 300);
};
Runnable user2Task = () -> {
UUID userId = UUID.randomUUID();
System.out.println("thread 2 executing..");
userTransactionService.addGoldToClan(userId, clan, 100);
};
Runnable user3Task = () -> {
UUID userId = UUID.randomUUID();
System.out.println("thread 3 executing..");
userTransactionService.takeGoldFromClan(userId, clan, 200);
};
Runnable user4Task = () -> {
UUID userId = UUID.randomUUID();
System.out.println("thread 4 executing..");
userTransactionService.addGoldToClan(userId, clan, 120);
};
new Thread(user1Task).start();
new Thread(user2Task).start();
new Thread(user3Task).start();
new Thread(user4Task).start();
}
}