protected override void endGameHandler(UserThread client, CSMessage msg) { try { CSUser challenger = (CSUser)msg.get(MessageType.USER); bool play = (bool)msg.get(MessageType.PLAY_GAME); Object gameStuff = (Object)msg.get(MessageType.GAME_STUFF); GameState state = GameState.LOST; if (msg.get(MessageType.WIN_GAME) != null) { client.ClientUser.win(); state = GameState.WON; } else if (msg.get(MessageType.DRAW_GAME) != null) { client.ClientUser.draw(); state = GameState.DRAW; } client.ClientUser.incrementParties(); if (view.isOpened()) { view.endGameHandler(client.ClientUser, challenger, state, play, gameStuff); } } catch (Exception e) { log(Severiry.ERROR, "Controller[EndGame] - " + e.Message); } }
protected override void loginHandler(UserThread client, CSMessage msg) { try { if ((bool)msg.get(MessageType.STATE)) { client.ClientUser = (CSUser)msg.get(MessageType.USER); if (view.isOpened()) { view.loginHandler(client.ClientUser); } } else { if (view.isOpened()) { view.showRegisterOption(); view.connectionHandler(client.ClientUser); } log(Severiry.ERROR, (string)msg.get(MessageType.ERROR)); } } catch (Exception e) { log(Severiry.ERROR, "Controller - " + 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); } }
protected void receiveMsgHandler(UserThread client, CSMessage msg) { try { CSUser user = (CSUser)msg.get(MessageType.USER); string message = (string)msg.get(MessageType.MESSAGE); if (view.isOpened()) { view.receiveMsgHandler(user, message); } } catch (Exception e) { log(Severiry.ERROR, "Controller [ReceiveMsg] - " + e.Message); } }
protected void startGameHandler(UserThread client, CSMessage msg) { try { CSUser user = (CSUser)msg.get(MessageType.USER); bool play = (bool)msg.get(MessageType.PLAY_GAME); client.To = null; client.Froms.Clear(); if (view.isOpened()) { view.startGameHandler(user, play); } } catch (Exception e) { log(Severiry.ERROR, "Controller[StartGame] - " + e.Message); } }
protected override void challengerHandler(UserThread client, CSMessage msg) { try { CSUser user = (CSUser)msg.get(MessageType.USER); if (msg.get(MessageType.REQUEST) != null) { client.Froms.Add(user); if (view.isOpened()) { view.requestHandler(user); } } else if ((msg.get(MessageType.ABORT) != null)) { if (user.Equals(client.To)) { client.To = null; if (view.isOpened()) { view.abortHandler(user, true); } } else { client.Froms.Remove(user); if (view.isOpened()) { view.abortHandler(user, false); } } } } catch (Exception e) { log(Severiry.ERROR, "Controller [Challenging] - " + e.Message); } }
protected override void stopGameHandler(UserThread client, CSMessage msg) { try { CSUser user = (CSUser)msg.get(MessageType.USER); if (view.isOpened()) { view.stopGameHandler(user); } } catch (Exception e) { log(Severiry.ERROR, "Controller[StopGame] - " + e.Message); } }
protected void gameHandler(UserThread client, CSMessage msg) { try { Object gameStuff = msg.get(MessageType.GAME_STUFF); if (view.isOpened()) { view.gameHandler(gameStuff); } } catch (Exception e) { log(Severiry.ERROR, "Controller[InGame] - " + e.Message); } }
protected void connectionHandler(UserThread client, CSMessage msg) { try { CSUser user = (CSUser)msg.get(MessageType.USER); client.ClientUser = user; if (view.isOpened()) { view.connectionHandler(user); } } catch (Exception e) { log(Severiry.ERROR, "Controller[Connection] - " + e.Message); } }
protected void userListHandler(UserThread client, CSMessage msg) { try { List <CSUser> users = (List <CSUser>)msg.get(MessageType.USER_LIST); users.Remove(client.ClientUser); if (view.isOpened()) { view.userListHandler(users); } } catch (Exception e) { log(Severiry.ERROR, "Controller [UserList] - " + e.Message); } }
protected override void challengerHandler(UserThread client, CSMessage msg) { try { ServerThread to; CSUser user = (CSUser)msg.get(MessageType.USER); if ((to = findClient(user)) != null && !to.playing()) { if (msg.get(MessageType.REQUEST) != null) { if (!user.Equals(client.To) && !client.Froms.Contains(user)) { abortRequest(client, client.ClientUser); client.To = to.ClientUser; to.Froms.Add(client.ClientUser); msg.add(MessageType.USER, client.ClientUser); to.send(msg); } } else if (msg.get(MessageType.RESPONSE) != null) { if (client.Froms.Contains(user) && to.To.Equals(client.ClientUser)) { abortRequest(client, client.ClientUser); client.To = null; client.Froms.Remove(to.ClientUser); to.To = null; foreach (CSUser u in client.Froms) { abortRequest(u, client.ClientUser); } foreach (CSUser u in to.Froms) { abortRequest(u, to.ClientUser); } client.Froms.Clear(); to.Froms.Clear(); beginGame((ServerThread)client, to); } } else if (msg.get(MessageType.ABORT) != null) { if (user.Equals(client.To)) { client.To = null; to.Froms.Remove(client.ClientUser); } else { to.To = null; client.Froms.Remove(user); } msg.add(MessageType.USER, client.ClientUser); to.send(msg); } } else { msg.clear(); msg.Type = MessageType.CHALLENGING; msg.add(MessageType.ABORT, true).add(MessageType.USER, user); client.To = null; client.Froms.Remove(user); client.send(msg); } } catch (Exception e) { log(Severiry.ERROR, "Controller[PARTY] - " + e.Message); } }
protected override void loginHandler(UserThread client, CSMessage msg) { try { CSUser user = (CSUser)msg.get(MessageType.USER); CSUser tempUser = new CSUser(client.ClientUser.Login); bool error = true; if (msg.get(MessageType.REGISTRATION) != null) { msg.clear(); if (manager.exist(user.Login)) { msg.add(MessageType.STATE, false).add(MessageType.ERROR, "Login '" + user.Login + "' already exists !"); } else { manager.create(user); if (view != null && view.isOpened()) { view.update(client.ClientUser.Login, user); } client.ClientUser.Login = user.Login; msg.add(MessageType.STATE, true).add(MessageType.USER, client.ClientUser); error = false; } } else if (findClient(user) == null) { msg.clear(); if ((user = manager.find(user)) != null) { client.ClientUser.Points = user.Points; client.ClientUser.NbParties = user.NbParties; if (view != null && view.isOpened()) { view.update(client.ClientUser.Login, user); } client.ClientUser.Login = user.Login; msg.add(MessageType.STATE, true).add(MessageType.USER, client.ClientUser); error = false; } else { msg.add(MessageType.STATE, false).add(MessageType.ERROR, "Incorrect login or password !"); } } else { msg.add(MessageType.STATE, false).add(MessageType.ERROR, "Login already connected !"); } if (!error) { abortRequest(client, tempUser); foreach (CSUser u in client.Froms) { abortRequest(u, tempUser); } client.To = null; client.Froms.Clear(); CSMessage clearmsg = new CSMessage(MessageType.CLEAR); client.send(clearmsg); } client.send(msg); if (!error) { broadcast(); } } catch (Exception e) { log(Severiry.ERROR, "Manager[" + manager + "] - " + e.Message); } }