aboutsummaryrefslogtreecommitdiff
path: root/public/face.js
blob: d1e5431faa8a0cbc48faee6356b5ebbe7de2818f (plain)
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
jQuery(function($) {
  "use strict";

  var BASE_PRICE = 55.73;
  var CURRENT = 10;
  var DATA_URL = './current.json';

  function to_percent(ask) {
    return Math.round((ask - BASE_PRICE) / BASE_PRICE * 10000) / 100.0;
  }

  /**
   * get the current time, in milliseconds since the epoch.
   */
  function now() {
    return (new Date()).getTime();
  }

  function to_rect(pos) {
    return {
      x: 14 + 26 * pos.x,
      y: 13 + 31 * pos.y,
      w: 24,
      h: 29,
    };
  }

  function to_css(rect) {
    return {
      width: rect.w + 'px',
      height: rect.h + 'px',
      'background-position': [rect.x, rect.y].map(function(v) {
        return (-v) + 'px';
      }).join(' '),
    };
  }

  var FRAMES = [{
    // hurt0-c
    time: { min: 0, max: 250 },
    rate: { min: -5, max: 5 },
    pos:  { x: 1, y: 0 },
  }, {
    // hurt0-r
    time: { min: 250, max: 500 },
    rate: { min: -5, max: 5 },
    pos:  { x: 2, y: 0 },
  }, {
    // hurt0-c
    time: { min: 500, max: 750 },
    rate: { min: -5, max: 5 },
    pos:  { x: 1, y: 0 },
  }, {
    // hurt0-l
    time: { min: 750, max: 1000 },
    rate: { min: -5, max: 5 },
    pos:  { x: 0, y: 0 },
  }, {
    // hurt1-c
    time: { min: 0, max: 250 },
    rate: { min: -15, max: -5 },
    pos:  { x: 1, y: 1 },
  }, {
    // hurt1-r
    time: { min: 250, max: 500 },
    rate: { min: -15, max: -5 },
    pos:  { x: 2, y: 1 },
  }, {
    // hurt1-c
    time: { min: 500, max: 750 },
    rate: { min: -15, max: -5 },
    pos:  { x: 1, y: 1 },
  }, {
    // hurt1-l
    time: { min: 750, max: 1000 },
    rate: { min: -15, max: -5 },
    pos:  { x: 0, y: 1 },
  }, {
    // hurt2-c
    time: { min: 0, max: 250 },
    rate: { min: -30, max: -15 },
    pos:  { x: 1, y: 2 },
  }, {
    // hurt2-r
    time: { min: 250, max: 500 },
    rate: { min: -30, max: -15 },
    pos:  { x: 2, y: 2 },
  }, {
    // hurt2-c
    time: { min: 500, max: 750 },
    rate: { min: -30, max: -15 },
    pos:  { x: 1, y: 2 },
  }, {
    // hurt2-l
    time: { min: 750, max: 1000 },
    rate: { min: -30, max: -15 },
    pos:  { x: 0, y: 2 },
  }, {
    // hurt3-c
    time: { min: 0, max: 250 },
    rate: { min: -50, max: -30 },
    rect: { x: 14 + 26 * 1, y: 14 + 31 * 3, w: 24, h: 29 },
  }, {
    // hurt3-r
    time: { min: 250, max: 500 },
    rate: { min: -50, max: -30 },
    rect: { x: 14 + 26 * 2, y: 14 + 31 * 3, w: 24, h: 29 },
  }, {
    // hurt3-c
    time: { min: 500, max: 750 },
    rate: { min: -50, max: -30 },
    rect: { x: 14 + 26 * 1, y: 14 + 31 * 3, w: 24, h: 29 },
  }, {
    // hurt3-l
    time: { min: 750, max: 1000 },
    rate: { min: -50, max: -30 },
    rect: { x: 14 + 26 * 0, y: 14 + 31 * 3, w: 24, h: 29 },
  }, {
    // hurt4-c
    time: { min: 0, max: 250 },
    rate: { min: -75, max: -50 },
    rect: { x: 14 + 26 * 1, y: 16 + 31 * 4, w: 24, h: 29 },
  }, {
    // hurt4-r
    time: { min: 250, max: 500 },
    rate: { min: -75, max: -50 },
    rect: { x: 14 + 26 * 2, y: 16 + 31 * 4, w: 24, h: 29 },
  }, {
    // hurt4-c
    time: { min: 500, max: 750 },
    rate: { min: -75, max: -50 },
    rect: { x: 14 + 26 * 1, y: 16 + 31 * 4, w: 24, h: 29 },
  }, {
    // hurt4-l
    time: { min: 750, max: 1000 },
    rate: { min: -75, max: -50 },
    rect: { x: 14 + 26 * 0, y: 16 + 31 * 4, w: 24, h: 29 },
  }, {
    // dead
    time: { min: 0, max: 1001 },
    rate: { min: -10000, max: -75 },
    rect: { x: 14, y: 335, w: 24, h: 31 },
  }, {
    // invincible
    time: { min: 0, max: 1001 },
    rate: { min: 5, max: 10000 },
    rect: { x: 40, y: 335, w: 24, h: 29 },
  }].map(function(row) {
    return {
      time: row.time,
      rate: row.rate,
      css: to_css(row.rect ? row.rect : to_rect(row.pos)),
    };
  });

  function get_frame(time, rate) {
    // time, modulo 1000
    var tm = time % 1000;

    var frames = FRAMES.filter(function(row) {
      return (
        (tm >= row.time.min) && (tm < row.time.max) &&
        (rate >= row.rate.min) && (rate < row.rate.max)
      );
    });

    return ((frames.length > 0) ? frames[0] : FRAMES[0]);
  }

  // 14x13, 24x29

  function tick() {
    var time = now(),
        rate = +$('#rate').val(),
        frame = get_frame(time, rate);
    // console.log({time: time % 1000, rect: rect});

    // update #face
    $('#face').css(frame.css);
  }

  setInterval(tick, 250);
  $('#rate').keyup(tick).focus();

  $('.set-rate').click(function() {
    var val = $(this).data('val');

    // set value
    $('#rate').val((val == 'current') ? CURRENT : val);

    // stop event
    return false;
  });

  $.ajax({
    method: 'GET',
    url: DATA_URL,
    dataType: 'json',
  }).fail(function(r) {
    alert("Couldn't fetch current data.");
  }).done(function(r) {
    CURRENT = to_percent(r.ask);
    $('#rate').val(CURRENT);
  });
});