Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
nicol committed Sep 14, 2023
1 parent bc4a59f commit b801410
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,4 @@ public String toString() {
return ToStringBuilder.reflectionToString(this);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ protected SearchMessage(Player p, MTGProduct item) {
public MTGProduct getItem() {
return item;
}



}
3 changes: 1 addition & 2 deletions src/main/java/org/magic/api/interfaces/MTGNetworkClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
import org.magic.api.beans.abstracts.AbstractMessage;
import org.magic.api.beans.abstracts.AbstractMessage.MSG_TYPE;
import org.magic.api.beans.messages.SearchMessage;
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{



TalkMessage consume() throws IOException;
AbstractMessage consume() throws IOException;

void switchAddress(String topicName) throws IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

import org.magic.api.beans.abstracts.AbstractMessage;
import org.magic.api.beans.abstracts.AbstractMessage.MSG_TYPE;
import org.magic.api.beans.messages.SearchMessage;
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;
import org.magic.game.model.Player.STATUS;
import org.magic.services.network.URLTools;

public abstract class AbstractNetworkProvider extends AbstractMTGPlugin implements MTGNetworkClient {

Expand Down Expand Up @@ -41,8 +43,30 @@ protected AbstractNetworkProvider() {


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

var txt = read();

if(txt==null)
return null;

var json = URLTools.toJson(txt);
var type = AbstractMessage.MSG_TYPE.valueOf(json.getAsJsonObject().get("typeMessage").getAsString());

switch(type)
{
case TALK : return serializer.fromJson(txt, TalkMessage.class);
case CONNECT: return serializer.fromJson(txt, StatutMessage.class);
case DISCONNECT: return serializer.fromJson(txt, StatutMessage.class);
case CHANGESTATUS :return serializer.fromJson(txt, StatutMessage.class);
case SEARCH :return serializer.fromJson(txt, SearchMessage.class);
default : return serializer.fromJson(txt, TalkMessage.class);
}





}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
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.MagicCard;
import org.magic.api.beans.abstracts.AbstractMessage;
import org.magic.api.beans.abstracts.AbstractMessage.MSG_TYPE;
import org.magic.api.beans.enums.EnumItems;
import org.magic.api.beans.messages.SearchMessage;
import org.magic.api.beans.messages.StatutMessage;
import org.magic.api.beans.messages.TalkMessage;
import org.magic.api.interfaces.MTGDao;
Expand Down Expand Up @@ -131,7 +134,7 @@ protected String read() throws IOException {
@Override
public void logout() throws IOException {
try {
sendMessage(new StatutMessage(player,Player.STATUS.CONNECTED));
sendMessage(new StatutMessage(player,Player.STATUS.DISCONNECTED));
session.close();
} catch (ActiveMQException e) {
throw new IOException(e);
Expand Down Expand Up @@ -176,13 +179,13 @@ public String getVersion() {


@Override
public void searchStock(TalkMessage s) throws IOException {
public void searchStock(SearchMessage 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()) {
logger.info(ret);
if(s.getItem().getTypeProduct()==EnumItems.CARD)
{
var ret = MTG.getEnabledPlugin(MTGDao.class).listStocks((MagicCard)s.getItem());
}
} catch (SQLException e) {
logger.error(e);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/magic/game/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Player extends Observable implements Serializable {
private static final long serialVersionUID = 1L;

public enum STATUS {
CONNECTED, BUSY, AWAY, GAMING,TYPING
DISCONNECTED,CONNECTED, BUSY, AWAY, GAMING,TYPING
}

private Long id;
Expand Down
27 changes: 16 additions & 11 deletions src/main/java/org/magic/gui/components/NetworkChatPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
Expand Down Expand Up @@ -194,24 +195,28 @@ protected void process(List<AbstractMessage> chunks) {
editorPane.setEditable(true);



for(var s : chunks)
{
switch(s.getTypeMessage())
{
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 TALK:listMsgModel.addElement((TalkMessage)s);break;
case CHANGESTATUS:
var msg = (StatutMessage)s;
switch(msg.getStatut())
{
case CONNECTED : listPlayerModel.addElement(s.getAuthor());break;
case DISCONNECTED:listPlayerModel.removeElement(s.getAuthor());break;
default: Collections.list(listPlayerModel.elements()).stream().filter(p->p.getId().equals(s.getAuthor().getId())).forEach(p->p.setState(msg.getStatut()));break;
}
break;

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

default:break;
default:break;
}
}
listPlayers.updateUI();


listMsg.ensureIndexIsVisible( listMsg.getModel().getSize() - 1 );

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/test/TestConsumers.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private static void runThreadFor(ActiveMQNetworkClient client) {
new Thread(()->{
try {
while(client.isActive())
System.out.println(client.getPlayer() +" receive"+ client.consume().getMessage());
System.out.println(client.getPlayer() +" receive"+ client.consume());
} catch (IOException e) {
e.printStackTrace();
}
Expand Down

0 comments on commit b801410

Please sign in to comment.