diff --git a/Events/Event.java b/Events/Event.java new file mode 100644 index 0000000..e744bda --- /dev/null +++ b/Events/Event.java @@ -0,0 +1,18 @@ +package Events; + + +import game.User; + +public abstract class Event { + + private User user; + private String message; + + public String getMessage() { + return message; + } + + public User getUser() { + return user; + } +} diff --git a/Events/EventBus.java b/Events/EventBus.java new file mode 100644 index 0000000..6f348d9 --- /dev/null +++ b/Events/EventBus.java @@ -0,0 +1,21 @@ +package Events; + +import java.util.List; + +public class EventBus { + List events; + List subscriber; + + public List getEvents() { + return events; + } + + public void addEvents(Event event) { + this.events.add(event); + } + + public void setSubscriber(Subscriber subscriber) { + this.subscriber.add(subscriber); + } + +} diff --git a/Events/Subscriber.java b/Events/Subscriber.java new file mode 100644 index 0000000..64ad308 --- /dev/null +++ b/Events/Subscriber.java @@ -0,0 +1,9 @@ +package Events; + +import java.util.function.Function; + +public class Subscriber { + + Subscriber(Function function){ + } +} diff --git a/Events/WinEvent.java b/Events/WinEvent.java new file mode 100644 index 0000000..5f0abb6 --- /dev/null +++ b/Events/WinEvent.java @@ -0,0 +1,5 @@ +package Events; + +public class WinEvent extends Event{ + +} diff --git a/Main.java b/Main.java index f0d48b7..332bdc4 100644 --- a/Main.java +++ b/Main.java @@ -1,8 +1,10 @@ -import api.AIEngine; -import api.GameEngine; -import api.RuleEngine; +import Service.EmailService; +import Service.SMSService; +import api.*; import boards.History; import boards.TicTacToeBoard; +import commands.builder.EmailCommandBuilder; +import commands.builder.SMSCommandBuilder; import game.Board; import game.Cell; import game.Move; @@ -10,6 +12,8 @@ import java.util.Scanner; +import static java.util.concurrent.TimeUnit.DAYS; + public class Main { public static void main(String[] args) { @@ -19,10 +23,14 @@ public static void main(String[] args) { RuleEngine ruleEngine = new RuleEngine(); Board board = gameEngine.start("TicTacToe"); Scanner scanner = new Scanner(System.in); + Player computer = new Player("O"); + Player opponent = new Player("X"); + if(opponent.getUser().activeAfter(1,DAYS)){ + EmailService emailService = new EmailService(); + emailService.execute(new EmailCommandBuilder().user(opponent.getUser()).message("Glad, you are back").build()); + } while(!ruleEngine.getState(board).isOver()){ System.out.println("Make your Move"); - Player computer = new Player("O"); - Player opponent = new Player("X"); int row = scanner.nextInt(); int col = scanner.nextInt(); Move opponentMove = new Move(opponent,new Cell(row,col)); @@ -38,6 +46,14 @@ public static void main(String[] args) { History boardHistory = board1.getHistory(); boardHistory.printHistory(); } + // Problem with below approach is that it is not extensible, if link support, image support is needed then we had to change everywhere + if(ruleEngine.getState(board).getWinner().equals(opponent.symbol())){ + EmailService emailService = new EmailService(); + emailService.execute(new EmailCommandBuilder().user(opponent.getUser()).message("Congratulations, you won the match").link("https://suii.com").build()); + + SMSService smsService = new SMSService(); + smsService.execute(new SMSCommandBuilder().user(opponent.getUser()).message("Congratulations, you won the match").build()); + } System.out.println("Game winner is " + ruleEngine.getState(board).getWinner()); } diff --git a/Service/EmailService.java b/Service/EmailService.java new file mode 100644 index 0000000..28541dd --- /dev/null +++ b/Service/EmailService.java @@ -0,0 +1,13 @@ +package Service; + +import commands.implementation.EmailCommand; +import game.User; + +public class EmailService { + private void sendEmail(User user, String message){ + System.out.println("sendEmail has been envoked"); + } + public void execute(EmailCommand command){ + sendEmail(command.getNotificationDetails().getReciever(),command.getNotificationDetails().getMessage()); + } +} diff --git a/Service/SMSService.java b/Service/SMSService.java new file mode 100644 index 0000000..68551f0 --- /dev/null +++ b/Service/SMSService.java @@ -0,0 +1,13 @@ +package Service; + +import commands.implementation.SMSCommand; +import game.User; + +public class SMSService { + private void sendSMS(User user, String message){ + System.out.println("sendEmail has been envoked"); + } + public void execute(SMSCommand command){ + sendSMS(command.getNotificationDetails().getReciever(),command.getNotificationDetails().getMessage()); + } +} \ No newline at end of file diff --git a/commands/builder/EmailCommandBuilder.java b/commands/builder/EmailCommandBuilder.java new file mode 100644 index 0000000..eaecf85 --- /dev/null +++ b/commands/builder/EmailCommandBuilder.java @@ -0,0 +1,37 @@ +package commands.builder; + + +import commands.implementation.EmailCommand; +import game.User; + +public class EmailCommandBuilder { + NotificationBuilder notificationBuilder; + String link; + String templateId; + String tempalate; + + + public EmailCommandBuilder link(String link){ + this.link = link; + return this; + } + public EmailCommandBuilder templateId(String templateId){ + this.templateId = templateId; + return this; + } + public EmailCommandBuilder user(User user){ + this.notificationBuilder.user(user); + return this; + } + public EmailCommandBuilder message(String message){ + this.notificationBuilder.message(message); + return this; + } + public EmailCommandBuilder template(String template){ + this.tempalate = template; + return this; + } + public EmailCommand build(){ + return new EmailCommand(notificationBuilder.build()); + } +} diff --git a/commands/builder/NotificationBuilder.java b/commands/builder/NotificationBuilder.java new file mode 100644 index 0000000..9c24878 --- /dev/null +++ b/commands/builder/NotificationBuilder.java @@ -0,0 +1,22 @@ +package commands.builder; + +import commands.implementation.NotificationDetails; +import game.User; + +public class NotificationBuilder { + + User user; + String message; + + public NotificationBuilder user(User user){ + this.user = user; + return this; + } + public NotificationBuilder message(String message){ + this.message = message; + return this; + } + public NotificationDetails build(){ + return new NotificationDetails(user,message); + } +} diff --git a/commands/builder/SMSCommandBuilder.java b/commands/builder/SMSCommandBuilder.java new file mode 100644 index 0000000..c32b76b --- /dev/null +++ b/commands/builder/SMSCommandBuilder.java @@ -0,0 +1,32 @@ +package commands.builder; + +import commands.implementation.SMSCommand; +import game.User; + + +public class SMSCommandBuilder { + NotificationBuilder notificationBuilder; + String templateId; + String template; + + public SMSCommandBuilder templateId(String templateId){ + this.templateId = templateId; + return this; + } + public SMSCommandBuilder template(String template){ + this.template = template; + return this; + } + public SMSCommandBuilder user(User user){ + this.notificationBuilder.user(user); + return this; + } + public SMSCommandBuilder message(String message){ + this.notificationBuilder.message(message); + return this; + } + public SMSCommand build(){ + return new SMSCommand(notificationBuilder.build()); + } + +} diff --git a/commands/implementation/EmailCommand.java b/commands/implementation/EmailCommand.java new file mode 100644 index 0000000..891bff7 --- /dev/null +++ b/commands/implementation/EmailCommand.java @@ -0,0 +1,18 @@ +package commands.implementation; + +import game.User; + +public class EmailCommand { + NotificationDetails notificationDetails; + String link; + String templateId; + String template; + + public EmailCommand(NotificationDetails notificationDetails){ + notificationDetails = new NotificationDetails(notificationDetails.getReciever(),notificationDetails.getMessage()); + } + + public NotificationDetails getNotificationDetails() { + return notificationDetails; + } +} diff --git a/commands/implementation/NotificationDetails.java b/commands/implementation/NotificationDetails.java new file mode 100644 index 0000000..723d6ae --- /dev/null +++ b/commands/implementation/NotificationDetails.java @@ -0,0 +1,22 @@ +package commands.implementation; + +import game.User; + +public class NotificationDetails { + + User reciever; + String message; + + public User getReciever() { + return reciever; + } + + public String getMessage() { + return message; + } + + public NotificationDetails(User reciever, String message){ + this.reciever = reciever; + this.message = message; + } +} diff --git a/commands/implementation/SMSCommand.java b/commands/implementation/SMSCommand.java new file mode 100644 index 0000000..7bdc046 --- /dev/null +++ b/commands/implementation/SMSCommand.java @@ -0,0 +1,17 @@ +package commands.implementation; + +import game.User; + +public class SMSCommand { + NotificationDetails notificationDetails; + String templateId; + String template; + + public SMSCommand(NotificationDetails notificationDetails){ + notificationDetails = new NotificationDetails(notificationDetails.getReciever(),notificationDetails.getMessage()); + } + + public NotificationDetails getNotificationDetails() { + return notificationDetails; + } +} diff --git a/game/Player.java b/game/Player.java index 101b3cc..b6599c8 100644 --- a/game/Player.java +++ b/game/Player.java @@ -2,8 +2,10 @@ public class Player { private final String playerSymbol; + private User user; public Player(String playerSymbol){ this.playerSymbol=playerSymbol; + this.user = new User(); } public String symbol(){ return playerSymbol; @@ -11,4 +13,7 @@ public String symbol(){ public Player flip(){ return new Player(playerSymbol.equals("X") ? "O":"X"); } + public User getUser() { + return user; + } } diff --git a/game/User.java b/game/User.java new file mode 100644 index 0000000..88717d3 --- /dev/null +++ b/game/User.java @@ -0,0 +1,12 @@ +package game; + +import java.util.concurrent.TimeUnit; + +public class User { + int id; + long lastActiveTime; + + public boolean activeAfter(int threshold, TimeUnit timeUnit){ + return System.currentTimeMillis()-lastActiveTime>timeUnit.toMillis(threshold); + } +}