Skip to content

Commit

Permalink
abstract message for chat
Browse files Browse the repository at this point in the history
  • Loading branch information
nicol committed Sep 13, 2023
1 parent dc0ca77 commit 6398b36
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 96 deletions.
52 changes: 0 additions & 52 deletions src/main/java/org/magic/api/beans/JsonMessage.java

This file was deleted.

14 changes: 13 additions & 1 deletion src/main/java/org/magic/api/beans/abstracts/AbstractMessage.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
package org.magic.api.beans.abstracts;

import java.time.Instant;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.magic.game.model.Player;
import org.magic.services.tools.CryptoUtils;

public abstract class AbstractMessage extends AbstractAuditableItem {

public enum MSG_TYPE { CONNECT, CHANGESTATUS, DISCONNECT, TALK, SYSTEM, SEARCH, ANSWER}


private Player author;
private static final long serialVersionUID = 1L;
private String id;
private MSG_TYPE typeMessage;

protected AbstractMessage() {
setTypeMessage(MSG_TYPE.TALK);
setId(CryptoUtils.generateMD5(UUID.randomUUID().toString()+new Date()+typeMessage));
setStart(Instant.now());
setEnd(Instant.now());
}

public Player getAuthor() {
return author;
}
public void setAuthor(Player author) {
this.author = author;
}

public void setId(String id) {
this.id = id;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/magic/api/beans/messages/ConnectionMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.magic.api.beans.messages;

import org.magic.api.beans.abstracts.AbstractMessage;
import org.magic.game.model.Player;

public class ConnectionMessage extends AbstractMessage {

private static final long serialVersionUID = 1L;

public ConnectionMessage(Player p, boolean online) {
setTypeMessage(online?MSG_TYPE.CONNECT:MSG_TYPE.DISCONNECT);
setAuthor(p);
}

}
25 changes: 25 additions & 0 deletions src/main/java/org/magic/api/beans/messages/StatutMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.magic.api.beans.messages;

import org.magic.api.beans.abstracts.AbstractMessage;
import org.magic.game.model.Player;
import org.magic.game.model.Player.STATUS;

public class StatutMessage extends AbstractMessage {

private static final long serialVersionUID = 1L;
private STATUS statut;

public StatutMessage(Player p, STATUS statut) {
setTypeMessage(MSG_TYPE.CHANGESTATUS);
setAuthor(p);
this.statut=statut;
}

public void setStatut(STATUS statut) {
this.statut = statut;
}

public STATUS getStatut() {
return statut;
}
}
44 changes: 44 additions & 0 deletions src/main/java/org/magic/api/beans/messages/TalkMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.magic.api.beans.messages;

import java.awt.Color;

import org.magic.api.beans.abstracts.AbstractMessage;
import org.magic.game.model.Player;

public class TalkMessage extends AbstractMessage{

private static final long serialVersionUID = 1L;

private String message;
private Color color;

public String toChatString() {
return getAuthor().getName() + " : " + message;
}

public TalkMessage(Player author, String message, Color color) {
super();
setAuthor(author);
this.message = message;
this.color = color;

}



public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}

public Color getColor() {
return color;
}

public void setColor(Color color) {
this.color = color;
}

}
9 changes: 5 additions & 4 deletions src/main/java/org/magic/api/interfaces/MTGNetworkClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,33 @@
import java.awt.Color;
import java.io.IOException;

import org.magic.api.beans.JsonMessage;
import org.magic.api.beans.abstracts.AbstractMessage;
import org.magic.api.beans.abstracts.AbstractMessage.MSG_TYPE;
import org.magic.api.beans.messages.TalkMessage;
import org.magic.game.model.Player;
import org.magic.game.model.Player.STATUS;

public interface MTGNetworkClient extends MTGPlugin{



JsonMessage consume() throws IOException;
TalkMessage consume() throws IOException;

void switchAddress(String topicName) throws IOException;

void join(Player p, String url, String topic) throws IOException;

void sendMessage(String text, Color c,MSG_TYPE type) throws IOException;

void sendMessage(JsonMessage obj) throws IOException;
void sendMessage(AbstractMessage obj) throws IOException;

void logout() throws IOException;

void changeStatus(STATUS selectedItem) throws IOException;

boolean isActive();

void searchStock(JsonMessage s) throws IOException;
void searchStock(TalkMessage s) throws IOException;

public Player getPlayer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import java.io.IOException;
import java.time.Instant;

import org.magic.api.beans.JsonMessage;
import org.magic.api.beans.abstracts.AbstractMessage;
import org.magic.api.beans.abstracts.AbstractMessage.MSG_TYPE;
import org.magic.api.beans.messages.ConnectionMessage;
import org.magic.api.beans.messages.StatutMessage;
import org.magic.api.beans.messages.TalkMessage;
import org.magic.api.exports.impl.JsonExport;
import org.magic.api.interfaces.MTGNetworkClient;
import org.magic.game.model.Player;
Expand Down Expand Up @@ -39,12 +42,12 @@ protected AbstractNetworkProvider() {


@Override
public JsonMessage consume() throws IOException {
return serializer.fromJson(read(),JsonMessage.class);
public TalkMessage consume() throws IOException {
return serializer.fromJson(read(),TalkMessage.class);
}


protected String toJson(JsonMessage obj) {
protected String toJson(AbstractMessage obj) {
return serializer.toJson(obj);
}

Expand All @@ -59,22 +62,22 @@ public void join(Player p, String url,String adress) throws IOException {

switchAddress(adress);

sendMessage(new JsonMessage(player,"connected",Color.black,MSG_TYPE.CONNECT));
sendMessage(new ConnectionMessage(player,true));

logger.info("Connected to server {} with id={}",url,player.getId());
}

@Override
public void changeStatus(STATUS selectedItem) throws IOException {
player.setState(selectedItem);
sendMessage(new JsonMessage(player,selectedItem.name(),Color.black,MSG_TYPE.CHANGESTATUS));
sendMessage(new StatutMessage(player,selectedItem));
}


@Override
public void sendMessage(String text, Color c,MSG_TYPE type) throws IOException {

sendMessage(new JsonMessage(player,text,c,type));
sendMessage(new TalkMessage(player,text,c));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import org.apache.activemq.artemis.api.core.client.ClientSession;
import org.apache.activemq.artemis.api.core.client.ClientSessionFactory;
import org.apache.activemq.artemis.api.core.client.ServerLocator;
import org.magic.api.beans.JsonMessage;
import org.magic.api.beans.abstracts.AbstractMessage;
import org.magic.api.beans.abstracts.AbstractMessage.MSG_TYPE;
import org.magic.api.beans.messages.ConnectionMessage;
import org.magic.api.beans.messages.TalkMessage;
import org.magic.api.interfaces.MTGDao;
import org.magic.api.interfaces.abstracts.AbstractNetworkProvider;
import org.magic.services.tools.MTG;
Expand Down Expand Up @@ -96,7 +98,7 @@ private QueueConfiguration createQueueConf(String adress) {


@Override
public void sendMessage(JsonMessage obj) throws IOException {
public void sendMessage(AbstractMessage obj) throws IOException {
var message = session.createMessage(obj.getTypeMessage()==MSG_TYPE.TALK);
message.getBodyBuffer().writeString(toJson(obj));

Expand Down Expand Up @@ -130,7 +132,7 @@ protected String read() throws IOException {
@Override
public void logout() throws IOException {
try {
sendMessage(new JsonMessage(player,"disconnect",Color.black,MSG_TYPE.DISCONNECT));
sendMessage(new ConnectionMessage(player,false));
session.close();
} catch (ActiveMQException e) {
throw new IOException(e);
Expand Down Expand Up @@ -175,15 +177,13 @@ public String getVersion() {


@Override
public void searchStock(JsonMessage s) throws IOException {
public void searchStock(TalkMessage s) throws IOException {
try {
logger.info("Getting a search stock query {}",s);
var ret = MTG.getEnabledPlugin(MTGDao.class).listStocks(s.getMessage(), MTG.getEnabledPlugin(MTGDao.class).listCollections());

if(!ret.isEmpty()) {
var msg = new JsonMessage(getPlayer(), FALSE, Color.GREEN, MSG_TYPE.ANSWER);
msg.setMessage("I got "+ s.getMessage() + " in stock :"+ ret.stream().map(st->st.getMagicCollection().getName()).distinct().collect(Collectors.joining(";")));
sendMessage(msg);
logger.info(ret);
}
} catch (SQLException e) {
logger.error(e);
Expand Down
23 changes: 11 additions & 12 deletions src/main/java/org/magic/gui/components/NetworkChatPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import javax.swing.SwingWorker;
import javax.swing.border.TitledBorder;

import org.magic.api.beans.JsonMessage;
import org.magic.api.beans.abstracts.AbstractMessage;
import org.magic.api.beans.abstracts.AbstractMessage.MSG_TYPE;
import org.magic.api.beans.messages.StatutMessage;
import org.magic.api.beans.messages.TalkMessage;
import org.magic.api.beans.MTGNotification;
import org.magic.api.beans.MTGNotification.MESSAGE_TYPE;
import org.magic.api.interfaces.MTGNetworkClient;
Expand All @@ -50,14 +52,14 @@ public class NetworkChatPanel extends MTGUIComponent {
private static final long serialVersionUID = 1L;
private JTextField txtServer;
private JList<Player> listPlayers;
private JList<JsonMessage> listMsg;
private JList<TalkMessage> listMsg;
private JButton btnConnect;
private JButton btnLogout;
private JTextArea editorPane;
private JComboBox<STATUS> cboStates;
private JButton btnColorChoose;
private JButton btnSearch;
private DefaultListModel<JsonMessage> listMsgModel;
private DefaultListModel<TalkMessage> listMsgModel;
private DefaultListModel<Player> listPlayerModel;
private transient MTGNetworkClient client;

Expand Down Expand Up @@ -158,7 +160,7 @@ private void initActions() {



var sw = new SwingWorker<Void, JsonMessage>(){
var sw = new SwingWorker<Void, AbstractMessage>(){

@Override
protected Void doInBackground() throws Exception {
Expand All @@ -184,7 +186,7 @@ protected void done() {


@Override
protected void process(List<JsonMessage> chunks) {
protected void process(List<AbstractMessage> chunks) {

txtServer.setEnabled(false);
btnConnect.setEnabled(false);
Expand All @@ -196,21 +198,18 @@ protected void process(List<JsonMessage> chunks) {
{
switch(s.getTypeMessage())
{
case CHANGESTATUS:Collections.list(listPlayerModel.elements()).stream().filter(p->p.getId().equals(s.getAuthor().getId())).forEach(p->p.setState(STATUS.valueOf(s.getMessage())));listPlayers.updateUI();break;
case CHANGESTATUS:Collections.list(listPlayerModel.elements()).stream().filter(p->p.getId().equals(s.getAuthor().getId())).forEach(p->p.setState(((StatutMessage)s).getStatut()));listPlayers.updateUI();break;

case CONNECT:listPlayerModel.addElement(s.getAuthor());
listPlayers.updateUI();
if(!client.getPlayer().getId().equals(s.getAuthor().getId()) && client.getPlayer().getState()!=STATUS.BUSY)
MTGControler.getInstance().notify(new MTGNotification("New connection", s.getAuthor() + " is online", MESSAGE_TYPE.INFO));
break;
case DISCONNECT:listPlayerModel.removeElement(s.getAuthor());listPlayers.updateUI();break;
case SEARCH: try {
MTG.getEnabledPlugin(MTGNetworkClient.class).searchStock(s);
} catch (IOException e) {
logger.error(e);
}break;
default:listMsgModel.addElement(s);break;

case TALK:listMsgModel.addElement((TalkMessage)s);break;

default:break;
}
}

Expand Down
Loading

0 comments on commit 6398b36

Please sign in to comment.