Skip to content

Instantly share code, notes, and snippets.

@Dugy

Dugy/generate_updates.cpp Secret

Created Nov 22, 2020
Embed
What would you like to do?
//usr/bin/g++ $0 -o ${o=`mktemp`} && exec $o $*
#include <iostream>
#include <fstream>
#include <string>
#include <array>
#include <memory>
#include <chrono>
#include <cstring>
unsigned int now() {
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
std::array<char, 16> generateName() {
static const std::array<std::string, 6> names = { "Hashashash", "Karshash", "Dagragar", "Kavarash", "Erzagar", "Gambas" };
std::string name = names[rand() % names.size()];
name.push_back('1' + (rand() % 5));
std::array<char, 16> generated;
memset(&generated[0], '\0', 16);
memcpy(&generated[0], name.c_str(), name.size());
return generated;
}
enum MessageIdentifier : char {
LOCATION_UPDATE = 1,
LIFE_UPDATE = 2,
LAST_ATTACK_UPDATE = 3
};
struct LocationUpdate {
static constexpr MessageIdentifier id = LOCATION_UPDATE;
std::array<char, 16> name = generateName();
int x = rand() % 128 - 64;
int y = rand() % 128 - 64;
};
struct LifeUpdate {
static constexpr MessageIdentifier id = LIFE_UPDATE;
std::array<char, 16> name = generateName();
int newLife = rand() % 16;
};
struct LastAttackUpdate {
static constexpr MessageIdentifier id = LAST_ATTACK_UPDATE;
std::array<char, 16> name = generateName();
unsigned int lastAttackTime = now();
};
template <typename T>
void streamOut(std::ostream& out, const T& streamed) {
out.put(T::id);
for (unsigned int i = 0; i < sizeof(T); i++) {
out.put(reinterpret_cast<const char*>(&streamed)[i]);
}
}
int main(int argc, char** argv)
{
if (argc != 2) {
std::cout << "Use " << argv[0] << " BINARY_FILE_NAME" << std::endl;
return 1;
}
std::ofstream file(argv[1]);
srand(time(nullptr));
for (int i = 0; i < 1 << 12; i++) {
switch(rand() % 3) {
case 0: streamOut(file, LocationUpdate()); break;
case 1: streamOut(file, LifeUpdate()); break;
case 2: streamOut(file, LastAttackUpdate()); break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment