about summary refs log tree commit diff stats
path: root/lunatic.cpp
blob: 5dd198c3505ad1aed7999ce5d6b42f0b9edbf635 (plain) (blame)
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
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <random>
#include <fstream>
#include <twitter.h>
#include <chrono>
#include <thread>
#include <Magick++.h>
#include <list>
#include <string>
#include <set>
#include <sstream>
#include "database.h"

template <class Container>
Container split(std::string input, std::string delimiter)
{
  Container result;

  while (!input.empty())
  {
    int divider = input.find(delimiter);
    if (divider == std::string::npos)
    {
      result.push_back(input);

      input = "";
    } else {
      result.push_back(input.substr(0, divider));

      input = input.substr(divider+delimiter.length());
    }
  }

  return result;
}

int main(int argc, char** argv)
{
  if (argc != 2)
  {
    std::cout << "usage: lunatic [configfile]" << std::endl;
    return -1;
  }

  Magick::InitializeMagick(nullptr);

  std::string configfile(argv[1]);
  YAML::Node config = YAML::LoadFile(configfile);

  twitter::auth auth;
  auth.setConsumerKey(config["consumer_key"].as<std::string>());
  auth.setConsumerSecret(config["consumer_secret"].as<std::string>());
  auth.setAccessKey(config["access_key"].as<std::string>());
  auth.setAccessSecret(config["access_secret"].as<std::string>());

  //twitter::client client(auth);

  database db(config["database"].as<std::string>());

  std::random_device randomDevice;
  std::mt19937 rng(randomDevice());

  std::set<std::string> blacklist;
  if (config["blacklist"].IsScalar())
  {
    std::ifstream blfile(config["blacklist"].as<std::string>());

    if (!blfile.is_open())
    {
      throw std::invalid_argument("Could not find blacklist");
    }

    std::string line;
    while (getline(blfile, line))
    {
      if (line.back() == '\r')
      {
        line.pop_back();
      }

      blacklist.insert(line);
    }
  }

  for (;;)
  {
    std::cout << "Generating tweet" << std::endl;

    achievement ach = db.getRandomAchievement();

    if (blacklist.count(ach.title))
    {
      continue;
    }

    Magick::Image moonColor;
    moonColor.read("res/" + ach.color + ".png");

    try
    {
      // Start with the Odyssey text overlay
      Magick::Image overlay;
      overlay.read("res/overlay.png");

      // Add the moon image
      overlay.composite(moonColor, 672, 85, Magick::OverCompositeOp);

      // Add the name of the achievement
      overlay.fontPointsize(54);
      overlay.fillColor("white");
      overlay.font("@" + config["title_font"].as<std::string>());

      std::list<std::string> words = split<std::list<std::string>>(
        ach.title,
        " ");
      std::ostringstream wrappingStream;
      std::string curline;
      int lines = 1;

      Magick::TypeMetric metric;
      while (!words.empty())
      {
        std::string temp = curline;

        if (!curline.empty())
        {
          temp += " ";
        }

        temp += words.front();

        overlay.fontTypeMetrics(temp, &metric);

        if (metric.textWidth() > 1200)
        {
          wrappingStream << std::endl;
          curline = words.front();

          lines++;
        } else {
          if (!curline.empty())
          {
            wrappingStream << " ";
          }

          curline = temp;
        }

        wrappingStream << words.front();
        words.pop_front();
      }

      std::string wrapped = wrappingStream.str();

      overlay.annotate(
        wrapped,
        Magick::Geometry(1600, 228, 0, 710),
        Magick::GravityType::NorthGravity);

      // Add the achievement date
      did theDid = db.getRandomDidForAchievement(ach.achievementId);

      overlay.fontTypeMetrics(wrapped, &metric);

      overlay.fontPointsize(20);
      overlay.font("@" + config["date_font"].as<std::string>());
      overlay.annotate(
        theDid.date,
        Magick::Geometry(1600, 228, 0, 710 + metric.textHeight() * lines - 22),
        Magick::GravityType::NorthGravity);

      // Make a shadow copy
      Magick::Image shadow(overlay);
      shadow.negate();
      shadow.blur(0, 12);

      // Read the game image, using a default if the game has no images
      Magick::Image image;

      if (db.doesGameHaveImages(ach.gameId))
      {
        std::string imageName = db.getRandomImageForGame(ach.gameId);
        std::string imagePath = config["images"].as<std::string>()
          + "/" + imageName;

        image.read(imagePath);

        // Stretch and pixelate it
        image.transform("1600x900!");
        image.scale("80x45");
        image.scale("1600x900");
      } else {
        image.read("res/default.png");
        image.transform("1600x900!");
      }

      // Add the generated overlay to it
      image.composite(shadow, 0, 0, Magick::OverCompositeOp);
      image.composite(overlay, 0, 0, Magick::OverCompositeOp);

      // Output for debug
      image.magick("png");
      image.write("output.png");
    } catch (const Magick::WarningCoder& ex)
    {
      // Ok
    }

    std::string header = "YOU GOT A MOON!";

    std::string action = header + "\n" + ach.title;
    action.resize(140);

    /*try
    {
      client.updateStatus(action);
    } catch (const twitter::twitter_error& e)
    {
      std::cout << "Twitter error: " << e.what() << std::endl;
    }*/

    std::cout << action << std::endl;
    std::cout << "Waiting" << std::endl;

    std::this_thread::sleep_for(std::chrono::hours(1));

    std::cout << std::endl;
  }
}