summaryrefslogtreecommitdiffstats
path: root/src/client.cpp
blob: be2424b8660743646bec2b592ebebc0ecb527fd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <SDL.h>
#include <SDL_net.h>
#include <stdlib.h>
#include <iostream>
#include "defines.h"
#include "messageout.h"

int main(int argc, char *argv[])
{
    // Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
        printf("SDL_Init: %s\n", SDL_GetError());
        exit(1);
    }

    // Set SDL to quit on exit
    atexit(SDL_Quit);

    // Initialize SDL_net
    if (SDLNet_Init() == -1) {
        printf("SDLNet_Init: %s\n", SDLNet_GetError());
        exit(2);
    }

    // Try to connect to server
    IPaddress ip;
    TCPsocket tcpsock;

    if (SDLNet_ResolveHost(&ip, "localhost", 9601) == -1) {
        printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
        exit(1);
    }

    tcpsock = SDLNet_TCP_Open(&ip);
    if (!tcpsock) {
        printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
        exit(2);
    }

    printf("Successfully connected!\n");

    int answer = 1;
    char line[256] = "";

    while (answer != 0)
    {
        bool responseRequired = true;
        MessageOut msg;

        printf ("0) Quit\n");
        printf ("1) Register\n");
        printf ("2) Login\n");
        printf ("3) Chat\n");
        printf ("4) Create character\n");
        printf ("5) Character selection\n");
        printf ("6) Move character\n");
        printf ("7) Equip item\n");
        printf ("Choose your option: ");
        std::cin >> answer;

        switch (answer) {
            case 1:
                // Register
                msg.writeShort(CMSG_REGISTER);
                printf("Account name: ", line);
                std::cin >> line;
                msg.writeString(line);
                printf("Password: ", line);
                std::cin >> line;
                msg.writeString(line);
                printf("Email address: ", line);
                std::cin >> line;
                msg.writeString(line);
                break;

            case 2:
                // Login
                msg.writeShort(CMSG_LOGIN);
                printf("Account name: ", line);
                std::cin >> line;
                msg.writeString(line);
                printf("Password: ", line);
                std::cin >> line;
                msg.writeString(line);
                break;

            case 3:
                // Chat
                msg.writeShort(CMSG_SAY);
                printf("\nChat: ", line);
                std::cin >> line;
                msg.writeString(line);
                msg.writeShort(0);
                responseRequired = false;
                break;

            case 4:
            {
                // Create character
                msg.writeShort(CMSG_CHAR_CREATE);
                printf("\nName: ");
                std::cin >> line;
                msg.writeString(line);
                msg.writeByte(0);
            } break;

            case 5:
            {
                // Select character
                msg.writeShort(CMSG_CHAR_SELECT);
                printf("\nCharacter ID: ");
                std::cin >> line;
                msg.writeByte(atoi(line));
            } break;

            case 6:
            {
                // Move character
                long x, y;
                std::cout << "X: ";
                std::cin >> x;
                std::cout << "Y: ";
                std::cin >> y;

                msg.writeShort(CMSG_WALK);
                msg.writeLong(x);
                msg.writeLong(y);

                responseRequired = false;
            } break;

            case 7:
            {
                // Equip
                unsigned int itemId;
                unsigned int slot;
                std::cout << "Item ID: ";
                std::cin >> itemId;
                std::cout << "Slot: ";
                std::cin >> slot;
                msg.writeShort(CMSG_EQUIP);
                msg.writeLong(itemId);
                msg.writeByte(slot);
            } break;

            default:
                continue;
        }
        printf("\n");
        
        // Message hex
        for (unsigned int i = 0; i < msg.getPacket()->length; i++) {
            printf("%x ", msg.getPacket()->data[i]);
        }
        printf("\n\n");

        SDLNet_TCP_Send(tcpsock, msg.getPacket()->data,
                        msg.getPacket()->length);

        if (responseRequired) {
            char data[1024];
            int recvLength = SDLNet_TCP_Recv(tcpsock, data, 1024);
            printf("Received:\n");
            if (recvLength != -1) {
                for (unsigned int i = 0; i < recvLength; i++) {
                    printf("%x ", data[i]);
                }
            } else {
                printf("ERROR!");
            }
            printf("\n\n");
        }
    }
    
    SDLNet_TCP_Close(tcpsock);

    return 0;
}