2525import java .io .IOException ;
2626import java .nio .file .Path ;
2727import java .nio .file .Paths ;
28- import java .util .HashMap ;
28+ import java .util .* ;
2929import java .util .concurrent .ConcurrentLinkedDeque ;
3030
3131public final class PolychatServer {
3232 private final ConcurrentLinkedDeque <GenericEvent > queue ;
3333 private final Server server ;
3434 private final PolychatProtobufMessageDispatcher polychatProtobufMessageDispatcher ;
35- private final JDA jda ;
3635 private final HashMap <String , OnlineServer > onlineServers ;
37- private final TextChannel generalChannel ;
3836 private final MessageReceivedHandler messageReceivedHandler ;
37+ private final Broadcaster broadcaster ;
3938
4039 private final static Logger logger = LoggerFactory .getLogger (PolychatServer .class );
40+
4141 public static final int TICK_TIME_IN_MILLIS = 50 ;
4242
4343 private PolychatServer () throws IOException , LoginException , InterruptedException {
@@ -47,13 +47,19 @@ private PolychatServer() throws IOException, LoginException, InterruptedExceptio
4747 // set up TCP;
4848 server = new Server (yamlConfig .get ("tcpPort" ), yamlConfig .get ("bufferSize" ));
4949
50+ // set up broadcasts
51+ List <String > broadcastMessages = yamlConfig .getOrDefault ("broadcastMsgs" , new ArrayList <String >());
52+ String broadcastID = yamlConfig .getOrDefault ("broadcastID" , "BROADCAST" );
53+ String broadcastPrefix = yamlConfig .getOrDefault ("broadcastPrefix" , "[System]" );
54+ broadcaster = new Broadcaster (broadcastID , broadcastPrefix , broadcastMessages , server );
55+
5056 // set up JDA event queue & servers hashmap;
5157 queue = new ConcurrentLinkedDeque <GenericEvent >();
5258 onlineServers = new HashMap <>();
5359
5460 // set up JDA commands;
5561 CommandClient commandClient = new CommandClientBuilder ()
56- .setOwnerId (yamlConfig .get ("ownerId" )) // will need to be retrieved from YAML;
62+ .setOwnerId (yamlConfig .get ("ownerId" ))
5763 .setPrefix (yamlConfig .get ("commandPrefix" ))
5864 .addCommands (
5965 new ExecCommand (server , onlineServers ),
@@ -64,14 +70,14 @@ private PolychatServer() throws IOException, LoginException, InterruptedExceptio
6470 .build ();
6571
6672 // set up main JDA;
67- jda = JDABuilder .createDefault (yamlConfig .get ("token" )) // will need to be retrieved from YAML;
73+ JDA jda = JDABuilder .createDefault (yamlConfig .get ("token" ))
6874 .addEventListeners (
6975 commandClient ,
7076 new GenericJdaEventHandler (queue )
7177 )
7278 .build ()
7379 .awaitReady ();
74- generalChannel = jda .getTextChannelById (yamlConfig .get ("generalChannelId" )); // same as above here ;
80+ TextChannel generalChannel = jda .getTextChannelById (yamlConfig .get ("generalChannelId" ));
7581 messageReceivedHandler = new MessageReceivedHandler (generalChannel , server );
7682
7783 // set up Protobuf message handlers;
@@ -87,37 +93,6 @@ private PolychatServer() throws IOException, LoginException, InterruptedExceptio
8793 );
8894 }
8995
90- private YamlConfig getDefaultConfig (Path path ) throws IOException {
91- YamlConfig def = YamlConfig .fromInMemoryString ("" );
92- def .set ("token" , "" );
93- def .set ("ownerId" , "" );
94- def .set ("commandPrefix" , "!" );
95- def .set ("generalChannelId" , "" );
96- def .set ("tcpPort" , 5005 );
97- def .set ("bufferSize" , 4096 );
98- def .saveToFile (path );
99- return def ;
100- }
101-
102- public YamlConfig getConfig () {
103- try {
104- Path configPath = Paths .get ("polychat.yml" );
105-
106- if (configPath .toFile ().createNewFile ()) {
107- getDefaultConfig (configPath );
108- logger .error ("You must have a config to use polychat! Creating default polychat.yml..." );
109- System .exit (0 );
110- }
111-
112- return YamlConfig .fromFilesystem (configPath );
113- } catch (IOException e ) {
114- logger .error ("Failed to create a new config!" );
115- e .printStackTrace ();
116- System .exit (1 );
117- }
118- return YamlConfig .fromInMemoryString ("" );
119- }
120-
12196 public static void main (String [] args ) {
12297 try {
12398 new PolychatServer ().spin ();
@@ -142,6 +117,8 @@ private void spin() {
142117 }
143118
144119 private void spinOnce () {
120+ broadcaster .tick ();
121+
145122 try {
146123 for (Message message : server .poll ()) {
147124 polychatProtobufMessageDispatcher .handlePolychatMessage (message );
@@ -158,4 +135,39 @@ private void spinOnce() {
158135 logger .error ("Error occurred in Polychat server event loop" , e );
159136 }
160137 }
138+
139+ private YamlConfig getDefaultConfig (Path path ) throws IOException {
140+ YamlConfig def = YamlConfig .fromInMemoryString ("" );
141+ def .set ("broadcastsPrefix" , "" );
142+ def .set ("token" , "" );
143+ def .set ("ownerId" , "" );
144+ def .set ("commandPrefix" , "!" );
145+ def .set ("generalChannelId" , "" );
146+ def .set ("tcpPort" , 5005 );
147+ def .set ("bufferSize" , 4096 );
148+ def .set ("broadcastMsgs" , Arrays .asList ("example broadcast message 1" , "example broadcast message 2" ));
149+ def .set ("broadcastID" , "BROADCAST" );
150+ def .set ("broadcastPrefix" , "[System]" );
151+ def .saveToFile (path );
152+ return def ;
153+ }
154+
155+ public YamlConfig getConfig () {
156+ try {
157+ Path configPath = Paths .get ("polychat.yml" );
158+
159+ if (configPath .toFile ().createNewFile ()) {
160+ getDefaultConfig (configPath );
161+ logger .error ("You must have a config to use polychat! Creating default polychat.yml..." );
162+ System .exit (0 );
163+ }
164+
165+ return YamlConfig .fromFilesystem (configPath );
166+ } catch (IOException e ) {
167+ logger .error ("Failed to create a new config!" );
168+ e .printStackTrace ();
169+ System .exit (1 );
170+ }
171+ return YamlConfig .fromInMemoryString ("" );
172+ }
161173}
0 commit comments