Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 82 lines (66 sloc) 1.694 kb
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
#ifndef __JSON_RATED_HPP_
#define __JSON_RATED_HPP_

#include <string>
#include <assert.h>
#include <iostream>
#include <map>

#include "filegetter.hpp"
#include "readjson.hpp"

/*
This class maintains "ratings.json", the database of how the local
user has rated various games.
*/

class JsonRated
{
  typedef std::map<std::string,int> IntMap;
  IntMap ratings;

  /* Note: at first we tried using a Json::Value directly here, but
this crashed on exit. I suspect this is the fault of the jsoncpp
lib, as I've experienced other crash bugs with it in certain
(avoidable) corner cases.
*/

  void write()
  {
    Json::Value v;

    for(IntMap::iterator it = ratings.begin();
        it != ratings.end(); it++)
      {
        v[it->first] = it->second;
      }

    std::string filename = get.getPath("ratings.json");
    writeJson(filename, v);
  }

public:
  int getRating(const std::string &game)
  {
    int rate = -1;
    if(ratings.find(game) != ratings.end())
      rate = ratings[game];
    if(rate < -1 || rate > 5) rate = -1;
    return rate;
  }

  void setRating(const std::string &game, int rate)
  {
    assert(rate >= 0 && rate <= 5);
    ratings[game] = rate;
    write();
  }

  void read()
  {
    using namespace Json;

    std::string filename = get.getPath("ratings.json");

    Value v;

    try { v = readJson(filename); }
    // A missing file is normal, just ignore it.
    catch(...) {}

    // Convert json value into std::map
    Value::Members keys = v.getMemberNames();
    Value::Members::iterator it;
    for(it = keys.begin(); it != keys.end(); it++)
      {
        ratings[*it] = v[*it].asInt();
      }
  }
};

JsonRated ratings;
#endif
Something went wrong with that request. Please try again.