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