aboutsummaryrefslogtreecommitdiff
path: root/analyzer/render.c
blob: e888b492b76c1796113b81ffdd77f1c39746bfab (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "render.h"

#include <stdio.h>
#include <string.h>

// strlen counting utf-8 characters as one
size_t utf8_strlen(char *s) {
    size_t len = 0;
    while (*s) {
        // if the byte is not a continuation byte (10xxxxxx), it's a new character
        if ((*s & 0xC0) != 0x80)
            len++;
        s++;
    }
    return len;
}

void print_row_separator() {
    for (size_t i = 0; i < TABLE_LINE_WIDTH; i++)
        printf("%c", TABLE_ROW_SEPARATOR_CHAR);
    printf("\n");
}

// print string with given color
void print_color(char *str, int color) {
    printf("\033[%dm%s\033[0m", color, str);
}

void print_center(char *text, int color, size_t width, char padding_char) {
    size_t post_char_count = (width - strlen(text)) / 2;
    size_t pre_char_count = width - post_char_count - strlen(text);

    for (size_t i = 0; i < pre_char_count; i++)
        printf("%c", padding_char);

    print_color(text, color);

    for (size_t i = 0; i < post_char_count; i++)
        printf("%c", padding_char);
}

void print_enemy_list(DynArray enemies) {
    printf(TABLE_LINE_START);
    size_t line_length = TABLE_LINE_START_WIDTH;

    EnemyResults *enemy = NULL;
    while ((enemy = array_next(enemies, enemy))) {
        char enemy_buffer[TABLE_LINE_WIDTH + 1];
        sprintf(
            enemy_buffer,
            "%s (%d)%s",
            enemy->name,
            enemy->level,
            array_next(enemies, enemy) ? ", " : ""
        );

        // break line if length would be too big
        if (line_length + utf8_strlen(enemy_buffer) > TABLE_LINE_WIDTH - TABLE_LINE_END_WIDTH) {
            printf("%*s\n", (int)(TABLE_LINE_WIDTH - line_length), TABLE_LINE_END);
            printf(TABLE_LINE_START);
            line_length = TABLE_LINE_START_WIDTH;
        }

        printf("%s", enemy_buffer);
        line_length += utf8_strlen(enemy_buffer);
    }

    printf("%*s\n", (int)(TABLE_LINE_WIDTH - line_length), TABLE_LINE_END);
}

void print_column_names() {
    printf(TABLE_LINE_START);
    for (size_t i = 0; i < ARR_LEN(columns); i++) {
        print_center(columns[i].name, columns[i].color, columns[i].width, ' ');
        printf(i < ARR_LEN(columns) - 1 ? TABLE_LINE_SEPARATOR : TABLE_LINE_END);
    }
    printf("\n");
}

int item_type_to_color(ItemType type) {
    switch (type) {
        case NORMAL:
            return COLOR_WHITE;
        case DRIF:
            return COLOR_YELLOW;
        case ORB:
            return COLOR_GOLD;
        case SYNG:
            return COLOR_RED;
        case SET:
            return COLOR_GREEN;
        case RARE:
            return COLOR_AQUA;
        case EPIC:
            return COLOR_PURPLE;
        default:
            return COLOR_WHITE;
    }
}

void print_player_rewards(FriendlyResults results) {
    char buffers[ARR_LEN(columns)][TABLE_LINE_WIDTH];

    sprintf(buffers[0], "%s (%d)", results.name, results.level);
    sprintf(buffers[1], "%d", results.exp);
    sprintf(buffers[2], "%d", results.gold);

    if (results.psycho > 0)
        sprintf(buffers[3], "%d", results.psycho);
    else
        sprintf(buffers[3], "-");

    if (results.saturation > 0)
        sprintf(buffers[4], "%d (%s)", results.saturation, results.saturation_type);
    else
        sprintf(buffers[4], "-");

    if (results.splinters > 0)
        sprintf(buffers[5], "%d", results.splinters);
    else
        sprintf(buffers[5], "-");

    printf(TABLE_LINE_START);
    for (size_t i = 0; i < ARR_LEN(columns); i++) {
        char to_color[columns[i].width + 1];
        sprintf(to_color, "%*s", (int)columns[i].width, buffers[i]);
        print_color(to_color, columns[i].color);
        printf(i < ARR_LEN(columns) - 1 ? TABLE_LINE_SEPARATOR : TABLE_LINE_END);
    }
    printf("\n");

    if (results.items.count > 0) {
        printf(TABLE_LINE_START);
        print_center("ITEMS", COLOR_WHITE, TABLE_ITEMS_WIDTH, ' ');
        printf(TABLE_LINE_END "\n" TABLE_LINE_START);

        size_t line_len = 0;
        Item *item = NULL;
        while ((item = array_next(results.items, item))) {
            char *sep = array_next(results.items, item) ? ", " : "";
            size_t item_len = utf8_strlen(item->data) + strlen(sep);
            if (line_len + item_len > TABLE_ITEMS_WIDTH) {
                printf(
                    "%*s" TABLE_LINE_END "\n" TABLE_LINE_START,
                    (int)(TABLE_ITEMS_WIDTH - line_len),
                    ""
                );
                line_len = 0;
            }
            print_color(item->data, item_type_to_color(item->type));
            printf("%s", sep);
            line_len += item_len;
        }
        printf("%*s" TABLE_LINE_END "\n", (int)(TABLE_ITEMS_WIDTH - line_len), "");
    }
}

void render_fight_results(FightResults results) {
    print_center("ENEMIES", COLOR_RED, TABLE_LINE_WIDTH, TABLE_ROW_SEPARATOR_CHAR);
    printf("\n");

    print_enemy_list(results.enemy_results);

    print_center("PLAYER REWARDS", COLOR_GREEN, TABLE_LINE_WIDTH, TABLE_ROW_SEPARATOR_CHAR);
    printf("\n");

    print_column_names();
    print_row_separator();

    FriendlyResults *friendly = NULL;
    while ((friendly = array_next(results.friendly_results, friendly))) {
        print_player_rewards(*friendly);
        print_row_separator();
    }
}

// TODO: improve this ugly shit
void render_total_rewards(TotalRewards rewards) {
    printf("\n");
    print_center("TOTAL REWARDS", COLOR_GREEN, TABLE_LINE_WIDTH, TABLE_ROW_SEPARATOR_CHAR);
    printf("\n");

    print_column_names();
    print_row_separator();

    char buffers[ARR_LEN(columns)][TABLE_LINE_WIDTH];
    sprintf(buffers[0], "---");
    sprintf(buffers[1], "%d", rewards.exp);
    sprintf(buffers[2], "%d", rewards.gold);
    sprintf(buffers[3], "%d", rewards.psycho);
    int *sat = hashmap_get(rewards.saturations, "Yantrei");
    sprintf(buffers[4], "%d Yan", sat ? *sat : 0);
    sprintf(buffers[5], "---");

    printf(TABLE_LINE_START);
    for (size_t i = 0; i < ARR_LEN(columns); i++) {
        char to_color[columns[i].width + 1];
        sprintf(to_color, "%*s", (int)columns[i].width, buffers[i]);
        print_color(to_color, columns[i].color);
        printf(i < ARR_LEN(columns) - 1 ? TABLE_LINE_SEPARATOR : TABLE_LINE_END);
    }
    printf("\n");

    for (size_t i = 0; i < MAX_ITEM_TYPE; i++) {
        if (rewards.items[i].count > 0) {
            printf(TABLE_LINE_START);
            print_center("ITEMS", COLOR_WHITE, TABLE_ITEMS_WIDTH, ' ');
            printf(TABLE_LINE_END "\n" TABLE_LINE_START);

            size_t line_len = 0;
            char **item = NULL;
            while ((item = array_next(rewards.items[i], item))) {
                char *sep = array_next(rewards.items[i], item) ? ", " : "";
                size_t item_len = utf8_strlen(*item) + strlen(sep);
                if (line_len + item_len > TABLE_ITEMS_WIDTH) {
                    printf(
                        "%*s" TABLE_LINE_END "\n" TABLE_LINE_START,
                        (int)(TABLE_ITEMS_WIDTH - line_len),
                        ""
                    );
                    line_len = 0;
                }
                print_color(*item, item_type_to_color(i));
                printf("%s", sep);
                line_len += item_len;
            }
            printf("%*s" TABLE_LINE_END "\n", (int)(TABLE_ITEMS_WIDTH - line_len), "");
        }
    }
    print_row_separator();
}