Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. /*
  2. ** EPITECH PROJECT, 2018
  3. ** PSU_zappy_2017
  4. ** File description:
  5. ** incantation_command.c
  6. */
  7.  
  8. #include "server_reply.h"
  9. #include "incantation_command.h"
  10.  
  11. static const elevation_t elevation_g[9];
  12.  
  13. bool cell_ctn(unsigned int level, cell_t cell)
  14. {
  15. if (cell.items[LINEMATE] >= elevation_g[level].item_required[LINEMATE]
  16. && cell.items[DERAUMERE] >= elevation_g[level].item_required[DERAUMERE]
  17. && cell.items[SIBUR] >= elevation_g[level].item_required[SIBUR]
  18. && cell.items[MENDIANE] >= elevation_g[level].item_required[MENDIANE]
  19. && cell.items[PHIRAS] >= elevation_g[level].item_required[PHIRAS]
  20. && cell.items[THYSTAME] >= elevation_g[level].item_required[THYSTAME]) {
  21. return (true);
  22. }
  23. return (false);
  24. }
  25.  
  26. unsigned int count_client_on_cell(position_t pos, zappy_server_t *srv)
  27. {
  28. unsigned int i = 0;
  29. unsigned int count = 0;
  30.  
  31. while (i < srv->db_size) {
  32. if (srv->client[i]->used
  33. && srv->client[i]->logged
  34. && srv->client[i]->infos->pos.y == pos.y
  35. && srv->client[i]->infos->pos.x == pos.x) {
  36. ++count;
  37. }
  38. ++i;
  39. }
  40. return (count);
  41. }
  42.  
  43. int incant_all_cli(zappy_server_t *srv, client_t *cli, const char *info)
  44. {
  45. for (unsigned int i = 1; i < srv->db_size; ++i) {
  46. if (srv->client[i]->used && srv->client[i]->logged
  47. && srv->client[i]->infos->pos.x == cli->infos->pos.x
  48. && srv->client[i]->infos->pos.y == cli->infos->pos.y
  49. && cli->infos->level == srv->client[i]->infos->level) {
  50. srv->client[i]->wait_time = cli->wait_time;
  51. c_queue_push_reply(srv->reply_queue,
  52. create_reply(srv->client[i]->socket,
  53. "Elevation underway\n", 0, no_arg_g));
  54. c_queue_push_reply(srv->reply_queue,
  55. create_reply(srv->client[i]->socket, info,
  56. srv->client[i]->wait_time, (reply_options_t){0, 1, 0}));
  57. }
  58. }
  59. return (0);
  60. }
  61.  
  62. int incantation_command(__attribute__((unused))
  63. char *cmd, zappy_server_t *srv, client_t *cli)
  64. {
  65. char *info = malloc(512);
  66.  
  67. if (!info)
  68. return (-1);
  69. snprintf(info, 512, "Current level: %d\n", cli->infos->level + 1);
  70. if (count_client_on_cell(cli->infos->pos, srv)
  71. >= elevation_g[cli->infos->level].player_count &&
  72. cell_ctn(cli->infos->level,
  73. srv->game->map->map[cli->infos->pos.y][cli->infos->pos.x])) {
  74. incant_all_cli(srv, cli, info);
  75. }
  76. else
  77. c_queue_push_reply(srv->reply_queue, create_reply(cli->socket,
  78. "ko\n", 0, no_arg_g));
  79. free(info);
  80. return (0);
  81. }
  82.  
  83. static const elevation_t elevation_g[9] = {
  84. {0, {0, 0, 0, 0, 0, 0, 0}},
  85. {1, {0, 1, 0, 0, 0, 0, 0}},
  86. {2, {0, 1, 1, 1, 0, 0, 0}},
  87. {2, {0, 2, 0, 1, 0, 2, 0}},
  88. {4, {0, 1, 1, 2, 0, 1, 0}},
  89. {4, {0, 1, 2, 1, 3, 0, 0}},
  90. {6, {0, 1, 2, 3, 0, 1, 0}},
  91. {6, {0, 2, 2, 2, 2, 2, 1}},
  92. {99999, {0, 0, 0, 0, 0, 0, 0}}
  93. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement