Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Events/Event.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
21 changes: 21 additions & 0 deletions Events/EventBus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Events;

import java.util.List;

public class EventBus {
List<Event> events;
List<Subscriber> subscriber;

public List<Event> getEvents() {
return events;
}

public void addEvents(Event event) {
this.events.add(event);
}

public void setSubscriber(Subscriber subscriber) {
this.subscriber.add(subscriber);
}

}
9 changes: 9 additions & 0 deletions Events/Subscriber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package Events;

import java.util.function.Function;

public class Subscriber {

Subscriber(Function<Event,Void> function){
}
}
5 changes: 5 additions & 0 deletions Events/WinEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package Events;

public class WinEvent extends Event{

}
26 changes: 21 additions & 5 deletions Main.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
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;
import game.Player;

import java.util.Scanner;

import static java.util.concurrent.TimeUnit.DAYS;

public class Main {

public static void main(String[] args) {
Expand All @@ -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));
Expand All @@ -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());
}

Expand Down
13 changes: 13 additions & 0 deletions Service/EmailService.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
13 changes: 13 additions & 0 deletions Service/SMSService.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
37 changes: 37 additions & 0 deletions commands/builder/EmailCommandBuilder.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
22 changes: 22 additions & 0 deletions commands/builder/NotificationBuilder.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
32 changes: 32 additions & 0 deletions commands/builder/SMSCommandBuilder.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
18 changes: 18 additions & 0 deletions commands/implementation/EmailCommand.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
22 changes: 22 additions & 0 deletions commands/implementation/NotificationDetails.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
17 changes: 17 additions & 0 deletions commands/implementation/SMSCommand.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
5 changes: 5 additions & 0 deletions game/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

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;
}
public Player flip(){
return new Player(playerSymbol.equals("X") ? "O":"X");
}
public User getUser() {
return user;
}
}
12 changes: 12 additions & 0 deletions game/User.java
Original file line number Diff line number Diff line change
@@ -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);
}
}