forked from kilobyte/kbtin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticks.c
216 lines (192 loc) · 5.85 KB
/
ticks.c
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
/*********************************************************************/
/* file: ticks.c - functions for the ticker stuff */
/* TINTIN III */
/* (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t */
/* coded by peter unold 1992 */
/*********************************************************************/
#include "tintin.h"
#include "protos/events.h"
#include "protos/globals.h"
#include "protos/hooks.h"
#include "protos/math.h"
#include "protos/print.h"
#include "protos/parse.h"
#include "protos/utils.h"
#include "protos/vars.h"
/*********************/
/* the #tick command */
/*********************/
void tick_command(const char *arg, struct session *ses)
{
timens_t to_tick = ses->tick_size - (current_time() - ses->time0) % ses->tick_size;
tintin_printf(ses, "THERE'S NOW %lld.%03d SECONDS TO NEXT TICK.",
to_tick/NANO, msec(to_tick));
}
/************************/
/* the #tickoff command */
/************************/
void tickoff_command(const char *arg, struct session *ses)
{
ses->tickstatus = false;
if (ses->mesvar[MSG_TICK])
tintin_printf(ses, "#TICKER IS NOW OFF.");
}
/***********************/
/* the #tickon command */
/***********************/
void tickon_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE], *err;
timens_t x=0;
timens_t ct = current_time();
get_arg(arg, left, 1, ses);
substitute_vars(left, left, ses);
if (*left)
{
x = str2timens(left, &err);
if (*err || !*left)
return tintin_eprintf(ses, "#SYNTAX: #tickon [<offset>]");
if (x < 0)
return tintin_eprintf(ses, "#NEGATIVE TICK OFFSET");
ses->time0 = ct - ses->tick_size + x;
}
else if (!ses->time0)
ses->time0 = ct;
if (ses->mesvar[MSG_TICK])
{
if (!ses->tickstatus)
tintin_printf(ses, "#TICKER IS NOW ON.");
else if (!*left)
tintin_printf(ses, "#TICKER IS ALREADY ON.");
}
ses->tickstatus = true;
if (ses->time0 + ses->tick_size - ses->pretick <= ct)
ses->time10 = ses->time0;
if (*left && ses->mesvar[MSG_TICK])
{
nsecstr(left, x);
tintin_eprintf(ses, "#TICKER SET TO %s", left);
}
}
/*************************/
/* the #ticksize command */
/*************************/
void ticksize_command(const char *arg, struct session *ses)
{
timens_t x;
char left[BUFFER_SIZE], *err;
get_arg(arg, left, 1, ses);
if (!*left || !isadigit(*left))
return tintin_eprintf(ses, "#SYNTAX: #ticksize <number>");
x=str2timens(left, &err);
if (*err || x<=0)
return tintin_eprintf(ses, "#INVALID TICKSIZE");
ses->tick_size = x;
ses->time0 = current_time();
ses->time10 = 0;
usecstr(left, x);
if (ses->mesvar[MSG_TICK])
tintin_printf(ses, "#OK. TICKSIZE SET TO %s", left);
}
/************************/
/* the #pretick command */
/************************/
void pretick_command(const char *arg, struct session *ses)
{
timens_t x;
char left[BUFFER_SIZE], *err;
get_arg(arg, left, 1, ses);
if (!*left)
x=ses->pretick? 0 : 10 * NANO;
else
{
x=str2timens(left, &err);
if (*err || x<0)
return tintin_eprintf(ses, "#INVALID PRETICK DELAY");
}
if (x>=ses->tick_size)
{
char right[32];
usecstr(left, x);
usecstr(right, ses->tick_size);
tintin_eprintf(ses, "#PRETICK (%s) has to be smaller than #TICKSIZE (%s)",
left, right);
return;
}
ses->pretick = x;
if (current_time() - ses->pretick < ses->time0)
ses->time10 = ses->time0;
else
ses->time10 = 0;
if (!ses->mesvar[MSG_TICK])
return;
if (!x)
tintin_printf(ses, "#OK. PRETICK TURNED OFF");
else
{
usecstr(left, x);
tintin_printf(ses, "#OK. PRETICK SET TO %s", left);
}
}
void show_pretick_command(const char *arg, struct session *ses)
{
pretick_command(arg, ses);
}
timens_t timetilltick(struct session *ses)
{
return ses->tick_size - (current_time() - ses->time0) % ses->tick_size;
}
/* returns the time (since 1970) of next event (tick) */
timens_t check_event(timens_t time, struct session *ses)
{
timens_t tt; /* tick time */
timens_t et; /* event time */
struct eventnode *ev;
assert(ses);
/* events check - that should be done in #delay */
while ((ev=ses->events) && (ev->time<=time))
{
ses->events=ev->next;
execute_event(ev, ses);
SFREE(ev->event);
TFREE(ev, struct eventnode);
if (any_closed)
return -1;
}
et = (ses->events) ? ses->events->time : 0;
/* ticks check */
tt = ses->time0 + ses->tick_size; /* expected time of tick */
if (tt <= time)
{
if (ses->tickstatus)
{
if (do_hook(ses, HOOK_TICK, 0, false) == ses
&& ses->mesvar[MSG_TICK])
{
tintin_puts1("#TICK!!!", ses);
}
}
if (any_closed)
return -1;
ses->time0 = time - (time - ses->time0) % ses->tick_size;
tt = ses->time0 + ses->tick_size;
}
else if (ses->tickstatus && tt-ses->pretick<=time
&& ses->tick_size>ses->pretick && ses->time10<ses->time0)
{
if (do_hook(ses, HOOK_PRETICK, 0, false) == ses
&& ses->mesvar[MSG_TICK])
{
char buf[BUFFER_SIZE], num[32];
usecstr(num, ses->pretick);
sprintf(buf, "#%s SECONDS TO TICK!!!", num);
tintin_puts1(buf, ses);
}
if (any_closed)
return -1;
ses->time10 = ses->time0;
}
if (ses->tickstatus && ses->tick_size>ses->pretick && tt-time>ses->pretick)
tt-=ses->pretick;
return (et<tt && et) ? et : tt;
}