Wednesday, July 5, 2023

flutter background Image.asset, pass stateObject to custom widget by setState, Stack is framelayout

 main.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'pages/my_home_page.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(
      const SystemUiOverlayStyle(
        statusBarColor: Colors.orange,
      ),
    );
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
        overlays: [] //[SystemUiOverlay.top], is statusBar
        );
    return MaterialApp(
      debugShowCheckedModeBanner: false, // remove a DEBUG banner
      title: 'Devil Hunter',
      theme: ThemeData(
        // colorScheme: ColorScheme.fromSeed(
        //   seedColor: Colors.grey,
        //   primary: Colors.orange,
        //   secondary: Colors.green,
        // ),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Devil Hunter - Chotgor'),
      routes: {
        'home-page': (context) => const MyHomePage(title: 'aaaaaa'),
      },
    );
  }
}

customWidget.dart

import 'package:flutter/material.dart';
import 'package:game1chotgor/pages/my_home_page.dart';

class MyLoginWidget extends StatefulWidget {
  final RuntimeStateObject stateObject;

  const MyLoginWidget({Key? key, required this.stateObject}) : super(key: key);

  @override
  State<MyLoginWidget> createState() => MyLoginWidgetState();
}

class MyLoginWidgetState extends State<MyLoginWidget>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  String get stateCounter => '\$${widget.stateObject.counter}';
  int get stateCounterIf =>
      (widget.stateObject.counter < 10 ? 10 : widget.stateObject.counter);
  double get stateFont => stateCounterIf.toDouble();

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.transparent,
      alignment: Alignment.bottomCenter,
      child: Container(
        color: Colors.amber,
        padding: const EdgeInsets.all(15),
        margin: const EdgeInsets.symmetric(vertical: 100, horizontal: 15),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            Expanded(
              child: Container(
                color: Colors.red,
                child: const Text(
                  'This is a very long text that won\'t fit the line.',
                  style: TextStyle(fontSize: 20),
                ),
              ),
            ),
            Container(
              color: Colors.green,
              child: Text(
                stateCounter,
                style: TextStyle(fontSize: stateFont),
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}

backgroundWidget.dart

import 'package:flutter/material.dart';

class MyBackgroundWidget extends StatelessWidget {
  final String assetImageUrl;

  const MyBackgroundWidget({
    Key? key,
    this.assetImageUrl = 'images/background.jpg',
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.transparent,
      width: double.infinity,
      margin: const EdgeInsets.all(0),
      padding: const EdgeInsets.all(0),
      child: Image.asset(
        assetImageUrl,
        fit: BoxFit.cover,
      ),
    );
  }
}

homePage.dart

import 'package:flutter/material.dart';

import '../widgets/my_background_widget.dart';
import '../widgets/my_login_widget.dart';

class RuntimeStateObject {
  int counter = 0;
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final RuntimeStateObject _stateObject = RuntimeStateObject();

  void _incrementCounter() {
    setState(() {
      _stateObject.counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // setState дуудагдах бүрт энэ аргыг дахин ажиллуулдаг
    return Scaffold(
      //   appBar: AppBar(
      //     systemOverlayStyle: const SystemUiOverlayStyle(
      //       statusBarColor: Colors.orangeAccent,
      //     ),
      //     backgroundColor: Theme.of(context).colorScheme.primary,
      //     title: Text(widget.title),
      //     //   leading: Image(image: NetworkImage()),
      //     elevation: 0,
      //     toolbarHeight: 0,
      //   ),
      backgroundColor: Colors.black87,
      body: Center(
        child: Stack(
          children: <Widget>[
            const MyBackgroundWidget(
              assetImageUrl: 'images/background.jpg',
            ),
            // MyHomeLogoWidget(),
            MyLoginWidget(
              stateObject: _stateObject,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}



Tuesday, June 27, 2023

How to lock an object when websocketgateway has many requests mutex

 test.ts

class Mutex {   
    queue: any[];
    queue_args: any[];
    locked: boolean;

    constructor() {
        this.locked = false;
        this.queue = [];
        this.queue_args = [];
    }

    lock(data: any) {
        return new Promise<void>(resolve => {
            if (this.locked) {
                this.queue.push(resolve);
                this.queue_args.push(data);
                console.log('lock', this.queue_args);
            } else {
                this.locked = true;
                resolve();
            }
        });
    }

    unlock() {
        if (this.queue.length > 0) {
            const nextResolver = this.queue.shift();
            const nextArgs = this.queue_args.shift();
            console.log('unlock', nextArgs, this.queue_args);
            nextResolver();
        } else {
            this.locked = false;
        }
    }
}

class WebSocketGateway {
    data: {};
    mutex: Mutex;

    constructor() {
        this.data = {};
        this.mutex = new Mutex();
    }

    async handleRequest(request) {
        await this.mutex.lock(request.data);
        try {
            await new Promise(resolve => setTimeout(resolve, 1000));
        } finally {
            this.mutex.unlock();
        }
    }
}

const gateway = new WebSocketGateway();
for (var i = 0; i < 4; i++)
    gateway.handleRequest({ data: 'request-' + i });

Tuesday, April 11, 2023

netstat Command line for looking at specific port

In cmd:

netstat -na | find "8080"

In bash:

netstat -na | grep "8080"

In PowerShell:

netstat -na | Select-String "8080"

 

C:\Users\User>netstat -aon | find "8001"

  TCP    [::1]:8001             [::]:0                 LISTENING       7940


C:\Users\User>taskkill /F /pid 7940

SUCCESS: The process with PID 7940 has been terminated.

Saturday, February 18, 2023

Scratch Project - godzilla running


https://github.com/broject/scratch-flowgorithm-algorithm-sample/blob/5b21fe92b6e71545d61bf16978809202be426de4/Scratch%20Project%20-%20godzilla%20running.sb3 


 

Flowgorithm - algorithm creating example

 тэгш тооны нийлбэр олох алгоритм




Subnet Cheat Sheet – Subnet Mask and other IP Address CIDR Network References

 

CIDRSUBNET MASKWILDCARD MASK# OF IP ADDRESSES# OF USABLE IP ADDRESSES
/32255.255.255.2550.0.0.011
/31255.255.255.2540.0.0.122*
/30255.255.255.2520.0.0.342
/29255.255.255.2480.0.0.786
/28255.255.255.2400.0.0.151614
/27255.255.255.2240.0.0.313230
/26255.255.255.1920.0.0.636462
/25255.255.255.1280.0.0.127128126
/24255.255.255.00.0.0.255256254
/23255.255.254.00.0.1.255512510
/22255.255.252.00.0.3.2551,0241,022
/21255.255.248.00.0.7.2552,0482,046
/20255.255.240.00.0.15.2554,0964,094
/19255.255.224.00.0.31.2558,1928,190
/18255.255.192.00.0.63.25516,38416,382
/17255.255.128.00.0.127.25532,76832,766
/16255.255.0.00.0.255.25565,53665,534
/15255.254.0.00.1.255.255131,072131,070
/14255.252.0.00.3.255.255262,144262,142
/13255.248.0.00.7.255.255524,288524,286
/12255.240.0.00.15.255.2551,048,5761,048,574
/11255.224.0.00.31.255.2552,097,1522,097,150
/10255.192.0.00.63.255.2554,194,3044,194,302
/9255.128.0.00.127.255.2558,388,6088,388,606
/8255.0.0.00.255.255.25516,777,21616,777,214
/7254.0.0.01.255.255.25533,554,43233,554,430
/6252.0.0.03.255.255.25567,108,86467,108,862
/5248.0.0.07.255.255.255134,217,728134,217,726
/4240.0.0.015.255.255.255268,435,456268,435,454
/3224.0.0.031.255.255.255536,870,912536,870,910
/2192.0.0.063.255.255.2551,073,741,8241,073,741,822
/1128.0.0.0127.255.255.2552,147,483,6482,147,483,646
/00.0.0.0255.255.255.2554,294,967,2964,294,967,294