<commit_before>"use strict";

var ServerActionCreators = require("../actions/ServerActionCreators.react.js");
var request = require("superagent");
var APIRoot = require("../constants/CKPTConstants").API_ROOT;

var _endpoints = {
  players: APIRoot + "/players",
  login: APIRoot + "/login",
  locations: APIRoot + "/locations",
  news: APIRoot + "/news",
  tournaments: APIRoot + "/tournaments",
  seasons: APIRoot + "/seasons"
};

var _dummytasks = ["foo"];

module.exports = {

  loadTasks: function() {
    // var uuid = sessionStorage.getItem("uuid");
    var json = JSON.stringify(_dummytasks);
    ServerActionCreators.receiveTasks(json, null);
  },

  loadPlayers: function() {

    request.get(_endpoints.players)
      .set("Authorization", "CKPT " + sessionStorage.getItem("accessToken"))
      .accept("json")
      .end(function(err, res) {
        if (err) { throw err; }
        var json = JSON.stringify(res.body);
        ServerActionCreators.receivePlayers(json, null);
      });

    request.get(_endpoints.players + "/quotes")
      .set("Authorization", "CKPT " + sessionStorage.getItem("accessToken"))
      .accept("json")
      .end(function(err, res) {
        if (err) { throw err; }
        var json = JSON.stringify(res.body);
        ServerActionCreators.receiveQuotes(json, null);
      });
  },

  savePlayerProfile: function(uuid, profile) {

    request.put(_endpoints.players + "/" + uuid + "/profile")
      .set("Authorization", "CKPT " + sessionStorage.getItem("accessToken"))
      .accept("json")
      .send(profile)
      .end(function(err, res) {
        if (err) { throw err; }
        // TODO: Propagate error action? This should probably result in another action fetching all players again, if the store cannot rollback on its own. Also, spawn a notification via some generic error propagation in the UI.
      });
  },

  saveUserSettings: function(uuid, settings, password) {

    if (password) {
      request.put(_endpoints.players + "/" + uuid + "/user/password")
        .set("Authorization", "CKPT " + sessionStorage.getItem("accessToken"))
        .accept("json")
        .send({"password": password})
        .end(function(err, res) {
          if (err) { throw err; }
          // TODO: Propagate error action? This should probably result in another action fetching all players again, if the store cannot rollback on its own. Also, spawn a notification via some generic error propagation in the UI.
          // TODO: Propagate action USER_SETTINGS_SAVED on success, which can be used by notification store.
      });
    }

  },

  addDebt: function(creditor, debitor, amount, reason) {
    var debt = {creditor: creditor, amount: amount, description: reason};
    request.post(_endpoints.players + "/" + debitor + "/debts")
      .set("Authorization", "CKPT " + sessionStorage.getItem("accessToken"))
      .accept("json")
      .send(debt)
      .end(function(err, res) {
        ServerActionCreators.addDebt(debt, err);
        // TODO: Propagate error action? This should probably result in another action fetching all players again, if the store cannot rollback on its own. Also, spawn a notification via some generic error propagation in the UI.
        // TODO: Propagate HOST_SAVED on success, which can be used by some store.
      });
  },

  settleDebt: function(uuid, debitor) {

    request.del(_endpoints.players + "/" + debitor + "/debts/" + uuid)
      .set("Authorization", "CKPT " + sessionStorage.getItem("accessToken"))
      .accept("json")
      .end(function(err, res) {
        ServerActionCreators.settleDebt(err);
        // TODO: Propagate error action? This should probably result in another action fetching all players again, if the store cannot rollback on its own. Also, spawn a notification via some generic error propagation in the UI.
        // TODO: Propagate action PLAYER_DEBT_DELETED on success, which can be used by notification store.<commit_msg>Reomove completed TODO comments<commit_after>"use strict";

var ServerActionCreators = require("../actions/ServerActionCreators.react.js");
var request = require("superagent");
var APIRoot = require("../constants/CKPTConstants").API_ROOT;

var _endpoints = {
  players: APIRoot + "/players",
  login: APIRoot + "/login",
  locations: APIRoot + "/locations",
  news: APIRoot + "/news",
  tournaments: APIRoot + "/tournaments",
  seasons: APIRoot + "/seasons"
};

var _dummytasks = ["foo"];

module.exports = {

  loadTasks: function() {
    // var uuid = sessionStorage.getItem("uuid");
    var json = JSON.stringify(_dummytasks);
    ServerActionCreators.receiveTasks(json, null);
  },

  loadPlayers: function() {

    request.get(_endpoints.players)
      .set("Authorization", "CKPT " + sessionStorage.getItem("accessToken"))
      .accept("json")
      .end(function(err, res) {
        if (err) { throw err; }
        var json = JSON.stringify(res.body);
        ServerActionCreators.receivePlayers(json, null);
      });

    request.get(_endpoints.players + "/quotes")
      .set("Authorization", "CKPT " + sessionStorage.getItem("accessToken"))
      .accept("json")
      .end(function(err, res) {
        if (err) { throw err; }
        var json = JSON.stringify(res.body);
        ServerActionCreators.receiveQuotes(json, null);
      });
  },

  savePlayerProfile: function(uuid, profile) {

    request.put(_endpoints.players + "/" + uuid + "/profile")
      .set("Authorization", "CKPT " + sessionStorage.getItem("accessToken"))
      .accept("json")
      .send(profile)
      .end(function(err, res) {
        if (err) { throw err; }
        // TODO: Propagate error action? This should probably result in another action fetching all players again, if the store cannot rollback on its own. Also, spawn a notification via some generic error propagation in the UI.
      });
  },

  saveUserSettings: function(uuid, settings, password) {

    if (password) {
      request.put(_endpoints.players + "/" + uuid + "/user/password")
        .set("Authorization", "CKPT " + sessionStorage.getItem("accessToken"))
        .accept("json")
        .send({"password": password})
        .end(function(err, res) {
          if (err) { throw err; }
          // TODO: Propagate error action? This should probably result in another action fetching all players again, if the store cannot rollback on its own. Also, spawn a notification via some generic error propagation in the UI.
          // TODO: Propagate action USER_SETTINGS_SAVED on success, which can be used by notification store.
      });
    }

  },

  addDebt: function(creditor, debitor, amount, reason) {
    var debt = {creditor: creditor, amount: amount, description: reason};
    request.post(_endpoints.players + "/" + debitor + "/debts")
      .set("Authorization", "CKPT " + sessionStorage.getItem("accessToken"))
      .accept("json")
      .send(debt)
      .end(function(err, res) {
        ServerActionCreators.addDebt(debt, err);
      });
  },

  settleDebt: function(uuid, debitor) {

    request.del(_endpoints.players + "/" + debitor + "/debts/" + uuid)
      .set("Authorization", "CKPT " + sessionStorage.getItem("accessToken"))
      .accept("json")
      .end(function(err, res) {
        ServerActionCreators.settleDebt(err);
{'fraction_non_alphanumeric': 0.13427074717397297, 'fraction_numerical': 0.0, 'mean_word_length': 3.191218948584633, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6571553', 'n_tokens_mistral': 2123, 'n_tokens_neox': 2128, 'n_words': 583}