protected override void stopGameHandler(UserThread client, CSMessage msg) { try { ServerThread cl1 = (ServerThread)client; if (cl1.playing()) { ServerThread cl2 = cl1.CurrentChallenger; if (cl2.playing()) { msg.add(MessageType.USER, cl1.ClientUser); cl2.send(msg); } cl2.CurrentChallenger = null; cl1.CurrentChallenger = null; if (view != null && view.isOpened()) { view.removeParty(cl1.ClientUser, cl2.ClientUser); } broadcast(); } } catch (Exception e) { log(Severiry.ERROR, "Controller[STOPGAME] - " + e.Message); } }
protected override void endGameHandler(UserThread client, CSMessage msg) { try { ServerThread cl1 = (ServerThread)client; ServerThread cl2 = cl1.CurrentChallenger; CSMessage msg2 = new CSMessage(MessageType.END_GAME). add(MessageType.GAME_STUFF, msg.get(MessageType.GAME_STUFF)); bool play = new Random().Next(2) != 0; if (msg.get(MessageType.WIN_GAME) != null) { cl1.ClientUser.win(); msg2.add(MessageType.LOSE_GAME, true); } if (msg.get(MessageType.DRAW_GAME) != null) { cl1.ClientUser.draw(); cl2.ClientUser.draw(); msg2.add(MessageType.DRAW_GAME, true); } cl1.ClientUser.incrementParties(); cl2.ClientUser.incrementParties(); if (cl1.isLogged()) { manager.update(cl1.ClientUser); } if (cl2.isLogged()) { manager.update(cl2.ClientUser); } if (view != null && view.isOpened()) { view.update(cl1.ClientUser.Login, cl1.ClientUser); } if (view != null && view.isOpened()) { view.update(cl2.ClientUser.Login, cl2.ClientUser); } msg.add(MessageType.USER, cl2.ClientUser); msg.add(MessageType.PLAY_GAME, play); msg2.add(MessageType.USER, cl1.ClientUser); msg2.add(MessageType.PLAY_GAME, !play); cl1.send(msg); cl2.send(msg2); } catch (Exception e) { log(Severiry.ERROR, "Controller[ENDGAME] - " + e.Message); } }
/// <summary> /// Send the abort message form the given user 'from' to the given user 'to'. /// </summary> /// <param name="to"></param> /// <param name="from"></param> private void abortRequest(CSUser to, CSUser from) { ServerThread cl = findClient(to); if (cl != null) { CSMessage msg = new CSMessage(MessageType.CHALLENGING) .add(MessageType.ABORT, true) .add(MessageType.USER, from); cl.send(msg); cl.To = null; } }
protected override void deconnectionHandler(UserThread client) { if (!ps.isStarted()) { return; } try { ServerThread cl1 = (ServerThread)client; if (cl1.playing()) { ServerThread cl2 = cl1.CurrentChallenger; CSMessage msg = new CSMessage(MessageType.STOP_GAME).add(MessageType.USER, cl1.ClientUser); cl2.send(msg); cl2.CurrentChallenger = null; cl1.CurrentChallenger = null; if (view != null && view.isOpened()) { view.removeParty(cl1.ClientUser, cl2.ClientUser); } } else { abortRequest(client, client.ClientUser); foreach (CSUser u in client.Froms) { abortRequest(u, client.ClientUser); } } if (view != null && view.isOpened()) { view.delete(cl1.ClientUser); } cl1.stop(); clients.Remove(cl1); broadcast(); } catch (Exception e) { log(Severiry.ERROR, "Controller[DECONNECTION] - " + e.Message); } }
/// <summary> /// Manage incoming message from the given client's socket. /// </summary> /// <param name="tcpclient"></param> private void incomingClient(TcpClient tcpclient) { ServerThread client = new ServerThread(tcpclient); client.messageHandler = new UserThread.newMessageHandler(handleMessage); client.loginHandler = new UserThread.newLoginHandler(loginHandler); client.challengerHandler = new UserThread.newChallengerHandler(challengerHandler); client.deconnectionHandler = new UserThread.newDeconnectionHandler(deconnectionHandler); client.endGameHandler = new UserThread.newEndGameHandler(endGameHandler); client.stopGameHandler = new UserThread.newStopGameHandler(stopGameHandler); CSMessage tosend = new CSMessage(MessageType.CONNECTION); tosend.add(MessageType.USER, client.ClientUser); client.send(tosend); client.start(); clients.Add(client); broadcast(); if (view != null && view.isOpened()) { view.addUser(client.ClientUser); } }
/// <summary> /// Start a party between the users behind the given threads. /// </summary> /// <param name="c1">First user thread</param> /// <param name="c2">Second user thread</param> private void beginGame(ServerThread c1, ServerThread c2) { c1.CurrentChallenger = c2; c2.CurrentChallenger = c1; bool play = new Random().Next(2) != 0; CSMessage startMsg = new CSMessage(MessageType.START_GAME); startMsg.add(MessageType.PLAY_GAME, play); startMsg.add(MessageType.USER, c2.ClientUser); c1.send(startMsg); startMsg.add(MessageType.PLAY_GAME, !play); startMsg.add(MessageType.USER, c1.ClientUser); c2.send(startMsg); broadcast(); if (view != null && view.isOpened()) { view.newParty(c1.ClientUser, c2.ClientUser); } }