interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / core / mobhub / main.om

456 lines · 9115 bytes Raw Blame History
1note core/mobhub/main.om: CLI reading structured faction operations commands on stdin
2note and reporting JSON results on stdout.
3note Compose with: si/directives/om2.om core/mobhub/lib.om core/mobhub/main.om
4note
5note Supported stdin line commands:
6note VALUATION <respect> <rate> <armory> <cash>
7note GYM <stat> <start_stat> <happy> <energy> <e_per_train> <dots_x10>
8note PARSE_NEWS <raw_torn_html_news_line>
9note REDACT <raw_log_or_url_line>
10note MEMBER <torn_id> <flat_respect_x100> <war_hits> <assists> <outside_hits>
11note PAYOUT <flat_rate|pool_share> <cache_value>
12note RESET
13
14global mh_m_in uchar 4096
15global mh_m_line uchar 4096
16global mh_m_out uchar 4096
17global mh_m_olen ulong 1
18global mh_m_nums long 8
19
20text mhm_c_val "VALUATION"
21text mhm_c_gym "GYM"
22text mhm_c_news "PARSE_NEWS"
23text mhm_c_red "REDACT"
24text mhm_c_mem "MEMBER"
25text mhm_c_pay "PAYOUT"
26text mhm_c_rst "RESET"
27text mhm_m_flat "flat_rate"
28text mhm_m_pool "pool_share"
29
30text mhm_j_err "{\"ok\":false,\"error\":\"invalid_or_refused\",\"code\":"
31text mhm_j_end "}\n"
32text mhm_j_rst "{\"ok\":true,\"op\":\"reset\"}\n"
33text mhm_j_mem "{\"ok\":true,\"op\":\"member\",\"count\":"
34text mhm_j_val_1 "{\"ok\":true,\"op\":\"valuation\",\"respect_value\":"
35text mhm_j_val_2 ",\"armory_value\":"
36text mhm_j_val_3 ",\"faction_cash_value\":"
37text mhm_j_val_4 ",\"total_value\":"
38text mhm_j_gym_1 "{\"ok\":true,\"op\":\"gym\",\"trains\":"
39text mhm_j_gym_2 ",\"total_gain_x100\":"
40text mhm_j_gym_3 ",\"end_stat_x100\":"
41text mhm_j_gym_4 ",\"end_happy_x100\":"
42text mhm_j_news_1 "{\"ok\":true,\"op\":\"parse_news\",\"torn_id\":"
43text mhm_j_news_2 ",\"amount\":"
44text mhm_j_red_1 "{\"ok\":true,\"op\":\"redact\",\"text\":\""
45text mhm_j_red_2 "\"}\n"
46text mhm_j_pay_1 "{\"ok\":true,\"op\":\"payout\",\"members_pool\":"
47text mhm_j_pay_2 ",\"faction_fund\":"
48text mhm_j_pay_3 ",\"member_payout_total\":"
49text mhm_j_pay_4 ",\"grand_total\":"
50text mhm_j_pay_5 ",\"unallocated\":"
51
52fn mhm_reset_out to void
53put mh_m_olen 0 0
54end
55
56fn mhm_byte b uchar to void
57p = get mh_m_olen 0
58if lt p 4096
59put mh_m_out p b
60put mh_m_olen 0 add p 1
61end
62end
63
64fn mhm_text s view uchar * to void
65n = len s
66i = ulong 0
67while lt i n bound 256
68mhm_byte get s i
69i = add i 1
70end
71end
72
73fn mhm_i64 v long to void
74digs = new uchar 24 0
75if eq v 0
76mhm_byte 48
77ret
78end
79x = v
80if lt x 0
81mhm_byte 45
82x = neg x
83end
84u = ulong x
85k = ulong 0
86while gt u 0 bound 24
87put digs k uchar add 48 rem u 10
88u = div u 10
89k = add k 1
90end
91while gt k 0 bound 24
92k = sub k 1
93mhm_byte get digs k
94end
95end
96
97fn mhm_flush to bool
98n = get mh_m_olen 0
99off = ulong 0
100while lt off n bound 256
101w = write 1 mh_m_out off sub n off
102if le w 0
103ret false
104end
105off = add off ulong w
106end
107ret true
108end
109
110fn mhm_emit_err code ulong to bool
111mhm_reset_out
112mhm_text mhm_j_err
113mhm_i64 long code
114mhm_text mhm_j_end
115ret mhm_flush
116end
117
118fn mhm_skip_sp buf view uchar * pos ulong blen ulong to ulong
119p = pos
120while lt p blen bound 4096
121if ne get buf p 32
122if ne get buf p 9
123break
124end
125end
126p = add p 1
127end
128ret p
129end
130
131fn mhm_tok_end buf view uchar * pos ulong blen ulong to ulong
132p = pos
133while lt p blen bound 4096
134if or eq get buf p 32 eq get buf p 9
135break
136end
137p = add p 1
138end
139ret p
140end
141
142note Parse signed decimal integer in buf[pos..endp]. Returns false on error.
143fn mhm_parse_i64 buf view uchar * pos ulong endp ulong dst edit long 1 to bool
144if ge pos endp
145ret false
146end
147p = pos
148negf = false
149if eq get buf p 45
150negf = true
151p = add p 1
152if ge p endp
153ret false
154end
155end
156v = long 0
157while lt p endp bound 32
158c = get buf p
159if not mh_is_digit c
160ret false
161end
162v = add mul v 10 long sub c 48
163p = add p 1
164end
165if negf
166v = neg v
167end
168put dst 0 v
169ret true
170end
171
172fn mhm_parse_n_i64 lbuf view uchar * pos ulong llen ulong cnt ulong to bool
173tmp = new long 1 0
174k = ulong 0
175cur = pos
176while lt k cnt bound 8
177cur = mhm_skip_sp lbuf cur llen
178e = mhm_tok_end lbuf cur llen
179if not mhm_parse_i64 lbuf cur e tmp
180ret false
181end
182put mh_m_nums k get tmp 0
183cur = e
184k = add k 1
185end
186ret true
187end
188
189fn mhm_run_valuation to ulong
190a0 = f64 get mh_m_nums 0
191a1 = f64 get mh_m_nums 1
192a2 = f64 get mh_m_nums 2
193a3 = f64 get mh_m_nums 3
194ret mh_compute_valuation a0 a1 a2 a3
195end
196
197fn mhm_cmd_valuation lbuf view uchar * p ulong llen ulong to bool
198if not mhm_parse_n_i64 lbuf p llen 4
199ret mhm_emit_err MH_ERR_PARSE
200end
201rc = mhm_run_valuation
202if ne rc MH_OK
203ret mhm_emit_err rc
204end
205mhm_reset_out
206mhm_text mhm_j_val_1
207mhm_i64 get mh_g_val_resp 0
208mhm_text mhm_j_val_2
209mhm_i64 get mh_g_val_armory 0
210mhm_text mhm_j_val_3
211mhm_i64 get mh_g_val_cash 0
212mhm_text mhm_j_val_4
213mhm_i64 get mh_g_val_total 0
214mhm_text mhm_j_end
215ret mhm_flush
216end
217
218fn mhm_run_gym sid ulong to ulong
219mh_gym_reset_opts
220a0 = f64 get mh_m_nums 0
221a1 = f64 get mh_m_nums 1
222a2 = f64 get mh_m_nums 2
223a3 = f64 get mh_m_nums 3
224a4 = div f64 get mh_m_nums 4 10.0f64
225ret mh_gym_simulate sid a0 a1 a2 a3 a4
226end
227
228fn mhm_cmd_gym lbuf view uchar * p ulong llen ulong to bool
229cur = mhm_skip_sp lbuf p llen
230e = mhm_tok_end lbuf cur llen
231sid = mh_stat_parse lbuf cur sub e cur
232if eq sid MH_STAT_INVALID
233ret mhm_emit_err MH_ERR_BAD_STAT
234end
235if not mhm_parse_n_i64 lbuf e llen 5
236ret mhm_emit_err MH_ERR_PARSE
237end
238rc = mhm_run_gym sid
239if ne rc MH_OK
240ret mhm_emit_err rc
241end
242mhm_reset_out
243mhm_text mhm_j_gym_1
244mhm_i64 long get mh_g_gym_trains 0
245mhm_text mhm_j_gym_2
246mhm_i64 mh_round_i64 mul get mh_g_gym_tgain 0 100.0f64
247mhm_text mhm_j_gym_3
248mhm_i64 mh_round_i64 mul get mh_g_gym_estat 0 100.0f64
249mhm_text mhm_j_gym_4
250mhm_i64 mh_round_i64 mul get mh_g_gym_ehappy 0 100.0f64
251mhm_text mhm_j_end
252ret mhm_flush
253end
254
255fn mhm_run_member to ulong
256t = get mh_m_nums 0
257r = div f64 get mh_m_nums 1 100.0f64
258w = get mh_m_nums 2
259a = get mh_m_nums 3
260o = get mh_m_nums 4
261ret mh_member_add t r w a o
262end
263
264fn mhm_cmd_member lbuf view uchar * p ulong llen ulong to bool
265if not mhm_parse_n_i64 lbuf p llen 5
266ret mhm_emit_err MH_ERR_PARSE
267end
268rc = mhm_run_member
269if ne rc MH_OK
270ret mhm_emit_err rc
271end
272mhm_reset_out
273mhm_text mhm_j_mem
274mhm_i64 long get mh_g_in_cnt 0
275mhm_text mhm_j_end
276ret mhm_flush
277end
278
279fn mhm_cmd_payout lbuf view uchar * p ulong llen ulong to bool
280cur = mhm_skip_sp lbuf p llen
281e = mhm_tok_end lbuf cur llen
282mlen = sub e cur
283if mh_eq_ci lbuf cur mlen mhm_m_flat
284put mh_g_pol_model 0 MH_MODEL_FLAT_RATE
285else
286if mh_eq_ci lbuf cur mlen mhm_m_pool
287put mh_g_pol_model 0 MH_MODEL_POOL_SHARE
288else
289ret mhm_emit_err MH_ERR_BAD_MODEL
290end
291end
292cur = mhm_skip_sp lbuf e llen
293e2 = mhm_tok_end lbuf cur llen
294tmp = new long 1 0
295if not mhm_parse_i64 lbuf cur e2 tmp
296ret mhm_emit_err MH_ERR_PARSE
297end
298rc = mh_compute_payout get tmp 0
299if ne rc MH_OK
300ret mhm_emit_err rc
301end
302mhm_reset_out
303mhm_text mhm_j_pay_1
304mhm_i64 get mh_g_res_mpool 0
305mhm_text mhm_j_pay_2
306mhm_i64 get mh_g_res_ffund 0
307mhm_text mhm_j_pay_3
308mhm_i64 get mh_g_res_mpay_tot 0
309mhm_text mhm_j_pay_4
310mhm_i64 get mh_g_res_grand 0
311mhm_text mhm_j_pay_5
312mhm_i64 get mh_g_res_unalloc 0
313mhm_text mhm_j_end
314ret mhm_flush
315end
316
317fn mhm_cmd_news lbuf view uchar * p ulong llen ulong to bool
318cur = mhm_skip_sp lbuf p llen
319if ge cur llen
320ret mhm_emit_err MH_ERR_NO_MATCH
321end
322rc = mh_parse_money_log part lbuf cur sub llen cur sub llen cur
323if ne rc MH_OK
324ret mhm_emit_err rc
325end
326mhm_reset_out
327mhm_text mhm_j_news_1
328mhm_i64 get mh_g_parse_tid 0
329mhm_text mhm_j_news_2
330mhm_i64 get mh_g_parse_amt 0
331mhm_text mhm_j_end
332ret mhm_flush
333end
334
335fn mhm_cmd_redact lbuf view uchar * p ulong llen ulong to bool
336cur = mhm_skip_sp lbuf p llen
337if ge cur llen
338ret mhm_emit_err MH_ERR_PARSE
339end
340n = mh_redact_secrets part lbuf cur sub llen cur sub llen cur
341mhm_reset_out
342mhm_text mhm_j_red_1
343i = ulong 0
344while lt i n bound 4000
345c = get mh_g_buf i
346if or eq c 34 eq c 92
347mhm_byte 92
348end
349mhm_byte c
350i = add i 1
351end
352mhm_text mhm_j_red_2
353ret mhm_flush
354end
355
356fn mhm_dispatch llen ulong to bool
357lbuf = new uchar 4096 0
358if eq llen 0
359ret true
360end
361i = ulong 0
362while lt i llen bound 4096
363put lbuf i get mh_m_line i
364i = add i 1
365end
366s = mhm_skip_sp lbuf 0 llen
367if ge s llen
368ret true
369end
370e = mhm_tok_end lbuf s llen
371clen = sub e s
372if mh_eq_ci lbuf s clen mhm_c_rst
373mh_policy_default_flat
374mh_members_reset
375mhm_reset_out
376mhm_text mhm_j_rst
377ret mhm_flush
378end
379if mh_eq_ci lbuf s clen mhm_c_val
380ret mhm_cmd_valuation lbuf e llen
381end
382if mh_eq_ci lbuf s clen mhm_c_gym
383ret mhm_cmd_gym lbuf e llen
384end
385if mh_eq_ci lbuf s clen mhm_c_mem
386ret mhm_cmd_member lbuf e llen
387end
388if mh_eq_ci lbuf s clen mhm_c_pay
389ret mhm_cmd_payout lbuf e llen
390end
391if mh_eq_ci lbuf s clen mhm_c_news
392ret mhm_cmd_news lbuf e llen
393end
394if mh_eq_ci lbuf s clen mhm_c_red
395ret mhm_cmd_redact lbuf e llen
396end
397ret mhm_emit_err MH_ERR_PARSE
398end
399
400fn main to int
401mh_policy_default_flat
402mh_members_reset
403llen = ulong 0
404over = false
405done = false
406while not done bound 100000
407n = read 0 mh_m_in 0 4096
408if lt n 0
409ret 1
410end
411if eq n 0
412if gt llen 0
413if over
414if not mhm_emit_err MH_ERR_CAPACITY
415ret 2
416end
417else
418if not mhm_dispatch llen
419ret 2
420end
421end
422end
423done = true
424break
425end
426rn = ulong n
427i = ulong 0
428while lt i rn bound 4096
429b = get mh_m_in i
430if eq b 10
431if over
432if not mhm_emit_err MH_ERR_CAPACITY
433ret 2
434end
435else
436if not mhm_dispatch llen
437ret 2
438end
439end
440llen = 0
441over = false
442else
443if ne b 13
444if lt llen 4096
445put mh_m_line llen b
446llen = add llen 1
447else
448over = true
449end
450end
451end
452i = add i 1
453end
454end
455ret 0
456end