Exemplo n.º 1
0
        private void handleReq(Socket handler, Dictionary <string, string> income)
        {
            switch (income["method"])
            {
            case "join":
                if (isGame)
                {
                    SendToClient(handler,
                                 builder.joinResp(1,
                                                  "Please wait,game is currently running")
                                 .build());

                    break;
                }

                ClientData client = new ClientData(
                    income["username"],
                    income["udp_address"],
                    short.Parse(income["port"]));

                if (addClient(client))
                {
                    SendToClient(handler,
                                 builder.joinResp(0,
                                                  client.getId())
                                 .build());
                }
                else
                {
                    SendToClient(handler,
                                 builder.joinResp(1,
                                                  "User exists")
                                 .build());
                }
                break;

            case "leave":
                try
                {
                    leaveGame(handler.RemoteEndPoint.ToString());

                    SendToClient(handler,
                                 builder.response(0)
                                 .build());
                } catch (SystemException e)
                {
                    string msg = e.Message;
                    Console.Write(msg);

                    SendToClient(handler,
                                 builder.response(1, msg)
                                 .build());
                }
                break;

            case "ready":
                isClientReady[
                    findByRemote(handler.RemoteEndPoint.ToString())
                    .getId()]
                    = true;

                SendToClient(handler,
                             builder.response(0,
                                              "Waiting for other player to start")
                             .build());

                break;

            case "client_address":
                SendToClient(handler,
                             builder.listClientResp(0,
                                                    "list of clients retrieved",
                                                    clients).build());
                break;

            case "accepted_proposal":
                kpuId = int.Parse(income["kpu_id"]);

                SendToClient(handler,
                             builder.response(0, "").build());
                break;

            case "vote_result":
            case "vote_result_civilian":
            case "vote_result_werewolf":
                if (income["kpu_id"].Equals("1"))
                {
                    killed = int.Parse(income["player_killed"]);
                }
                break;
            }
        }