|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include "conf.h" |
| 5 | +#include "statistics.h" |
| 6 | + |
| 7 | +statistics stats; |
| 8 | + |
| 9 | +static char *fields[6] = { |
| 10 | + "PlanetsDestroyed", |
| 11 | + "BunkersDestroyed", |
| 12 | + "Level", |
| 13 | + "PodsAcquired", |
| 14 | + "FuelAcquired", |
| 15 | + "FuelDestroyed" |
| 16 | +}; |
| 17 | + |
| 18 | +void |
| 19 | +clearstatgroup(statgroup *s) |
| 20 | +{ |
| 21 | + s->planetsdestroyed = 0; |
| 22 | + s->bunkersdestroyed = 0; |
| 23 | + s->level = 0; |
| 24 | + s->podsacquired = 0; |
| 25 | + s->fuelacquired = 0; |
| 26 | + s->fueldestroyed = 0; |
| 27 | +} |
| 28 | + |
| 29 | +void |
| 30 | +parsestatline(char *line, statgroup *s) |
| 31 | +{ |
| 32 | + char *separator = strstr(line, " = "); |
| 33 | + if (separator) { |
| 34 | + long val = atol(separator + 3); |
| 35 | + if ((strncmp(line, fields[0], strlen(fields[0])) == 0) && (strlen(fields[0]) == separator - line)) |
| 36 | + s->planetsdestroyed = val; |
| 37 | + else if ((strncmp(line, fields[1], strlen(fields[1])) == 0) && (strlen(fields[1]) == separator - line)) |
| 38 | + s->bunkersdestroyed = val; |
| 39 | + else if ((strncmp(line, fields[2], strlen(fields[2])) == 0) && (strlen(fields[2]) == separator - line)) |
| 40 | + s->level = val; |
| 41 | + else if ((strncmp(line, fields[3], strlen(fields[3])) == 0) && (strlen(fields[3]) == separator - line)) |
| 42 | + s->podsacquired = val; |
| 43 | + else if ((strncmp(line, fields[4], strlen(fields[4])) == 0) && (strlen(fields[4]) == separator - line)) |
| 44 | + s->fuelacquired = val; |
| 45 | + else if ((strncmp(line, fields[5], strlen(fields[5])) == 0) && (strlen(fields[5]) == separator - line)) |
| 46 | + s->fueldestroyed = val; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +void |
| 51 | +writestatgroup(FILE *f, statgroup *s) |
| 52 | +{ |
| 53 | + fprintf(f, "%s = %ld\n", fields[0], s->planetsdestroyed); |
| 54 | + fprintf(f, "%s = %ld\n", fields[1], s->bunkersdestroyed); |
| 55 | + fprintf(f, "%s = %ld\n", fields[2], s->level); |
| 56 | + fprintf(f, "%s = %ld\n", fields[3], s->podsacquired); |
| 57 | + fprintf(f, "%s = %ld\n", fields[4], s->fuelacquired); |
| 58 | + fprintf(f, "%s = %ld\n", fields[5], s->fueldestroyed); |
| 59 | +} |
| 60 | + |
| 61 | +void |
| 62 | +initstatistics() |
| 63 | +{ |
| 64 | + clearstatgroup(&stats.overall); |
| 65 | + clearstatgroup(&stats.best); |
| 66 | + clearstatgroup(&stats.current); |
| 67 | + char *filename = getuserfile("statistics"); |
| 68 | + FILE *f = fopen(filename, "rt"); |
| 69 | + if (f) { |
| 70 | + char line[256]; |
| 71 | + statgroup *s = NULL; |
| 72 | + while (!feof(f)) |
| 73 | + { |
| 74 | + fgets(line, 256, f); |
| 75 | + if (line[0] == '[') { |
| 76 | + if (strncmp("Overall]", line + 1, 8) == 0) |
| 77 | + s = &stats.overall; |
| 78 | + else if (strncmp("Best]", line + 1, 5) == 0) |
| 79 | + s = &stats.best; |
| 80 | + else |
| 81 | + s = NULL; |
| 82 | + } |
| 83 | + else if (s != NULL) |
| 84 | + parsestatline(line, s); |
| 85 | + } |
| 86 | + fclose(f); |
| 87 | + } |
| 88 | + free(filename); |
| 89 | +} |
| 90 | + |
| 91 | +void |
| 92 | +writestatistics() |
| 93 | +{ |
| 94 | + char *filename = getuserfile("statistics"); |
| 95 | + FILE *f = fopen(filename, "wt"); |
| 96 | + if (f) { |
| 97 | + fputs("[Overall]\n", f); |
| 98 | + writestatgroup(f, &stats.overall); |
| 99 | + fputs("\n[Best]\n", f); |
| 100 | + writestatgroup(f, &stats.best); |
| 101 | + fclose(f); |
| 102 | + } |
| 103 | + free(filename); |
| 104 | +} |
| 105 | + |
| 106 | +void |
| 107 | +clearcurrentstats() |
| 108 | +{ |
| 109 | + clearstatgroup(&stats.current); |
| 110 | +} |
| 111 | + |
| 112 | +void |
| 113 | +updatestatistics(long planets, long bunkers, long level, long pods, long fuelacquired, long fueldestroyed) |
| 114 | +{ |
| 115 | + stats.current.planetsdestroyed += planets; |
| 116 | + stats.current.bunkersdestroyed += bunkers; |
| 117 | + stats.current.level += level; |
| 118 | + stats.current.podsacquired += pods; |
| 119 | + stats.current.fuelacquired += fuelacquired; |
| 120 | + stats.current.fueldestroyed += fueldestroyed; |
| 121 | + stats.overall.planetsdestroyed += planets; |
| 122 | + stats.overall.bunkersdestroyed += bunkers; |
| 123 | + stats.overall.level += level; |
| 124 | + stats.overall.podsacquired += pods; |
| 125 | + stats.overall.fuelacquired += fuelacquired; |
| 126 | + stats.overall.fueldestroyed += fueldestroyed; |
| 127 | + if (stats.current.planetsdestroyed > stats.best.planetsdestroyed) |
| 128 | + stats.best.planetsdestroyed = stats.current.planetsdestroyed; |
| 129 | + if (stats.current.bunkersdestroyed > stats.best.bunkersdestroyed) |
| 130 | + stats.best.bunkersdestroyed = stats.current.bunkersdestroyed; |
| 131 | + if (stats.current.level > stats.best.level) |
| 132 | + stats.best.level = stats.current.level; |
| 133 | + if (stats.current.podsacquired > stats.best.podsacquired) |
| 134 | + stats.best.podsacquired = stats.current.podsacquired; |
| 135 | + if (stats.current.fuelacquired > stats.best.fuelacquired) |
| 136 | + stats.best.fuelacquired = stats.current.fuelacquired; |
| 137 | + if (stats.current.fueldestroyed > stats.best.fueldestroyed) |
| 138 | + stats.best.fueldestroyed = stats.current.fueldestroyed; |
| 139 | +} |
0 commit comments