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// hooks
45
var client;
46
var canvasse;
47
var jQuery;
48
49
var currentDrawPosition = babaevski.width*babaevski.height / 2;
50
var currentLoop = 0;
51
52
r.placeModule("babaevski", function(e){
53
  client = e("client");
54
  canvasse = e("canvasse");
55
  jQuery = e("jQuery");
56
 
57
  for(var i=0; i<client.palette.length; i++){
58
    colorsABGR[i] = client.getPaletteColorABGR(i);
59
  }
60
  
61
  attempt();
62
 
63
});
64
 
65
function attempt(){
66
  console.log("tried to draw");  
67
  var toWait = client.getCooldownTimeRemaining();
68
  if(toWait === 0)
69
  {
70
    tryDrawPixel();
71
  }
72
  else
73
  {
74
    setTimeout(attempt, toWait + Math.round(Math.random() * 1500));
75
    console.log("set a timeout");
76
  }
77
}
78
79
function tryDrawPixel()
80
{
81
  var toWait = client.getCooldownTimeRemaining();
82
  if(toWait === 0 && currentLoop < babaevski.width*babaevski.height)
83
  {
84
    currentLoop++;
85
    console.log("checking pixel " + currentDrawPosition);
86
    var targetPoint = getPoint(currentDrawPosition);
87
    
88
    $.get( "/api/place/pixel.json", { x:targetPoint.x, y:targetPoint.y }, function( data ) 
89
    {
90
      setTimeout(function() {
91
        //wait an extra half second for reddits servers
92
        
93
      
94
        console.log("x" + targetPoint.x + " y"+ targetPoint.y);
95
        if (data.color == undefined)
96
          data.color = 0;
97
        canvasse.drawTileAt(data.x, data.y, colorsABGR[data.color]);
98
        if (colors[currentDrawPosition] != data.color && colors[currentDrawPosition] !== -1)
99
        {
100
          client.setColor(colors[currentDrawPosition]);
101
          client.drawTile(targetPoint.x, targetPoint.y);
102
          console.log("drew pixel at " + targetPoint.x + "/" + targetPoint.y);
103
          var toWait = client.getCooldownTimeRemaining();
104
          setTimeout(attempt, toWait + Math.round(Math.random() * 1500));
105
          currentLoop = 0;
106
          console.log("set a timeout");
107
        }
108
        else
109
        {
110
          var positionIncrease = Math.floor(Math.random() * (Math.min(0.2*currentLoop, 40))) + 1;
111
          currentDrawPosition += positionIncrease;
112
          //console.log("" + currentLoop + ", " +positionIncrease);
113
          if (currentDrawPosition >= babaevski.width*babaevski.height)
114
            currentDrawPosition = 0;
115
          tryDrawPixel(currentDrawPosition);
116
        }
117
      }, 300);
118
    });
119
  }
120
  else
121
  {
122
    var added_timeout = 0;
123
    if (currentLoop == babaevski.width*babaevski.height)
124
      added_timeout = perfect_timeout*1000;
125
    setTimeout(attempt, toWait + Math.round(Math.random() * 1500) + added_timeout);
126
    currentLoop = 0;
127
    console.log("set a timeout");
128
  }
129
  
130
}
131
 
132
function drawTestRTC(){
133
  for(var i=0; i < babaevski.width*babaevski.height; i++){
134
    if(colors[i] === -1){
135
      continue;
136
    }
137
    var targetPoint = getPoint(i);
138
    canvasse.drawTileAt(targetPoint.x, targetPoint.y, colorsABGR[colors[i]]);
139
  }
140
}
141
 
142
function getPoint(i){
143
  var x = i % babaevski.width;
144
  return {
145
    x: babaevski.x + x,
146
    y: babaevski.y + (i - x) / babaevski.width - babaevski.height
147
  };
148
}
149
 
150
function getPixel(x, y){
151
  return canvasse.writeBuffer[canvasse.getIndexFromCoords(x, y)];
152
}